-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
839d408
commit 5481956
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
Keyboard Events | ||
================ | ||
|
||
Event in pygame are handled through the pygame.event module. | ||
The pygame.event.get() method gets all the events from the event queue. | ||
The event queue is a list of event objects that are sent to the program by the window manager | ||
or by the pygame program itself. | ||
The pygame.event.get() method returns a list of pygame.event.Event objects. | ||
Each Event object has an event type stored in the type attribute. | ||
The type attribute can be compared to the constants defined in the pygame.locals module. | ||
The pygame.locals module defines constants for the event types. | ||
The event types are used to determine what type of event has occurred. | ||
The event types are listed below: | ||
|
||
+ KEYDOWN | ||
+ KEYUP | ||
+ MOUSEMOTION | ||
+ MOUSEBUTTONDOWN | ||
+ MOUSEBUTTONUP | ||
|
||
KEYDOWN is an event type that is fired when a key is pressed down. like when you press a key on the keyboard. | ||
|
||
KEYUP is an event type that is fired when a key is released. like when you lift your finger off a key. | ||
|
||
.. activecode:: ac_l60_6_en | ||
:language: python3 | ||
:python3_interpreter: brython | ||
|
||
from browser import load, timer | ||
load('../../_static/game.js') | ||
load('../../_static/pygame.brython.js') | ||
|
||
^^^^ | ||
import pygame | ||
from pygame.locals import * | ||
|
||
# Pygame to show keyevents | ||
# green box moves with arrow keys | ||
|
||
# set up the colors | ||
BLACK = pygame.color.Color('black') | ||
WHITE = pygame.color.Color('white') | ||
GREEN = pygame.color.Color('green') | ||
|
||
screen = pygame.display.set_mode((250, 250)) | ||
screen.fill(WHITE) | ||
|
||
def main(): | ||
# set up the block | ||
block = pygame.Rect(100, 100, 50, 50) | ||
|
||
# draw the white background onto the surface | ||
|
||
|
||
# draw a green box onto the surface | ||
pygame.draw.rect(screen, GREEN, block) | ||
|
||
# keyboard event loop | ||
for event in pygame.event.get(): | ||
if event.type == pygame.KEYDOWN: | ||
print('key down') | ||
if event.key == pygame.K_LEFT: | ||
print('left arrow pressed') | ||
block.left -= 10 | ||
if event.key == pygame.K_RIGHT: | ||
print('right arrow pressed') | ||
block.left += 10 | ||
if event.key == pygame.K_UP: | ||
print('up arrow pressed') | ||
block.top -= 10 | ||
if event.key == pygame.K_DOWN: | ||
print('down arrow pressed') | ||
block.top += 10 | ||
|
||
|
||
|
||
# draw the window onto the screen | ||
pygame.display.update() | ||
|
||
pygame.init() | ||
|
||
timer1 = timer.set_interval(main,80) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,4 @@ Pygame | |
TWP60_3_en.rst | ||
TWP60_4_en.rst | ||
TWP60_5_en.rst | ||
TWP60_6_en.rst |