Reputation: 47
I'm working on creating a Snake Game from a Udemy course and am having trouble getting the Python Game application to launch.
Every time I run my module, the application will load and then just freeze with the Mac spinning pinwheel in the application window.
I'm still in the process of building it, but my code matches the courses and I still cannot launch the game. Here is my code:
`import pygame
import sys
import random
import time`
`check_errors = pygame.init()
if check_errors[1] > 0:
print("(!) Had {0} initializing errors,
exiting...".format(check_errors[1]))
sys.exit(-1)
else:
print("(+) PyGame successfully initialized!")`
`# Play surface
playSurface = pygame.display.set_mode((720, 460))
pygame.display.set_caption('Snake game!')`
`# Colors
red = pygame.Color(255, 0, 0) # gameover
green = pygame.Color(0, 255, 0) # snake
blue = pygame.Color(0, 0, 255)
black = pygame.Color(0, 0, 0) #score
white = pygame.Color(255, 255, 255) #background
brown = pygame.Color(165, 42, 42) #food`
`# FPS controller
fpsController = pygame.time.Clock()`
`# Important Variables
snakePos = [100, 50]
snakeBody = [[100, 50], [90, 50], [80, 50]]`
`foodPos = [random.randrange(1,72)*10,random.randrange(1,46)*10]
foodSpawn = True`
`direction = 'RIGHT'
changeto = direction`
`# Game over function
def game0ver():
myFont = pygame.font.SysFont('monaco', 72)
GOsurf = myFont.render('Game over!', True, red)
GOrect = GOsurf.get_rect()
GOrect.midtop = (360, 15)
playSurface.blit(GOsurf,GOrect)
pygame.display.flip()`
`gameOver()
time.sleep(10)`
Upvotes: 1
Views: 96
Reputation: 3245
You need to handle events so your operating system doesn't think your application has crashed. Even if your application is still a work in progress you'll something like the following:
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
Any pygame application needs a game loop that:
Upvotes: 1