Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import time
- import sys
- import random
- import math
- WIDTH, HEIGHT = 900, 600
- WHITE = (255, 255, 255)
- BLACK = (0,0,0)
- PINK = (255, 0, 120)
- #### SETTINGS ###
- PLAYER_SPEED = 3
- #### SETTINGS ###
- pygame.init()
- canvas = pygame.display.set_mode((WIDTH,HEIGHT))
- pygame.display.set_caption('Asterodis')
- clock = pygame.time.Clock()
- def quitGame():
- pygame.quit()
- sys.exit(0)
- class Ship():
- def __init__(self, a):
- self.a = a
- self.apex = [pygame.math.Vector2(-self.a, self.a), pygame.math.Vector2(self.a, self.a), pygame.math.Vector2(0, -self.a)]
- self.newApex = [0, 0, 0]
- self.offset = pygame.math.Vector2(WIDTH / 2, HEIGHT / 2)
- def rotate(self, angle):
- self.apex[0] = self.apex[0].rotate(angle)
- self.apex[1] = self.apex[1].rotate(angle)
- self.apex[2] = self.apex[2].rotate(angle)
- self.newApex = [self.apex[0] + self.offset, self.apex[1] + self.offset, self.apex[2] + self.offset]
- def move(self, direction):
- self.speed = PLAYER_SPEED
- self.offset += self.speed * direction
- self.theta = self.apex[2].angle_to(pygame.math.Vector2(self.a, 0)) * 0.01745329252
- def show(self):
- pygame.draw.polygon(canvas, PINK, self.newApex, 2)
- pygame.draw.circle(canvas, WHITE, (int(self.offset.x), int(self.offset.y)), 2)
- ship = Ship(30)
- while True:
- canvas.fill((BLACK))
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- quitGame()
- angle = 0
- direction = pygame.math.Vector2(0, 0)
- keys = pygame.key.get_pressed()
- if keys[pygame.K_LEFT]:
- angle = -1
- elif keys[pygame.K_RIGHT]:
- angle = 1
- if keys[pygame.K_UP]:
- direction = pygame.math.Vector2(math.cos(ship.theta), -math.sin(ship.theta))
- ship.rotate(angle)
- ship.move(direction)
- ship.show()
- pygame.display.flip()
- clock.tick(60)
Advertisement
Add Comment
Please, Sign In to add comment