简体   繁体   中英

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. 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. 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.blit(...)

screen.fill(white)

Or you have to use

gameDisplay = pygame.display.set_mode(...)

instead of

screen = pygame.display.set_mode(...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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