2013-11-09 44 views
0

感謝您的幫助。我是Python新手,使用這個項目來學習Python,現在它使用一個函數在屏幕上繪製圖塊,並且我正在努力讓一個精靈能夠按下按鍵來移動。問題是我的函數keyMoveSprite()看不到我的全局變量x_coordinate和y_coordinate。它們在我的函數中,但是這導致我的精靈不能移動,因爲每次迭代x和y變量都被設置爲0,所以我想他們需要在循環之外。有什麼建議麼?這是我的代碼。我認爲這個問題可能是由於它在遊戲的主循環中反覆調用的函數,但它需要反覆調用以跟蹤輸入。爲什麼我的函數keyMoveSprite()看不到全局變量x_coordinate,y_coordinate?

import pygame,sys 
from pygame.locals import * 

pygame.init() 

#Global Variables 
screen_x = 800 
screen_y = 480 
screen = pygame.display.set_mode((screen_x, screen_y)) 
x_coordinate = 0 
y_coordinate = 0 
moveX, moveY = 0, 0 


#This function will take the screen coordinates and draw 
#a tile to the entire screen. It will draw tiles no matter 
#the screen size. 

def keyMoveSprite(): 
    sprite = pygame.image.load("character.png").convert_alpha() 

    for event in pygame.event.get(): 

     if event.type == KEYDOWN: 
      if event.key == K_LEFT: 
       moveX = -10 
      elif event.key == K_RIGHT: 
       moveX = +10 
      elif event.key == K_UP: 
       moveY = -10 
      elif event.key == K_DOWN: 
       moveY = +10 

     if event.type == KEYUP: 
      if event.key == K_LEFT: 
       moveX = 0 
      elif event.key == K_RIGHT: 
       moveX = 0 
      elif event.key == K_UP: 
       moveY = 0 
      elif event.key == K_DOWN: 
       moveY = 0 

    x_coordinate = x_coordinate + moveX 
    y_coordinate = y_coordinate + moveY 

    screen.blit(sprite,(x_coordinate, y_coordinate)) 
    pygame.display.update() 
    print("Character drawn at:", x_coordinate, y_coordinate) 


def mapDraw(screen_x, screen_y): 



    floor = pygame.image.load("floor_tile.png") 
    wall = pygame.image.load("wall_tile.png") 
    treasure = pygame.image.load("treasure_chest.png") 

# Intialize row for loop 
    row = 0 
    mapColumn = 0 
    mapRow = 0 

#Loop between the values 0 to the screen's y variable, in intervals of 
#32, and store them in the variable row once per loop. 

    mapArray = [ 
     [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], 
     [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], 
     [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], 
     [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], 
     [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], 
     [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], 
     [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], 
     [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], 
     [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], 
     [1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1], 
     [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1], 
     [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1], 
     [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1], 
     [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1], 
     [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] 
     ] 


    for row in range(0, screen_y, 32): 
     column = 0 # Set column to 0 per iteration, this allows reset of the x coordinate 
     print("Map Row Value:", mapRow) 
     mapColumn = 0 # Resets mapColumn variable to 0 
     for column in range(0, screen_x, 32): 

      if mapArray[mapRow][mapColumn] == 0: 

       screen.blit(floor, (column, row)) 
       #print(column,row) 
       pygame.display.update() 
       #print("Map Column Value:",mapColumn) 
       print("MapCol",mapColumn,"MapRow",mapRow) 

      elif mapArray[mapRow][mapColumn] == 1: 
       screen.blit(wall, (column, row)) 
       pygame.display.update() 







      mapColumn = mapColumn + 1 

     mapRow = mapRow + 1 


def main(): 

    mapFloor = mapDraw(800,480) 
    while True: 

     keyMoveSprite() 

main() 

回答

1

嘗試使用global keyword,這樣解釋可以的x_coordinatey_coordinate地方分配間的歧義:

def keyMoveSprite(): 
    global x_coordinate, y_coordinate 
    # The rest of your code... 

一個更好的辦法來解決這個問題是創建一個類來表示您的播放器,其位置以及與之相關的其他狀態,而不是將全部內容存儲在全局變量中。

+0

這工作,非常感謝。任何你有想法的機會如何讓我的精靈不會在屏幕上「拖動」? – DougLuce

+0

@DougLuce「drag」是什麼意思?另一個問題/答案可能已經存在關於你遇到的這個新問題。也許我可以幫你找到它。 – kevintodisco

+0

完成,新的網站,但我找到了複選框。我的意思是它在每次迭代時都會將圖像粘貼到屏幕上,但是我需要找出一種方法在移動後「移除」每個圖像。 – DougLuce

相關問題