overactive

Untitled

Sep 23rd, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.79 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. MIN_ENEMY_SPEED = 4
  14. MAX_ENEMY_SPEED = 6
  15. PLAYER_SPEED = pygame.math.Vector2(0,1)
  16.  
  17. #### SETTINGS ###
  18.  
  19. pygame.init()
  20. canvas = pygame.display.set_mode((WIDTH,HEIGHT))
  21. pygame.display.set_caption('Asterodis')
  22. clock = pygame.time.Clock()
  23.  
  24. def quitGame():
  25.     pygame.quit()
  26.     sys.exit(0)
  27.  
  28. class Ship():
  29.     def __init__(self, a):
  30.         self.apex = [pygame.math.Vector2(-a, a), pygame.math.Vector2(a, a), pygame.math.Vector2(0, -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.  
  44.         self.apex[0] += self.speed * direction
  45.         self.apex[1] += self.speed * direction
  46.         self.apex[2] += self.speed * direction
  47.         self.offset += self.speed * direction
  48.        
  49.     def show(self):
  50.         pygame.draw.polygon(canvas, PINK, self.newApex, 2)
  51.         pygame.draw.circle(canvas, WHITE, (int(self.offset.x), int(self.offset.y)), 2)     
  52.  
  53. ship = Ship(30)
  54.  
  55. while True:
  56.     canvas.fill((BLACK))
  57.     for event in pygame.event.get():
  58.         if event.type == pygame.QUIT:
  59.             quitGame()
  60.  
  61.     angle = 0
  62.     direction = 0
  63.  
  64.     keys = pygame.key.get_pressed()
  65.     if keys[pygame.K_LEFT]:
  66.         angle = -1
  67.     elif keys[pygame.K_RIGHT]:
  68.         angle  = 1
  69.     if keys[pygame.K_UP]:
  70.         direction = -1
  71.     elif keys[pygame.K_DOWN]:
  72.         direction = 1
  73.    
  74.    
  75.     ship.rotate(angle)
  76.     ship.move(direction)
  77.     ship.show()
  78.    
  79.     pygame.display.flip()  
  80.     clock.tick(60)
Advertisement
Add Comment
Please, Sign In to add comment