'PyGame is Freezing
I don't know why but my PyGame game freezes when starts the game() funcion.
I tried to remove line by line in the function that gives me the problem, and in all attempts the game stills freezing.
The function that gives me the problem is:
def game():
global Bone1_pos
global Bone2_pos
global Bone1_y
global Bone2_y
Bone1_x = 500
Bone2_x = 1100
clock.tick(FPS)
#Pos 1
if Bone1_pos == 1:
Bone1_y = -80
if Bone2_pos == 1:
Bone2_y = -80
#Pos 2
if Bone1_pos == 2:
Bone1_y = -40
if Bone2_pos == 2:
Bone2_y = -40
#Pos 3
if Bone1_pos == 3:
Bone1_y = 25
if Bone2_pos == 3:
Bone2 = 25
pygame.event.get()
BoneUp1.rect = pygame.Rect(Bone1_x, Bone1_y, 75, 173)
BoneDown1.rect = pygame.Rect(Bone1_x, Bone1_y + 450, 75, 173)
BoneUp2.rect = pygame.Rect(Bone2_x, Bone2_y, 75, 173)
BoneDown2.rect = pygame.Rect(Bone2_x, Bone2_y + 450, 75, 173)
#Sprite blit
screen.blit(background, (0, 0))
screen.blit(BoneDown1.image, (Bone1_x, Bone1_y + 400))
screen.blit(BoneUp1.image, (Bone1_x, Bone1_y))
screen.blit(BoneDown2.image, (Bone2_x, Bone2_y + 400))
screen.blit(BoneUp2.image, (Bone2_x, Bone2_y))
screen.blit(spikeUp0.image, (0, 0))
screen.blit(spikeDown0.image, (0, 436))
screen.blit(Dog.image, (Dog.rect.topleft))
if pygame.key.get_pressed()[K_UP]: # Up
Dog.rect.y -= 1
pygame.time.delay(2)
else: # Down
Dog.rect.y += 1
pygame.time.delay(2)
if pygame.key.get_pressed() [K_LEFT]: # Left
Dog.rect.x -= 1
pygame.time.delay(2)
if pygame.key.get_pressed() [K_RIGHT]: # Right
Dog.rect.x += 1
pygame.time.delay(2)
# Barrier
if Dog.rect.x < 0:
Dog.rect.x = 0
elif Dog.rect.x > 822:
Dog.rect.x = 822
# Collison
for s in [spikeUp0, spikeDown0, BoneUp1, BoneDown1, BoneUp2, BoneDown2]:
if pygame.sprite.collide_rect(Dog, s):
pygame.quit()
sys.exit()
pygame.display.update()
Bone1_x -= 1
Bone2_x -= 1
if Bone1_x <= -40 :
Bone1_pos = random.randint(1,3)
Bone1_x = 1100
if Bone2_x <= -40:
Bone2_pos = random.randint(1,3)
Bone2_x = 1100
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
Solution 1:[1]
See Faster version of 'pygame.event.get()'. Why are events being missed and why are the events delayed?. The issue is that pygame.event.get() is called twice:
def game(): # [...] clock.tick(FPS) # [...] pygame.event.get() # [...] for event in pygame.event.get(): if event.type == pygame.QUIT: # [...]
Note, pygame.event.get() gets all the messages and remove them from the queue. So the first call gets the messages, remove them and puts them nowhere. The 2nd call doesn't get any messages at all.
At least you would've to do a "delay" between the calls of pygame.event.get() (e.g. clock.tick(FPS)).
Delete the first call of pygame.event.get() and move the event loop to the begin of game:
def game():
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
# [...]
Further note, it makes no sens to call pygame.quit() and to call pygame.display.update() after that. pygame.quit() uninitialize all pygame modules and causes all further calls to pygame operations to crash.
Don't forget to update the display in the game loop by either pygame.display.update() or pygame.display.flip().
Add a return value to the function game. Return True, when the game loop should continue and False when the application should stop:
def game():
returnCode = True
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
returnCode = False
# [...]
for s in [spikeUp0, spikeDown0, BoneUp1, BoneDown1, BoneUp2, BoneDown2]:
if pygame.sprite.collide_rect(Dog, s):
returnCode = False
# [...]
return returnCode
run = True
while run:
run = game()
pygame.display.update()
pygame.quit()
sys.exit()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
