-
Notifications
You must be signed in to change notification settings - Fork 0
/
objects.py
224 lines (180 loc) · 7.94 KB
/
objects.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import math
import os
from random import randint, choice
import pygame.sprite
from resources import *
IMMUNITY_INTERVALS = [125 * i for i in range(1, 17)]
MAX_VEL = 10
CWD = os.getcwd()
class Player(pygame.sprite.Sprite):
"""handle player's spaceship"""
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image(os.path.join(CWD, "images", "rsz_2black-arrow.png"))
self.original = self.image
self.transparent = load_image(os.path.join(CWD, "images", "transparent-arrow.png"))[0]
self.area = pygame.display.get_surface().get_rect()
self.rect.center = (self.area.width / 2, self.area.height / 2)
self.radius = self.image.get_width() / 2
self.nose = self.rect.midright
self.vel = 0
self.x_momentum = 0
self.y_momentum = 0
self.angle = 0
self.ang_vel = 0
self.lives = 3
self.immune = 0
def update(self):
"""moves player and maintains it onscreen"""
self.angle += self.ang_vel
dx = math.cos(math.radians(self.angle)) * self.vel
dy = -math.sin(math.radians(self.angle)) * self.vel
nose_dx = math.cos(math.radians(self.angle)) * self.radius
nose_dy = -math.sin(math.radians(self.angle)) * self.radius
self.nose = tuple(map(lambda x, y: x + y, (nose_dx, nose_dy), self.rect.center))
# the minus in -sin is because the y coordinate increases downwards
self.rect.move_ip(dx, dy)
# self.rect.move_ip(self.x_momentum, self.y_momentum) # realistic movement
self.original_nose = self.rect.midright
if self.rect.left < self.area.left:
self.rect.left = self.area.left
self.x_momentum = 0
if self.rect.right > self.area.right:
self.rect.right = self.area.right
self.x_momentum = 0
if self.rect.top < self.area.top:
self.rect.top = self.area.top
self.y_momentum = 0
if self.rect.bottom > self.area.bottom:
self.rect.bottom = self.area.bottom
self.y_momentum = 0
for i in range(0, len(IMMUNITY_INTERVALS), 2):
if IMMUNITY_INTERVALS[i] < self.immune <= IMMUNITY_INTERVALS[i + 1] :
self.image = self.transparent
break
else:
self.image = pygame.transform.rotozoom(self.original, self.angle, 1)
self.rect = self.image.get_rect(center=self.rect.center)
def turn(self, value=6):
self.ang_vel = value
def stop_turn(self):
self.ang_vel = 0
def move(self, value=5):
self.vel = value
def stop(self):
self.vel = 0
def accel(self, value=5):
"""use this method instead of move to get a 'realistic' movement"""
self.x_momentum += math.cos(math.radians(self.angle)) * value
if self.x_momentum > MAX_VEL:
self.x_momentum = MAX_VEL
self.y_momentum += -math.sin(math.radians(self.angle)) * value
if self.y_momentum > MAX_VEL:
self.y_momentum = MAX_VEL
class Asteroids(pygame.sprite.Sprite):
"""create and control asteroids"""
def __init__(self, x_vel=0, y_vel=0, size=0, center=0):
pygame.sprite.Sprite.__init__(self)
self.image = load_image(os.path.join(CWD, "images", "black-boulder-pixel-cropped.png"))[0]
self.area = pygame.display.get_surface().get_rect()
if x_vel == 0:
self.x_vel = randint(1, 3)
else:
self.x_vel = x_vel
if y_vel == 0:
self.y_vel = randint(1, 3)
else:
self.y_vel = y_vel
self.angle = randint(0, 360)
if size == 0:
self.size = randint(1, 5) / 10
else:
self.size = size
self.original = pygame.transform.rotozoom(self.image, self.angle, self.size)
self.image = self.original
self.rect = self.image.get_rect()
if center == 0:
self.rect.center = (choice((randint(-200, 0), randint(800, 1000))),
choice((randint(-200, 0), randint(600, 800))))
else:
self.rect.center = center
def update(self):
"""moves asteroid and bounces it 300 pixels offscreen"""
self.rect.move_ip(self.x_vel, self.y_vel)
if self.rect.left < self.area.left - 300:
self.rect.left = self.area.left - 300
self.x_vel = -self.x_vel
if self.rect.right > self.area.right + 300:
self.rect.right = self.area.right + 300
self.x_vel = -self.x_vel
if self.rect.top < self.area.top - 300:
self.rect.top = self.area.top - 300
self.y_vel = -self.y_vel
if self.rect.bottom > self.area.bottom + 300:
self.rect.bottom = self.area.bottom + 300
self.y_vel = -self.y_vel
# splits asteroid into two new smaller asteroids
def split(self):
astA_vector = rotate_vector([self.x_vel, self.y_vel], 45)
astB_vector = rotate_vector([self.x_vel, self.y_vel], -45)
return Asteroids(astA_vector[0][0], astA_vector[1][0], self.size - 0.1, self.rect.center),\
Asteroids(astB_vector[0][0], astB_vector[1][0], self.size - 0.1, self.rect.center)
def debris(self):
astA_vector = rotate_vector([self.x_vel, self.y_vel], 45)
astB_vector = rotate_vector([self.x_vel, self.y_vel], -45)
return Asteroids(astA_vector[0][0], astA_vector[1][0], self.size - 0.09, self.rect.center),\
Asteroids(astB_vector[0][0], astB_vector[1][0], self.size - 0.09, self.rect.center)
class Bullets(pygame.sprite.Sprite):
"""handle bullets"""
def __init__(self, pos, angle, vel=10):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image(os.path.join(CWD, "images", "bullet-v2.png"))
self.area = pygame.display.get_surface().get_rect()
self.vel = vel
self.rect.center = pos
self.angle = angle
self.image = pygame.transform.rotozoom(self.image, self.angle, 1)
def update(self):
"""moves bullet"""
dx = math.cos(math.radians(self.angle)) * self.vel
dy = -math.sin(math.radians(self.angle)) * self.vel
self.rect.move_ip(dx, dy)
class Scoreboard:
"""handle score and highscore"""
def __init__(self):
self.score = 0
self.highscore = self.get_highscore()
self.font_obj = pygame.font.Font(os.path.join(CWD, "fonts", "VT323-Regular.ttf"), 30)
self.text = f"Score: 0 Highscore: {self.highscore}"
self.surface = self.font_obj.render(self.text, True, (0, 0, 0))
self.area = pygame.display.get_surface().get_rect()
self.pos = (self.area.midtop[0] - self.surface.get_width() / 2, self.area.midtop[1])
def update_score(self):
"""updates rendered text"""
self.text = f"Score: {self.score} Highscore: {self.highscore}"
self.surface = self.font_obj.render(self.text, True, (0, 0, 0))
def update_highscore(self):
"""updates highscore and saves it to file"""
if self.score > self.highscore:
self.highscore = self.score
self.save_highscore()
def get_highscore(self):
"""get highscore from file, creates file if inexistent"""
if "highscore.txt" in os.listdir():
with open("highscore.txt", mode="r") as file:
score = file.read()
score = score.strip()
return int(score)
else:
return 0
def save_highscore(self):
"""saves highscore to file"""
with open("highscore.txt", mode="w") as file:
file.write(str(self.highscore))
class Heart(pygame.sprite.Sprite):
def __init__(self, pos=(0, 0)):
pygame.sprite.Sprite.__init__(self)
self.image = load_image(os.path.join(CWD, "images", "black-heart.jpg"))[0]
self.image = pygame.transform.scale(self.image, (38, 35))
self.rect = self.image.get_rect()
self.rect.topleft = pos