0

I am trying to understand how to define exiting in pygame. I am making a car game where you have to avoid obstacles from coming into contact with the car.

My error is

line 71, in <module>
    while not gameExit:
NameError: name 'gameExit' is not defined

My code is

import pygame
import time
import random

pygame.init()

display_width = 800
display_height = 600

black = (0,0,0)
white = (255, 255, 255)
red = (255,0,0)

car_width = 73

gameDisplay = pygame.display.set_mode((display_width, display_height))

pygame.display.set_caption("Hi There Fucker")

clock = pygame.time.Clock()

carImg = pygame.image.load("mycar.png")

def things_dodged(count):
    font = pygame.font.SysFont(None, 25)
    text = font.render("Dodged: " + str(count), True, black)
    gameDisplay.blit(text, (0,0))

def things(thingx, thingy, thingw, thingh, color):
    pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])

def car(x,y):
    gameDisplay.blit(carImg, (x,y))


def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()


def message_display(text):
    largeText = pygame.font.Font("freesansbold.ttf", 115)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2), (display_height/2))
    gameDisplay.blit(TextSurf, TextRect)

    pygame.display.update()

    time.sleep(2)

    game_loop()

def crash():
    message_display("You crashed")

def game_loop():
    x = (display_width * 0.1)
    y = (display_height * 0.3)

    x_change = 0

    thing_starx = random.randrange(0, display_width)
    thing_starty = -600
    thing_speed = 7
    thing_width = 100
    thing_height = 100

    gameExit = False

while not gameExit:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
CDspace
  • 2,639
  • 18
  • 30
  • 36
BechirM
  • 17
  • 5
  • Just indent the while loop. It should most likely be inside of the `game_loop` function (hence the name game_**loop**). – skrx Jun 12 '18 at 22:06

2 Answers2

0
   thing_height = 100
   # this doesn't define it at a global scope. 
   gameExit = False
## gameExit is not defined
while not gameExit:

The problem is that you don't have that value gameExit defined yet. Consider defining it earlier at a global scope.

pygame.init()

gameExit=False
display_width = 800
corn3lius
  • 4,857
  • 2
  • 31
  • 36
0

This is a scoping issue. Your variable gameExit is only defined inside the functions that use it.

The easiest way to fix this would be to declare it to False at the start of the program and put global gameExit at the top of all of your functions that set it, thus making sure they access the global version of it and don't just re-define it.

For you're purposes, I'm sure this is fine, but you might want to check out the top answer on this question.

J. Doe
  • 305
  • 1
  • 11
  • Having a global at the start of the function is redundant unless he changes the value of that variable. – yakobyd Jun 12 '18 at 22:52
  • @yakobyd Which he does... look through his code. Specifically, his `game_loop` function – J. Doe Jun 12 '18 at 23:15
  • I have not sayed it is redundant in that scenario I am just correcting his statement "put global gameExit at the top of all of your functions that use it". – yakobyd Jun 12 '18 at 23:31
  • @yakobyd You're right. "Changed it" would be better. Edited – J. Doe Jun 13 '18 at 00:24