2014-02-21 21 views
-4

所以我有這個成熟的遊戲。我需要一個變量創建者。很難解釋。下面的代碼...如何用代碼創建一個變量(Python)

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等等。

我仍然需要ANSWERS。

+1

我不明白你的問題和你的意思是通過新的變量。請修改您的代碼並只保留相關部分。 – Kikohs

+3

列表,字典,容器。他們的全部目的就是這樣。 –

+0

您可以通過創建類然後創建這些類的實例來完成此任務。 – jramirez

回答

4

A - 如果它是很難解釋,你可能缺少一些清晰度所必需的編碼

B-熟悉「字典」

move = {"up":False, "down":True} 

是你想要的。

可以再添加值

move["jump"]=True 
+1

「如果實現很難解釋,這是一個壞主意。如果實現很容易解釋,這可能是一個好主意。」 –

+0

這不是我剛纔講的......函數platform_spawnandcheck()看起來像是需要你給我的東西嗎? –

+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) 
+0

你給我的東西已經是我用platform_spawnandcheck()所做的了。 –

+0

平臺產生,然後如果我產生另一個,他們之間切換顯示他們在pygame之前,它必須重用一個變量。換句話說,平臺閃爍...... D:這與我們兩個人的問題是一樣的。 –

1

所以,你需要創建一個使用邏輯,而不是進行直接賦值的變量。這是我從你的問題中得到的。因此,而不是這個,

a = 10 

你想要做

if some logic: 
    make_var(10) # somehow returns (a,10) in bizaro python 

雖然,這是一個方法,使一個變量賦值,這是不正確的做法。什麼vish是想告訴你的是,你可以通過創建Python字典,並使得字典內的任務作爲

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

+0

那麼,如果我在shell中搜索變量,那麼製作變量的名稱是什麼? –

相關問題