| | |
Closing Program on ESCAPE
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jun 2009
Posts: 5
Reputation:
Solved Threads: 0
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
python Syntax (Toggle Plain Text)
##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. python Syntax (Toggle Plain Text)
if event.type == QUIT: exit() elif event.type == KEYDOWN: if event.key == K_ESCAPE: exit()
List of pygame keys
![]() |
Similar Threads
- Evaluate my C Program (C)
- Plz Help Urgently In My Program (C++)
- Data Read Error at startup after Power Failure (Windows NT / 2000 / XP)
- Making a Fullscreen form and hiding the Windows Taskbar (Pascal and Delphi)
- Installing NTL Broadband UK (Novell)
- replacing unprintable characters (C)
- Dont want to display command window (Java)
- Pop3 Mail Watcher (Part 1) (Visual Basic 4 / 5 / 6)
- Memory Loss?Tom Coyote Tweak (Windows tips 'n' tweaks)
Other Threads in the Python Forum
- Previous Thread: non-static zeropadding length
- Next Thread: IDE for python
| Thread Tools | Search this Thread |
address aliased anydbm bash beginner bits calling casino changecolor class clear conversion convert corners count cturtle cursor curves definedlines dictionary digital dynamic dynamically events examples excel external file float format frange function gui handling hints homework i/o iframe import info input java line linux list lists loan loop matching mouse multiple number numbers output parsing path port prime programming projects py py2exe pygame python random rational raw_input recursion recursive scrolledtext searchingfile shebang signal singleton string strings subprocess table tails terminal text thread threading time tkinter tlapse tooltip tuple tutorial type ubuntu unicode urllib urllib2 valueerror variable web-scrape whileloop word wxpython





