Reputation: 65
I am attempting to make a game in pygame where a player moves the player rectangle and when they collide with other rectangles, they get points. Right now, I am just trying to get the blue point rectangles to spawn at a regular interval (4 secs) at a random spot on the screen, and disappear 3.5 seconds after that, then reappear at a different spot. However, at some point the program just stops working. It stops right after it prints 'def blue point' (added that for testing) and the pygame screen just stops loading, it says 'not responding' and I have to shut it down. There is no error message, so its hard for me to figure out what is wrong.
I tried to simplify the code, but nothing is really working. I am new to pygame, so I'm stumped.
Here is my code that I think is relevant:
import pygame
import sys
import random
running = True
#set timers:
#lightbluepts timer, 4 sec
time_delay2 = 4000
BLUEPT = pygame.USEREVENT + 2
pygame.time.set_timer(BLUEPT , time_delay2, 7)
#how long blue pts last, 3.5 secs
time_delay3 = 3500
ENDBLPT = pygame.USEREVENT + 3
pygame.init()
while running:
events = pygame.event.get()
for event in events:
if event.type == BLUEPT:
blpt = True
bluerect = deflightbluepts()
pygame.time.set_timer(ENDBLPT, time_delay3, 1)
print('def blue pt')
elif event.type == ENDBLPT:
blpt = False
print('END blue pt')
elif event.type == TIME_UP:
print("tick tock")
screen.fill((0,0,0))
pygame.draw.rect(screen, "green", player_rect)
#draw point rectangles
while blpt:
pygame.draw.rect(screen, "blue", bluerect)
pygame.display.update()
fpsClock.tick_busy_loop(FPS)
Upvotes: 2
Views: 49
Reputation: 210878
while blpt:
is an infinite loop. Replace it with if blpt:
. blpt
only decides whether the blurect
is drawn in the current frame:
while running:
# [...]
screen.fill((0,0,0))
pygame.draw.rect(screen, "green", player_rect)
#draw point rectangles
if blpt:
pygame.draw.rect(screen, "blue", bluerect)
pygame.display.update()
Upvotes: 3