-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.py
259 lines (199 loc) · 6.54 KB
/
controller.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
import pygame
from collections import namedtuple
import time
# 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)
# 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
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):
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()
# TODO: Use getters and setters ?
def set_position(self, x, y):
'''
This defines where the enemies will start in a level
:param x: x position of the screen
:param y: y position of the screen
:return: none
'''
self.rect.x = x
self.rect.y = y
return
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 Menu():
def __init__(self):
self.background = WHITE
self.titleStyle = pygame.font.Font('freesansbold.ttf', 100)
self.title = 'Game'
def show(self):
return
def game_intro(menu):
'''
Introduction menu for the user
:return: none
'''
intro = True
while intro:
# Check for any event
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# Show menu
display.fill(WHITE)
menu.show()
# Initialize pygame
pygame.init()
display = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.set_caption('Welcome!')
# Create a clock for the game
clock = pygame.time.Clock()
# This is a list of all sprites in the game
all_sprites_list = pygame.sprite.Group()
# List of enemies
enemies = pygame.sprite.Group()
# Exit
level_exit = pygame.sprite.Group()
# 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()
enemy1.set_position(400, 300)
enemy2 = Enemy()
enemy2.set_position(200, 200)
all_sprites_list.add(enemy1, enemy2)
enemies.add(enemy1, enemy2)
# hard coding the start position for now to the bottom right corner
end_position = Position(x=SCREEN_WIDTH - END_SIZE.width, y=SCREEN_HEIGHT - END_SIZE.height)
start_position = Position(x=0, y=0)
# 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_list.add(start_gate, end_gate)
level_exit.add(end_gate)
all_sprites_list.add(player) # this has to be the last sprite added in order to take priority
# Run game until it crashes
crashed = False
menu = Menu()
game_intro(menu=menu)
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_postion(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!')
# Draw all sprites
all_sprites_list.draw(display)
# Update the display
pygame.display.flip()
# 60 frames per second
clock.tick(FPS)
# Quit out of pygame when it crashes
pygame.quit()