Reputation: 31
I am attempting to write a program in Python 3.6, with the help of pygame (A Python module) which is supposed to flash Red, Green, and Blue quickly on the screen. The program runs as intended for approximately ten to fifteen seconds before it stops responding. (I have noticed that only 3 events are printed to the console, when there should be A LOT more.)
import pygame
import threading
import time
'''
IMPORTS ARE ABOVE THIS LINE
'''
class EventHandler(threading.Thread):
def run(self):
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
'''
CLASSES ARE ABOVE THIS LINE
'''
# Initializer
gameInit = pygame.init()
# Colors
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
# Setup Crap
gameDisplay = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Pygame Colors")
# Event Handler
handler = EventHandler()
handler.start()
# Game Loop
while True:
gameDisplay.fill(red)
pygame.display.update()
time.sleep(0.1)
gameDisplay.fill(green)
pygame.display.update()
time.sleep(0.1)
gameDisplay.fill(blue)
pygame.display.update()
time.sleep(0.1)
Upvotes: 2
Views: 3001
Reputation: 20438
You need a while loop in the run
method and also put the main loop into a function.
import pygame
import threading
import time
class EventHandler(threading.Thread):
def run(self):
while True:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameInit = pygame.init()
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
gameDisplay = pygame.display.set_mode((800, 600))
def main_loop():
while True:
gameDisplay.fill(red)
pygame.display.update()
time.sleep(0.4)
gameDisplay.fill(green)
pygame.display.update()
time.sleep(0.4)
gameDisplay.fill(blue)
pygame.display.update()
time.sleep(0.4)
handler = EventHandler()
handler.start()
t = threading.Thread(target=main_loop)
t.start()
However, there's really no need for threading
in this case and the code looks rather strange to me. You can just use pygame.time.get_ticks()
to calculate the passed time and then change the color when it's above your time limit. itertools.cycle
is pretty handy if you want to cycle through several values infinitely.
import itertools
import pygame
pygame.init()
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
# An infinite iterator that cycles through these colors.
colors = itertools.cycle((red, green, blue))
color = next(colors)
gameDisplay = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
start_time = 0
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
current_time = pygame.time.get_ticks()
if current_time - start_time > 500: # 500 milliseconds.
color = next(colors)
start_time = current_time
gameDisplay.fill(color)
pygame.display.flip()
clock.tick(60)
pygame.quit()
Upvotes: 2
Reputation: 375
I would suggest that your code was running too fast, and to increase the time.sleep
value.
Upvotes: 0