This repository has been archived by the owner on Oct 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplayer.py
309 lines (239 loc) · 11.5 KB
/
player.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
import pygame
from random import randint
from pathfinder import Graph
wall_list = pygame.sprite.Group()
node_step = 2
GRID_INCREMENT = 12
class Player(pygame.sprite.Sprite):
solid_object = pygame.sprite.Group()
@staticmethod
def set_objects(objects):
solid_object = objects
def __init__(self, player_id):
super(Player, self).__init__()
self.image = pygame.Surface([16, 16])
self.rect = pygame.Rect(0, 0, 16, 16)
# new movement variables
self.previous_x = self.rect.x
self.current_x = self.rect.x
self.previous_y = self.rect.y
self.current_y = self.rect.y
# set the color and position of each player
if player_id == 1:
self.image.fill((255, 0, 0))
self.rect.x = 24
self.rect.y = 24
self.player_id = 1
elif player_id == 2:
self.image.fill((0, 0, 255))
self.rect.x = 600
self.rect.y = 410
self.player_id = 2
elif player_id == 3:
self.image.fill((255, 255, 255))
self.rect.x = randint(20, 300)
self.rect.y = randint(20, 300)
#game variables
self.score = 0
self.hit_points = 100
self.ammo = 100
# variables for A.I. behavior
self.healthy = 1.0
self.is_healthy = True
self.winning = False
def set_healthy(self):
self.healthy = self.hit_points / float(100)
def is_player_healthy(self):
return self.healthy > 0.5
def move(self, dx, dy):
# Move each axis separately. Note that this checks for collisions both times.
if dx != 0:
self.move_single_axis(dx, 0)
if dy != 0:
self.move_single_axis(0, dy)
def move_single_axis(self, dx, dy):
# Move the rect
self.rect.x += dx
self.rect.y += dy
#set the variables to check if we are moving
self.current_x = self.rect.x
# If you collide with an object, move out based on velocity
for wall in wall_list:
if self.rect.colliderect(wall.rect):
if dx > 0: # Moving right; Hit the left side of the wall
self.rect.right = wall.rect.left
self.previous_x = self.current_x
elif dx < 0: # Moving left; Hit the right side of the wall
self.rect.left = wall.rect.right
self.previous_x = self.current_x
elif dy > 0: # Moving down; Hit the top side of the wall
self.rect.bottom = wall.rect.top
elif dy < 0: # Moving up; Hit the bottom side of the wall
self.rect.top = wall.rect.bottom
def move_to_next_node(self, node_coordinate):
if node_coordinate[0] < self.rect.centerx:
self.move(-2, 0)
elif node_coordinate[0] > self.rect.centerx:
self.move(2, 0)
# up and down
if node_coordinate[1] < self.rect.centery:
self.move(0, -2)
elif node_coordinate[1] > self.rect.centery:
self.move(0, 2)
def chase_object(self, target):
# left and right movement
if target[0] < self.rect.x:
self.move(-2,0)
elif target[0] > self.rect.x:
self.move(2, 0)
#up and down
if target[1] < self.rect.y:
self.move(0, -2)
elif target[1] > self.rect.y:
self.move(0, 2)
def run_away(self, coordinate):
self.move(-(coordinate[0] % 6), -(coordinate[1] % 4))
# print(-coordinate[0] % 4)
def shoot_up(self):
bullet = Bullet(1)
bullet.rect.x = self.rect.centerx
bullet.rect.y = self.rect.y - 8
return bullet
def shoot_down(self):
bullet = Bullet(2)
bullet.rect.x = self.rect.centerx
bullet.rect.y = self.rect.y + 16
return bullet
def shoot_left(self):
bullet = Bullet(3)
bullet.rect.x = self.rect.x - 16
bullet.rect.y = self.rect.centery
return bullet
def shoot_right(self):
bullet = Bullet(4)
bullet.rect.x = self.rect.x + 16
bullet.rect.y = self.rect.centery
return bullet
def getColor(self):
if self.player_id == 1:
return (255, 0, 0)
elif self.player_id == 2:
return (0, 0, 255)
class Wall(pygame.sprite.Sprite):
def __init__(self, x, y, length, width):
super(Wall, self).__init__()
self.image = pygame.Surface([length, width])
self.rect = pygame.Rect(x, y, length, width)
self.image.fill((255, 255, 255))
self.rect.x = x
self.rect.y = y
@staticmethod
def build_map():
# Holds the arena layout using an array of strings.
# W = 12 X 12 brick
# S = spacing, moves map down by 24 pixels
# B = 84 X 12 wall
# and so on...
level = [
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
"W W W",
"W W W",
"W W W W",
"W W W W",
"W W W W",
"W W W W",
"W W W W",
"W W W W",
"W W W W",
"W WWWWWWWWWWWWWWWWWWWWWWWWWW W W",
"W W W",
"W WWWWWWWWWWWWWWWW W",
"W W W",
"W W W",
"W W W",
"W WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW W",
"W W W",
"W W W",
"W W W W",
"W WWWWWWWW W W",
"W W W WWWWW W",
"W W WWWWW WWWWWWWWWWWWWWWWWWWWWWW W W",
"W WWWWWWWWWWWWWWWWW W WWWWWWWWWWWW",
"W W W W",
"W W W W",
"W W W W W",
"W W WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW W W",
"WWWWWWWWWWWWWWWWW W W",
"W W W W",
"W W W W",
"W W W W",
"W W W W",
"W WWWWWWWWWWWWWWWW WWWWWWWWWWW W",
"W W W W",
"W W W W",
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
]
blueprint = insert_nodes(level, node_step)
map_graph = Graph(blueprint, node_step)
node_graph = map_graph.node_list
# Parse the level string above.
x = y = node_counter = 0
for row in blueprint:
for col in row:
if col == "W":
wall = Wall(x, y, 4, 4)
wall_list.add(wall)
x += GRID_INCREMENT
y += GRID_INCREMENT
x = 0
return node_graph
green = (0,255,0)
class Bullet(pygame.sprite.Sprite):
def __init__(self, direction):
# Call the parent class (Sprite) constructor
super(Bullet,self).__init__()
self.image = pygame.Surface([2, 6])
self.image.fill(green)
# for shooting directions: 1 = up, 2 = down, 3 = left, 4 = right
self.direction = direction
self.rect = pygame.Rect(0, 0, 6, 2)
def move_up(self):
self.rect.y -= 8
def move_down(self):
self.rect.y += 8
def move_left(self):
self.rect.x -= 8
def move_right(self):
self.rect.x += 8
def update(self, wall_list):
movement = {1 : self.move_up, 2 : self.move_down, 3 : self.move_left, 4 : self.move_right}
movement[self.direction]()
return self.check_collision(wall_list)
def check_collision(self, wall_list):
if self.rect.x < 0 or self.rect.x > 1116 or self.rect.y < 0 or self.rect.y > 444:
return True
for object in wall_list:
if self.rect.colliderect(object):
# print("Bullet colliding")
if isinstance(object, Player):
object.hit_points -= 10
object.set_healthy()
# print(wall.hit_points)
# print(wall.healthy)
# print(wall.is_player_healthy())
return True
return False
def insert_nodes(blueprint, step):
node_blueprint = []
for y, line in enumerate(blueprint):
if y % step == 0:
letter_list = []
for x, letter in enumerate(line):
if x % step == 0 and letter != 'W':
letter = 'N'
letter_list.append(letter)
node_line = ''.join(letter_list)
node_blueprint.append(node_line)
else:
node_blueprint.append(line)
return node_blueprint