overactive

Untitled

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