[Tutor] help with my python app
james middendorff
james2dope at yahoo.com
Tue May 17 05:40:30 CEST 2005
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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: newesttest.py
Type: text/x-python
Size: 7004 bytes
Desc: 643111960-newesttest.py
Url : http://mail.python.org/pipermail/tutor/attachments/20050516/a93ff5a6/newesttest-0001.py
More information about the Tutor
mailing list