Advertisement
Guest User

Untitled

a guest
Mar 25th, 2014
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 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. BLACK = ( 0, 0, 0)
  12. WHITE = ( 255, 255, 255)
  13. background = pygame.image.load('space.jpg')
  14. pygame.display.set_caption("Alien Invasion!")
  15. explosion = pygame.mixer.Sound('explosion.wav')
  16. score = 0
  17.  
  18. class Block(pygame.sprite.Sprite):
  19. def __init__(self, color):
  20. pygame.sprite.Sprite.__init__(self)
  21.  
  22. self.image = pygame.image.load("spaceship.png")
  23. self.rect = self.image.get_rect()
  24.  
  25. class Player(pygame.sprite.Sprite):
  26. def __init__(self):
  27. pygame.sprite.Sprite.__init__(self)
  28.  
  29. self.x=0
  30. self.y=0
  31. self.image = pygame.image.load("alien.png")
  32. self.rect = self.image.get_rect()
  33.  
  34. def update(self):
  35.  
  36. pos = pygame.mouse.get_pos()
  37. self.rect.x = pos[0]
  38.  
  39. def render(self):
  40.  
  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.  
  52. self.rect.y -= 3
  53.  
  54. for i in range(30):
  55. block = Block(BLACK)
  56. block.rect.x = random.randrange(screen_width)
  57. block.rect.y = random.randrange(330)
  58. block_list.add(block)
  59. all_sprites_list.add(block)
  60.  
  61. player = Player()
  62.  
  63. all_sprites_list.add(player)
  64.  
  65. done = False
  66.  
  67. clock = pygame.time.Clock()
  68.  
  69. player.rect.y = 480
  70.  
  71. k_space = False
  72.  
  73. while True:
  74. for event in pygame.event.get():
  75. if (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE) or pygame.QUIT:
  76. pygame.quit()
  77. sys.exit()
  78.  
  79. elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
  80. k_space = True
  81.  
  82. if k_space:
  83. bullet = Bullet()
  84. bullet.rect.x = player.rect.x
  85. bullet.rect.y = player.rect.y
  86. all_sprites_list.add(bullet)
  87. bullet_list.add(bullet)
  88.  
  89. k_space = False
  90.  
  91. all_sprites_list.update()
  92.  
  93. for bullet in bullet_list:
  94.  
  95. block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True)
  96.  
  97. for block in block_hit_list:
  98. explosion.play()
  99. bullet_list.remove(bullet)
  100. all_sprites_list.remove(bullet)
  101. score += 10
  102.  
  103. if bullet.rect.y < -10:
  104. bullet_list.remove(bullet)
  105. all_sprites_list.remove(bullet)
  106.  
  107. font = pygame.font.Font(None, 36)
  108. text = font.render("Score: " + str(score), True, WHITE)
  109. textpos = text.get_rect(centerx=screen.get_width()/12)
  110.  
  111. screen.blit(background, (0,0))
  112.  
  113. screen.blit(text, textpos)
  114.  
  115. all_sprites_list.draw(screen)
  116.  
  117. pygame.display.update()
  118.  
  119. clock.tick(80)
  120.  
  121. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement