-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.py
393 lines (346 loc) · 16.1 KB
/
Game.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ---------------------------
# Importacion de los módulos
# ---------------------------
SERIAL = False
import pygame, datetime, Tweet, threading
from pygame.locals import *
import Tile, Hud, os, sys, random, math, time
if SERIAL:
import Serial
# -----------
# Constantes
# -----------
SCREEN_WIDTH = 320
SCREEN_HEIGHT = 240
FPS = 30
BOMB_TIME = 3
TIME_LIMIT = 90
UP = 0
RIGHT = 1
DOWN = 2
LEFT = 3
DIRECTIONS = [UP, RIGHT, DOWN, LEFT]
# ------------------------------
# Clases y Funciones utilizadas
# ------------------------------
def texts(text, x, y):
font=pygame.font.Font(None,30)
scoretext=font.render(text, 1,(255,255,255))
screen.blit(scoretext, (x, y))
# -----------------------------------------------
# Creamos los sprites (clases) de los objetos del juego:
class Game():
def __init__(self):
self.score = 0
self.player = Tile.Bomberman(32,48,1)
self.player2 = Tile.Bomberman(288,208,2)
self.players = [self.player, self.player2]
self.hud = Hud.Hud()
self.tiles = pygame.sprite.Group()
self.bombs = pygame.sprite.Group()
self.fires = pygame.sprite.Group()
self.powerups = pygame.sprite.Group()
self.win = None
self.map = [[None]*13 for i in range(19)]
self.createMap()
self.tock = 0
def timeLeft(self):
return TIME_LIMIT-self.tock/FPS
def startGame(self, screen):
clock = pygame.time.Clock()
pygame.key.set_repeat(1, 15)
while self.win is None and self.timeLeft() >= 0:
clock.tick(FPS)
self.checkEvents()
self.drawGame(screen)
self.tock += 1
return self.win, self.timeLeft()
def createMap(self):
for x in range(0,19):
for y in range(0,13):
xGrid = 16+16*x
yGrid = 32+16*y
# Bloques bordeantes
if x == 0 or y == 0 or x == 18 or y == 12:
b = Tile.Block(xGrid,yGrid)
self.tiles.add(b)
self.map[x][y] = b
# Bloques internos
elif y % 2 == 0 and x % 2 == 0:
b = Tile.Block(xGrid,yGrid)
self.tiles.add(b)
self.map[x][y] = b
# Ladrillos al azar
elif random.randint(1,8) == 4 and (x > 2 or y > 2) and (x < 16 or y < 10):
b = Tile.Brick(xGrid,yGrid,self.getRandomPowerUp(xGrid,yGrid))
self.tiles.add(b)
self.map[x][y] = b
def getRandomPowerUp(self,x,y):
n = random.randint(1,27)
if n < 9:
return Tile.BombPower(x,y)
elif n < 15:
return Tile.FirePower(x,y)
elif n == 15:
return Tile.SpeedPower(x,y)
elif n < 19:
return Tile.TransportPower(x,y)
else:
return None
def destroy(self, tile):
powerup = tile.destroy()
if powerup:
self.powerups.add(powerup)
self.map[powerup.x][powerup.y] = powerup
elif issubclass(tile.__class__, Tile.Bomb):
self.explodeBomb(tile)
else:
self.map[tile.x][tile.y] = None
def explodeBomb(self, bomb):
bomb.owner.removeBomb(bomb)
fire = Tile.Fire(bomb.rect.centerx, bomb.rect.centery,FPS*1, bomb.expansion)
self.fires.add(fire)
bomb.kill()
self.map[bomb.x][bomb.y] = None
for direction in DIRECTIONS:
for pos in range(1,fire.expansion[direction]+1):
x,y = fire.position(direction, pos)
tile = self.map[x][y]
if tile:
if tile.isBreakable:
self.destroy(tile)
fire.expansion[direction] = pos-1
break
fire.updateSprite()
def checkFire(self, player, fire):
if player.x == fire.x and player.y == fire.y:
return True
for direction in DIRECTIONS:
for cell in range(1,fire.expansion[direction]+1):
x,y = fire.position(direction, cell)
if player.x == x and player.y == y:
return True
return False
def drawGame(self, screen):
for bomb in self.bombs:
bomb.timer -= 1
if bomb.timer <= 0:
self.explodeBomb(bomb)
for fire in self.fires:
fire.timer -= 1
if fire.timer <= 0:
fire.kill()
else:
for player in self.players:
if self.checkFire(player, fire):
self.win = 1 if player.playerNumber is 2 else 2
screen.fill((16,120,48))
self.tiles.draw(screen)
#self.bombs.draw(screen)
for bomb in self.bombs:
bomb.draw(screen)
self.fires.draw(screen)
self.powerups.draw(screen)
self.player.draw(screen)
self.player2.draw(screen)
self.hud.draw(screen, self.timeLeft())
pygame.display.flip()
def putBomb(self, player):
otherPlayer = self.players[player.number%2]
if otherPlayer.x != player.x or otherPlayer.y != player.y:
if not self.map[player.x][player.y] and not pygame.sprite.spritecollideany(player, self.bombs):
bomb = player.createBomb(FPS*BOMB_TIME)
if bomb:
self.bombs.add(bomb)
self.map[bomb.x][bomb.y] = bomb
def transportPlayer(self, player):
while True:
x = random.randint(1,18)
y = random.randint(1,12)
if self.map[x][y] is None:
break
xr, yr = player.getSpriteCoordinates(x,y)
player.rect.centerx = xr
player.rect.centery = yr
player.updatePosition()
player.transport = False
self.hud.removePower(player.number)
def movePlayer(self, player, dx, dy):
player.rect.centerx += dx
player.rect.centery += dy
for tile in self.tiles:
if not tile.isCrossable and player.rect.colliderect(tile.rect):
if dx > 0: # Moving right; Hit the left side of the tile
player.rect.right = tile.rect.left
if dx < 0: # Moving left; Hit the right side of the tile
player.rect.left = tile.rect.right
if dy > 0: # Moving down; Hit the top side of the tile
player.rect.bottom = tile.rect.top
if dy < 0: # Moving up; Hit the bottom side of the tile
player.rect.top = tile.rect.bottom
for bomb in self.bombs:
if player.rect.colliderect(bomb.rect):
if not bomb == player.insideBomb:
if dx > 0: # Moving right; Hit the left side of the bomb
player.rect.right = bomb.rect.left
if dx < 0: # Moving left; Hit the right side of the bomb
player.rect.left = bomb.rect.right
if dy > 0: # Moving down; Hit the top side of the bomb
player.rect.bottom = bomb.rect.top
if dy < 0: # Moving up; Hit the bottom side of the bomb
player.rect.top = bomb.rect.bottom
elif bomb == player.insideBomb:
player.insideBomb = None
for powerup in self.powerups:
if player.rect.colliderect(powerup.rect):
if issubclass(powerup.__class__, Tile.TransportPower):
self.hud.putPower(player.number)
powerup.activate(player)
powerup.kill()
self.map[powerup.x][powerup.y] = None
player.updatePosition()
def checkEvents(self):
if SERIAL:
PlayController = Serial.keysPS()
else:
PlayController = {"PS1_CUADRADO":False, "PS1_TRIANGULO":False, "PS1_CIRCULO":False, "PS1_EQUIS":False, "PS1_ARRIBA":False, "PS1_ABAJO":False, "PS1_IZQUIERDA":False, "PS1_DERECHA":False, "PS1_L1":False, "PS1_R1":False, "PS1_L2":False, "PS1_R2":False, "PS1_L3":False, "PS1_R3":False, "PS1_START":False, "PS1_SELECT":False, "PS1_JLARRIBA":False, "PS1_JLABAJO":False, "PS1_JLIZQUIERDA":False, "PS1_JLDERECHA":False, "PS1_JRARRIBA":False, "PS1_JRABAJO":False, "PS1_JRIZQUIERDA":False, "PS1_JRDERECHA":False, "PS2_CUADRADO":False, "PS2_TRIANGULO":False, "PS2_CIRCULO":False, "PS2_EQUIS":False, "PS2_ARRIBA":False, "PS2_ABAJO":False, "PS2_IZQUIERDA":False, "PS2_DERECHA":False, "PS2_L1":False, "PS2_R1":False, "PS2_L2":False, "PS2_R2":False, "PS2_L3":False, "PS2_R3":False, "PS2_START":False, "PS2_SELECT":False, "PS2_JLARRIBA":False, "PS2_JLABAJO":False, "PS2_JLIZQUIERDA":False, "PS2_JLDERECHA":False, "PS2_JRARRIBA":False, "PS2_JRABAJO":False, "PS2_JRIZQUIERDA":False, "PS2_JRDERECHA":False}
keys = pygame.key.get_pressed()
if keys[K_ESCAPE]:
sys.exit(0)
# Player 1 controls
if (keys[K_DOWN] or keys[K_UP] or keys[K_RIGHT] or keys[K_LEFT] or
PlayController["PS1_ABAJO"] or PlayController["PS1_JLABAJO"] or
PlayController["PS1_ARRIBA"] or PlayController["PS1_JLARRIBA"] or
PlayController["PS1_DERECHA"] or PlayController["PS1_JLDERECHA"] or
PlayController["PS1_IZQUIERDA"] or PlayController["PS1_JLIZQUIERDA"]):
self.player.moving=True
else:
self.player.moving=False
if keys[K_DOWN]:
self.player.direction = DOWN
self.movePlayer(self.player,0,4*self.player.speed)
if keys[K_UP]:
self.player.direction = UP
self.movePlayer(self.player,0,-4*self.player.speed)
if keys[K_RIGHT]:
self.player.direction = RIGHT
self.movePlayer(self.player,4*self.player.speed,0)
if keys[K_LEFT]:
self.player.direction = LEFT
self.movePlayer(self.player,-4*self.player.speed,0)
if keys[K_RCTRL]:
self.putBomb(self.player)
if keys[K_RSHIFT] and self.player.transport:
self.transportPlayer(self.player)
if PlayController["PS1_ABAJO"] or PlayController["PS1_JLABAJO"]:
self.player.direction = DOWN
self.movePlayer(self.player,0,4*self.player.speed)
if PlayController["PS1_ARRIBA"] or PlayController["PS1_JLARRIBA"]:
self.player.direction = UP
self.movePlayer(self.player,0,-4*self.player.speed)
if PlayController["PS1_DERECHA"] or PlayController["PS1_JLDERECHA"]:
self.player.direction = RIGHT
self.movePlayer(self.player,4*self.player.speed,0)
if PlayController["PS1_IZQUIERDA"] or PlayController["PS1_JLIZQUIERDA"]:
self.player.direction = LEFT
self.movePlayer(self.player,-4*self.player.speed,0)
if PlayController["PS1_EQUIS"]:
self.putBomb(self.player)
if PlayController["PS1_CIRCULO"] and self.player.transport:
self.transportPlayer(self.player)
# Player 2 controls
if (keys[K_w] or keys[K_s] or keys[K_a] or keys[K_d] or
PlayController["PS2_ABAJO"] or PlayController["PS2_JLABAJO"] or
PlayController["PS2_ARRIBA"] or PlayController["PS2_JLARRIBA"] or
PlayController["PS2_DERECHA"] or PlayController["PS2_JLDERECHA"] or
PlayController["PS2_IZQUIERDA"] or PlayController["PS2_JLIZQUIERDA"]):
self.player2.moving=True
else:
self.player2.moving=False
if keys[K_s]:
self.player2.direction = DOWN
self.movePlayer(self.player2,0,4*self.player2.speed)
if keys[K_w]:
self.player2.direction = UP
self.movePlayer(self.player2,0,-4*self.player2.speed)
if keys[K_d]:
self.player2.direction = RIGHT
self.movePlayer(self.player2,4*self.player2.speed,0)
if keys[K_a]:
self.player2.direction = LEFT
self.movePlayer(self.player2,-4*self.player2.speed,0)
if keys[K_LCTRL]:
self.putBomb(self.player2)
if keys[K_LSHIFT] and self.player2.transport:
self.transportPlayer(self.player2)
if PlayController["PS2_ABAJO"] or PlayController["PS2_JLABAJO"]:
self.player2.direction = DOWN
self.movePlayer(self.player2,0,4*self.player2.speed)
if PlayController["PS2_ARRIBA"] or PlayController["PS2_JLARRIBA"]:
self.player2.direction = UP
self.movePlayer(self.player2,0,-4*self.player2.speed)
if PlayController["PS2_DERECHA"] or PlayController["PS2_JLDERECHA"]:
self.player2.direction = RIGHT
self.movePlayer(self.player2,4*self.player2.speed,0)
if PlayController["PS2_IZQUIERDA"] or PlayController["PS2_JLIZQUIERDA"]:
self.player2.direction = LEFT
self.movePlayer(self.player2,-4*self.player2.speed,0)
if PlayController["PS2_EQUIS"]:
self.putBomb(self.player2)
if PlayController["PS2_CIRCULO"] and self.player2.transport:
self.transportPlayer(self.player2)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
# ------------------------------
# Funcion principal del juego
# ------------------------------
def main():
pygame.init()
# creamos la ventana y le indicamos un titulo:
screen = pygame.display.set_mode((320, 240))
pygame.display.set_caption("BomberPi")
while True:
game = Game()
whoWin, timeleft = game.startGame(screen)
date_end = datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S")
if not whoWin is None:
message = "Gano el jugador %s en la fecha %s a falta de %s segundos" % (whoWin, date_end, timeleft)
else:
message = "En la fecha %s hubo un empate de los dos jugadores" % (date_end)
t = threading.Thread(target=Tweet.sendTweet, args=(message, ))
t.start(); t.join
#End of game
scoreBadge=pygame.image.load("scoreframe.png")
scoreBadge.convert_alpha()
screen.blit(scoreBadge,(10,10))
scoreFont=pygame.font.Font(None,52)
scoreFont2=pygame.font.Font(None,22)
if whoWin is None:
statusText=scoreFont.render('Draw game',True,(255,255,255))
screen.blit(statusText,(66,90))
else:
statusText=scoreFont.render('Player '+str(whoWin)+' wins',True,(255,255,255))
screen.blit(statusText,(44,90))
statusText2=scoreFont2.render('Press r to restart',True,(255,255,255))
screen.blit(statusText2,(104,130))
pygame.display.flip()
## Wait for the player to restart
restart = False
while not restart:
if SERIAL:
PlayController = Serial.keysPS()
else:
PlayController = {"PS1_CUADRADO":False, "PS1_TRIANGULO":False, "PS1_CIRCULO":False, "PS1_EQUIS":False, "PS1_ARRIBA":False, "PS1_ABAJO":False, "PS1_IZQUIERDA":False, "PS1_DERECHA":False, "PS1_L1":False, "PS1_R1":False, "PS1_L2":False, "PS1_R2":False, "PS1_L3":False, "PS1_R3":False, "PS1_START":False, "PS1_SELECT":False, "PS1_JLARRIBA":False, "PS1_JLABAJO":False, "PS1_JLIZQUIERDA":False, "PS1_JLDERECHA":False, "PS1_JRARRIBA":False, "PS1_JRABAJO":False, "PS1_JRIZQUIERDA":False, "PS1_JRDERECHA":False, "PS2_CUADRADO":False, "PS2_TRIANGULO":False, "PS2_CIRCULO":False, "PS2_EQUIS":False, "PS2_ARRIBA":False, "PS2_ABAJO":False, "PS2_IZQUIERDA":False, "PS2_DERECHA":False, "PS2_L1":False, "PS2_R1":False, "PS2_L2":False, "PS2_R2":False, "PS2_L3":False, "PS2_R3":False, "PS2_START":False, "PS2_SELECT":False, "PS2_JLARRIBA":False, "PS2_JLABAJO":False, "PS2_JLIZQUIERDA":False, "PS2_JLDERECHA":False, "PS2_JRARRIBA":False, "PS2_JRABAJO":False, "PS2_JRIZQUIERDA":False, "PS2_JRDERECHA":False}
keys = pygame.key.get_pressed()
if keys[K_ESCAPE]:
sys.exit(0)
if keys[K_r] or PlayController["PS1_START"] or PlayController["PS2_START"]:
restart = True
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if __name__ == "__main__":
main()