[Tutor] help with my python app
Alan G
alan.gauld at freenet.co.uk
Tue May 17 09:20:06 CEST 2005
Hi James,
I can't answer your question because I don't know anything
about pygame, but your code would look a lot better if you
wrote a function (handleKeyEvent() say?) to avoid repeating
all that code. One of the big advantages of using functions
- aside from saving typing effort - is that they make the
code more readable by replacing a lot of detail with a
short descriptive phrase(the functions name). The other
advantage is that if the code has to be fixed you only
change it in one place so there's less chance of you
accidentaly missing out one of the event handlers.
If the code is slightly diffeent each time - although from
a quick look this doesn't seem to be the case here - you
can parameterise the function so you can pass in either the
changing values or some flag to indicate which variant you
need. It's all part of helping us to see the wood instead of
the trees...
HTH
Alan G.
----- Original Message -----
From: "james middendorff" <james2dope at yahoo.com>
To: <tutor at python.org>
Sent: Tuesday, May 17, 2005 4:40 AM
Subject: [Tutor] help with my python app
> Hello, I would like to be able to use the arrow keys
> to control a remote control car, which I can do but
> only one key at a time. I would like to press the up
> key, then while it is moving forward, press the left
> or right key to turn while it is moving forward? I am
> sure there are probably better ways to write the code,
> I am still learning all of this. Also if this
> indention is off I am including the file
> thanks
>
> #!/usr/bin/python
> import parallel
> import pygame
> from pygame.locals import *
>
> p=parallel.Parallel()
> p.setData(0)
>
>
>
>
>
>
>
> def main():
> # Initialise screen
> pygame.init()
> screen = pygame.display.set_mode((640, 480))
> pygame.display.set_caption("James' custom RC
> Car Application")
>
> # Fill background
> background = pygame.Surface(screen.get_size())
> background = background.convert()
> background.fill((250, 250, 250))
>
> # Display some text
> font = pygame.font.Font(None, 36)
> text = font.render("Which Way? ", 1, (10, 10,
> 10))
> textpos = text.get_rect()
> textpos.centerx =
> background.get_rect().centerx
> background.blit(text, textpos)
>
> # Blit everything to the screen
> screen.blit(background, (0, 0))
> pygame.display.flip()
>
> # Event loop
> while 1:
> for event in pygame.event.get():
> if event.type == QUIT:
> return
> elif event.type == KEYDOWN:
> if event.key == K_UP:
> background.fill((250, 250, 250))
> font = pygame.font.Font(None, 36)
> text = font.render("Forward", 1, (10, 10,
> 10))
> textpos = text.get_rect()
> textpos.centerx =
> background.get_rect().centerx
> background.blit(text, textpos)
> screen.blit(background, (0, 0))
> pygame.display.flip()
> screen.blit(background, (0, 0))
> pygame.display.flip()
> p.setData(1)
>
>
> if event.key == K_DOWN:
> background.fill((250, 250, 250))
> font =
> pygame.font.Font(None, 36)
> text =
> font.render("Reverse", 1, (10, 10, 10))
> textpos =
> text.get_rect()
>
> textpos.centerx = background.get_rect().centerx
>
> background.blit(text, textpos)
>
> screen.blit(background, (0, 0))
>
> pygame.display.flip()
>
> screen.blit(background, (0, 0))
>
> pygame.display.flip()
> p.setData(2)
>
>
> if event.key == K_LEFT:
> background.fill((250, 250, 250))
> font =
> pygame.font.Font(None, 36)
> text =
> font.render("LEFT", 1, (10, 10, 10))
> textpos =
> text.get_rect()
> textpos.centerx =
> background.get_rect().centerx
> background.blit(text,
> textpos)
>
> screen.blit(background, (0, 0))
> pygame.display.flip()
>
> screen.blit(background, (0, 0))
> pygame.display.flip()
> p.setData(8)
>
>
> if event.key == K_RIGHT:
> background.fill((250, 250, 250))
> font =
> pygame.font.Font(None, 36)
> text =
> font.render("RIGHT", 1, (10, 10, 10))
> textpos =
> text.get_rect()
>
> textpos.centerx = background.get_rect().centerx
>
> background.blit(text, textpos)
>
> screen.blit(background, (0, 0))
>
> pygame.display.flip()
>
> screen.blit(background, (0, 0))
>
> pygame.display.flip()
> p.setData(4)
>
>
>
> elif event.type == KEYUP:
> if event.key == K_UP:
> background.fill((250, 250, 250))
> font =
> pygame.font.Font(None, 36)
> text =
> font.render("Which Way? ", 1, (10, 10, 10))
> textpos =
> text.get_rect()
>
> textpos.centerx = background.get_rect().centerx
>
> background.blit(text, textpos)
>
> screen.blit(background, (0, 0))
>
> pygame.display.flip()
>
> screen.blit(background, (0, 0))
>
> pygame.display.flip()
> p.setData(0)
>
>
> if event.key == K_DOWN:
>
> background.fill((250, 250, 250))
> font =
> pygame.font.Font(None, 36)
> text =
> font.render("Which way? ", 1, (10, 10, 10))
> textpos =
> text.get_rect()
>
> textpos.centerx = background.get_rect().centerx
>
> background.blit(text, textpos)
>
> screen.blit(background, (0, 0))
>
> pygame.display.flip()
>
> screen.blit(background, (0, 0))
>
> pygame.display.flip()
> p.setData(0)
>
>
> if event.key == K_LEFT:
>
> background.fill((250, 250, 250))
> font =
> pygame.font.Font(None, 36)
> text =
> font.render("Which way? ", 1, (10, 10, 10))
> textpos =
> text.get_rect()
>
> textpos.centerx = background.get_rect().centerx
>
> background.blit(text, textpos)
>
> screen.blit(background, (0, 0))
>
> pygame.display.flip()
>
> screen.blit(background, (0, 0))
>
> pygame.display.flip()
> p.setData(0)
>
>
> if event.key == K_RIGHT:
>
> background.fill((250, 250, 250))
> font =
> pygame.font.Font(None, 36)
> text =
> font.render("Which way? ", 1, (10, 10, 10))
> textpos =
> text.get_rect()
>
> textpos.centerx = background.get_rect().centerx
>
> background.blit(text, textpos)
>
> screen.blit(background, (0, 0))
>
> pygame.display.flip()
>
> screen.blit(background, (0, 0))
>
> pygame.display.flip()
> p.setData(0)
>
> if __name__ == '__main__': main()
>
> "I would kill everyone in this room
> for a drop of sweet beer."
> ----Homer Simpson----
>
>
>
> Yahoo! Mail
> Stay connected, organized, and protected. Take the tour:
> http://tour.mail.yahoo.com/mailtour.html
>
----------------------------------------------------------------------
----------
> #!/usr/bin/python
> import parallel
> import pygame
> from pygame.locals import *
>
> p=parallel.Parallel()
> p.setData(0)
>
>
>
>
>
>
>
> def main():
> # Initialise screen
> pygame.init()
> screen = pygame.display.set_mode((640, 480))
> pygame.display.set_caption("James' custom RC Car
Application")
>
> # Fill background
> background = pygame.Surface(screen.get_size())
> background = background.convert()
> background.fill((250, 250, 250))
>
> # Display some text
> font = pygame.font.Font(None, 36)
> text = font.render("Which Way? ", 1, (10, 10, 10))
> textpos = text.get_rect()
> textpos.centerx = background.get_rect().centerx
> background.blit(text, textpos)
>
> # Blit everything to the screen
> screen.blit(background, (0, 0))
> pygame.display.flip()
>
> # Event loop
> while 1:
> for event in pygame.event.get():
> if event.type == QUIT:
> return
> elif event.type == KEYDOWN:
> if event.key == K_UP:
> background.fill((250, 250, 250))
> font = pygame.font.Font(None, 36)
> text = font.render("Forward", 1, (10, 10, 10))
> textpos = text.get_rect()
> textpos.centerx = background.get_rect().centerx
> background.blit(text, textpos)
> screen.blit(background, (0, 0))
> pygame.display.flip()
> screen.blit(background, (0, 0))
> pygame.display.flip()
> p.setData(1)
>
>
> if event.key == K_DOWN:
> background.fill((250, 250, 250))
> font =
pygame.font.Font(None, 36)
> text =
font.render("Reverse", 1, (10, 10, 10))
> textpos = text.get_rect()
> textpos.centerx =
background.get_rect().centerx
> background.blit(text,
textpos)
> screen.blit(background, (0,
0))
> pygame.display.flip()
> screen.blit(background, (0,
0))
> pygame.display.flip()
> p.setData(2)
>
>
> if event.key == K_LEFT:
> background.fill((250, 250, 250))
> font = pygame.font.Font(None, 36)
> text = font.render("LEFT", 1, (10,
10, 10))
> textpos = text.get_rect()
> textpos.centerx =
background.get_rect().centerx
> background.blit(text, textpos)
> screen.blit(background, (0, 0))
> pygame.display.flip()
> screen.blit(background, (0, 0))
> pygame.display.flip()
> p.setData(8)
>
>
> if event.key == K_RIGHT:
> background.fill((250, 250, 250))
> font =
pygame.font.Font(None, 36)
> text = font.render("RIGHT",
1, (10, 10, 10))
> textpos = text.get_rect()
> textpos.centerx =
background.get_rect().centerx
> background.blit(text,
textpos)
> screen.blit(background, (0,
0))
> pygame.display.flip()
> screen.blit(background, (0,
0))
> pygame.display.flip()
> p.setData(4)
>
>
>
> elif event.type == KEYUP:
> if event.key == K_UP:
> background.fill((250, 250, 250))
> font =
pygame.font.Font(None, 36)
> text = font.render("Which
Way? ", 1, (10, 10, 10))
> textpos = text.get_rect()
> textpos.centerx =
background.get_rect().centerx
> background.blit(text,
textpos)
> screen.blit(background, (0,
0))
> pygame.display.flip()
> screen.blit(background, (0,
0))
> pygame.display.flip()
> p.setData(0)
>
>
> if event.key == K_DOWN:
> background.fill((250, 250,
250))
> font =
pygame.font.Font(None, 36)
> text = font.render("Which
way? ", 1, (10, 10, 10))
> textpos = text.get_rect()
> textpos.centerx =
background.get_rect().centerx
> background.blit(text,
textpos)
> screen.blit(background, (0,
0))
> pygame.display.flip()
> screen.blit(background, (0,
0))
> pygame.display.flip()
> p.setData(0)
>
>
> if event.key == K_LEFT:
> background.fill((250, 250,
250))
> font =
pygame.font.Font(None, 36)
> text = font.render("Which
way? ", 1, (10, 10, 10))
> textpos = text.get_rect()
> textpos.centerx =
background.get_rect().centerx
> background.blit(text,
textpos)
> screen.blit(background, (0,
0))
> pygame.display.flip()
> screen.blit(background, (0,
0))
> pygame.display.flip()
> p.setData(0)
>
> if event.key == K_RIGHT:
> background.fill((250, 250,
250))
> font =
pygame.font.Font(None, 36)
> text = font.render("Which
way? ", 1, (10, 10, 10))
> textpos = text.get_rect()
> textpos.centerx =
background.get_rect().centerx
> background.blit(text,
textpos)
> screen.blit(background, (0,
0))
> pygame.display.flip()
> screen.blit(background, (0,
0))
> pygame.display.flip()
> p.setData(0)
>
> if __name__ == '__main__': main()
>
More information about the Tutor
mailing list