-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathobjects.py
More file actions
196 lines (145 loc) · 5.73 KB
/
Copy pathobjects.py
File metadata and controls
196 lines (145 loc) · 5.73 KB
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
import random
import time
import uuid
import pygame
from pygame.constants import K_LEFT, K_a, K_RIGHT, K_d
class BaseObject(pygame.sprite.Sprite):
def __init__(self, ipath, position, scale=0.5):
pygame.sprite.Sprite.__init__(self)
self.uuid = str(uuid.uuid4())
self.image = BaseObject.__scale__(ipath, position, scale)
self.rect = self.image.get_rect(center=position)
def get_center(self):
return self.rect.center
def draw(self, surface, **kwargs):
raise NotImplementedError('Not yet implemented')
@staticmethod
def __scale__(ipath, position, scale):
img = pygame.image.load(ipath)
rect = img.get_rect(center=position)
w, h = rect.size[0], rect.size[1]
w, h = int(w * scale), int(h * scale)
return pygame.transform.scale(img, (w, h))
class Bullet(BaseObject):
def __init__(self, position, scale=1.0):
BaseObject.__init__(self, './images/bullet.png', position, scale)
def draw(self, surface, **kwargs):
x, y = self.rect.center
self.rect.center = x, y - 5
surface.blit(self.image, self.rect.center)
if y <= 0:
self.kill()
@staticmethod
def instance(**kwargs):
return Bullet(kwargs['position'])
class Rock(BaseObject):
def __init__(self, position, scale=1.0):
BaseObject.__init__(self, './images/rock.png', position, scale)
def draw(self, surface, **kwargs):
x, y = self.rect.center
self.rect.center = x, y + 1
surface.blit(self.image, self.rect.center)
if y >= kwargs['height']:
self.kill()
class Ship(BaseObject):
def __init__(self, position, scale=1.0):
BaseObject.__init__(self, './images/ship.png', position, scale)
def draw(self, surface, **kwargs):
width, height = kwargs['width'], kwargs['height']
x, y = self.rect.center
w, h = self.rect.size
if 'keys' in kwargs:
keys = kwargs['keys']
if keys[K_LEFT] or keys[K_a]:
if x - w / 2.0 >= 0:
x -= 4
elif keys[K_RIGHT] or keys[K_d]:
if x + w / 2.0 <= width:
x += 4
self.rect.center = x, y
surface.blit(self.image, self.rect.center)
class RockGenerator(object):
def __init__(self, width, height):
rock = Rock((width / 2.0, height / 2.0))
w, h = rock.rect.size
self.x_pos = [x for x in range(0, width, w)]
self.prev_x = None
self.start_time = None
def should_generate(self):
if self.start_time is None:
self.start_time = time.time()
return True
stop_time = time.time()
diff = int(stop_time - self.start_time)
if diff >= 2:
self.start_time = stop_time
return True
return False
def next(self):
while True:
curr_x = random.choice(self.x_pos)
if curr_x != self.prev_x:
self.prev_x = curr_x
return Rock((curr_x, 20))
class GameInfo(object):
def __init__(self, score, lives):
self.font = pygame.font.Font('freesansbold.ttf', 20)
self.color = (0, 0, 0)
self.position = 10, 10
self.score = score
self.lives = lives
def __get_drawing_objects__(self):
text_surface = self.font.render(f'Score: {self.score}, Lives: {self.lives}', True, self.color)
rect = text_surface.get_rect()
rect.x, rect.y = 10, 10
return text_surface, rect
def draw(self, surface, **kwargs):
self.score = kwargs['score']
self.lives = kwargs['lives']
text_surface, rect = self.__get_drawing_objects__()
surface.blit(text_surface, rect)
class GameOverMessage(object):
def __init__(self, width, height, score, lives):
self.game_over_font = pygame.font.Font('freesansbold.ttf', 30)
self.game_info_font = pygame.font.Font('freesansbold.ttf', 20)
self.game_action_font = pygame.font.Font('freesansbold.ttf', 20)
self.game_over_color = (255, 0, 0)
self.game_info_color = (0, 0, 0)
self.game_action_color = (0, 0, 0)
self.center = width / 2.0, height / 2.0
self.score = score
self.lives = lives
def __get_game_over__(self):
text = 'Game Over'
text_surface = self.game_over_font.render(text, True, self.game_over_color)
rect = text_surface.get_rect()
rect.center = self.center
return text_surface, rect
def __get_game_info__(self):
text = f'Score: {self.score}, Lives: {self.lives}'
text_surface = self.game_info_font.render(text, True, self.game_info_color)
rect = text_surface.get_rect()
rect.center = self.center[0], self.center[1] + 30
return text_surface, rect
def __get_game_action__(self):
text = 'Hit "q" to quit. Hit "c" to continue.'
text_surface = self.game_action_font.render(text, True, self.game_action_color)
rect = text_surface.get_rect()
rect.center = self.center[0], self.center[1] + 60
return text_surface, rect
def draw(self, surface):
text_surface, rect = self.__get_game_over__()
surface.blit(text_surface, rect)
text_surface, rect = self.__get_game_info__()
surface.blit(text_surface, rect)
text_surface, rect = self.__get_game_action__()
surface.blit(text_surface, rect)
class AudioEffects(object):
def __init__(self):
self.effects = {
'bullet': pygame.mixer.Sound('./audio/bullet.wav'),
'explosion': pygame.mixer.Sound('./audio/explosion.wav')
}
def play(self, effect):
if effect in self.effects:
self.effects[effect].play()