-
Notifications
You must be signed in to change notification settings - Fork 0
/
brianCraft.py
345 lines (309 loc) · 12.6 KB
/
brianCraft.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
335
336
337
338
339
340
341
342
343
344
345
# Project began Dec 20, 2014
import pygame, os
class Player(object):
def __init__(self, x, y, width, height, falling, health, crouching):
self.x = x
self.y = y
self.width = width
self.height = height
self.rect = pygame.Rect(x, y, self.width, self.height)
self.falling = True
self.health = 1000
self.crouching = crouching
def crouch(self):
self.rect = pygame.Rect(self.rect.x, self.rect.y, self.width, self.height/2)
self.crouching = True
def uncrouch(self):
x_cord = self.rect.x
y_cord = self.rect.y
for x in range(0, 900, 30):
if x <= x_cord and x_cord <= x+30:
x_cord = x
index_x = x/30
for y in range(0, 830, 30):
if y <= y_cord <= y+30:
y_cord = y
index_y = y/30
if (tile_map[index_y][index_x][0] == 1 and tile_map[index_y][index_x+1][0] == 1):
return
elif (tile_map[index_y][index_x-1][0] == 1 and tile_map[index_y][index_x][0] == 1):
return
elif tile_map[index_y][index_x][0] == 1:
return
else:
self.rect = pygame.Rect(self.rect.x, self.rect.y, self.width, self.height)
self.crouching = False
def move(self, dx, dy):
# Move each axis separately. Note that this checks for collisions both times.
self.falling = True
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):
self.rect.x += dx
self.rect.y += dy
for wall in walls:
if self.rect.colliderect(wall.rect): # When player collies with wall
if dx < 0: # Moving left; Hit the right side of the wall
self.rect.left = wall.rect.right
if dx > 0: # Moving right; Hit the left side of the wall
self.rect.right = wall.rect.left
if dy > 0: # Moving down; Hit the top side of the wall
self.rect.bottom = wall.rect.top
if dy < 0: # Moving up; Hit the bottom side of the wall
self.rect.top = wall.rect.bottom
for border in borders:
if self.rect.colliderect(border.rect): # When player collies with wall
if dx < 0: # Moving left; Hit the right side of the wall
self.rect.left = border.rect.right
if dx > 0: # Moving right; Hit the left side of the wall
self.rect.right = border.rect.left
if dy > 0: # Moving down; Hit the top side of the wall
self.rect.bottom = border.rect.top
if dy < 0: # Moving up; Hit the bottom side of the wall
self.rect.top = border.rect.bottom
for enemy in enemies:
if self.rect.colliderect(enemy.rect): # When player collides with enemy
self.health -= 2
if dx < 0: # Moving left; Hit the right side of the enemy
self.rect.left = enemy.rect.right
if dx > 0: # Moving right; Hit the left side of the enemy
self.rect.right = enemy.rect.left
if dy > 0: # Moving down; Hit the top side of the enemy
self.rect.bottom = enemy.rect.top
if dy < 0: # Moving up; Hit the bottom side of the enemy
self.rect.top = enemy.rect.bottom
def place(self, block, pos):
x_cord = pos[0]
y_cord = pos[1]
#if block == "wall":
for x in range(0, 900, 30):
if x <= x_cord and x_cord <= x+30:
x_cord = x
index_x = x/30
for y in range(0, 830, 30):
if y <= y_cord <= y+30:
y_cord = y
index_y = y/30
if enemy.rect.y-20 <= y_cord and y_cord <= enemy.rect.y+(enemy.height-5) and enemy.rect.x-20 <= x_cord and x_cord <= enemy.rect.x+20:
return
if player.rect.y-20 <= y_cord and y_cord <= player.rect.y+(player.height - 5) and player.rect.x-20 <= x_cord and x_cord <= player.rect.x+20:
return
if tile_map[index_y][index_x][0] == 1:
return
tile_map[index_y][index_x][0] = 2
def destroy(self, block, pos):
x_cord = pos[0]
y_cord = pos[1]
if block == "wall":
for x in range(0, 900, 30):
if x <= x_cord and x_cord <= x+30:
x_cord = x
index_x = x/30
for y in range(0, 830, 30):
if y <= y_cord <= y+30:
y_cord = y
index_y = y/30
if tile_map[index_y][index_x][0] == 2:
tile_map[index_y][index_x][0] = 0
else:
return
class Enemy(object):
def __init__(self, x, y, width, height, health):
enemies.append(self)
self.x = x
self.y = y
self.height = height
self.width = width
self.rect = pygame.Rect(x, y, width, height)
self.health = health
enemies.append(self)
def move(self, dx, dy):
self.rect.x += dx
self.rect.y += dy
# If you collide with a wall, move out based on velocity
for wall in walls:
if self.rect.colliderect(wall.rect): # When enemy collides with wall
if dx < 0: # Moving left; Hit the right side of the wall
self.rect.left = wall.rect.right
if dx > 0: # Moving right; Hit the left side of the wall
self.rect.right = wall.rect.left
if dy > 0: # Moving down; Hit the top side of the wall
self.rect.bottom = wall.rect.top
if dy < 0: # Moving up; Hit the bottom side of the wall
self.rect.top = wall.rect.bottom
if self.rect.colliderect(player.rect): # When enemy collies with player
player.health -= 2
if dx < 0: # Moving left; Hit the right side of the player
self.rect.left = player.rect.right
if dx > 0: # Moving right; Hit the left side of the player
self.rect.right = player.rect.left
if dy > 0: # Moving down; Hit the top side of the player
self.rect.bottom = player.rect.top
if dy < 0: # Moving up; Hit the bottom side of the player
self.rect.top = player.rect.bottom
for enemy in enemies:
if enemy != self:
if self.rect.colliderect(enemy.rect): # When enemy collies with player
if dx < 0: # Moving left; Hit the right side of the player
self.rect.left = enemy.rect.right
if dx > 0: # Moving right; Hit the left side of the player
self.rect.right = enemy.rect.left
if dy > 0: # Moving down; Hit the top side of the player
self.rect.bottom = enemy.rect.top
if dy < 0: # Moving up; Hit the bottom side of the player
self.rect.top = enemy.rect.bottom
class Wall(object):
def __init__(self, x, y):
walls.append(self) # Make it so that this adds it to the tile_map
self.x = x
self.y = y
self.rect = pygame.Rect(x, y, 30, 30)
def destroy(self):
walls.remove(self)
class Border(object):
def __init__(self, x, y):
borders.append(self) # Make it so that this adds it to the tile_map
self.x = x
self.y = y
self.rect = pygame.Rect(x, y, 30, 30)
def gravity(player):
surface = False
while surface == False:
player.rect.y += 3
for wall in walls:
if player.rect.colliderect(wall.rect):
player.rect.bottom = wall.rect.top
player.falling = False
for border in borders:
if player.rect.colliderect(border.rect):
player.rect.bottom = border.rect.top
player.falling = False
surface = True
def tile_map_update():
global walls
global borders
walls = []
borders = []
x = y = 0
for row in tile_map:
for col in row:
if col[0] == 1:
Border(x, y)
if col[0] == 2:
Wall(x, y)
x += 30
y += 30
x = 0
def enemy_AI():
for enemy in enemies:
if player.rect.x < enemy.rect.x:
enemy.move(-1, 0)
elif player.rect.x > enemy.rect.x:
enemy.move(1, 0)
elif player.rect.x == enemy.rect.x:
enemy.move(0, 0)
# Initialise pygame
os.environ["SDL_VIDEO_CENTERED"] = "1"
pygame.init()
# Set up the display
pygame.display.set_caption("Brian Craft")
screen = pygame.display.set_mode((900, 830))
clock = pygame.time.Clock()
walls = [] # List to hold the walls
enemies = [] # Lsit all the enemies
borders = []
player = Player(30, 300, 25, 55, False, 100, False) # Create the player
enemy = Enemy(500, 300, 30, 90, 500) # Create the Enemy
font = pygame.font.SysFont(None, 50)
LEFT = 1
RIGHT = 3
# Move tile_map to different file at some point
tile_map = [
[[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1]],
[[1],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[1]],
[[1],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[1]],
[[1],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[1]],
[[1],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[1]],
[[1],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[1]],
[[1],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[1]],
[[1],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[1]],
[[1],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[1]],
[[1],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[1]],
[[1],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[1]],
[[1],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[1]],
[[1],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[1]],
[[1],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[1]],
[[1],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[1]],
[[1],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[1]],
[[1],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[1]],
[[1],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[1]],
[[1],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[1]],
[[1],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[1]],
[[1],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[1]],
[[1],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[1]],
[[1],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[1]],
[[1],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[1]],
[[1],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[1]],
[[1],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[1]],
[[1],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[1]],
[[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1]],
]
tile_map_update()
running = True
while running:
# Sets framerate
clock.tick(60)
# Gravity
gravity(player)
for enemy in enemies:
gravity(enemy)
# Enemy AI
enemy_AI()
# Leave game, and jump
for e in pygame.event.get():
if e.type == pygame.QUIT:
running = False
if e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE:
running = False
if e.type == pygame.KEYUP:
if e.key == pygame.K_SPACE and player.falling == False and player.crouching == False:
player.move(0, -60)
if e.type == pygame.MOUSEBUTTONUP and e.button == RIGHT:
pos = pygame.mouse.get_pos()
player.place("wall", pos) # Places the block at that x-y coord
tile_map_update()
if e.type == pygame.MOUSEBUTTONUP and e.button == LEFT:
pos = pygame.mouse.get_pos()
player.destroy("wall", pos) # Breaks the block at that x-y coord
tile_map_update()
# Move the player if an arrow key is pressed
key = pygame.key.get_pressed()
if key[pygame.K_a]:
player.move(-3, 0)
if key[pygame.K_d]:
player.move(3, 0)
if key[pygame.K_LSHIFT]:
player.crouch()
else:
player.uncrouch()
# Game Over
if player.health < 1:
print 'Game Over!'
break
# Drawing the scene
screen.fill((0, 0, 0))
for wall in walls:
pygame.draw.rect(screen, (100, 100, 100), wall.rect)
pygame.draw.rect(screen, (200, 103, 123), player.rect)
for enemy in enemies:
pygame.draw.rect(screen, (50, 100, 50), enemy.rect)
player_health = font.render(str(player.health), True, (0, 128, 0))
screen.blit(player_health, (player.rect.x-20, player.rect.y-40))
for enemy in enemies:
enemy_health = font.render(str(enemy.health), True, (255, 51, 51))
screen.blit(enemy_health, (enemy.rect.x-20, enemy.rect.y-40))
for border in borders:
pygame.draw.rect(screen, (255, 255, 255), border.rect)
pygame.display.flip()