Created
August 16, 2017 18:52
-
-
Save natebot13/b71a1143a5aa0c4424253dbde5162cdb to your computer and use it in GitHub Desktop.
asteroids template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
from math import sin, cos, radians | |
class Asteroid: | |
def __init__(self, x, y): | |
self.shape = None | |
self.position = (x,y) | |
class Ship: | |
def __init__(self, x, y): | |
self.direction = 0 | |
self.speed = 0.0 | |
self.drag = 0.005 | |
self.shape = [(0, 10), (-5, -5), (-5, 5)] | |
self.position = (x, y) | |
def draw(self, screen): | |
rotatedShape = [] | |
for point in self.shape: | |
x, y = point | |
x = x * cos(radians(self.direction)) - y * sin(radians(self.direction)) | |
y = x * sin(radians(self.direction)) + y * sin(radians(self.direction)) | |
rotatedShape.append((x,y)) | |
relativeShape = [(x + self.position[0], y + self.position[1]) for x, y in rotatedShape] | |
pygame.draw.polygon(screen, (255,255,255), relativeShape, 1) | |
def accelerate(self, accel, dt): | |
self.speed -= accel*dt | |
x, y = self.position | |
x += cos(radians(self.direction)) * self.speed | |
y += sin(radians(self.direction)) * self.speed | |
self.position = (x, y) | |
def rotate(self, amount): | |
pass | |
class Bullet: | |
def __init__(self, x, y, direction): | |
self.position = (x, y) | |
self.speed = 10 | |
self.direction = direction | |
def draw(self, screen): | |
pass | |
def update(self, dt): | |
x, y = self.position | |
x += cos(radians(self.direction)) * self.speed | |
y += sin(radians(self.direction)) * self.speed | |
self.position = (x, y) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment