Pygame -error

+1 Samuel Abusamra · December 18, 2014
Thank you for these great tutorials. I was using "Pygame (Python Game Development) Tutorial - 14 - Adding Text to the Screen" but the last time i run the script i get an error


import pygame
import time

pygame.init()

#colors

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

display_width = 800
display_hieght = 600


gameDisplay = pygame.display.set_mode((display_width,display_hieght)) #set width, hieght #hardcoding
pygame.display.set_caption('Crusader') #set title

gameExit = False

lead_x = display_width/2
lead_y = display_hieght/2
lead_x_change = 0
lead_y_change = 0


clock = pygame.time.Clock() #"import" FPS

block_size = 10
FPS = 30 #placement matters

font = pygame.font.SysFont(None, 25)

def message_to_screen(msg,color):
    screen_text = font.render(msg, True, color)
    gameDisplay.blit(screen_text, [display_width/2,display_hieght/2]

while not gameExit:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameExit = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                lead_x_change = -block_size
                lead_y_change = 0
            if event.key == pygame.K_RIGHT:
                lead_x_change = block_size
                lead_y_change = 0    
            if event.key == pygame.K_UP:
                lead_y_change = -block_size
                lead_x_change = 0
            if event.key == pygame.K_DOWN:
                lead_y_change = block_size
                lead_x_change = 0

#GameOver, set boundries for up,down, left, right
    if lead_x >= display_width or lead_x < 0 or lead_y >= display_hieght or lead_y < 0:
        gameExit = True

    lead_x += lead_x_change
    lead_y += lead_y_change
    gameDisplay.fill(red)
    pygame.draw.rect(gameDisplay, black, [lead_x, lead_y, block_size, block_size]) #size of rectanlge + location
    pygame.display.update()
    
    clock.tick(FPS) #frames per second 

message_to_screen("Lose", red)
pygame.display.update()
time.sleep(2)
pygame.quit()
quit()



It tells me that "while" from while not gameExit is a syntax error, but when the code is run in the video it works fine. Any ideas please?

Post a Reply

Replies

Oldest  Newest  Rating
0 Steven the awesome · December 18, 2014
You are missing a ) behind 

gameDisplay.blit(screen_text, [display_width/2,display_hieght/2]
0 Samuel Abusamra · December 20, 2014
Thanks for the reply, figured out why before, but i guess my post didn't submit
  • 1

Python

103,568 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator