Advertisement
ZEdKasat

Untitled

Sep 30th, 2021
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. import pygame
  2. import random
  3.  
  4. PlayerColor = (255, 128, 0)
  5. WIDTH, HEIGHT = 800, 600
  6. BackgroundColor = (0,0,0)
  7. PLAYERSPEED = 10
  8. CloudColor = (200,200,200)
  9. EnemyColor = (255, 0, 0)
  10. PlayerBulletColor = (0, 0, 255)
  11. EnemyBulletColor = (0, 250, 255)
  12. BULLETSPEED = 10
  13. ENEMYSPEED = 7
  14. pygame.init()
  15. window = pygame.display.set_mode((WIDTH, HEIGHT))
  16.  
  17. class Player(pygame.sprite.Sprite):
  18. def __init__(self):
  19. super().__init__()
  20. self.image = pygame.Surface((50,35))
  21. self.image.fill(PlayerColor)
  22. self.rect = self.image.get_rect()
  23. self.rect.center = (200, 200)
  24.  
  25. def update(self):
  26. keys = pygame.key.get_pressed()
  27. dx, dy = 0, 0 # change in x, change in y
  28. # dx -> speed of player going right
  29. # dy -> speed of player going down
  30. if keys[pygame.K_w] and self.rect.top > 0:
  31. dy = -PLAYERSPEED
  32. if keys[pygame.K_a] and self.rect.left > 0:
  33. dx = -PLAYERSPEED
  34. if keys[pygame.K_s] and self.rect.bottom < HEIGHT:
  35. dy = PLAYERSPEED
  36. if keys[pygame.K_d] and self.rect.right < WIDTH:
  37. dx = PLAYERSPEED
  38. self.rect.move_ip(dx, dy)
  39. # pos = pygame.mouse.get_pos()
  40. # self.rect.center = pos
  41.  
  42.  
  43. class Cloud(pygame.sprite.Sprite):
  44. def __init__(self):
  45. super().__init__()
  46. self.size = random.randrange(50,100)
  47. self.image = pygame.Surface((self.size*2,self.size))
  48. self.image.fill(CloudColor)
  49. self.rect = self.image.get_rect()
  50. self.rect.topleft = (WIDTH, random.randrange(0, HEIGHT-50))
  51. self.speed = random.randrange(2,7)
  52.  
  53. def update(self):
  54. self.rect.move_ip(-self.speed, 0)
  55. if self.rect.right < 0: # if cloud goes outside the screen
  56. self.kill()
  57.  
  58. class Enemy(pygame.sprite.Sprite):
  59. def __init__(self):
  60. super().__init__()
  61. self.image = pygame.Surface((50,35))
  62. self.image.fill(EnemyColor)
  63. self.rect = self.image.get_rect()
  64. self.rect.topleft = (WIDTH, random.randrange(0, HEIGHT))
  65. self.speed = random.randrange(ENEMYSPEED-5, ENEMYSPEED)
  66.  
  67. def update(self):
  68. self.rect.move_ip(-self.speed, 0)
  69. if self.rect.right < 0:
  70. self.kill()
  71.  
  72. class Bullet(pygame.sprite.Sprite):
  73. def __init__(self, player_position):
  74. super().__init__()
  75. self.image = pygame.Surface((15,10))
  76. self.image.fill((PlayerBulletColor))
  77. self.speed = BULLETSPEED
  78. self.rect = self.image.get_rect()
  79. self.rect.center = player_position
  80.  
  81. def update(self):
  82. self.rect.move_ip(self.speed, 0)
  83. if self.rect.left > WIDTH:
  84. self.kill()
  85.  
  86.  
  87. class Bullet(pygame.sprite.Sprite):
  88. def __init__(self, player_position):
  89. super().__init__()
  90. self.image = pygame.Surface((15,10))
  91. self.image.fill((PlayerBulletColor))
  92. self.speed = BULLETSPEED
  93. self.rect = self.image.get_rect()
  94. self.rect.center = player_position
  95.  
  96. def update(self):
  97. self.rect.move_ip(self.speed, 0)
  98. if self.rect.left > WIDTH:
  99. self.kill()
  100.  
  101. class Star(pygame.sprite.Sprite):
  102. def __init__(self):
  103. super().__init__()
  104. self.image = pygame.Surface((50,35))
  105. self.image.fill((255,255,0))
  106. self.rect = self.image.get_rect()
  107. self.rect.center = (random.randrange(50, WIDTH-50), -50)
  108. self.speed = random.randrange(ENEMYSPEED-5, ENEMYSPEED)
  109.  
  110. def update(self):
  111. self.rect.move_ip(0, self.speed)
  112. if self.rect.top > HEIGHT:
  113. self.kill()
  114.  
  115. pygame.display.set_caption("Sky Shooter")
  116.  
  117. run = True
  118. clock = pygame.time.Clock()
  119. elements = pygame.sprite.Group()
  120. clouds = pygame.sprite.Group()
  121. enemies = pygame.sprite.Group()
  122. stars = pygame.sprite.Group()
  123. playerbullets = pygame.sprite.Group()
  124. enemybullets = pygame.sprite.Group()
  125. event_cloud = pygame.USEREVENT + 1
  126. event_enemy = pygame.USEREVENT + 2
  127. event_enemy_shoots = pygame.USEREVENT + 3
  128. event_star = pygame.USEREVENT + 3
  129. pygame.time.set_timer(event_cloud, 800)
  130. pygame.time.set_timer(event_enemy, 300)
  131. pygame.time.set_timer(event_enemy_shoots, 200)
  132. pygame.time.set_timer(event_star, 2000)
  133. player = Player()
  134. elements.add(player)
  135. pygame.mouse.set_visible(False)
  136. while run:
  137. clock.tick(60)
  138. window.fill(BackgroundColor)
  139. for event in pygame.event.get():
  140. if event.type == pygame.QUIT:
  141. run = False
  142. if event.type == pygame.KEYDOWN:
  143. if event.key == pygame.K_SPACE:
  144. b = Bullet(player.rect.center)
  145. playerbullets.add(b)
  146. if event.type == event_enemy_shoots and enemies:
  147. e = random.choice(list(enemies))
  148. b = Bullet(e.rect.center, 2)
  149. enemybullets.add(b)
  150. if event.type == event_cloud:
  151. c = Cloud()
  152. clouds.add(c)
  153. if event.type == event_enemy:
  154. e = Enemy()
  155. enemies.add(e)
  156. if event.type == event_star:
  157. s = Star()
  158. stars.add(s)
  159. hit = pygame.sprite.spritecollideany(player, enemies)
  160. if hit:
  161. run = False
  162. pygame.sprite.groupcollide(playerbullets, enemies, True, True)
  163.  
  164. playerbullets.update()
  165. playerbullets.draw(window)
  166. enemybullets.update()
  167. enemybullets.draw(window)
  168. elements.update()
  169. elements.draw(window)
  170. stars.update()
  171. stars.draw(window)
  172. enemies.update()
  173. enemies.draw(window)
  174. clouds.update()
  175. clouds.draw(window)
  176. pygame.display.update()
  177. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement