Skip to content

Instantly share code, notes, and snippets.

@natebot13
Created August 16, 2017 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natebot13/b71a1143a5aa0c4424253dbde5162cdb to your computer and use it in GitHub Desktop.
Save natebot13/b71a1143a5aa0c4424253dbde5162cdb to your computer and use it in GitHub Desktop.
asteroids template
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