I'm working on learning on programming with Pygame by following a tutorial linked from the Pygame site. I seem to have stuck myself in a hole, however, because I can't seem to set keys to do different things within the program. I'm trying to first close the program by pressing the ESC button, but can't seem to find the proper code to do so. Here's what I have so far

##Import Modules
import pygame, sys,os
from pygame.locals import *

##Initialize Pygame

##Set Up Screen
window = pygame.display.set_mode((468, 60))
pygame.display.set_caption('Python Tut')
screen = pygame.display.get_surface()

##Construct Monkey Filename
monkey_head_file_name = os.path.join("C:\\Users\\family\Desktop\Arin'sstuff\Programs\pygametut", "chimp.bmp")

##Loading Monkey Head image
monkey_surface = pygame.image.load(monkey_head_file_name)

## Drawing monkey on screen
screen.blit (monkey_surface, (0,0))

##flipping the display

##Adding a way to quit
def input(events): 
   for event in events: 
      if (input(event.key.get_pressed(K_ESCAPE)) == True) or (event.type == QUIT): 
         print event  

##The Main loop
while True: 

if event.type == QUIT , I can set the sys.exit(0) to if (event.type == KEYDOWN) or (event.type == QUIT , without a problem. But, how do I use a specific key? I went through most of the site's Python code snippets without finding anything, and search the documentation for Pygame and Python to no avail. Thanks in advance for any help.

Recommended Answers

All 2 Replies

if event.type == QUIT:

elif event.type == KEYDOWN:
	if event.key == K_ESCAPE:

List of pygame keys

Cool. I'd seen the Pygame keys, but they do no good if you don 't know what to do with them. Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.