简体   繁体   English

未定义全局变量 - Python 和 PyGame

[英]Global variable is not defined - Python and PyGame

i try to create a Flappy Bird clone.我尝试创建一个 Flappy Bird 克隆。 When i try to define some Global variables visual studio said me that this variables aren't defined in global scope.当我尝试定义一些全局变量时,Visual Studio 告诉我这个变量没有在全局 scope 中定义。

Some could help me??有人可以帮我吗??

I tried to move the variables in the global scope and it works, but i don't understand why this solution doesn 't work.我试图移动全局 scope 中的变量并且它有效,但我不明白为什么这个解决方案不起作用。

This is my code Thanks in advance这是我的代码提前谢谢

import pygame
import random

pygame.init()

background = pygame.image.load('img/background.png')
base = pygame.image.load('img/base.png')
bird = pygame.image.load('img/bird.png')
gameover = pygame.image.load('img/gameover.png')
pipe_down = pygame.image.load('img/pipe.png')
pipe_up = pygame.transform.flip(pipe_down, False, True)

windowX = 288
winwowY = 512
frame_rate = 50

display_surface = pygame.display.set_mode((windowX, winwowY))
FPS = frame_rate

pygame.display.set_caption('Flappy Bird')

def drawObject():
    display_surface.blit(background, (0, 0))
    display_surface.blit(bird, (birdX, birdY))

def update():
    pygame.display.update()
    pygame.time.Clock().tick(FPS)

# Here is where define my global vars
def initializes():
    global birdX, birdY, birdSpeedY
    birdX = 60
    birdY = 150
    birdSpeedY = 0

initializes()

while True:
    birdSpeedY += 1
    birdY += birdSpeedY

    drawObject()
    update()

The message is telling you exactly what the issue is.该消息准确地告诉您问题所在。 The global variables aren't defined in global scope.全局变量未在全局 scope 中定义。

That means that for these variables that you are telling it you want to use from the global namespace global birdX, birdY, birdSpeedY , it expects to find a definition of those in that uppermost namespace.这意味着对于您告诉它要从全局命名空间global birdX, birdY, birdSpeedY使用的这些变量,它希望在最上面的命名空间中找到这些变量的定义。 The global keyword does NOT create them in the global namespace just because you use it. global关键字不会因为您使用它而在全局命名空间中创建它们。 They must exist there independent of that.它们必须独立存在于那里。

For them to be defined in the global scope there needs to be an assignment to them in the global namespace, not inside a function or a class.要在全局 scope 中定义它们,需要在全局命名空间中对它们进行分配,而不是在 function 或 class 中。 That cannot be something a += either since that is a reference and an assignment and so it assumes that the definition must be elsewhere (or it would be being referenced before a value was assigned).那也不能是 a += ,因为那是一个引用和一个赋值,因此它假定定义必须在其他地方(或者它会在赋值之前被引用)。

So somewhere in the global namespace you need an assignment.所以在全局命名空间的某个地方你需要一个赋值。 If you want to handle the initialization in a function (as you are doing) it must still be defined/assigned outside that function, but it can be any value, like None .如果您想在 function 中处理初始化(正如您正在做的那样),它仍然必须在 function 之外定义/分配,但它可以是任何值,例如None So you could add this near the top of you program:所以你可以在你的程序顶部附近添加这个:

birdX = None
birdY = None
birdSpeedY = None

Then still use your initializes() as you are.然后仍然按原样使用您的initializes()

Or in your case you would probably just take the stuff inside initializes() and put it at the top /global level.或者在您的情况下,您可能只是将initializes()中的内容放在顶部/全局级别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM