Connecting Tech Pros Worldwide Help | Site Map

problem with pygame

Member
 
Join Date: Apr 2007
Posts: 53
#1: Jul 3 '08
hi guy,when i run my program which contains the following portion of code in the mainloop
Expand|Select|Wrap|Line Numbers
  1. clock=pygame.time.Clock()
  2. while True:
  3.     sound1.play()
  4.     clock.tick(60)
  5.     pKeys = pygame.key.get_pressed()
  6.     Eves=pygame.event.get()
  7.     villian.face()
  8.     villian.hert()
  9.     if Eves[KEYDOWN] and pKeys[K_UP]:
  10.         hero.moveup()
  11.     if Eves[KEYUP] and pKeys[K_UP]:
  12.         hero.gravity()
  13.     if Eves[KEYDOWN] and pKeys[K_DOWN]:
  14.         hero.movedown()
  15.     if Eves[KEYDOWN] and pKeys[K_RIGHT]:
  16.         hero.forward()
  17.     if Eves[KEYDOWN] and pKeys[K_LEFT]:
  18.         hero.back()
  19.     if Eves[KEYDOWN] and pKeys[97]:
  20.         sound.play()
  21.         hero.reShape()
  22.     if Eves[KEYUP] and pKeys[97]:
  23.         hero.Shape()
  24.     if Eves[KEYDOWN] and pKeys[115]:
  25.         hero.Kick()
  26.     if Eves[0]:
  27.         pygame.quit()
  28.         raise SystemExit()
  29.     allsprites.update()
  30.     screen.blit(background,(0,0))
  31.     allsprites.draw(screen)
  32.     pygame.display.flip()
  33.     time.sleep(1/30)
i get this error
Expand|Select|Wrap|Line Numbers
  1. Traceback (most recent call last):
  2.   File "C:\Python24\RealSuperDynamo.py", line 145, in ?
  3.     if Eves[KEYDOWN] and pKeys[K_UP]:
  4. IndexError: list index out of range
can anyone help?Thanks in advance
yoda's Avatar
Needs Regular Fix
 
Join Date: Dec 2006
Location: canada
Posts: 273
#2: May 21 '09

re: problem with pygame


I use this code

Expand|Select|Wrap|Line Numbers
  1. while keepGoing:
  2.         clock.tick(30)
  3.         for event in pygame.event.get():
  4.             if event.type == pygame.QUIT: # if X box clicked, game closes
  5.                 keepGoing = False
  6.             if event.type == pygame.KEYDOWN:
  7.                 if event.key == pygame.K_ESCAPE: #press escape the game closes
  8.                     keepGoing = False
  9.  
  10.         if event.type == pygame.KEYDOWN:
  11.             if event.key == pygame.K_LEFT:
  12.                 circle.moveLeft() # calls your methods to be ran if this key is pressed
  13.             if event.key == pygame.K_RIGHT:
  14.                 circle.moveRight()
  15.             if event.key == pygame.K_SPACE:
  16.                 circle.shoot()
  17.  
This code works well, but i don't know if this is what you are looking for. I'm using Python 2.5.4, wxPython 2.8 , and Pygame 1.8
Reply