-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.py
334 lines (265 loc) · 8.98 KB
/
control.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import pygame
from collections import namedtuple
import time
import sys
''' START GLOBAL VARIABLES '''
# Some namedtuples that will be used to clear things up
Color = namedtuple('Color', ['red', 'green', 'blue'])
Position = namedtuple('Position', ['x', 'y'])
Size = namedtuple('Size', ['width', 'height'])
# Some colors
WHITE = Color(red=255, green=255, blue=255)
BLACK = Color(red=0, green=0, blue=0)
RED = Color(red=255, green=0, blue=0)
BLUE = Color(red=0, green=0, blue=255)
YELLOW = Color(red=255, green=255, blue=0)
PURPLE = Color(red=255, green=0, blue=255)
# Screen Settings
SCREEN_SIZE = SCREEN_WIDTH, SCREEN_HEIGHT = (800, 700)
FPS = 60
# Start and End Settings
START_SIZE = END_SIZE = Size(width=100, height=100)
GATE_SIZE = START_SIZE
''' END GLOBAL VARIABLES '''
''' START SPRITE CLASSES '''
class Player(pygame.sprite.Sprite):
color = RED
width = 50
height = 50
def __init__(self):
super().__init__()
# Create an image of the player
self.image = pygame.Surface((self.width, self.height))
self.image.fill(color=self.color)
# Update the position of this object by setting the values of rect.x and rect.y
self.rect = self.image.get_rect()
# How fast the player can move
self.velocity = 10
def move(self, direction):
'''
Moves player on screen. Have to enforce boundaries so player can't leave the screen.
:param direction: LEFT, RIGHT, UP, or DOWN
:return:
'''
if direction == 'LEFT':
# Make sure player doesn't leave screen
if (self.rect.x - self.velocity) > 0:
self.rect.x -= self.velocity
# Set the position so it hits the wall
else:
self.rect.x = 0
if direction == 'RIGHT':
if (self.rect.x + self.width + self.velocity) < SCREEN_WIDTH:
self.rect.x += self.velocity
else:
self.rect.x = SCREEN_WIDTH - self.width
# TODO: Fix top boundary
if direction == 'UP':
if (self.rect.y + self.velocity) > 0:
self.rect.y -= self.velocity
else:
self.rect.y = 0
if direction == 'DOWN':
if (self.rect.y + self.height + self.velocity) < SCREEN_HEIGHT:
self.rect.y += self.velocity
else:
self.rect.y = SCREEN_HEIGHT - self.height
return
def starting_position(self, x, y):
'''
Sets the starting position of the player
:param x: starting x
:param y: starting y
:return:
'''
self.rect.x = x
self.rect.y = y
return
class Enemy(pygame.sprite.Sprite):
color = BLUE
width = 25
height = 25
def __init__(self, start):
super().__init__()
# Create an image of the player
self.image = pygame.Surface((self.width, self.height))
self.image.fill(color=self.color)
# Update the position of this object by setting the values of rect.x and rect.y
self.rect = self.image.get_rect()
self.rect.x = start.x
self.rect.y = start.y
class MovingEnemy(Enemy):
'''
This allows some enemies to move
'''
def __init__(self, start, end, velocity, direction):
super().__init__(start)
self.velocity = velocity
self.direction = direction
self.start = start
self.end = end
def move(self):
''' Only allows sideways movement at the time being '''
if self.velocity > 0:
if self.rect.x + self.velocity < self.end.x:
self.rect.x += self.velocity
else:
self.velocity *= -1
else:
if self.rect.x + self.velocity > self.start.x:
self.rect.x += self.velocity
else:
self.velocity *= -1
class Gate(pygame.sprite.Sprite):
'''
Exit and Entrance to level
'''
width = GATE_SIZE.width
height = GATE_SIZE.height
def __init__(self, x, y):
super().__init__()
# Create the image for the gate
self.image = pygame.Surface((self.width, self.height))
self.image.fill(color=WHITE)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
class Collectible(pygame.sprite.Sprite):
'''
Collectible items
'''
def __init__(self):
super().__init__()
class Coin(Collectible):
'''
All coins must be collected to pass a level. Currently takes a hitbox of a rectangle, could switch it later
'''
width = 25
height = 25
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface((self.width, self.height))
self.image.fill(color=BLACK)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
# draws
pygame.draw.ellipse(self.image, YELLOW, (0, 0, self.width, self.height))
''' END SPRITE CLASSES '''
class Wall:
'''
Creates walls in the level
'''
width = 50
height = 50
def __init__(self, x, y):
self.rect = pygame.Rect(x, y, self.width, self.height)
# row consists of 16 columns, 14 rows (screen / wall)
mock_level = [
"WWWWWWWWWWWWWWWW",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"WWWWWWWWWWWWWWWW",
]
def main():
# Initialize pygame display
pygame.init()
display = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.set_caption('Welcome!')
# Create a clock for the game
clock = pygame.time.Clock()
# List of sprite groups
all_sprites = pygame.sprite.Group()
enemies = pygame.sprite.Group()
moving_enemies = pygame.sprite.Group()
level_exit = pygame.sprite.Group()
coins = pygame.sprite.Group()
y = 0
walls = []
for row in mock_level:
x = 0
for block in row:
if block == 'W':
wall = Wall(x=x, y=y)
walls.append(wall)
x += 50
y += 50
coin = Coin(x=150, y=250)
coins.add(coin)
all_sprites.add(coin)
# Initialize a player
player_start = Position(x=0, y=0)
player = Player()
player.starting_position(x=player_start.x, y=player_start.y)
# Initialize some enemies
enemy1 = Enemy(start=Position(x=100, y=100))
enemy2 = Enemy(start=Position(x=500, y=500))
enemy3 = MovingEnemy(start=Position(x=250, y=250), end=Position(x=500, y=500), velocity=5, direction='L')
# Add sprites to their corresponding groups
all_sprites.add(enemy1, enemy2, enemy3)
enemies.add(enemy1, enemy2, enemy3)
moving_enemies.add(enemy3)
# hard coding the start position for now to the bottom right corner (levels will be different)
end_position = Position(x=SCREEN_WIDTH - END_SIZE.width, y=SCREEN_HEIGHT - END_SIZE.height)
start_position = Position(x=100, y=300)
# Start and end gates for the level
start_gate = Gate(x=start_position.x, y=start_position.y)
end_gate = Gate(x=end_position.x, y=end_position.y)
all_sprites.add(start_gate, end_gate)
level_exit.add(end_gate)
all_sprites.add(player) # this has to be the last sprite added in order to take priority
# Run game until it crashes
crashed = False
while not crashed:
display.fill(color=BLACK)
# Check for any event
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# Key Movement for player
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player.move(direction='LEFT')
if keys[pygame.K_RIGHT]:
player.move(direction='RIGHT')
if keys[pygame.K_UP]:
player.move(direction='UP')
if keys[pygame.K_DOWN]:
player.move(direction='DOWN')
# Check for collision between the player and the enemies
collision = pygame.sprite.spritecollide(sprite=player, group=enemies, dokill=False)
if collision:
time.sleep(.5)
player.starting_position(x=10, y=20)
# Check to see if player made it to the end
win = pygame.sprite.spritecollide(sprite=player, group=level_exit, dokill=False)
if win:
print('You win!')
# Keeps moving enemies in action
for enemy in moving_enemies:
enemy.move()
for wall in walls:
pygame.draw.rect(display, PURPLE, wall.rect)
collect_coins = pygame.sprite.spritecollide(sprite=player, group=coins, dokill=True)
# Draw all sprites
all_sprites.draw(display)
# Update the display
pygame.display.flip()
# 60 frames per second
clock.tick(FPS)
# Quit out of pygame when it crashes
pygame.quit()
sys.exit()
if __name__ == '__main__':
main()