943,610 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 470
  • Python RSS
Jun 29th, 2009
0

Closing Program on ESCAPE

Expand Post »
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)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
arinlares is offline Offline
5 posts
since Jun 2009
Jun 30th, 2009
0

Re: Closing Program on ESCAPE

python Syntax (Toggle Plain Text)
  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
Reputation Points: 27
Solved Threads: 3
Light Poster
Grigor is offline Offline
26 posts
since Jan 2007
Jun 30th, 2009
0

Re: Closing Program on ESCAPE

Cool. I'd seen the Pygame keys, but they do no good if you don 't know what to do with them. Thank you!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
arinlares is offline Offline
5 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: non-static zeropadding length
Next Thread in Python Forum Timeline: IDE for python





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC