Closing Program on ESCAPE

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jun 2009
Posts: 5
Reputation: arinlares is an unknown quantity at this point 
Solved Threads: 0
arinlares arinlares is offline Offline
Newbie Poster

Closing Program on ESCAPE

 
0
  #1
Jun 29th, 2009
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

  1. ##Import Modules
  2. import pygame, sys,os
  3. from pygame.locals import *
  4. dir(pygame)
  5.  
  6. ##Initialize Pygame
  7. pygame.init()
  8.  
  9. ##Set Up Screen
  10. window = pygame.display.set_mode((468, 60))
  11. pygame.display.set_caption('Python Tut')
  12. screen = pygame.display.get_surface()
  13.  
  14. ##Construct Monkey Filename
  15. monkey_head_file_name = os.path.join("C:\\Users\\family\Desktop\Arin'sstuff\Programs\pygametut", "chimp.bmp")
  16.  
  17. ##Loading Monkey Head image
  18. monkey_surface = pygame.image.load(monkey_head_file_name)
  19.  
  20. ## Drawing monkey on screen
  21. screen.blit (monkey_surface, (0,0))
  22.  
  23. ##flipping the display
  24. pygame.display.flip()
  25.  
  26. ##Adding a way to quit
  27. def input(events):
  28. for event in events:
  29. if (input(event.key.get_pressed(K_ESCAPE)) == True) or (event.type == QUIT):
  30. sys.exit(0)
  31. else:
  32. print event
  33.  
  34.  
  35.  
  36. ##The Main loop
  37. while True:
  38. 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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 25
Reputation: Grigor is an unknown quantity at this point 
Solved Threads: 3
Grigor's Avatar
Grigor Grigor is offline Offline
Light Poster

Re: Closing Program on ESCAPE

 
0
  #2
Jun 30th, 2009
  1. if event.type == QUIT:
  2. exit()
  3.  
  4. elif event.type == KEYDOWN:
  5. if event.key == K_ESCAPE:
  6. exit()

List of pygame keys
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 5
Reputation: arinlares is an unknown quantity at this point 
Solved Threads: 0
arinlares arinlares is offline Offline
Newbie Poster

Re: Closing Program on ESCAPE

 
0
  #3
Jun 30th, 2009
Cool. I'd seen the Pygame keys, but they do no good if you don 't know what to do with them. Thank you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC