如何使用代码(Python)创建变量
所以我有一个平板游戏。我需要一个变量创建者。这很难解释。这是密码如何使用代码(Python)创建变量,python,pygame,python-3.3,Python,Pygame,Python 3.3,所以我有一个平板游戏。我需要一个变量创建者。这很难解释。这是密码 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,
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)
import pygame,time,random,sys
pygame.display.set_标题('My Platformer V1.0')
x=10
y=30
导入操作系统
操作系统环境['SDL\u视频窗口位置]=%d,%d%(x,y)
绿色=(0255,0)
白色=(0,0,0)
黑色=(0,0,0)
蓝色=(0,0255)
浅蓝色=(60,60240)
浅棕色=(255236139)
红色=(255,0,0)
黄色=(255255,0)
DARKRED=(102,0,0)
灰色=(160160)
窗宽=1200
窗高=700
mainClock=pygame.time.Clock()
从pygame.locals导入*
pygame.init()
PlayerRect=pygame.Rect(0,0,50,80)
全球PlayerX
全球游戏
PlayerX=0
PlayerY=0
moveRight=False
moveLeft=False
moveUp=False
向下移动=错误
重力=假
跳转=错误
跳跃时间=0
全球PPX
全球PPY
PPX=播放器
嬉戏
screen=pygame.display.set_模式((窗口宽度,窗口高度),0,32)
#定义函数-----------------------------------------#
def平台检查(x、y、sx、sy):
Rect2=pygame.Rect(x,y,sx,sy)
pygame.draw.rect(屏幕,白色,Rect2)
pygame.draw.rect(屏幕、绿色、玩家等)
pygame.display.update()
y2=y
y2+=80
全球游戏
如果PlayerYy2:
如果PlayerRect.colliderect(Rect2):
全球PPX
PlayerX=PPX
尽管如此:
#进行过去的协调------------------#
PPX=播放器
嬉戏
#跳转-----------------------------------------#
如果跳转==真:
跳转时间+=1
玩耍-=12
如果JumpTime==30:
跳转=错误
跳跃时间=0
#重力------------------------------------------#
重力=真
#运动-----------------------------------------#
如果moveRight==True:
PlayerX+=5
如果moveLeft==True:
PlayerX-=5
如果PlayerY>620:
重力=假
如果moveUp==True:
玩耍-=3
如果重力==假:
跳转=真
如果重力==真:
玩耍+=5
#重新加载播放器Rect------------------#
PlayerRect=pygame.Rect(PlayerX,PlayerY,50,80)
#检查有无按键------------------#
对于pygame.event.get()中的事件:
如果event.type==退出:
pygame.quit()
sys.exit()
如果event.type==KEYDOWN:
如果event.key==ord('d'):
moveRight=True
如果event.key==K_空间:
向上移动=真
如果event.key==ord('a'):
moveLeft=True
如果event.type==KEYUP:
如果event.key==ord('d'):
moveRight=False
如果event.key==K_空间:
moveUp=False
如果event.key==ord('a'):
moveLeft=False
#更新------------------------------------------#
屏幕填充(浅蓝色)
平台检查(500650100,20)
pygame.draw.rect(屏幕、绿色、玩家等)
pygame.display.update()
主时钟。滴答声(60)
所以如果你看一下代码。。。
您将看到,我需要一个变量生成器来为各个平台生成新变量。(我说的是功能“platform_spawnandcheck”。(平台是用于pygame的。)
def平台检查(x、y、sx、sy):
Rect2=pygame.Rect(x,y,sx,sy)
pygame.draw.rect(屏幕,白色,Rect2)
pygame.draw.rect(屏幕、绿色、玩家等)
pygame.display.update()
y2=y
y2+=80
全球游戏
如果PlayerYy2:
如果PlayerRect.colliderect(Rect2):
全球PPX
PlayerX=PPX
只需回答有关生成变量的部分。我想要一些东西来为我生成变量,比如让它生成var1 var2 var3 var4等等
我仍然需要答案。A-如果很难解释,您可能缺少一些编码所必需的清晰性 B-熟悉“dict” 这就是你想要的 然后可以添加值
move["jump"]=True
您应该为使用一些随机位置初始化的平台创建一个类 类的
\uuuu init\uuuu
方法是该类的“生成器”,也称为构造函数。使用Platform()
构造类的实例,调用\uuuu init\uuuu
方法并返回新的平台对象
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)
因此,您需要使用逻辑创建变量,而不是直接赋值。这就是我从你的问题中得到的。所以不是这个,
a = 10
你想做什么
if some logic:
make_var(10) # somehow returns (a,10) in bizaro python
虽然这是进行变量赋值的一种方法,但这不是正确的方法。试图告诉您的是,您可以通过创建一个python dict并在dict中赋值来生成变量
made_vars = {}
if some logic:
made_vars[a] = 10
print made_vars[a] # should print 10
所以你完成了你的任务。但是,与名称var1、var2等不同,您可以将变量名称设置为make_var[var1]、make_var[var2]等
我在网上寻找更好的解释,我想到了这个。我不明白你的问题,也不明白你提出新变量的意思。请编辑您的代码并仅保留相关部分。列表、目录、容器。他们的全部目的就是这样。你可以通过创建类,然后创建这些类的实例来实现。如果很难解释,那可能意味着你不太明白你想做什么?退一步,试着想想为什么你需要做你认为你需要做的事情。我在最后说得很清楚。“如果实现很难解释,那是个坏主意。如果实现很容易解释,那可能是个好主意。”这不是我说的。。。有没有功能平台_
if some logic:
make_var(10) # somehow returns (a,10) in bizaro python
made_vars = {}
if some logic:
made_vars[a] = 10
print made_vars[a] # should print 10