Advertisement
C-Gian

enemy shoot

Jan 6th, 2020
645
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.33 KB | None | 0 0
  1. import pygame
  2. import random
  3. import time
  4. import math
  5. from pygame.math import Vector2
  6.  
  7. pygame.init()
  8.  
  9. display_width = 800
  10. display_height = 650
  11. screen = pygame.display.set_mode((display_width, display_height))
  12. fps = 60
  13. shooting_enemy_image = pygame.image.load('cave-painting.png').convert_alpha()
  14. first_player_spaceship_image = pygame.image.load('spaceship.png').convert_alpha()
  15. laser_bullet = pygame.image.load('laser1.png').convert_alpha()
  16.  
  17. def initialize():
  18.     global shooting_enemies_list, enemy_shooting_bullets_list, player_one_istance
  19.  
  20.     shooting_enemies_list, enemy_shooting_bullets_list = [], []
  21.     shooting_enemies_list.append(ShootingEnemy())
  22.     player_one_istance = Player(display_width // 2, display_height // 2, first_player_spaceship_image)
  23.  
  24.  
  25. def update():
  26.     pygame.display.update()
  27.     pygame.time.Clock().tick(fps)
  28.  
  29.  
  30. def drawAll():
  31.     drawBackground()
  32.     drawShootingEnemy()
  33.     drawShootingEnemyBullets()
  34.     player_one_istance.drawPlayer()
  35.  
  36.  
  37. def drawBackground():
  38.     screen.fill((0, 0, 0))
  39.  
  40.  
  41. def drawShootingEnemy():
  42.     # draw shooting enemy
  43.     for shooting_enemy in shooting_enemies_list:
  44.         shooting_enemy.continueMoveShootingEnemy()
  45.         shooting_enemy.rotate()
  46.         shooting_enemy.drawShootingEnemies()
  47.  
  48.  
  49. def drawShootingEnemyBullets():
  50.     for bullets_shooting_enemy in enemy_shooting_bullets_list:
  51.         bullets_shooting_enemy.MoveBulletEnemyShooting()
  52.         bullets_shooting_enemy.rotate()
  53.         bullets_shooting_enemy.drawBulletEnemyShooting()
  54.  
  55.  
  56. class Player:
  57.     # inizialize the player
  58.     def __init__(self, x, y, player):
  59.         self.x = x
  60.         self.y = y
  61.         self.image = first_player_spaceship_image
  62.         self.life = 6
  63.         self.original_player_image = first_player_spaceship_image
  64.         self.angle = 0
  65.         self.rect = self.image.get_rect()
  66.  
  67.     # move the player
  68.     def movePlayer(self):
  69.         if key[0]:
  70.             self.x -= 10
  71.         elif key[1]:
  72.             self.x += 10
  73.         if key[2]:
  74.             self.y -= 10
  75.         elif key[3]:
  76.             self.y += 10
  77.  
  78.         # check borders
  79.         if self.x <= 0:
  80.             self.x = 0
  81.         if self.x + shooting_enemy_image.get_width() >= display_width:
  82.             self.x = display_width - first_player_spaceship_image.get_width()
  83.         if self.y <= 0:
  84.             self.y = 0
  85.         if self.y + first_player_spaceship_image.get_height() >= display_height:
  86.             self.y = display_height - first_player_spaceship_image.get_height()
  87.  
  88.     # rotate the player where the mouse is aiming
  89.     def rotate(self):
  90.         mouse_x, mouse_y = pygame.mouse.get_pos()
  91.         rel_x, rel_y = mouse_x - self.x, mouse_y - self.y
  92.         self.angle = (180 / math.pi) * -math.atan2(rel_y, rel_x) - 90
  93.         self.image = pygame.transform.rotate(self.original_player_image, int(self.angle))
  94.         orig_center = self.original_player_image.get_rect(topleft=(self.x, self.y)).center
  95.         self.rect = self.image.get_rect(center=orig_center)
  96.  
  97.     # draw the player
  98.     def drawPlayer(self):
  99.         screen.blit(self.image, self.rect.topleft)
  100.  
  101.  
  102. class ShootingEnemy:
  103.     # inizialize the enemy
  104.     def __init__(self):
  105.         self.image = shooting_enemy_image
  106.         self.new_shooting_enemy_image = shooting_enemy_image
  107.         self.x = display_width // 2
  108.         self.y = 0
  109.         self.begin_down = True
  110.         self.choices = 0
  111.         self.timer = 51
  112.         self.left_again = False
  113.         self.right_again = False
  114.         self.up_again = False
  115.         self.down_again = False
  116.         self.rect = shooting_enemy_image.get_rect()
  117.         self.angle = 0
  118.  
  119.     # draw the shooting enemy
  120.     def continueMoveShootingEnemy(self):
  121.         if self.y < 30:
  122.             self.y += 1
  123.         if self.y == 30:
  124.             self.begin_down = False
  125.             self.y += 1
  126.         else:
  127.             if not self.begin_down and self.timer > 20:
  128.                 self.choices = random.choice([1, 2, 3, 4])
  129.                 self.timer = 0
  130.             self.timer += 1
  131.             if self.left_again:
  132.                 self.x += 1
  133.                 self.left_again = False
  134.             if self.right_again:
  135.                 self.x -= 1
  136.                 self.right_again = False
  137.             if self.up_again:
  138.                 self.y += 0
  139.                 self.up_again = False
  140.             if self.down_again:
  141.                 self.y -= 1
  142.                 self.down_again = False
  143.             else:
  144.                 if self.choices == 1:
  145.                     if self.x >= display_width:
  146.                         self.timer = 21
  147.                         self.right_again = True
  148.                     else:
  149.                         if self.y < 140 and self.y > 10:
  150.                             self.y += 1
  151.                             self.x += 4
  152.                         else:
  153.                             self.x += 4
  154.                 if self.choices == 2:
  155.                     if self.x <= 0:
  156.                         self.timer = 21
  157.                         self.left_again = True
  158.                     else:
  159.                         if self.y < 140 and self.y > 10:
  160.                             self.y += 1
  161.                             self.x -= 4
  162.                         else:
  163.                             self.x -= 4
  164.                 if self.choices == 3:
  165.                     if self.y >= 150:
  166.                         self.timer = 21
  167.                         self.down_again = True
  168.                     else:
  169.                         self.y += 2
  170.                 if self.choices == 4:
  171.                     if self.y <= 0:
  172.                         self.timer = 21
  173.                         self.up_again = True
  174.                     else:
  175.                         self.y -= 2
  176.  
  177.     # rotate the shooting enemy where the player is
  178.     def rotate(self):
  179.         temp_x, temp_y = player_one_istance.x - self.x, player_one_istance.y - self.y
  180.         self.angle = (180 / math.pi) * -math.atan2(temp_y, temp_x) - 90
  181.         self.image = pygame.transform.rotate(self.new_shooting_enemy_image, int(self.angle))
  182.         orig_center = self.new_shooting_enemy_image.get_rect(topleft=(self.x, self.y)).center
  183.         self.rect = self.image.get_rect(center=orig_center)
  184.  
  185.     def drawShootingEnemies(self):
  186.         screen.blit(self.image, (self.x, self.y))
  187.  
  188. class BulletEnemyShooting:
  189.     # initialize the bullet for the enemy shooting
  190.     def __init__(self, shooting_enemy):
  191.         self.x = shooting_enemy.x
  192.         self.y = shooting_enemy.y
  193.         self.image = laser_bullet
  194.         self.original_image = laser_bullet
  195.         self.angle = shooting_enemy.angle
  196.         self.vel_x = math.cos(self.angle) * 45
  197.         self.vel_y = math.sin(self.angle) * 45
  198.         self.rect = self.image.get_rect()
  199.  
  200.     # draw the bullet
  201.     def MoveBulletEnemyShooting(self):
  202.         self.x += self.vel_x
  203.         self.y += self.vel_y
  204.         if self.x < -laser_bullet.get_width() or self.x >= display_width + laser_bullet.get_width() \
  205.                 or self.y < -laser_bullet.get_height() or self.y >= display_height + laser_bullet.get_height():
  206.             enemy_shooting_bullets_list.pop(enemy_shooting_bullets_list.index(self))
  207.  
  208.     def drawBulletEnemyShooting(self):
  209.         screen.blit(self.image, (self.x, self.y))
  210.  
  211.     # rotate the bullet to the new angle
  212.     def rotate(self):
  213.         self.image = pygame.transform.rotate(self.original_image,
  214.                                              ((180 / math.pi) * -math.atan2(self.vel_y, self.vel_x) - 90))
  215.         self.rect = self.image.get_rect()
  216.  
  217.  
  218. # inizialize the all game var
  219. initialize()
  220.  
  221. # temp var
  222. key = [False, False, False, False]
  223. mouse = False
  224. temp = False
  225. running = True
  226. enemy_shooting_bool = False
  227.  
  228. # main loop
  229. while running:
  230.  
  231.     # event loop
  232.     for event in pygame.event.get():
  233.  
  234.         # move player if keys are pressed
  235.         if event.type == pygame.KEYDOWN:
  236.             if event.key == pygame.K_a:
  237.                 key[0] = True
  238.             if event.key == pygame.K_d:
  239.                 key[1] = True
  240.             if event.key == pygame.K_w:
  241.                 key[2] = True
  242.             if event.key == pygame.K_s:
  243.                 key[3] = True
  244.  
  245.             if event.key == pygame.K_LSHIFT:
  246.                 enemy_shooting_bool = True
  247.  
  248.         # if mouse is pressed then shoot
  249.         if event.type == pygame.MOUSEBUTTONDOWN:
  250.             mouse = True
  251.             temp = True
  252.  
  253.         # if mouse is not pressed then stop shooting
  254.         if event.type == pygame.MOUSEBUTTONUP:
  255.             mouse = False
  256.  
  257.         # stop player if keys are not pressed
  258.         if event.type == pygame.KEYUP:
  259.             if event.key == pygame.K_a:
  260.                 key[0] = False
  261.             if event.key == pygame.K_d:
  262.                 key[1] = False
  263.             if event.key == pygame.K_w:
  264.                 key[2] = False
  265.             if event.key == pygame.K_s:
  266.                 key[3] = False
  267.  
  268.             if event.key == pygame.K_SPACE:
  269.                 laser_bool = False
  270.  
  271.         # quit
  272.         if event.type == pygame.QUIT:
  273.             running = False
  274.             pygame.quit()
  275.             quit()
  276.  
  277.     if enemy_shooting_bool:
  278.         enemy_shooting_bullets_list.append(BulletEnemyShooting(shooting_enemies_list[0]))
  279.  
  280.     # move and rotate the player
  281.     player_one_istance.movePlayer()
  282.     player_one_istance.rotate()
  283.  
  284.     # draw all things
  285.     drawAll()
  286.     # update all things
  287.     update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement