Advertisement
Guest User

Untitled

a guest
Apr 14th, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. import pygame, sys, random
  2.  
  3. pygame.init()
  4.  
  5. screen_width = 800
  6. screen_height = 600
  7. screen = pygame.display.set_mode([screen_width, screen_height])
  8. all_sprites_list = pygame.sprite.Group()
  9. block_list = pygame.sprite.Group()
  10. bullet_list = pygame.sprite.Group()
  11. player_list = pygame.sprite.Group()
  12. BLACK = ( 0, 0, 0)
  13. WHITE = ( 255, 255, 255)
  14. background = pygame.image.load('space.jpg')
  15. pygame.display.set_caption("Alien Invasion!")
  16. explosion = pygame.mixer.Sound('explosion.wav')
  17. score = 0
  18.  
  19. class Block(pygame.sprite.Sprite):
  20. def __init__(self, color):
  21. pygame.sprite.Sprite.__init__(self)
  22. self.image = pygame.image.load("spaceship.png")
  23. self.rect = self.image.get_rect()
  24.  
  25. def update(self):
  26. self.rect.y += 1
  27.  
  28. class Player(pygame.sprite.Sprite):
  29. def __init__(self):
  30. pygame.sprite.Sprite.__init__(self)
  31. self.x=0
  32. self.y=0
  33. self.image = pygame.image.load("alien.png")
  34. self.rect = self.image.get_rect()
  35.  
  36. def update(self):
  37. pos = pygame.mouse.get_pos()
  38. self.rect.x = pos[0]
  39.  
  40. def render(self):
  41. if (self.currentImage==0):
  42. screen.blit(self.image, (self.x, self.y))
  43.  
  44. class Bullet(pygame.sprite.Sprite):
  45. def __init__(self):
  46. pygame.sprite.Sprite.__init__(self)
  47. self.image = pygame.image.load("bullet.png")
  48. self.rect = self.image.get_rect()
  49.  
  50. def update(self):
  51. self.rect.y -= 9
  52.  
  53. for i in range(15):
  54. block = Block(BLACK)
  55. block.rect.x = random.randrange(screen_width)
  56. block.rect.y = random.randrange(150)
  57. block_list.add(block)
  58. all_sprites_list.add(block)
  59.  
  60. player = Player()
  61.  
  62. all_sprites_list.add(player)
  63.  
  64. done = False
  65.  
  66. clock = pygame.time.Clock()
  67.  
  68. player.rect.y = 480
  69.  
  70. k_space = False
  71.  
  72. while True:
  73. for event in pygame.event.get():
  74. if event.type == pygame.QUIT:
  75. pygame.quit()
  76. sys.exit()
  77. if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
  78. pygame.quit()
  79. sys.exit()
  80.  
  81. elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
  82. k_space = True
  83.  
  84. if k_space:
  85. bullet = Bullet()
  86. bullet.rect.x = player.rect.x
  87. bullet.rect.y = player.rect.y
  88. all_sprites_list.add(bullet)
  89. bullet_list.add(bullet)
  90.  
  91. k_space = False
  92.  
  93. all_sprites_list.update()
  94.  
  95. for block in block_list:
  96.  
  97. player_hit_list = pygame.sprite.spritecollide(block, player_list, True)
  98.  
  99. for player in player_hit_list:
  100. explosion.play()
  101. block_list.remove(block)
  102. player_list.remove(player)
  103. all_sprites_list.remove(block)
  104. all_sprites_list.remove(player)
  105.  
  106. if block.rect.y < +10:
  107. block_list.remove(block)
  108. all_sprites_list.remove(block)
  109.  
  110. for bullet in bullet_list:
  111.  
  112. block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True)
  113.  
  114. for block in block_hit_list:
  115. explosion.play()
  116. bullet_list.remove(bullet)
  117. all_sprites_list.remove(bullet)
  118. score += 10
  119.  
  120. if bullet.rect.y < -10:
  121. bullet_list.remove(bullet)
  122. all_sprites_list.remove(bullet)
  123.  
  124. font = pygame.font.Font(None, 36)
  125. text = font.render("Score: " + str(score), True, WHITE)
  126. textpos = text.get_rect(centerx=screen.get_width()/12)
  127.  
  128. screen.blit(background, (0,0))
  129.  
  130. screen.blit(text, textpos)
  131.  
  132. all_sprites_list.draw(screen)
  133.  
  134. pygame.display.update()
  135.  
  136. clock.tick(80)
  137.  
  138. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement