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 *
dir(pygame)

##Initialize Pygame
pygame.init()

##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
pygame.display.flip()

##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): 
         sys.exit(0) 
      else: 
         print event  



##The Main loop
while True: 
   input(pygame.event.get())

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:
	exit()

elif event.type == KEYDOWN:
	if event.key == K_ESCAPE:
		exit()

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, networking, learning, and sharing knowledge.