0% found this document useful (0 votes)
89 views1 page

Key Events: Programming in Python: Events and Animation

This document discusses a Python program that draws a circle on the screen based on which keyboard key is pressed. The program initializes Pygame, sets the screen size, and defines color variables. It then contains a drawScene function that gets the state of all keyboard buttons, fills the screen with black, and draws a red circle for each pressed key at an x-coordinate based on the key value and a fixed y-coordinate. The key value is also displayed as the window caption. The main loop gets events, calls drawScene, and quits if the user closes the window.

Uploaded by

Tessa_Gray_
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views1 page

Key Events: Programming in Python: Events and Animation

This document discusses a Python program that draws a circle on the screen based on which keyboard key is pressed. The program initializes Pygame, sets the screen size, and defines color variables. It then contains a drawScene function that gets the state of all keyboard buttons, fills the screen with black, and draws a red circle for each pressed key at an x-coordinate based on the key value and a fixed y-coordinate. The key value is also displayed as the window caption. The main loop gets events, calls drawScene, and quits if the user closes the window.

Uploaded by

Tessa_Gray_
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Programming in Python: Events and Animation Key Events

Here is a simple program that draws a circle relative to the value associated to the keys on the keyboard. The value associated to each key will also be displayed in the windows caption (as long as Num, Caps, and Scroll Lock is turned off). Most key values have already been assigned a constant by Pygame. To see the names, go to http://www.pygame.org/docs/ref/key.html.
from pygame import * init() size = width, height = 800, 600 screen = display.set_mode(size) RED = (255, 0, 0) BLACK = (0, 0, 0) # This method draws a dot on the screen relative to the value of the key pressed def drawScene(screen): keys = key.get_pressed() # gets the state of all keyboard buttons screen.fill(BLACK) for i in range(len(keys)): # go through list of keys if keys[i]: # value will be True if the key is pressed draw.circle(screen, RED, (i*2,100), 2) display.set_caption(str(i)) # puts the key value as the caption display.flip() running = True myClock = time.Clock() while running: for evnt in event.get(): if evnt.type == QUIT: running = False drawScene(screen) myClock.tick(60) quit() # checks all events that happen

You might also like