Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- import time
- import math
- pygame.init()
- display_width = 950
- display_height = 700
- screen = pygame.display.set_mode((display_width, display_height))
- fps = 60
- font_ubuntumono = pygame.font.SysFont('ubuntumono', 50, True)
- player_image = pygame.image.load('spaceship.png').convert_alpha()
- original_player_image = pygame.image.load('spaceship.png').convert_alpha()
- laser_beam = pygame.image.load('laser.png').convert_alpha()
- def initialize():
- global player1, laser1
- player1 = Player(display_width // 2, display_height - 50, player_image)
- laser1 = Laser(player1.x, player1.y)
- def update():
- pygame.display.update()
- pygame.time.Clock().tick(fps)
- def drawBackground():
- # draw the background
- screen.fill((255, 255, 255))
- def drawLaser():
- laser1.continueDrawLaser()
- class Player:
- # inizialize the player
- def __init__(self, x, y, player):
- self.x = x
- self.y = y
- self.image = player
- self.is_shooting = False
- self.original_player_image = player
- self.angle = 0
- # move the player
- def movePlayer(self):
- if key[0]:
- self.x -= 10
- elif key[1]:
- self.x += 10
- if key[2]:
- self.y -= 10
- elif key[3]:
- self.y += 10
- # rotate the player where the mouse is aiming
- def rotate(self):
- mouse_x, mouse_y = pygame.mouse.get_pos()
- rel_x, rel_y = mouse_x - self.x, mouse_y - self.y
- self.angle = (180 / math.pi) * -math.atan2(rel_y, rel_x) - 90
- self.image = pygame.transform.rotate(self.original_player_image, int(self.angle))
- self.rect = self.image.get_rect()
- # draw the player
- def drawPlayer(self):
- screen.blit(self.image, (self.x, self.y))
- class Laser:
- def __init__(self, player_x, player_y):
- self.x = player_x
- self.y = player_y
- self.original_image = pygame.Surface((5, 150))
- self.original_image.set_colorkey((0, 0, 0))
- self.original_image.fill((255, 0, 0))
- self.copy_image = self.original_image.copy()
- self.copy_image.set_colorkey((0, 0, 0))
- self.rect = self.copy_image.get_rect()
- self.rect.center = self.x + player_image.get_width() // 2, self.y - 75
- self.new_image = pygame.Surface((5, 150))
- def continueDrawLaser(self):
- if laser_bool:
- screen.blit(self.new_image, self.rect)
- def rotate(self):
- mouse_x, mouse_y = pygame.mouse.get_pos()
- rel_x, rel_y = mouse_x - player1.x + player_image.get_width() // 2, mouse_y - player1.y - 75
- angle = (180 / math.pi) * -math.atan2(rel_y, rel_x) - 85
- self.new_image = pygame.transform.rotate(self.original_image, angle)
- self.rect = self.new_image.get_rect()
- self.rect.center = player1.x + player_image.get_width() // 2, player1.y - 75
- # inizialize the all game var
- initialize()
- # temp var
- laser_bool = False
- key = key = [False, False, False, False]
- running = True
- # main loop
- while running:
- # event loop
- for event in pygame.event.get():
- # move player if keys are pressed
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_a:
- key[0] = True
- if event.key == pygame.K_d:
- key[1] = True
- if event.key == pygame.K_w:
- key[2] = True
- if event.key == pygame.K_s:
- key[3] = True
- if event.key == pygame.K_SPACE:
- laser_bool = True
- # stop player if keys are not pressed
- if event.type == pygame.KEYUP:
- if event.key == pygame.K_a:
- key[0] = False
- if event.key == pygame.K_d:
- key[1] = False
- if event.key == pygame.K_w:
- key[2] = False
- if event.key == pygame.K_s:
- key[3] = False
- if event.key == pygame.K_SPACE:
- laser_bool = False
- # quit
- if event.type == pygame.QUIT:
- running = False
- pygame.quit()
- quit()
- if laser_bool:
- laser1.rotate()
- # move and rotate the player
- player1.movePlayer()
- player1.rotate()
- # update screen
- drawBackground()
- player1.drawPlayer()
- drawLaser()
- update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement