[英]I have an error: NameError: name 'GameDisplay' is not defined
I'm trying to make a game in pycharm using python and I have a name which isn't defined.我正在尝试使用 python 在 pycharm 中制作游戏,但我有一个未定义的名称。 I looked for information about how to fix this and the best fix I could find was changing the name from gameDisplay to GameDisplay in line 16 but it didn't help at all.我查找了有关如何解决此问题的信息,我能找到的最佳解决方法是在第 16 行将名称从 gameDisplay 更改为 GameDisplay,但它根本没有帮助。 Below is the full error message I received.以下是我收到的完整错误消息。
Traceback (most recent call last):
File "C:/Users/Zack's PC/AppData/Roaming/JetBrains/PyCharmCE2020.1/scratches/main1.py", line 30, in <module>
car(x,y)
File "C:/Users/Zack's PC/AppData/Roaming/JetBrains/PyCharmCE2020.1/scratches/main1.py", line 16, in car
GameDisplay.blit(carImg,(x,y))
NameError: name 'GameDisplay' is not defined
Process finished with exit code 1
this is the file:这是文件:
***import pygame
pygame.init()
display_width= 900
display_height=600
black= (0,0,0)
white= (255,255,255)
red= (255,0,0)
screen= pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Race Car')
clock = pygame.time.Clock()
carImg = pygame.image.load('C:/Users/Zack\'s PC/Pictures/gameimages/racecarimage.png')
def car(x,y):
gameDisplay.blit(carImg,(x,y))
x = (display_height * 0.45)
y = (display_width * 0.8)
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
car(x,y)
gameDisplay.fill(white)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()***
Problem is that you didn't create问题是你没有创建
gameDisplay = pygame.display.set_mode(...)
but但
screen = pygame.display.set_mode(...)
and now you have to use screen
instead of gameDisplay
现在你必须使用screen
而不是gameDisplay
screen.blit(...)
screen.fill(white)
Or you have to use或者你必须使用
gameDisplay = pygame.display.set_mode(...)
instead of代替
screen = pygame.display.set_mode(...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.