谢谢你提前帮忙。我对Python语言很陌生,并使用这个项目来学习Python,现在它使用一个函数将瓷砖绘制到屏幕上,我正在努力获得一个sprite,以便能够使用按键移动。问题是我的函数keyMoveSprite()不能看到全局变量x_coordinate和y_coordinate。它们在我的函数中,但这导致我的sprite没有移动,因为x和y变量每次迭代都被设置为0,所以我认为它们需要在循环之外。有什么建议吗?这是我的密码。我认为这个问题可能是因为在游戏的主循环中被反复调用的函数,但是需要反复调用它来跟踪输入。
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()发布于 2013-11-09 07:23:45
尝试使用global keyword,以便解释器可以消除x_coordinate和y_coordinate的本地分配之间的歧义。
def keyMoveSprite():
global x_coordinate, y_coordinate
# The rest of your code...解决这个问题的一个更好的方法是创建一个类来表示您的玩家、它的位置以及与它相关的其他状态,而不是将所有东西存储在全局中。
https://stackoverflow.com/questions/19873454
复制相似问题