Latest Awards
Complete 10 python programs which focus on loops.

morgan

Score over 100 points in total

vincebanter10

Complete any 5 python programs

vincebanter10

Complete 3 python list based tasks

vincebanter10

Complete 5 python list based tasks

vincebanter10

Complete 10 python list based tasks

dman12

Score over 1000 points in total

april-logan

Complete any pygame task

april-logan

Complete any 3 pygame tasks

april-logan

You have to complete 5 pygame tasks

april-logan

Score over 500 points in total

sam-edmund

Complete any pygame task

sam-edmund

Complete any 3 pygame tasks

sam-edmund

You have to complete 5 pygame tasks

sam-edmund

Complete any pygame task

12wrigleyf

Complete any 3 pygame tasks

12wrigleyf

You have to complete 5 pygame tasks

12wrigleyf

Score over 500 points in total

jhoughton12

Complete any 10 python prograns

jhoughton12

Complete 10 python programs which focus on loops.

jhoughton12

Python Score
tasks = 0
achivements = 0
freestyle = 0
Total = 0
Top 5 Scores
114gilo 3400
2joss-nolan-2 2470
3alexdawkins 2445
4byrnebrian 1980
512wattsl 1690
Click to view all scores
Who else completed this task?
No one has completed this task yet. Be the first!

Key events

Syntax

for
e
in
event.
get()
if e.type
==
KEYDOWN
:
if e.key
==
K_a
:

The event loop in pygame is a common feature of 99% of all pygame projects and as such, is one which you must master. It will always be found in the game loop and takes the form of a for loop. Regardless of the type of event you wish to manage, the loop will always have the same structure. When dealing with keys it is important to remember that this responds to events rather than waits for them.

Some sample code to try –

from pygame import *

init()
# we only need a screen to get access to the events!
screen = display.set_mode((640, 480))
display.set_caption('The amazing key presser!')

endProgram = False

while not endProgram:
	# pygame event loop
	for e in event.get():
		if e.type == KEYDOWN:
			if (e.key  == K_a):
				print "A was pressed"
			elif (e.key  == K_b):
				print "B was pressed"
			elif (e.key  == K_RETURN):
				print "return was pressed"
			elif e.key == K_ESCAPE:
				endProgram = True
			else:
				print "errorz - wrong key"


 

Helpful links

http://www.pygame.org/docs/ref/event.html – Types of events

http://www.pygame.org/docs/ref/key.html – Key codes used in pygame

http://openbookproject.net/thinkcs/python/english3e/pygame.html – Pygame game loop

 






                  

Leave a Reply

You must be logged in to post a comment.