def new(): global arr, e1, e2, e3, e4, bm, f1, f2, brick #create walls w = Walls() arr = w.structure() #flags use for bomb f1 = False f2 = False #random number of bricks num = random.randint(15, 25) for i in range(0, num): brick = Bricks(arr) arr = brick.place_brick() #four enemies e1 = Enemy(arr) e2 = Enemy(arr) e3 = Enemy(arr) e4 = Enemy(arr) #create bomberman instance bm = Bomberman(arr) arr = bm.place(arr, 4, 2) #create board instance b = Board(arr, lives, score) b.print_board()
def reset_game(grid, man): ''' To reset the game whenever the bomberman dies ''' man = Bomberman(2, 4) grid = man.populate_grid_with_person(grid) return [grid, man]
def __init__(self, level=1): level = Level(level) self.score = 0 grid = Grid(config.rows, config.cols) wall = Wall(grid) bomberman = Bomberman(config.init_row, config.init_col, grid) monsters = [] for i in range(len(level.monsters_row)): monsters.append( Monster(level.monsters_row[i], level.monsters_col[i], grid)) blocks = self.block_arrangement(grid) for i in range(10): pass bombs = [] count = 0 while True: count = count + 1 if count is not level.monsters_speed_factor else 0 if count is level.monsters_speed_factor: if len(bombs) > 0: for bomb in bombs: bomb.seconds_left = bomb.seconds_left - 1 grid.matrix[bomb.row][bomb.col] = 'bomb' + str( bomb.seconds_left) if bomb.seconds_left is 0: bomb.explode(grid) bombs.remove(bomb) self.check_if_dead(monsters, bomberman, grid) self.check_if_block_destroyed(blocks, grid) for monster in monsters: monster.move(grid) self.check_if_dead(monsters, bomberman, grid) grid.display() self.print_score() k = self.input_to() if k == 'x': bombs.append(Bomb(bomberman.row, bomberman.col, grid)) elif k == 'q': exit(0) elif k in ['w', 'a', 's', 'd', 'i', 'j', 'k', 'l']: bomberman.move(k, grid) grid.display() self.print_score()
async def agent_loop(server_address="localhost:8000", agent_name="student"): async with websockets.connect( f"ws://{server_address}/player") as websocket: # receive information about static game properties await websocket.send(json.dumps({"cmd": "join", "name": agent_name})) msg = await websocket.recv() game_properties = json.loads(msg) # you can create your own map representation or use the game representation: mapa = Map(size=game_properties["size"], mapa=game_properties["map"]) # init bomberman agent properties bomberman = Bomberman() logger.debug("STARTING GAME") while True: try: while websocket.messages: await websocket.recv() logger.debug(f"Websocket messages: {websocket.messages}") state = json.loads( await websocket.recv() ) # receive game state, this must be called timely or your game will get out of sync with the server if "lives" not in state or not state["lives"]: logger.debug("GAME OVER!") return mapa.walls = state["walls"] # update our bomberman state bomberman.update_state(state, mapa) # choose next move of bomberman key = bomberman.next_move() if key is None: logger.debug("RANDOM KEY") moves = ["w", "a", "s", "d"] key = random.choice(moves) logger.debug(f"P: {bomberman.pos} | K: {key}") await websocket.send( json.dumps({ "cmd": "key", "key": key }) ) # send key command to server - you must implement this send in the AI agent except websockets.exceptions.ConnectionClosedOK: print("Server has cleanly disconnected us") return
def movement(self, direction, x_cor, y_cor, index, type_c): if direction == 'a': if self.Board[x_cor][y_cor - 4] == '#' or self.Board[x_cor][y_cor - 4] == '%': return self.Board else: self.Board[x_cor][y_cor] = ' ' self.Board[x_cor][y_cor + 1] = ' ' self.Board[x_cor][y_cor + 2] = ' ' self.Board[x_cor][y_cor + 3] = ' ' self.Board[x_cor + 1][y_cor] = ' ' self.Board[x_cor + 1][y_cor + 1] = ' ' self.Board[x_cor + 1][y_cor + 2] = ' ' self.Board[x_cor + 1][y_cor + 3] = ' ' y_cor = y_cor - 4 if type_c == 1: b2 = Bomberman(x_cor, y_cor) b21 = b2.create_man(self.Board) self.x_v = x_cor self.y_v = y_cor self.x_b = x_cor self.y_b = y_cor return b21 elif type_c == 2: b3 = Enemy(x_cor, y_cor) b3 = b3.create_enemy(self.Board) self.enemy_array[index] = x_cor self.enemy_array1[index] = y_cor return b3 elif direction == 'd': if self.Board[x_cor][y_cor + 4] == '#' or self.Board[x_cor][y_cor + 4] == '%': return self.Board else: self.Board[x_cor][y_cor] = ' ' self.Board[x_cor][y_cor + 1] = ' ' self.Board[x_cor][y_cor + 2] = ' ' self.Board[x_cor][y_cor + 3] = ' ' self.Board[x_cor + 1][y_cor] = ' ' self.Board[x_cor + 1][y_cor + 1] = ' ' self.Board[x_cor + 1][y_cor + 2] = ' ' self.Board[x_cor + 1][y_cor + 3] = ' ' y_cor = y_cor + 4 if type_c == 1: b2 = Bomberman(x_cor, y_cor) b21 = b2.create_man(self.Board) self.x_v = x_cor self.y_v = y_cor self.x_b = x_cor self.y_b = y_cor return b21 elif type_c == 2: b3 = Enemy(x_cor, y_cor) b3 = b3.create_enemy(self.Board) self.enemy_array[index] = x_cor self.enemy_array1[index] = y_cor return b3 elif direction == 's': if self.Board[x_cor + 2][y_cor] == '#' or self.Board[x_cor + 2][y_cor] == '%': return self.Board else: self.Board[x_cor][y_cor] = ' ' self.Board[x_cor][y_cor + 1] = ' ' self.Board[x_cor][y_cor + 2] = ' ' self.Board[x_cor][y_cor + 3] = ' ' self.Board[x_cor + 1][y_cor] = ' ' self.Board[x_cor + 1][y_cor + 1] = ' ' self.Board[x_cor + 1][y_cor + 2] = ' ' self.Board[x_cor + 1][y_cor + 3] = ' ' x_cor = x_cor + 2 if type_c == 1: b2 = Bomberman(x_cor, y_cor) b21 = b2.create_man(self.Board) self.x_v = x_cor self.y_v = y_cor self.x_b = x_cor self.y_b = y_cor return b21 elif type_c == 2: b3 = Enemy(x_cor, y_cor) b3 = b3.create_enemy(self.Board) self.enemy_array[index] = x_cor self.enemy_array1[index] = y_cor return b3 elif direction == 'w': if self.Board[x_cor - 2][y_cor] == '#' or self.Board[x_cor - 2][y_cor] == '%': return self.Board else: self.Board[x_cor][y_cor] = ' ' self.Board[x_cor][y_cor + 1] = ' ' self.Board[x_cor][y_cor + 2] = ' ' self.Board[x_cor][y_cor + 3] = ' ' self.Board[x_cor + 1][y_cor] = ' ' self.Board[x_cor + 1][y_cor + 1] = ' ' self.Board[x_cor + 1][y_cor + 2] = ' ' self.Board[x_cor + 1][y_cor + 3] = ' ' x_cor = x_cor - 2 if type_c == 1: b2 = Bomberman(x_cor, y_cor) b21 = b2.create_man(self.Board) # b2.set_x_cor(x_cor) # b2.set_y_cor(y_cor) self.x_v = x_cor self.y_v = y_cor self.x_b = x_cor self.y_b = y_cor return b21 elif type_c == 2: b3 = Enemy(x_cor, y_cor) b3 = b3.create_enemy(self.Board) self.enemy_array1[index] = y_cor self.enemy_array[index] = x_cor return b3
def place_man(self): b2 = Bomberman(2, 4) b2 = b2.create_man(self.Board) return b2
return c qflag = 0 level = 1 while level <= 3: t = Terminal() bx = 0 by = 0 planted = False SCORE = 0 flag = 0 arr = [[' ' for i in range(76)] for y in range(38)] wall1 = Wall() arr = wall1.buildwall(arr) # populate the array with the wall. bricks = Brick(t.cyan('/')) arr = bricks.place(arr) # populate the array with the bricks. bomber = Bomberman(t.green('B')) num = randint(3*level, 4*level) enarr = [] var = False for i in range(num): enarr.append(Enemy(t.red('E'))) # enarr is populated with all the enemies. arr = bomber.spawn(arr) # bomberman is spawned. for i in range(0, num): arr = enarr[i].randspawn(arr) # all the enemies are randomly spawned. if level == 1: lives = 3 print t.blue('SCORE :- '), SCORE, t.blue('LIVES :- '), lives, t.blue('LEVEL :- '), level, t.yellow('\t\tBOMBERMAN\n') for i in range(38): print ''.join(arr[i]) while lives: # loop terminates when user has no lives left. flag = 0
import time import os import random import signal from board import Board from brick import Brick from bomberman import Bomberman from getchunix import GetchUnix from enemy import Enemy from bomb import Bomb from alarmexception import AlarmException EMB = Board(42, 84, 2, 4) # creates object b od class Board BNE = EMB.createboard() BNE = EMB.buildrigidboard(BNE) # Builds borders of board with walls BNE = EMB.buildmiddlewalls(BNE) # symmetric spaces of board with walls B = Bomberman(2, 4, 3, 0) # bom is our object bomberman BNE = B.generatebomberman(BNE) C = Brick(30, 2, 4, 42, 84) BNE = C.generatebricks(BNE) # Creates bricks(breakable) randomly on board EMB.enemies = [0] * 6 # Creates list of enemies G = GetchUnix() def alarmhandler(signum, frame): """ this method controls time and char relation """ signum = signum frame = frame raise AlarmException def input_to(timeout=1): # time after which enemies move automatically
LEVEL = 1 MAX_LIFES = 2 TOTAL_SCORE = 0 MAX_ENEMIES = 6 BRICKSLIMIT = 40 BOX = [] while LEVEL <= 3: BOARD = Board(42, 84, BRICKSLIMIT) GRID = BOARD.create_empty_board() GRID = BOARD.populate_board_with_walls(GRID) GRID = BOARD.populate_board_with_bricks(GRID) MAN = Bomberman(2, 4) GRID = MAN.populate_grid_with_person(GRID) GRID = BOARD.populate_board_with_enemies(GRID, MAX_ENEMIES) ENEMIES = len(BOARD.enemies) COUNT = 0 DETONATION = 0 ACTIVE = 0 MOVE_TYPE = 0 SCORE = 0 FLAG = 0 RESETFLAG = 0 NEXTINPUT = time.time() + 1 ''' This is a FLAG used to allow SYNCHRONOUS motion of the enemies and bomb DETONATION '''
['X'] * 3) final_array.append(['X'] + ([' '] * 4 + ['X'] * 4) * 9 + [' '] * 4 + ['X'] * 3) final_array.append(['X'] * 80) final_array.append(['X'] * 80) bricks(final_array) return final_array def printer(final_array): for arr in final_array: print("".join(arr)) a = empty() player = Bomberman() e1 = Enemy() e2 = Enemy() e3 = Enemy() #printer(a) (player.insert(a)) e1.place(a) e2.place(a) e3.place(a) #printer(a) print("Press z to start the game") new = getch() count = 0 while new != 'q': if player.check_wall(new, a):
''' The run file # Instances of classes created here # for running the application.''' import os from bomberman import Bomberman from manage import Manage # TEST is an object of Bomberman TEST = Bomberman() # TEST1 is an object of Manage TEST1 = Manage() # the initial Welcome Print. print("") print("Welcome, to this rendition of Bomberman!") print("") print("Press 'Enter' to start playing.") TAKENINPUT = input() # This generates bricks on the board TEST.generatebricks() # This generates enemies on the board TEST.generateenemies() # This generates the bomberman TEST.generatebomberman() i = 0 while True: os.system("tput reset")
def input_to(timeout=1): # Input function signal.signal(signal.SIGALRM, alarmHandler) signal.alarm(timeout) try: text = getch() signal.alarm(0) return text except AlarmException: os.system('clear') b.output(disp) print("Your Score is %d" % score) signal.signal(signal.SIGALRM, signal.SIG_IGN) return '' player = Bomberman() # different objects being created b = Board() b.display(disp) e = Enemy() u = Bomb() s = Bricks() e.generate_enemies(disp, 10) # enemies being generated for i in range( 10 ): # if you want more enemies just change the number 10 in this and above line e.change(i, disp) s.generate_bricks(disp) # brikcs being generated player.start(disp) b.output(disp) planted = 0 # different variables for various purposes count = 0
from bomb import bomb from getch import getch from bricks import bricks from enemy import enemy import os import time import glob import random from walls import walls from termcolor import colored #from __future__ import print_function import signal,copy,sys,time wall1=walls(42,84) bman=Bomberman(2,4) lives=10 level=0 bmb=0 no_of_bricks=0 no_of_enemy=0 # These three functions to handle the cases when user doesn't input anything class AlarmException(Exception): pass def alarmHandler(signum, frame): raise AlarmException def input_to(timeout=1): signal.signal(signal.SIGALRM, alarmHandler)
def ResetGame(grid, man): man = Bomberman(2, 4, 1) grid = man.PopulateGridWithPerson(grid) return [grid, man]
max_lifes = 2 TotalScore = 0 max_enemies = 6 brickslimit = 40 # BEGIN GAME while Level <= 3: # DECLARATIONS for each level a = Board(42, 84, brickslimit, 2, 4) b = a.CreateEmptyBoard() b = a.PopulateBoardWithWalls(b) b = a.PopulateBoardWithBricks(b) man = Bomberman(2, 4, 1) b = man.PopulateGridWithPerson(b) b = a.PopulateBoardWithEnemies(b, max_enemies) enemies = len(a.enemies) count = 0 detonation = 0 # Used as a detonator for the bomb Active = 0 # Indicates whether the bomb is active or not move_type = 0 # Indicates the type of move score = 0 # Indicates the score of each level flag = 0 Resetflag = 0 # Used to call ResetGame function NextInput = time.time() + 1 ''' This is a flag used to allow Synchronous motion of the enemies and bomb detonation ''' Synchronous = 0
import numpy from getch import get_char import random import time from board import Board from person import Person from enemy import Enemy from bomberman import Bomberman from wall import Wall from brick import Brick from bomb import Bomb board = Board(38, 76, 0.1) bomberman = Bomberman(2, 4, 2, 4, 3, 0) emptySpace = numpy.asarray([[' '] * 4] * 2) wall = Wall(2, 4) fps = board.fps board.initialise(bomberman, wall) def render(): playerInput = "none" playerInput = get_char() time.sleep(fps) # player input # if playerInput in ("W", "w"): bomberman.moveUp(board.gameBoard, emptySpace) elif playerInput in ("D", "d"): bomberman.moveRight(board.gameBoard, emptySpace)