[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] [PYGAME] Noob Help.
On Thu, Apr 12, 2007 at 08:38:28PM -0500, Lamonte(Scheols/Demonic) wrote:
> Ok I'm just trying to get un-noob with pygame and python it self.
>
> Alright I was reading on a tutorial from the pygame site located here:
> http://www.pygame.org/docs/tut/intro/intro.html
> So I'm trying to find out whats up with this:
> 14 for event in pygame.event.get():
> 15 if event.type == pygame.QUIT: sys.exit()
>
> So i've looked @ the python document for the for loop and im just trying
> to figure out the event.get() array can someone tell me how we got
> event.type and how does pygame.QUIT : sys.exit() work?
>
> Thanks in advanced for the help.
>
> Regards,
>
> Lamonte Harris.
pygame.event.get() is not an array. it is an iterator. Yes, the "for"
command can be used to loop through arrays, but it is also used with
iterators. Since arrays are the concept you are familiar with, think of
an iterator as a special kind of array that can only be read in
order from beginning to end.
Anyway, here is what that block of code does.
> 14 for event in pygame.event.get():
the "for" line iterates through all the available events. This includes
keypresses, mouse clicks, and other stuff too. But for now, what you
probably care about most is the keypresses.
Every time you press a key, a key event is generated. When your "event
loop" runs, it loops through all the new events.
> 15 if event.type == pygame.QUIT: sys.exit()
Now, inside the loop, the "event" variable holds information about the
event. event.type is the kind of event. besides pygame.QUIT you may see
things like pygame.KEYDOWN or pygame.KEYUP
If the event is keyboard related, you will also have access to
event.key which tells you which key the event represents. In my own
code, I like to do something like this, so that both clicking the "X" in
the top right corner and pressing ESC will both exit the program:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.ESC: sys.exit()
---
James Paige