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 __init__(self, username, levelNum): ## Initialize bomberman self.bomberman = Bomberman() ## String Player's username self.username = username # Level info ## Integer the level's number self.levelNum = levelNum ## Boolean True if the game has started self.isInitialized = False ## Nested List of Tile(s) representing the game board self.board = [] ## Queue containing the coords and time left of bombs laid by bomberman self.bombQueue = [] ## Queue containing the coords and time left of flame tiles produced by bomb(s) self.flashQueue = [] ## List coordinates of the powerup tile self.powerUpCoord = [0, 0] ## List coordinates of the exit tile self.exitCoord = [0, 0] ## Integer type of powerup self.powerUp = 0 ## Integer number of enemies in the board self.numberEnemies = 0 ## List of all enemies in the board self.listEnemies = [] ## List of Integer all enemies by type, e.g. value at index 0 represents the number of enemies of level 0 self.listTypeEnemies = [0, 0, 0, 0, 0, 0, 0, 0] # Status bar info ## Integer time left in the game self.timeLeft = 200 ## Boolean True if time has elapsed self.timeDone = False ## Integer player's score self.score = 0
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
class Level(object): ## Constructor of a level which takes a username and a level as argument def __init__(self, username, levelNum): ## Initialize bomberman self.bomberman = Bomberman() ## String Player's username self.username = username # Level info ## Integer the level's number self.levelNum = levelNum ## Boolean True if the game has started self.isInitialized = False ## Nested List of Tile(s) representing the game board self.board = [] ## Queue containing the coords and time left of bombs laid by bomberman self.bombQueue = [] ## Queue containing the coords and time left of flame tiles produced by bomb(s) self.flashQueue = [] ## List coordinates of the powerup tile self.powerUpCoord = [0, 0] ## List coordinates of the exit tile self.exitCoord = [0, 0] ## Integer type of powerup self.powerUp = 0 ## Integer number of enemies in the board self.numberEnemies = 0 ## List of all enemies in the board self.listEnemies = [] ## List of Integer all enemies by type, e.g. value at index 0 represents the number of enemies of level 0 self.listTypeEnemies = [0, 0, 0, 0, 0, 0, 0, 0] # Status bar info ## Integer time left in the game self.timeLeft = 200 ## Boolean True if time has elapsed self.timeDone = False ## Integer player's score self.score = 0 ## Reinitialize all of level's variables and bomberman's variable to start new level def setNewLevel(self): self.bomberman.reset() self.timeLeft = 200 self.board = [] self.bombQueue = [] self.flashQueue = [] self.powerUpCoord = [0, 0] self.exitCoord = [0, 0] self.powerUp = 0 self.numberEnemies = 0 self.listEnemies = [] self.listTypeEnemies = [0, 0, 0, 0, 0, 0, 0, 0] self.clearBoard() self.clearBombs() self.setLevelInfo() self.setConcrete() self.setExit() self.setPowerup() self.setBrick() self.setEnemies() self.setBomberman() ## This method activates the current level's powerup and add attribute to bomberman def gainPowerUp(self): if(self.powerUp == 1): self.bomberman.numBombs += 1 if(self.powerUp == 2): self.bomberman.rangeOfBombs += 1 if(self.powerUp == 3): self.bomberman.speed = 400 if(self.powerUp == 4): self.bomberman.wallPass = True if(self.powerUp == 5): self.bomberman.hasDetonator = True if(self.powerUp == 6): self.bomberman.bombPass = True if(self.powerUp == 7): self.bomberman.flamePass = True if(self.powerUp == 8): self.bomberman.invincible = True ## This method returns the tile at x and y from the board def tileAt(self, x, y): return self.board[y][x].peek() ## This method sets tile at x and y on the board def setTileAt(self, x, y, tile): self.board[y][x].push(tile) ## This method removes the tile at x and y from the board def popTileAt(self, x, y): self.board[y][x].pop() ## This method lays a bomb at bomberman's current position def setBomb(self): self.bombQueue.append((self.bomberman.curX, self.bomberman.curY, constant.TIME_BOMB)) tempTile = self.tileAt(self.bomberman.curX, self.bomberman.curY) self.popTileAt(self.bomberman.curX, self.bomberman.curY) self.setTileAt(self.bomberman.curX, self.bomberman.curY, Tile.Bomb) self.setTileAt(self.bomberman.curX, self.bomberman.curY, tempTile) # LEVEL SETUP ## This method clears all tiles on the board def clearBoard(self): self.board = [[Tile() for x in range(constant.BOARD_WIDTH)] for y in range(constant.BOARD_HEIGHT)] ## This method clears all bombs from the bomb queue def clearBombs(self): self.bombQueue = [] ## This method sets concrete tiles systematically on the board def setConcrete(self): for y in range(constant.BOARD_HEIGHT): for x in range(constant.BOARD_WIDTH): if x == 0 or x == 30 or y == 0 or y == 12: self.setTileAt(x,y,Tile.Concrete) elif x % 2 == 0 and y % 2 == 0: self.setTileAt(x,y,Tile.Concrete) ## This method sets an exit tile randomly on the board and set a brick on top of it def setExit(self): while True: tempX = random.randint(1, constant.BOARD_WIDTH) - 1 tempY = random.randint(1, constant.BOARD_HEIGHT) - 1 if (self.tileAt(tempX, tempY) == Tile.Empty and not (tempX == 1 and tempY == constant.BOARD_HEIGHT - 2) and not (tempX == 1 and tempY == constant.BOARD_HEIGHT - 3) and not (tempX == 2 and tempY == constant.BOARD_HEIGHT - 2)): self.setTileAt(tempX, tempY, Tile.Exit) self.setTileAt(tempX, tempY, Tile.Brick) self.exitCoord[0] = tempX self.exitCoord[1] = tempY break ## This method fetches a the list of enemies and powerup matching the current level number def setLevelInfo(self): self.listTypeEnemies, self.powerUp = Enemy.getEnemyListAndPowerUp(self.levelNum) ## This method sets a powerup tile randomly on the board and set a brick on top of it def setPowerup(self): while True: tempX = random.randint(1, constant.BOARD_WIDTH) - 1 tempY = random.randint(1, constant.BOARD_HEIGHT) - 1 if (self.tileAt(tempX, tempY) == Tile.Empty and not (tempX == 1 and tempY == constant.BOARD_HEIGHT - 2) and not (tempX == 1 and tempY == constant.BOARD_HEIGHT - 3) and not (tempX == 2 and tempY == constant.BOARD_HEIGHT - 2)): self.setTileAt(tempX, tempY, Tile.Powerup) self.setTileAt(tempX, tempY, Tile.Brick) self.powerUpCoord[0] = tempX self.powerUpCoord[1] = tempY break ## This method sets brick tiles randomly on the board def setBrick(self): for y in range(constant.BOARD_HEIGHT): for x in range (constant.BOARD_WIDTH): if (self.tileAt(x, y) == Tile.Empty and not (x == 1 and y == constant.BOARD_HEIGHT - 2) and not (x == 1 and y == constant.BOARD_HEIGHT - 3) and not (x == 2 and y == constant.BOARD_HEIGHT - 2)): if (random.random() <= constant.PERCENT_BRICK): self.setTileAt(x, y, Tile.Brick) ## This method sets bomberman at x and y on the board def setBomberman(self): self.setTileAt(self.bomberman.curX,self.bomberman.curY,Tile.Bomberman) ## This method sets enemies randomly on the map def setEnemies(self): # print self.listTypeEnemies for i in range(8): for j in range(self.listTypeEnemies[i]): while True: tempX = random.randint(1, constant.BOARD_WIDTH) - 1 tempY = random.randint(1, constant.BOARD_HEIGHT) - 1 if (self.tileAt(tempX, tempY) == Tile.Empty and not (tempX == 1 and tempY == constant.BOARD_HEIGHT - 2) and not (tempX == 1 and tempY == constant.BOARD_HEIGHT - 3) and not (tempX == 2 and tempY == constant.BOARD_HEIGHT - 2)): self.setTileAt(tempX, tempY, i + 8) tempList = [tempX, tempY, random.randint(0, 3), i + 8] self.listEnemies.append(tempList) self.numberEnemies += 1 break ## This method clears all enemies on the map and in the list of enemies and list of enemies type def clearEnemies(self): for enemy in self.listEnemies: self.popTileAt(enemy[0],enemy[1]) self.numberEnemies = 0 self.listEnemies = [] self.listTypeEnemies = [0, 0, 0, 0, 0, 0, 0, 0] ## This method sets 8 mega enemies everywhere, used when bomb detonates an exit tile or powerup tile def setChaos(self): highestIndex = 0 self.setLevelInfo() for x in xrange(len(self.listTypeEnemies)): if self.listTypeEnemies[x] != 0: highestIndex = x if highestIndex != 7: highestIndex += 1 self.clearEnemies() self.listTypeEnemies[highestIndex] = 8 self.setEnemies() ## This method sets 8 maximum level enemies everywhere def setSuperChaos(self): self.setLevelInfo() self.clearEnemies() self.listTypeEnemies[7] = 8 self.setEnemies()
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
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
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]
def place_man(self): b2 = Bomberman(2, 4) b2 = b2.create_man(self.Board) return b2
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
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)