130 likes | 303 Views
Pygame Events. Lecture 08. What is an Event?. We’ve been using them already. for event in pygame.event.get (): if event. type == pygame.QUIT : done = True. When to Use Events. Anytime you’re looking for input from the user Specifically keys, or mouse clicks
E N D
Pygame Events Lecture 08
What is an Event? We’ve been using them already for event inpygame.event.get(): ifevent.type == pygame.QUIT: done = True
When to Use Events Anytime you’re looking for input from the user Specifically keys, or mouse clicks Events are processed as fast as the FPS
Up Arrow - Pressed Let’s detect when the up arrow is pressed. Then print to the console when it is. for event inpygame.event.get(): ifevent.type == pygame.QUIT: done = True ifevent.type == pygame.KEYDOWN: ifevent.key == pygame.K_UP: print"The up arrow was pressed!"
Up Arrow - Released Events can also be on key releases ifevent.type == pygame.KEYDOWN: ifevent.key == pygame.K_UP: print"The up arrow was pressed!" ifevent.type == pygame.KEYUP: ifevent.key == pygame.K_UP: print"The up arrow was released!"
All Arrow Keys ifevent.type == pygame.KEYDOWN: ifevent.key == pygame.K_UP: print"The up arrow was pressed!" ifevent.key == pygame.K_DOWN: print"The down arrow was pressed!" ifevent.key == pygame.K_RIGHT: print"The right arrow was pressed!" ifevent.key == pygame.K_LEFT: print"The left arrow was pressed!" ifevent.type == pygame.KEYUP: ifevent.key == pygame.K_UP: print"The up arrow was released!" ifevent.key == pygame.K_DOWN: print"The down arrow was released!" ifevent.key == pygame.K_RIGHT: print"The right arrow was released!" ifevent.key == pygame.K_LEFT: print"The left arrow was released!"
Other Keys? If you want to use other keys (WASD) then go to: http://www.pygame.org/docs/ref/key.html
Control an Entity We just need to change what happens on events ifevent.type == pygame.KEYDOWN: ifevent.key == pygame.K_UP: dude.dy = -3 ifevent.key == pygame.K_DOWN: dude.dy = 3 ifevent.key == pygame.K_RIGHT: dude.dx = 3 ifevent.key == pygame.K_LEFT: dude.dx = -3
So That’s Basically a Game We can do all kinds of stuff