Advertisement
Guest User

Sandwich in my game needs to go down...

a guest
Jan 11th, 2013
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. from pygame import *
  2. import random
  3. from datetime import datetime
  4.  
  5. startTime = datetime.now()
  6.  
  7.  
  8. class Sprite:
  9. def __init__(self, xpos, ypos, filename):
  10. self.x = xpos
  11. self.y = ypos
  12. self.bitmap = image.load(filename)
  13. self.bitmap.set_colorkey((0, 0, 0))
  14.  
  15. def set_position(self, xpos, ypos):
  16. self.x = xpos
  17. self.y = ypos
  18.  
  19. def render(self):
  20. screen.blit(self.bitmap, (self.x, self.y))
  21.  
  22.  
  23. def Intersect(s1_x, s1_y, s2_x, s2_y):
  24. if (s1_x > s2_x - 32) and (s1_x < s2_x + 32) and (s1_y > s2_y - 32) and (s1_y < s2_y + 32):
  25. return 1
  26. else:
  27. return 0
  28.  
  29. init()
  30. screen = display.set_mode((640, 480))
  31. key.set_repeat(1, 1)
  32. display.set_caption('PyInvaders')
  33. backdrop = image.load('data/backdrop.bmp')
  34.  
  35. enemies = []
  36.  
  37. x = 0
  38.  
  39. for count in range(10):
  40. enemies.append(Sprite(50 * x + 50, 50, 'data/enemy.bmp'))
  41. enemies.append(Sprite(50 * x + 50, 100, 'data/enemy.bmp'))
  42. x += 1
  43.  
  44. hero = Sprite(304, 400, 'data/hero.bmp')
  45. ourmissile = Sprite(0, 480, 'data/heromissile.bmp')
  46. enemymissile = Sprite(0, 480, 'data/enemymissile.bmp')
  47. sandwich = Sprite(304, 20, 'data/sandwich.bmp')
  48.  
  49. quit = 0
  50. score = 0
  51. enemyspeed = 4
  52.  
  53. while quit == 0:
  54. screen.blit(backdrop, (0, 0))
  55.  
  56. for count in range(len(enemies)):
  57. enemies[count].x += + enemyspeed
  58. enemies[count].render()
  59.  
  60. if enemies[len(enemies) - 1].x > 590:
  61. enemyspeed = -4
  62. for count in range(len(enemies)):
  63. enemies[count].y += 5
  64.  
  65. if enemies[0].x < 10:
  66. enemyspeed = 4
  67. for count in range(len(enemies)):
  68. enemies[count].y += 5
  69.  
  70. if ourmissile.y < 479 and ourmissile.y > 0:
  71. ourmissile.render()
  72. ourmissile.y -= 5
  73.  
  74. if enemymissile.y >= 480 and len(enemies) > 0:
  75. enemymissile.x = enemies[random.randint(0, len(enemies) - 1)].x
  76. enemymissile.y = enemies[0].y
  77.  
  78. if Intersect(hero.x, hero.y, enemymissile.x, enemymissile.y):
  79. quit = 1
  80. print "...where mah sammich."
  81. print "You scored:", score, "/20"
  82.  
  83. for count in range(0, len(enemies)):
  84. if Intersect(ourmissile.x, ourmissile.y, enemies[count].x, enemies[count].y):
  85. score += 1
  86. ourmissile.y = 480
  87. del enemies[count]
  88. break
  89.  
  90. if Intersect(ourmissile.x, ourmissile.y, enemymissile.x, enemymissile.y):
  91. ourmissile.y = 480
  92. enemymissile.y = 480
  93.  
  94. if Intersect(hero.x, hero.y, sandwich.x, sandwich.y):
  95. score += 10
  96. quit = 1
  97. print "YEEEEE GOT ME MAH SAMMICH!"
  98. print "You scored", score, "/20"
  99.  
  100. if len(enemies) == 0:
  101. quit = 1
  102. print "...where mah sammich."
  103. print "You scored", score, "/20"
  104.  
  105. for ourevent in event.get():
  106. if ourevent.type == QUIT:
  107. quit = 1
  108. if ourevent.type == KEYDOWN:
  109. if ourevent.key == K_RIGHT and hero.x < 590:
  110. hero.x += 3
  111. if ourevent.key == K_LEFT and hero.x > 10:
  112. hero.x -= 3
  113. if ourevent.key == K_UP:
  114. hero.y -= 3
  115. if ourevent.key == K_DOWN:
  116. hero.y += 3
  117. if ourevent.key == K_SPACE:
  118. ourmissile.x = hero.x
  119. ourmissile.y = hero.y
  120.  
  121. enemymissile.render()
  122. enemymissile.y += 5
  123.  
  124. hero.render()
  125.  
  126. sandwich.render()
  127.  
  128. display.update()
  129.  
  130. print "It took you", (datetime.now() - startTime), "to play the game."
  131.  
  132. That is the code for the game, with no help from reddit. The following is the error I get when I replace
  133.  
  134. if len(enemies) == 0:
  135. quit = 1
  136. print "...where mah sammich."
  137. print "You scored", score, "/20"
  138.  
  139. with
  140.  
  141. if len(enemies) == 0:
  142. sandwich += 5
  143. print "...where mah sammich."
  144. print "You scored", score, "/20"
  145.  
  146. The error:
  147.  
  148. if enemies[len(enemies) - 1].x > 590:
  149. IndexError: list index out of range
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement