overactive

Untitled

Sep 23rd, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.77 KB | None | 0 0
  1. import pygame
  2. import time
  3. import sys
  4. import random
  5. import math
  6.  
  7. WIDTH, HEIGHT = 900, 600
  8. WHITE = (255, 255, 255)
  9. BLACK = (0,0,0)
  10. PINK = (255, 0, 120)
  11.  
  12. #### SETTINGS ###
  13.  
  14. PLAYER_SPEED = 3
  15.  
  16. #### SETTINGS ###
  17.  
  18. pygame.init()
  19. canvas = pygame.display.set_mode((WIDTH,HEIGHT))
  20. pygame.display.set_caption('Asterodis')
  21. clock = pygame.time.Clock()
  22.  
  23. def quitGame():
  24.     pygame.quit()
  25.     sys.exit(0)
  26.  
  27. class Ship():
  28.     def __init__(self, a):
  29.         self.a = a
  30.         self.apex = [pygame.math.Vector2(-self.a, self.a), pygame.math.Vector2(self.a, self.a), pygame.math.Vector2(0, -self.a)]
  31.         self.newApex = [0, 0, 0]
  32.         self.offset = pygame.math.Vector2(WIDTH / 2, HEIGHT / 2)
  33.  
  34.     def rotate(self, angle):
  35.         self.apex[0] = self.apex[0].rotate(angle)
  36.         self.apex[1] = self.apex[1].rotate(angle)
  37.         self.apex[2] = self.apex[2].rotate(angle)
  38.  
  39.         self.newApex = [self.apex[0] + self.offset, self.apex[1] + self.offset, self.apex[2] + self.offset]
  40.        
  41.     def move(self, direction):
  42.         self.speed = PLAYER_SPEED
  43.         self.offset += self.speed * direction
  44.         self.theta = self.apex[2].angle_to(pygame.math.Vector2(self.a, 0)) * 0.01745329252
  45.  
  46.     def show(self):
  47.         pygame.draw.polygon(canvas, PINK, self.newApex, 2)
  48.         pygame.draw.circle(canvas, WHITE, (int(self.offset.x), int(self.offset.y)), 2)     
  49.  
  50. ship = Ship(30)
  51.  
  52. while True:
  53.     canvas.fill((BLACK))
  54.     for event in pygame.event.get():
  55.         if event.type == pygame.QUIT:
  56.             quitGame()
  57.  
  58.     angle = 0
  59.     direction = pygame.math.Vector2(0, 0)
  60.  
  61.     keys = pygame.key.get_pressed()
  62.     if keys[pygame.K_LEFT]:
  63.         angle = -1
  64.     elif keys[pygame.K_RIGHT]:
  65.         angle  = 1
  66.     if keys[pygame.K_UP]:
  67.         direction = pygame.math.Vector2(math.cos(ship.theta), -math.sin(ship.theta))
  68.    
  69.     ship.rotate(angle)
  70.     ship.move(direction)
  71.     ship.show()
  72.    
  73.     pygame.display.flip()  
  74.     clock.tick(60)
Advertisement
Add Comment
Please, Sign In to add comment