robbiwood

Untitled

Jan 13th, 2013
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 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 len(enemies) > 0 and enemies[-1].x > 590:
  61. enemyspeed = -4
  62. for count in range(len(enemies)):
  63. enemies[count].y += 5
  64.  
  65. if len(enemies) > 0 and 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.  
  82. for count in range(0, len(enemies)):
  83. if Intersect(ourmissile.x, ourmissile.y, enemies[count].x, enemies[count].y):
  84. score += 1
  85. ourmissile.y = 480
  86. del enemies[count]
  87. break
  88.  
  89. if Intersect(ourmissile.x, ourmissile.y, enemymissile.x, enemymissile.y):
  90. ourmissile.y = 480
  91. enemymissile.y = 480
  92.  
  93. if len(enemies) == 0:
  94. sandwich.y += 2
  95. if Intersect(hero.x, hero.y, sandwich.x, sandwich.y):
  96. score += 10
  97. quit = 1
  98. print "YEEEEE GOT ME MAH SAMMICH!"
  99.  
  100. for ourevent in event.get():
  101. if ourevent.type == QUIT:
  102. quit = 1
  103. if ourevent.type == KEYDOWN:
  104. if ourevent.key == K_RIGHT and hero.x < 590:
  105. hero.x += 3
  106. if ourevent.key == K_LEFT and hero.x > 10:
  107. hero.x -= 3
  108. if ourevent.key == K_SPACE:
  109. ourmissile.x = hero.x
  110. ourmissile.y = hero.y
  111.  
  112. enemymissile.render()
  113. enemymissile.y += 5
  114.  
  115. hero.render()
  116.  
  117. sandwich.render()
  118.  
  119. display.update()
  120.  
  121. print "You scored", score, "/30"
  122. print "It took you", (datetime.now() - startTime), "to play the game."
Advertisement
Add Comment
Please, Sign In to add comment