所以我有这个平台游戏。我需要一个变量创建者。很难解释。这是代码......
import pygame,time,random,sys
pygame.display.set_caption('My Platformer V1.0')
x = 10
y = 30
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y)
GREEN = (0,255,0)
WHITE =(0,0,0)
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
LIGHTBLUE = (60,60,240)
LIGHTBROWN = (255, 236, 139)
RED = (255, 0, 0)
YELLOW = (255,255,0)
DARKRED = (102,0,0)
GRAY = (160,160,160)
WINDOWWIDTH = 1200
WINDOWHEIGHT = 700
mainClock = pygame.time.Clock()
from pygame.locals import *
pygame.init()
PlayerRect = pygame.Rect(0,0,50,80)
global PlayerX
global PlayerY
PlayerX = 0
PlayerY = 0
moveRight = False
moveLeft = False
moveUp = False
moveDown = False
gravity = False
Jump = False
JumpTime = 0
global PPX
global PPY
PPX = PlayerX
PPY = PlayerY
screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
# Define Functions ---------------------------------------- #
def platform_spawnandcheck(x,y,sx,sy):
Rect2 = pygame.Rect(x,y,sx,sy)
pygame.draw.rect(screen,WHITE,Rect2)
pygame.draw.rect(screen,GREEN,PlayerRect)
pygame.display.update()
y2 = y
y2 += 80
global PlayerY
if PlayerY < y2:
if PlayerRect.colliderect(Rect2):
global PPX
global PlayerY
PlayerY = PPY
if PlayerY > y2:
if PlayerRect.colliderect(Rect2):
global PPX
PlayerX = PPX
while True:
# Make Past Coordinance ------------------------------- #
PPX = PlayerX
PPY = PlayerY
# Jump ------------------------------------------------ #
if Jump == True:
JumpTime += 1
PlayerY -= 12
if JumpTime == 30:
Jump = False
JumpTime = 0
# Gravity --------------------------------------------- #
gravity = True
# Movement -------------------------------------------- #
if moveRight == True:
PlayerX += 5
if moveLeft == True:
PlayerX -= 5
if PlayerY > 620:
gravity = False
if moveUp == True:
PlayerY -= 3
if gravity == False:
Jump = True
if gravity == True:
PlayerY += 5
# Reload the player Rect ------------------------------ #
PlayerRect = pygame.Rect(PlayerX,PlayerY,50,80)
# Check for pressed keys ------------------------------ #
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == ord('d'):
moveRight = True
if event.key == K_SPACE:
moveUp = True
if event.key == ord('a'):
moveLeft = True
if event.type == KEYUP:
if event.key == ord('d'):
moveRight = False
if event.key == K_SPACE:
moveUp = False
if event.key == ord('a'):
moveLeft = False
# Update ---------------------------------------------- #
screen.fill(LIGHTBLUE)
platform_spawnandcheck(500,650,100,20)
pygame.draw.rect(screen,GREEN,PlayerRect)
pygame.display.update()
mainClock.tick(60)
所以,如果你看一下代码...... 您将看到我需要一个变量生成器来为各个平台创建新变量。 (我说的是“platform_spawnandcheck”函数。)(平台用于pygame。)
def platform_spawnandcheck(x,y,sx,sy):
Rect2 = pygame.Rect(x,y,sx,sy)
pygame.draw.rect(screen,WHITE,Rect2)
pygame.draw.rect(screen,GREEN,PlayerRect)
pygame.display.update()
y2 = y
y2 += 80
global PlayerY
if PlayerY < y2:
if PlayerRect.colliderect(Rect2):
global PPX
global PlayerY
PlayerY = PPY
if PlayerY > y2:
if PlayerRect.colliderect(Rect2):
global PPX
PlayerX = PPX
回答关于制作变量的部分。我想要为我做一些变量,比如它让var1 var2 var3 var4等等。
我仍然需要答案。
答案 0 :(得分:4)
A - 如果很难解释,你可能会错过编码所必需的清晰度
B-熟悉&#34; dict&#34;
move = {"up":False, "down":True}
是你想要的。
然后你可以添加值
move["jump"]=True
答案 1 :(得分:1)
因此,您需要使用逻辑创建变量,而不是直接赋值。这就是我从你的问题中得到的。所以,而不是这个,
a = 10
你想做什么
if some logic:
make_var(10) # somehow returns (a,10) in bizaro python
虽然这是进行变量赋值的一种方法,但这不是正确的方法。 vish试图告诉你的是你可以通过创建一个python dict并在dict中进行赋值来创建变量
made_vars = {}
if some logic:
made_vars[a] = 10
print made_vars[a] # should print 10
所以你完成了作业。但是,不是名称var1,var2等,而是将变量名称设为made_var [var1],made_var [var2]等。
我正在网上寻找更好的解释,我想出了这个link。
答案 2 :(得分:0)
您应该为使用某个随机位置初始化的平台创建一个类。
类的__init__
方法是该类的“生成器”,也称为构造函数。使用Platform()
构造类的实例,并调用__init__
方法并返回一个新的Platform对象。
class Platform:
def __init__(self):
self.x = random.randint(0, WINDOWWIDTH)
self.y = random.randint(0, WINDOWHEIGHT)
self.sx = 100
self.sy = 20
# creating platforms
platforms = []
for i in xrange(20):
platforms.append(Platform())
# accessing the coordinates of the platforms
for platform in platforms:
print platform.x, platform.y, platform.sx, platform.sy
# passing the platforms into your function
for platform in platforms:
platform_spawnandcheck(platform.x, platform.y, platform.sx, platform.sy)