def create_bombs(ai_settings, screen, bombs): if len(bombs) == 0: bomb = Bomb(ai_settings, screen) bomb_width = bomb.rect.width for bomb_number in range(2): bomb = Bomb(ai_settings, screen) bomb.x = bomb_width + 10 * bomb_width * random.random() bomb.rect.x = bomb.x bombs.add(bomb)
def fire(self): if (len(Bomb.bombs) == 0): self.save1 = self.pos bomb = Bomb(self.save1) if (len(Bomb.bombs) == 1): self.save2 = self.pos bomb = Bomb(self.save2) if (len(Bomb.bombs) == 2): self.save3 = self.pos bomb = Bomb(self.save3) if (len(Bomb.bombs) < 1): Bomb.bombs.append(bomb) self.lifecount += 50
def __init__(self): pygame.init() pygame.mixer.init() self.screen = pygame.display.set_mode((896, 896)) self.screen_rect = self.screen.get_rect() self.font = pygame.font.Font(None, 50) self.font2 = pygame.font.Font(None, 25) self.won = False self.lost = False pygame.display.set_caption("Bobby The Destroyer") self.clock = pygame.time.Clock() this_folder = os.path.dirname(os.path.abspath(__file__)) my_file = os.path.join(this_folder, 'maze_old.txt') self.level = Level(self.screen, my_file) self.player = Player(self.screen, 1, 1, self.level) self.guard = Guard(self.screen, 12, 1, self.player, self.level) self.bombs = [] self.numfin = 0 self.bombs.append(Bomb(self.screen, 3, 5, self.player, self.guard)) self.bombs.append(Bomb(self.screen, 3, 8, self.player, self.guard)) self.bombs.append(Bomb(self.screen, 10, 7, self.player, self.guard)) self.bombs.append(Bomb(self.screen, 10, 6, self.player, self.guard)) self.bombs.append(Bomb(self.screen, 12, 2, self.player, self.guard)) self.bombs.append(Bomb(self.screen, 12, 10, self.player, self.guard)) self.bombs.append(Bomb(self.screen, 7, 3, self.player, self.guard)) self.bombs.append(Bomb(self.screen, 7, 10, self.player, self.guard)) self.game_active = True self.bg = PhotoRect(self.screen, "background", 896, 896)
def __init__(self, width=400, height=300, bgColor=(0, 0, 0)): self.width = width self.height = height self.bgColor = bgColor self.enemyFireId = pygame.USEREVENT+1 pygame.init() self.screen = pygame.display.set_mode((self.width, self.height)) self.myTank = MyTank(self.screen) self.bullets = Group() self.clock = Clock() self.enemys = Group() self.enemyBullets = Group() for i in range(1, 5): enemy = EnemyTank(75 * i, 30) self.enemys.add(enemy) self.bomb = Bomb() # 游戏结束的标志 self.stop = False # 设置定时器 pygame.time.set_timer(self.enemyFireId,800) # 游戏结束显示的文字 myFont = pygame.font.SysFont("simhei", 24) self.fontImage = myFont.render("游戏结束!", True, (0, 255, 0))
def eventHandler(self): key = pygame.key.get_pressed() if self.player.isAlive: if key[pygame.K_LEFT]: self.player.walkCount += 1 self.player.walking = Directions["LEFT"] self.player.looking = Directions["LEFT"] self.player.moveLeft() elif key[pygame.K_RIGHT]: self.player.walkCount += 1 self.player.walking = Directions["RIGHT"] self.player.looking = Directions["RIGHT"] self.player.moveRight() elif key[pygame.K_UP]: self.player.walkCount += 1 self.player.walking = Directions["UP"] self.player.looking = Directions["UP"] self.player.moveUp() elif key[pygame.K_DOWN]: self.player.walkCount += 1 self.player.walking = Directions["DOWN"] self.player.looking = Directions["DOWN"] self.player.moveDown() # da modificare elif key[pygame.K_SPACE]: self.bombs.append( Bomb(self.player.x, self.player.y, self.screen)) else: self.player.walkCount = 0 self.player.walking = Directions["STAY"]
def player_attacks(self, player, new_pos_x, new_pos_y): """ function which returns a game object that would be create if player attacks and then moves :param player: :param new_pos_x: :param new_pos_y: :return: """ game_copy_attacks = deepcopy(self) player_copy = game_copy_attacks.get_player_by_name(player) if player_copy.bomb_dropped is None: # player drops a bomb game_copy_attacks.table[player_copy.position[0]][player_copy.position[1]] = 'b' new_bomb = Bomb(position=player_copy.position, owner=player_copy) player_copy.bomb_dropped = new_bomb game_copy_attacks.bombs.append(new_bomb) else: # player activates the bomb dropped game_copy_attacks.table[player_copy.position[0]][player_copy.position[1]] = ' ' for bomb in game_copy_attacks.bombs: if bomb.owner.player_name == player and not bomb.activated: bomb.activate(game_copy_attacks) break if game_copy_attacks.table[new_pos_x][new_pos_y] == 'p': game_copy_attacks.found_protection(player_copy, new_pos_x, new_pos_y) else: game_copy_attacks.not_found_protection(player_copy, new_pos_x, new_pos_y) return game_copy_attacks, player_copy
def bomb (pi_settings, screen, ship, bombs): #Create new bullet and add add it to bullets group if len(bombs) < pi_settings.bombs_allowed: new_bomb = Bomb(pi_settings, screen, ship) bombs.add(new_bomb) shoot_sound.play() pi_settings.bombs_allowed -= 1
def main(): bomb = Bomb() # Create a new bomb instance, which stores indicator/battery/other info # Pick a module while True: curr_module = input("What module would you like to solve? Your options are:\n\n"\ "Simple Wires / Button / Keypads / Simon Says / Who's on First\n"\ "Memory / Morse Code / Complicated Wires / Wire Sequences\n"\ "Maze / Passwords / Knobs / Quit\n\n") if curr_module.lower() == 'simple wires': simple_wires = SimpleWires() simple_wires.start_simple_wires(bomb) if curr_module.lower() == 'button': button = Button() button.start_button(bomb) if curr_module.lower() == 'keypads': keypads = Keypads() keypads.start_keypads() if curr_module.lower() == 'simon says': simon_says = SimonSays() simon_says.start_simon_says(bomb) if curr_module.lower() == 'who\'s on first': whos_on_first = WhosOnFirst() whos_on_first.start_whos_on_first() if curr_module.lower() == 'memory': memory = Memory() memory.start_memory(bomb) if curr_module.lower() == 'morse code': morse_code = MorseCode() morse_code.start_morse_code() if curr_module.lower() == 'complicated wires': complicated_wires = ComplicatedWires() complicated_wires.start_complicated_wires(bomb) if curr_module.lower() == 'wire sequences': wire_sequences = WireSequences() wire_sequences.start_wire_sequences() if curr_module.lower() == 'maze': maze = Maze() maze.start_maze() if curr_module.lower() == 'passwords': passwords = Passwords() passwords.start_passwords(bomb) if curr_module.lower() == 'knobs': knobs = Knobs() knobs.start_knobs() if curr_module.lower() == 'quit': break
def tick(self, playfield): if not self.alive: return if (self.cooldown > 0): self.cooldown -= 1 current_tile = playfield[self.y][self.x] if current_tile.exploding > 0: self.alive = False current_tile.setPlayer(None) return old_pos = [self.x, self.y] if (self.intent == 'U'): self.y -= 1 elif (self.intent == 'D'): self.y += 1 elif (self.intent == 'L'): self.x -= 1 elif (self.intent == 'R'): self.x += 1 elif (self.intent == 'B' and self.cooldown == 0): if current_tile.bomb == None: current_tile.bomb = Bomb(self.x, self.y, playfield) cooldown = 50 new_tile = playfield[self.y][self.x] if new_tile.isPassable(): playfield[old_pos[1]][old_pos[0]].setPlayer(None) new_tile.setPlayer(self) else: self.x = old_pos[0] self.y = old_pos[1]
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) elif playerInput in ("A", "a"): bomberman.moveLeft(board.gameBoard, emptySpace) elif playerInput in ("S", "s"): bomberman.moveDown(board.gameBoard, emptySpace) elif playerInput in ("B", "b"): board.bombs.append( Bomb(2, 4, bomberman.position_row, bomberman.position_col)) board.bombs[-1].show(board.gameBoard) elif playerInput in ("P", "p"): board.powerUp() elif playerInput in ("Q", "q"): print("BYE BYE ,SEE YOU SOON") bomberman.die() elif playerInput is None: pass # player input # board.operate(bomberman, wall, emptySpace)
def plant_bomb(self, map, time, bombs, bonuses): if self.bomb_limit > 0: bomb_posX = round(self.posX / 4) bomb_posY = round(self.posY / 4) # do not plant bomb on existing bomb for bomb in bombs: if bomb.posX == bomb_posX and bomb.posY == bomb_posY: return False #prevent bomb being planted in front of character if self.direction == 0: if self.posY / (4 * bomb_posY) < 1: return False elif self.direction == 1: if self.posX / (4 * bomb_posX) < 1: return False elif self.direction == 2: if self.posY / (4 * bomb_posY) > 1: return False elif self.direction == 3: if self.posX / (4 * bomb_posX) > 1: return False bomb = Bomb(self.bomb_range, round(self.posX / 4), round(self.posY / 4), map, self, time, bonuses) self.bomb_limit -= 1 bombs.append(bomb) map[bomb.posX][bomb.posY] = 3 return True
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()
def plot_bomb(self, grid, radius, timelimit, det): ''' plot bomb function plots a new bomb and appends the bomb to active array of the bomberman attribute. ''' new_bomb = Bomb([radius, timelimit, det], 3, self.get_lefttop_x(), self.get_lefttop_y()) self.active.append(new_bomb) return grid
def getShot(self): shot = [] shot.append( Bomb(self.bullet_image, self.screen, self.rect, self.config.bullet_speed, self.bullet_anim, rotation=180)) return shot
def test_put_bomb_in_backpack_and_get_bomb_index(self): level = Level('level1.txt') for b in level.BomberMan: bm = b bm.backpack.append(Bomb(bm, power=2)) index = level.put_bomb_and_get_bomb_index() self.assertEqual(Point(1, 1), level.Bombs[index].position) self.assertEqual(2, level.Bombs[index].power) self.assertEqual(0, len(bm.backpack)) self.assertTrue(level.Bombs[index].is_active)
def fire_bomb(ai_settings, screen, stats, sb, link, bombs, clock): """if bombs on screen are less than the limit, create a new instance of Bomb and add it to the bombs Group. also decrease the scoring multiplier by two. begin the timer for the bomb""" if len(bombs) < ai_settings.bomb_limit: stats.multiplier = 1 sb.prep_multiplier() new_bomb = Bomb(ai_settings, screen, link) bombs.add(new_bomb) new_bomb.timer = pygame.time.get_ticks()
def drop_bomb(ai_settings, screen, ship, aliens, bombs): #select a random alien from the list of aliens abp = list(aliens) if len(abp) > 0: alien_bomb_prepped = choice(abp) for alien in aliens.copy(): if alien_bomb_prepped.rect.x == ship.rect.x and len(bombs) < ai_settings.bombs_allowed: new_bomb = Bomb(ai_settings, screen, alien_bomb_prepped) bombs.add(new_bomb) pygame.mixer.Sound.play(bomb_sound)
def explode(self, bird): for i in xrange(0, random.randint(5, 10)): b = Bomb(bird) bird.add_bomb(b) b.timeout = 1600 b.rect.x = self.rect.x b.rect.y = self.rect.y - 32 * gl.RESIZE_FACTOR b.launch( random.randint(-200 * gl.RESIZE_FACTOR, 200 * gl.RESIZE_FACTOR), -random.randint(50 * gl.RESIZE_FACTOR, 300 * gl.RESIZE_FACTOR)) self.rect = pygame.rect.Rect(10000, 10000, 0, 0)
def release_bomb(self): if self.has_bomb: released_up = time.time() time_elapsed = released_up - self.pressed_down_start target_y = self.screen_height - time_elapsed * 200 # 100 pixels for every second held self.has_bomb = False del self.powerups[Bomb.name] self.pressed_down_start = None self.bomb.sprite = Bomb(self.rect.centerx,self.screen_height,target_y)
def detect_collision(screen, ship, ship_bullets, enemys, enemys_bullets, bombs, setting, score): # 爆炸时播放音效 music = "images/bomb.wav" pygame.mixer.Sound(music) pygame.mixer.Sound(music).set_volume(0.1) # 检测到击中敌机 hit = pygame.sprite.groupcollide(enemys, ship_bullets, True, True) if hit: for enemy in hit: pygame.mixer.Sound(music).play() # 加分并检测是否刷新最高分 setting.score += setting.points score.render_score() if setting.score > setting.high_score: setting.high_score = setting.score score.render_high_score() # 播放爆炸动画 enemy_rect = enemy.rect bomb = Bomb(screen) bomb.set_pos(enemy_rect.x, enemy_rect.y) bombs.add(bomb) bomb.visible = True # 检测到我方飞机被击中就清空屏幕,减少一条命,判断游戏状态 if pygame.sprite.spritecollideany(ship, enemys) \ or pygame.sprite.spritecollideany(ship, enemys_bullets): enemys.empty() enemys_bullets.empty() pygame.mixer.Sound(music).play() bomb = Bomb(screen) bomb.set_pos(ship.rect.x, ship.rect.y) bombs.add(bomb) bomb.visible = True setting.live -= 1 if setting.live > 0: ship.put() else: setting.game_active = False
def get_bombs(data): bombs = [] for bombData in data['Bombs']: roundsUntilExplodes = int(bombData['RoundsUntilExplodes']) parsedLocation = get_location_from_string(bombData['Location']) location = Location(parsedLocation[0], parsedLocation[1]) explosionRadius = int(bombData['ExplosionRadius']) bomb = Bomb(roundsUntilExplodes, location, explosionRadius) bomb.print_debug() bombs.append(bomb) return bombs
def test_bomb_explode(self): temp_bomb = Bomb(3, 11, 11, game.grid, game.enemy_list[0]) game.bombs.append(temp_bomb) game.update_bombs(2980) self.assertEqual(1, len(game.bombs)) self.assertEqual(20, temp_bomb.time) game.update_bombs(50) self.assertEqual(0, len(game.bombs)) self.assertEqual(1, len(game.explosions))
def move(self, level): #Character read from imput function''' getch = _Getch() key = getch() return_value = 0 '''Movement accordind to keys''' if key is 'w': return_value = self.move_bomberman(-1, 0) elif key is 's': return_value = self.move_bomberman(+1, 0) elif key is 'a': return_value = self.move_bomberman(0, -1) elif key is 'd': return_value = self.move_bomberman(0, +1) elif key is 'q': return -2 '''If pressed q exit the game right there''' '''Function to handle the bomb and its timers''' if self.bombs == 1: # Decrease yhe bomb timer self.p_bomb.tick_tick(self.x, self.y, level) b.game[self.p_bomb.x][self.p_bomb.y] = 'B' # If time is over call the neighbour function which displays # explosion around the bomb if (self.p_bomb.time_bomb == 0): # Displays the board and calls the neighbour also # call the function so that it calls the explosion and checks # for different objects around the bomb temp = self.p_bomb.tick_tick(self.x, self.y, level) # Set the bombs to 0 and del the bomb object self.bombs = 0 del self.p_bomb if temp is -1: return -1 elif (key is 'b') and self.bombs == 0: '''Add new bomb if the key is b and bombs are 0''' self.p_bomb = Bomb(self.x, self.y, time.time()) b.game[self.p_bomb.x][self.p_bomb.y] = 'B' self.bombs = 1 # Returns return_value which stores the status of the moves return return_value
def put_bomb(self, id_char): if id_char in self.__chars: char = self.__chars[id_char] if char.put_bomb(): x, y = char.get_position() x = round(x / Server.wall_factor) * Server.wall_factor y = round(y / Server.wall_factor) * Server.wall_factor model_bomb = self.__model_bomb[char.get_id_bomb_model()] bomb = Bomb( char.get_orientation(), [x, y], model_bomb.get_id(), [53, 53], 255, id_char, model_bomb.get_s_power() + char.get_power_bomb_add(), model_bomb.get_s_timer()) self.__bombs[bomb.get_id()] = bomb
def move(self): b = self.board self.step += 1 i, ln = 0, len(self.chek_lst) while i < ln and ln: self.chek_lst[i] -= 1 if not self.chek_lst[i]: self.list_bomb[i].die() del self.list_bomb[i], self.chek_lst[i] ln = len(self.chek_lst) i += 1 if self.step == 240: bx, by = b.get_coords((b.get_cell((self.rect.x, self.rect.y)))) bomb = Bomb(self.group, bx, by) bomb.resize(30) self.list_bomb.append(bomb) self.chek_lst.append(randint(300, 390)) self.step = 0 if not self.y and not self.x: while True: self.y = randint(1, (len(self.board.wall_map) - 3)) self.x = randint(1, (len(self.board.wall_map[0]) - 3)) if not self.board.wall_map[self.y][self.x]: self.x, self.y = self.board.get_coords((self.x, self.y)) break ls = (self.rect.x, self.rect.y) if self.y == self.rect.y and self.rect.x == self.x: self.x = self.y = 0 elif self.y == self.rect.y: if self.x > self.rect.x: if 'black' == b.get_color(b.get_cell((ls[0] + 30, ls[1]))): self.rect = self.rect.move(1, 0) else: self.x = self.rect.x elif self.x < self.rect.x: if 'black' == b.get_color(b.get_cell((ls[0] - 1, ls[1]))): self.rect = self.rect.move(-1, 0) else: self.x = self.rect.x else: if self.y > self.rect.y: if 'black' == b.get_color(b.get_cell((ls[0], ls[1] + 30))): self.rect = self.rect.move(0, 1) else: self.y = self.rect.y elif self.y < self.rect.y: if 'black' == b.get_color(b.get_cell((ls[0], ls[1] - 1))): self.rect = self.rect.move(0, -1) else: self.y = self.rect.y
def startgame(BNE): """ this method starts the game """ nextstep = time.time() + 1 # value to make enemies move automatically flag = 0 # bomb is not there on the board B.score = 0 while True: inp = input_to() if inp == 'q': break if inp == 'w': BNE = B.moveup(BNE, 1) os.system('clear') # clears screen or refreshes printboard(BNE) if inp == 's': BNE = B.movedown(BNE, 1) os.system('clear') printboard(BNE) if inp == 'a': BNE = B.moveleft(BNE, 1) os.system('clear') printboard(BNE) if inp == 'd': BNE = B.moveright(BNE, 1) os.system('clear') printboard(BNE) if flag != 1: # bomb is not placed if inp == 'b': boom = Bomb(B.x_pos, B.y_pos, 1) if flag == 0: bomb_start = time.time() BNE = boom.placebomb(BNE) flag = 1 os.system('clear') printboard(BNE) if flag == 1: # bomb is there on board if time.time() - bomb_start > 3: # if it has been more than 3 sec BNE = boom.explode(BNE, EMB, B) # after placing bomb flag = 0 os.system('clear') printboard(BNE) time.sleep(0.3) # shows board with e's for 0.3 sec os.system('clear') printboard1() if time.time() >= nextstep: nextstep = nextstep + 1 for i in EMB.enemies: BNE = i.randommove(BNE, B) os.system('clear') printboard(BNE)
def put_bomb_and_get_bomb_index(self): index_of_not_active_bomb = self.get_index_of_not_active_bomb() if index_of_not_active_bomb is None: return None for b in self.BomberMan: if not b.backpack: self.Bombs[index_of_not_active_bomb] = Bomb(b) self.points -= 10 else: self.Bombs[index_of_not_active_bomb] = b.backpack.pop() self.Bombs[index_of_not_active_bomb].position = copy.deepcopy( b.position) self.Bombs[index_of_not_active_bomb].is_active = True self.bombs_power[index_of_not_active_bomb] = self.Bombs[ index_of_not_active_bomb].power return index_of_not_active_bomb
def create_bombs(setting, screen, bombs): """ 创建随机bombs """ #生成大于10个不重复的二维数组,如果数组长度小于10则再次生成直到数组长度大于10为止 num = 0 while num < 10: random_number_0 = [[random.randint(0, 8) for i in range(2)] for j in range(12)] #将列表转换为tuple,再通过set去重复数据 random_number = list(set([tuple(t) for t in random_number_0])) num = len(random_number) for value in range(10): bomb = Bomb(setting, screen) bomb_width = bomb.rect.width bomb.x = 1.5 + bomb_width * random_number[value][0] bomb.rect.y = (setting.screen_height - setting.number_rows * 30) + bomb.rect.height * random_number[value][1] bomb.rect.x = bomb.x bombs.add(bomb)
def get_key_press(self,walls,bomb_list,primed_bomb_queue, bomb_expl_queue): keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: self.move(-5,0,walls) if keys[pygame.K_RIGHT]: self.move(5, 0, walls) if keys[pygame.K_UP]: self.move(0, -5, walls) if keys[pygame.K_DOWN]: self.move(0,5,walls) if keys[pygame.K_SPACE] and self.can_place_bomb== True: bomb = Bomb(self.get_player_posx(), self.get_player_posy(), 15) bomb.place_bomb(bomb_list) bomb.prime_bomb(bomb_list, primed_bomb_queue, bomb_expl_queue) self.set_can_place_bomb(False) timer = Timer(.5, self.set_can_place_bomb, [True]) timer.start() print (self.can_place_bomb)
def __init__(self, file_name, count_of_active_bomb=2): r = Reader(file_name) self.__dict__ = r.objects self.Bombs = [] for b in self.BomberMan: for i in range(count_of_active_bomb): self.Bombs.append(Bomb(b)) self.monster_speed = r.monster_speed self.height = r.height self.width = r.width self.is_over = False self.points = 0 self.scoring = {'Monster': 100, 'Wall': 5} self.is_BM_lives_changed = False self.bombs_power = [] for bomb in self.Bombs: self.bombs_power.append(bomb.power) self.direc = {'r': 1, 'l': -1, 'u': -1, 'd': 1}