-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
78 lines (57 loc) · 2.47 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from pygame.math import Vector2
from pygame.transform import rotozoom
from utils import load_sprite, wrap_position, get_random_velocity
UP = Vector2(0, -1)
class GameObject:
def __init__(self, position, sprite, velocity):
self.position = Vector2(position)
self.sprite = sprite
self.radius = sprite.get_width() / 2
self.velocity = Vector2(velocity)
def draw(self, surface):
blit_position = self.position - Vector2(self.radius)
surface.blit(self.sprite, blit_position)
def move(self, surface):
self.position = wrap_position(self.position + self.velocity, surface)
def collides_with(self, other_obj):
distance = self.position.distance_to(other_obj.position)
return distance < self.radius + other_obj.radius
def getPosition(self):
return self.position
def getRadius(self):
return self.radius
class Spaceship(GameObject):
MANEUVERABILITY = 10
ACCELERATION = 0.25
BOOST = 20
BULLET_SPEED = 3
def __init__(self, position, create_bullet_callback):
self.create_bullet_callback = create_bullet_callback
self.direction = Vector2(UP)
super().__init__(position, load_sprite("spaceship"), Vector2(0))
def rotate(self, clockwise = True):
sign = 1 if clockwise else -1
angle = self.MANEUVERABILITY * sign
self.direction.rotate_ip(angle)
def draw(self, surface):
angle = self.direction.angle_to(UP)
rotated_surface = rotozoom(self.sprite, angle, 1.0)
rotated_surface_size = Vector2(rotated_surface.get_size())
blit_position = self.position - rotated_surface_size * 0.5
surface.blit(rotated_surface, blit_position)
def accelerate(self):
self.velocity += self.direction * self.ACCELERATION
def boost(self, surface):
self.position = wrap_position(self.position + self.direction * self.BOOST, surface)
def shoot(self):
bullet_velocity = self.direction * self.BULLET_SPEED + self.velocity
bullet = Bullet(self.position, bullet_velocity)
self.create_bullet_callback(bullet)
class Asteroid(GameObject):
def __init__(self, position):
super().__init__(position, load_sprite("asteroid"), get_random_velocity(1, 5))
class Bullet(GameObject):
def __init__(self, position, velocity):
super().__init__(position, load_sprite("bullet"), velocity)
def move(self, surface):
self.position = self.position + self.velocity