Advertisement
Arcot

fighterplane bat edition

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