[source lang = "python"]
import pygame
from pygame.locals import *
from random import randint
pygame.init()
screen = pygame.display.set_mode((640,480), FULLSCREEN|HWSURFACE|DOUBLEBUF, 32)
gameRunning = True
while gameRunning:
for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN:
gameRunning = False
rand_col = (randint(0, 255), randint(0,255), randint(0, 255))
screen.lock()
for _ in xrange(100):
rand_pos = (randint(0, 639), randint(0, 479))
screen.set_at(rand_pos, rand_col)
screen.unlock()
pygame.display.flip()
pygame.display.quit()
python and event.get()
When I run this code it seems to wait till an event happens before adding new pixels to the screen. What can I do to get it to run until I click the mouse button.
You should google "pygame event handling" and study the articles you find. then, you can write code to perform a task when the user clicks, presses buttons, closes the window etc.
p.s. you should pick a better title for your thread, such as "how do I handle events in pygame".
p.s. you should pick a better title for your thread, such as "how do I handle events in pygame".
You've made a simple mistake, by putting the rendering code in the same scope as your event handling code. That is, the lines from 'rand_col = (randint(0, 255), randint(0,255), randint(0, 255))' to 'pygame.display.flip()' fall within the for loop that checks for events. If there are no events to process during a frame, the rendering code doesn't get executed either. That's clearly not what you want. Luckily, the problem is easy to correct:
import pygamefrom pygame.locals import *from random import randintpygame.init()screen = pygame.display.set_mode((640,480), FULLSCREEN|HWSURFACE|DOUBLEBUF, 32)gameRunning = Truewhile gameRunning: for event in pygame.event.get(): if event.type == MOUSEBUTTONDOWN: gameRunning = False rand_col = (randint(0, 255), randint(0,255), randint(0, 255)) screen.lock() for _ in xrange(100): rand_pos = (randint(0, 639), randint(0, 479)) screen.set_at(rand_pos, rand_col) screen.unlock() pygame.display.flip()pygame.display.quit()
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement