Skip to content

Commit

Permalink
Pygame controls
Browse files Browse the repository at this point in the history
  • Loading branch information
sujith-rek committed Jul 31, 2023
1 parent 839d408 commit 5481956
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
85 changes: 85 additions & 0 deletions _sources/lectures/TWP60/TWP60_6_en.rst
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)



1 change: 1 addition & 0 deletions _sources/lectures/TWP60/toctree_en.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ Pygame
TWP60_3_en.rst
TWP60_4_en.rst
TWP60_5_en.rst
TWP60_6_en.rst

0 comments on commit 5481956

Please sign in to comment.