Advertisement
C-Gian

MyGametToTest

Dec 31st, 2019
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.39 KB | None | 0 0
  1. import pygame
  2. import random
  3. import time
  4. import math
  5.  
  6. pygame.init()
  7.  
  8. display_width = 950
  9. display_height = 700
  10. screen = pygame.display.set_mode((display_width, display_height))
  11. fps = 60
  12. font_ubuntumono = pygame.font.SysFont('ubuntumono', 50, True)
  13. player_image = pygame.image.load('spaceship.png').convert_alpha()
  14. original_player_image = pygame.image.load('spaceship.png').convert_alpha()
  15. laser_beam = pygame.image.load('laser.png').convert_alpha()
  16.  
  17.  
  18. def initialize():
  19.     global player1, laser1
  20.     player1 = Player(display_width // 2, display_height - 50, player_image)
  21.     laser1 = Laser(player1.x, player1.y)
  22.  
  23.  
  24. def update():
  25.     pygame.display.update()
  26.     pygame.time.Clock().tick(fps)
  27.  
  28.  
  29. def drawBackground():
  30.     # draw the background
  31.     screen.fill((255, 255, 255))
  32.  
  33.  
  34. def drawLaser():
  35.     laser1.continueDrawLaser()
  36.  
  37.  
  38. class Player:
  39.     # inizialize the player
  40.     def __init__(self, x, y, player):
  41.         self.x = x
  42.         self.y = y
  43.         self.image = player
  44.         self.is_shooting = False
  45.         self.original_player_image = player
  46.         self.angle = 0
  47.  
  48.     # move the player
  49.     def movePlayer(self):
  50.         if key[0]:
  51.             self.x -= 10
  52.         elif key[1]:
  53.             self.x += 10
  54.         if key[2]:
  55.             self.y -= 10
  56.         elif key[3]:
  57.             self.y += 10
  58.  
  59.     # rotate the player where the mouse is aiming
  60.     def rotate(self):
  61.         mouse_x, mouse_y = pygame.mouse.get_pos()
  62.         rel_x, rel_y = mouse_x - self.x, mouse_y - self.y
  63.         self.angle = (180 / math.pi) * -math.atan2(rel_y, rel_x) - 90
  64.         self.image = pygame.transform.rotate(self.original_player_image, int(self.angle))
  65.         self.rect = self.image.get_rect()
  66.  
  67.     # draw the player
  68.     def drawPlayer(self):
  69.         screen.blit(self.image, (self.x, self.y))
  70.  
  71.  
  72. class Laser:
  73.     def __init__(self, player_x, player_y):
  74.         self.x = player_x
  75.         self.y = player_y
  76.         self.original_image = pygame.Surface((5, 150))
  77.         self.original_image.set_colorkey((0, 0, 0))
  78.         self.original_image.fill((255, 0, 0))
  79.         self.copy_image = self.original_image.copy()
  80.         self.copy_image.set_colorkey((0, 0, 0))
  81.         self.rect = self.copy_image.get_rect()
  82.         self.rect.center = self.x + player_image.get_width() // 2, self.y - 75
  83.         self.new_image = pygame.Surface((5, 150))
  84.  
  85.     def continueDrawLaser(self):
  86.         if laser_bool:
  87.             screen.blit(self.new_image, self.rect)
  88.  
  89.     def rotate(self):
  90.         mouse_x, mouse_y = pygame.mouse.get_pos()
  91.         rel_x, rel_y = mouse_x - player1.x + player_image.get_width() // 2, mouse_y - player1.y - 75
  92.         angle = (180 / math.pi) * -math.atan2(rel_y, rel_x) - 85
  93.         self.new_image = pygame.transform.rotate(self.original_image, angle)
  94.         self.rect = self.new_image.get_rect()
  95.         self.rect.center = player1.x + player_image.get_width() // 2, player1.y - 75
  96.  
  97.  
  98. # inizialize the all game var
  99. initialize()
  100.  
  101. # temp var
  102. laser_bool = False
  103. key = key = [False, False, False, False]
  104. running = True
  105.  
  106. # main loop
  107. while running:
  108.  
  109.     # event loop
  110.     for event in pygame.event.get():
  111.  
  112.         # move player if keys are pressed
  113.         if event.type == pygame.KEYDOWN:
  114.             if event.key == pygame.K_a:
  115.                 key[0] = True
  116.             if event.key == pygame.K_d:
  117.                 key[1] = True
  118.             if event.key == pygame.K_w:
  119.                 key[2] = True
  120.             if event.key == pygame.K_s:
  121.                 key[3] = True
  122.  
  123.             if event.key == pygame.K_SPACE:
  124.                 laser_bool = True
  125.  
  126.         # stop player if keys are not pressed
  127.         if event.type == pygame.KEYUP:
  128.             if event.key == pygame.K_a:
  129.                 key[0] = False
  130.             if event.key == pygame.K_d:
  131.                 key[1] = False
  132.             if event.key == pygame.K_w:
  133.                 key[2] = False
  134.             if event.key == pygame.K_s:
  135.                 key[3] = False
  136.  
  137.             if event.key == pygame.K_SPACE:
  138.                 laser_bool = False
  139.  
  140.         # quit
  141.         if event.type == pygame.QUIT:
  142.             running = False
  143.             pygame.quit()
  144.             quit()
  145.  
  146.         if laser_bool:
  147.             laser1.rotate()
  148.  
  149.     # move and rotate the player
  150.     player1.movePlayer()
  151.     player1.rotate()
  152.  
  153.     # update screen
  154.     drawBackground()
  155.     player1.drawPlayer()
  156.     drawLaser()
  157.     update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement