关于python:为什么我的函数keymovespire()不能看到全局变量x_coordinate,y_coordinate?

Why can't my function keyMoveSprite() see the global variable x_coordinate, y_coordinate?

本问题已经有最佳答案,请猛点这里访问。

感谢您的帮助。我刚接触到Python语言,并使用这个项目来学习Python,现在它使用一个函数来绘制屏幕上的瓷砖,我正在努力让sprite能够通过按键移动。问题是我的函数keymoveSpire()看不到全局变量x_坐标和y_坐标。它们在我的函数中,但这导致了sprite不移动,因为每次迭代x和y变量都被设置回0,所以我认为它们需要在循环之外。有什么建议吗?这是我的密码。我认为这个问题可能是因为在游戏的主循环中,函数被反复调用,但是需要反复调用它来跟踪输入。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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()

尝试使用global关键字,这样解释器就可以在x_coordinatey_coordinate的本地赋值之间消除歧义:

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

解决这个问题的一个更好的方法是创建一个类来表示你的玩家、它的位置以及与之相关的其他状态,而不是将所有内容存储在全局中。