def __init_level__(self, level): path = 'Levels/level_' + str(level) + '.txt' file = open(path, 'r') data = file.read().split('\n') y = 0 counter = 0 for string in data: x = 0 for char in string: if char in ENEMYS: counter += 1 if counter in [20, 15, 10, 5]: self.all_enemys.append(EnemyTank(char, True)) else: self.all_enemys.append(EnemyTank(char)) if char in TILES: type = TILE_TO_TYPE[char] obj = GameObject(type[0], QPoint( x, y), BLOCK_SIZE, BLOCK_SIZE, type[1], type[2]) self.objects.append(obj) x += BLOCK_SIZE y += BLOCK_SIZE file.close()
def test_moveable_obj_overlap_break(): tank = EnemyTank('5') brick = GameObject('BRICK', QPoint(0, 0), 32, 32, 1, True) scene.addItem(brick) scene.map.objects.append(brick) length = len(scene.items()) tank.processing_overlap(scene, brick) assert length > len(scene.items())
def run_game(): # Initialize pygame, settings, and screen object. pygame.init() tw_settings = Settings() screen = pygame.display.set_mode( (tw_settings.screen_width, tw_settings.screen_height)) pygame.display.set_caption("Alien Invasion") # Make a tank. tank = Tank(screen) enemy_tank = EnemyTank(screen) enemy_dedector = EnemyDedector(screen, enemy_tank) # Make a group to store bullets in. bullets = Group() enemy_bullets = Group() ########################################################### # Threads for each function. t1 = threading.Thread(target=thread_1_, args=(tw_settings, screen, tank, enemy_tank, enemy_dedector, bullets, enemy_bullets)) t2 = threading.Thread(target=thread_2_, args=(tw_settings, screen, tank, enemy_tank, enemy_dedector, bullets, enemy_bullets)) t3 = threading.Thread(target=thread_3_, args=(tw_settings, screen, tank, enemy_tank, enemy_dedector, bullets, enemy_bullets)) ########################################################### t1.start() t2.start() t3.start() """
def advanceToNextLevel(self): for bullet in self.bullet_list: self.setShapeAt(bullet.x, bullet.y, ElementType.NONE) self.bullet_list = [] self.player_2.active_bullet = None self.player_1.active_bullet = None self.current_level += 1 if (self.current_level > 8): #TODO level dart wader self.current_level = 1 self.loadLevel(self.current_level) self.random_values = [] self.random_values = sample(range(1, 32), self.num_of_enemies_per_level) for i in range(self.num_of_enemies_per_level): self.enemy_list.append(EnemyTank(self.random_values[i])) self.setShapeAt(self.enemy_list[i].x, self.enemy_list[i].y, ElementType.ENEMY) self.enemies_increaser += 1 self.num_of_all_enemies = 3 + self.enemies_increaser self.speed_up_signal.emit() self.sendBoard() self.updateLevel() # za statframe
def initGameBoard(self): self.num_of_all_enemies = 3 self.num_of_enemies_per_level = 4 self.current_level = 1 self.force_x = None self.force_y = None self.enemies_increaser = 0 self.chosen_enemy = None self.bullet_list = [] self.enemy_list = [] self.random_values = [] self.random_values = sample(range(1, 32), self.num_of_enemies_per_level) self.player_1 = Tank(PlayerType.PLAYER_1) self.player_1_starting_position = () self.player_2 = Tank(PlayerType.PLAYER_2) self.player_2_starting_position = () self.clearBoard() self.loadLevel(self.current_level) for i in range(self.num_of_enemies_per_level): self.enemy_list.append(EnemyTank(self.random_values[i])) self.setShapeAt(self.enemy_list[i].x, self.enemy_list[i].y, ElementType.ENEMY) self.sendBoard(True)
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 initMultiplayerOffline(self): self.random_values = [] self.random_values = sample(range(1, 32), self.num_of_enemies_per_level) self.board = [] self.bullet_dictionary = {} self.enemy_dictionary = {} for i in range(self.num_of_enemies_per_level): self.enemy_dictionary[EnemyTank( self.random_values[i])] = QLabel(self) self.player_1 = Tank(PlayerType.PLAYER_1) self.player_1_label = QLabel(self) self.player_1_starting_position = () self.player_2 = Tank(PlayerType.PLAYER_2) self.player_2_label = QLabel(self) self.player_2_starting_position = () self.clearBoard() self.setFocusPolicy(Qt.StrongFocus) self.setInitialMap() # region THREADS self.move_player_1_thread = MovePlayerThread(self.commands_1, self.player_1, self) self.move_player_1_thread.player_moved_signal.connect(self.playerMoved) self.move_player_1_thread.bullet_fired_signal.connect(self.bulletFired) self.move_player_1_thread.bullet_impact_signal.connect( self.bulletMoved) self.move_player_2_thread = MovePlayerThread(self.commands_2, self.player_2, self) self.move_player_2_thread.player_moved_signal.connect(self.playerMoved) self.move_player_2_thread.bullet_fired_signal.connect(self.bulletFired) self.move_player_2_thread.bullet_impact_signal.connect( self.bulletMoved) self.move_enemy_thread = MoveEnemyThread(self) self.move_enemy_thread.bullet_fired_signal.connect(self.bulletFired) self.move_enemy_thread.bullet_impact_signal.connect(self.bulletMoved) self.move_enemy_thread.enemy_move_signal.connect(self.enemyCallback) self.move_enemy_thread.dead_player_signal.connect( self.removeDeadPlayer) self.move_bullets_thread = MoveBulletsThread(self) self.move_bullets_thread.bullets_move_signal.connect(self.bulletMoved) self.move_bullets_thread.dead_player_signal.connect( self.removeDeadPlayer) self.ex_pipe, self.in_pipe = Pipe() self.deux_ex_machina_process = DeuxExMachina( pipe=self.ex_pipe, boardWidth=self.BoardWidth, boardHeight=self.BoardHeight) self.deux_ex_machina_thread = DeuxExMachinaThread(self)
def run(self): while True: self.clock.tick(15) self.handleEvent() self.screen.fill(self.bgColor) if not self.stop: self.myTank.update(self.screen) if len(self.enemys.sprites()) < 4: enemy = EnemyTank(80 * randint(1,4), 30) self.enemys.add(enemy) self.enemys.update(self.screen) for enemy in self.enemys: enemy.random_move() self.enemyBullets.update(self.screen) self.enemyBullets.update(self.screen) self.bullets.update(self.screen) # self.bomb.update(self.screen,30,30) pygame.sprite.groupcollide(self.bullets, self.enemyBullets, True, True) collisions = pygame.sprite.groupcollide(self.bullets, self.enemys, True, True) for bullet in collisions.keys(): bomb_rect = bullet.rect self.bomb.update(self.screen,bomb_rect.x,bomb_rect.y) if pygame.sprite.spritecollideany(self.myTank,self.enemyBullets): self.bomb.update(self.screen, self.myTank.rect.x, self.myTank.rect.y) self.myTank.kill() self.enemys.empty() self.enemyBullets.empty() self.bullets.empty() self.stop = True if pygame.sprite.spritecollideany(self.myTank, self.enemys): self.bomb.update(self.screen, self.myTank.rect.x, self.myTank.rect.y) self.myTank.kill() self.enemys.empty() self.enemyBullets.empty() self.bullets.empty() self.stop = True else: self.screen.blit(self.fontImage,(self.screen.get_rect().centerx -self.fontImage.get_rect().centerx,self.screen.get_rect().centery -self.fontImage.get_rect().centery)) pygame.display.update()
def run_game(): # Initialize pygame, settings, and screen object. pygame.init() tw_settings = Settings() screen = pygame.display.set_mode((tw_settings.screen_width, tw_settings.screen_height)) pygame.display.set_caption("Alien Invasion") # Make a tank. tank = Tank(screen) enemy_tank = EnemyTank(screen) enemy_dedector = EnemyDedector(screen, enemy_tank) # Make a group to store bullets in. bullets = Group() enemy_bullets = Group() # ScoreBoard. score = Scoreboard(screen, enemy_tank, tank) player1 = PlayerData() while True: """ if enemy_tank.enemy_health == 0 or tank.player_health == 0: if enemy_tank.enemy_health == 0: return 'You WOOOOONNNN' return 'Enemy Wooonnnn' """ gf.check_events(tw_settings, screen, tank, enemy_tank, bullets, enemy_bullets, player1) tank.update() enemy_tank.update() enemy_tank.update_auto_movement(tank) gf.update_bullets_for_collision(enemy_tank, bullets, enemy_bullets, score, tank) gf.update_enemy_bullets_for_collision(enemy_tank, bullets, enemy_bullets, tank, score) # Dedecting of player tank. gf.check_dedector_collision_with_player_tank(enemy_dedector, tank, enemy_tank, tw_settings, enemy_bullets) gf.check_collision_between_two_tanks(enemy_tank, tank) bullets.update() enemy_bullets.update() gf.update_screen(tw_settings, screen, tank, enemy_tank, bullets, enemy_bullets, enemy_dedector) score.show_score() # Saving player info. player1.save_tank_coordinates(tank)
def receiveInitEnemyFromHost(self): #with self.socket: #conn, addr = self.socket.accept() # with conn: text = "" while True: bin = self.socket.recv(1024) if not bin or len(bin) < 1024: break id, data = pickle.loads(bin) if id is "INIT_ENEMY": for i in range(data): self.enemy_dictionary[EnemyTank(data[i])] = QLabel(self) text = "OK" self.socket.sendall(text.encode("utf8"))
def addEnemy(self): rand_x = 0 while (True): rand_x = randint(0, self.BoardWidth) if self.getShapeType(rand_x, 0) == ElementType.NONE: break current_enemy = EnemyTank(rand_x) self.enemy_dictionary[current_enemy] = QLabel(self) enemy_label = self.enemy_dictionary[current_enemy] pix_map = current_enemy.pix_map.scaled(self.getSquareWidth(), self.getSquareHeight()) enemy_label.setPixmap(pix_map) self.setGameBoardLabelGeometry(enemy_label, current_enemy.x, current_enemy.y) self.setShapeAt(current_enemy.x, current_enemy.y, ElementType.ENEMY) enemy_label.show()
def advanceToNextLevel(self): if len(self.bullet_dictionary) > 0: for bullet in self.bullet_dictionary: self.bullet_dictionary[bullet].hide() self.bullet_dictionary.clear() if len(self.enemy_dictionary) > 0: for enemy in self.enemy_dictionary: self.enemy_dictionary[enemy].hide() self.enemy_dictionary.clear() for i in range(self.num_of_enemies_per_level): self.enemy_dictionary[EnemyTank( self.random_values[i])] = QLabel(self) for enemy in self.enemy_dictionary: pixmap3 = enemy.pix_map.scaled(self.getSquareWidth(), self.getSquareHeight()) self.enemy_dictionary[enemy].setPixmap(pixmap3) self.setGameBoardLabelGeometry(self.enemy_dictionary[enemy], enemy.x, enemy.y) self.setShapeAt(enemy.x, enemy.y, ElementType.ENEMY) self.enemy_dictionary[enemy].show() if self.current_level >= 9: #TODO self.current_level = 0 self.enemies_increaser += 1 self.force_y = None self.force_x = None self.num_of_all_enemies = 3 + self.enemies_increaser self.current_level += 1 self.setPlayersForNextLevel() self.loadLevel(self.current_level) self.change_enemies_left_signal.emit(self.num_of_all_enemies) self.change_level_signal.emit() self.speed_up_signal.emit() #TODO: SIGNAL to main_window: advance #TODO BUG: sleep duzi od 0.5, deux_ex_machina time.sleep(0.2)
def bulletImpactOnFire(self, new_x, new_y, bullet): if not (0 <= new_x <= self.parent_widget.BoardWidth - 1 and 0 <= new_y <= self.parent_widget.BoardHeight - 1): bullet.bullet_owner.active_bullet = None return next_shape = self.parent_widget.getShapeType(new_x, new_y) if next_shape == ElementType.WALL: self.parent_widget.setShapeAt(new_x, new_y, ElementType.NONE) elif next_shape == ElementType.BULLET or (ElementType.BULLET_UP <= next_shape <= ElementType.BULLET_LEFT): other_bullet = self.parent_widget.findBulletAt(new_x, new_y) if other_bullet is not None: self.parent_widget.setShapeAt(other_bullet.x, other_bullet.y, ElementType.NONE) self.parent_widget.bullet_list.remove(other_bullet) other_bullet.bullet_owner.active_bullet = None else: print("Move enemy thread: bulletImpactOnFire(): other_bullet is None") elif (next_shape == ElementType.ENEMY or ElementType.ENEMY_UP <= next_shape <= ElementType.ENEMY_LEFT) and bullet.type == BulletType.FRIEND: enemy_to_be_removed = self.parent_widget.findEnemyAt(new_x, new_y) if enemy_to_be_removed is not None: self.parent_widget.setShapeAt(enemy_to_be_removed.x, enemy_to_be_removed.y, ElementType.NONE) self.parent_widget.enemy_list.remove(enemy_to_be_removed) self.parent_widget.num_of_all_enemies -= 1 self.send_status_update(num_of_enemies=self.parent_widget.num_of_all_enemies) if self.parent_widget.num_of_all_enemies > 0: while (True): rand_x = randint(0, self.parent_widget.BoardWidth) if self.parent_widget.getShapeType(rand_x, 0) == ElementType.NONE: break self.parent_widget.enemy_list.append(EnemyTank(rand_x)) self.parent_widget.setShapeAt(rand_x, 0, ElementType.ENEMY_DOWN) elif self.parent_widget.num_of_all_enemies == -3: self.parent_widget.advanceToNextLevel() bullet.bullet_owner.active_bullet = None
def run_game(): # Initialize pygame, settings, and screen object. pygame.init() tw_settings = Settings() screen = pygame.display.set_mode( (tw_settings.screen_width, tw_settings.screen_height)) pygame.display.set_caption("Alien Invasion") # Make a tank. tank = Tank(screen) enemy_tank = EnemyTank(screen) enemy_dedector = EnemyDedector(screen, enemy_tank) # Make a group to store bullets in. bullets = Group() enemy_bullets = Group() ########################################################### # Threads for each function. t1 = threading.Thread(target=tank.update, args=()) t2 = threading.Thread(target=enemy_tank.update, args=()) ########################################################### while True: print('________active threads_____________ ', str(threading.activeCount())) gf.check_events(tw_settings, screen, tank, enemy_tank, bullets, enemy_bullets) tank.update() enemy_tank.update() enemy_tank.update_auto_movement(tank) gf.update_bullets_for_collision(enemy_tank, bullets, enemy_bullets) # Dedecting of player tank. gf.check_dedector_collision_with_player_tank(enemy_dedector, tank, enemy_tank, tw_settings, enemy_bullets) bullets.update() enemy_bullets.update() gf.update_screen(tw_settings, screen, tank, enemy_tank, bullets, enemy_bullets, enemy_dedector)
def test_player_destroy_enemys(): enemy = EnemyTank('4') scene.enemys.append(enemy) player.destroy_enemys(scene) assert enemy.health == 0
def moveEnemy(self): enemies_to_be_removed = [] for enemy in self.parent_widget.enemy_list: new_x = enemy.x new_y = enemy.y if enemy.direction == Orientation.UP: new_y -= 1 new_orientation = Orientation.RIGHT elif enemy.direction == Orientation.RIGHT: new_x += 1 new_orientation = Orientation.DOWN elif enemy.direction == Orientation.DOWN: new_y += 1 new_orientation = Orientation.LEFT elif enemy.direction == Orientation.LEFT: new_x -= 1 new_orientation = Orientation.UP if Helper.isCollision(self.parent_widget, new_x, new_y, ElementType.ENEMY): is_bullet_collision = False board_width = self.parent_widget.BoardWidth board_height = self.parent_widget.BoardHeight if 0 <= new_x <= board_width - 1 and 0 <= new_y <= board_height - 1: next_shape = self.parent_widget.getShapeType(new_x, new_y) try: type = self.parent_widget.findBulletAt(new_x, new_y).type except: type = BulletType.ENEMY if (next_shape == ElementType.BULLET or ElementType.BULLET_UP <= next_shape <= ElementType.BULLET_LEFT ) and type == BulletType.FRIEND: is_bullet_collision = True self.parent_widget.setShapeAt(enemy.x, enemy.y, ElementType.NONE) enemies_to_be_removed.append(enemy) bullet_to_die = self.parent_widget.findBulletAt( new_x, new_y) if bullet_to_die is not None: self.parent_widget.setShapeAt( bullet_to_die.x, bullet_to_die.y, ElementType.NONE) self.parent_widget.bullet_list.remove( bullet_to_die) bullet_to_die.bullet_owner.active_bullet = None if not is_bullet_collision: enemy.direction = new_orientation self.parent_widget.setShapeAt( enemy.x, enemy.y, Helper.enumFromOrientationEnemy(new_orientation)) else: self.parent_widget.setShapeAt(enemy.x, enemy.y, ElementType.NONE) enemy.x = new_x enemy.y = new_y self.parent_widget.setShapeAt( enemy.x, enemy.y, Helper.enumFromOrientationEnemy(enemy.direction)) for elemnt in enemies_to_be_removed: self.parent_widget.enemy_list.remove(elemnt) self.parent_widget.num_of_all_enemies -= 1 self.send_status_update( enemies_left=self.parent_widget.num_of_all_enemies) if self.parent_widget.num_of_all_enemies > 0: #dodavanje novog enemyja while (True): rand_x = randint(0, self.parent_widget.BoardWidth) if self.parent_widget.getShapeType(rand_x, 0) == ElementType.NONE: break self.parent_widget.enemy_list.append(EnemyTank(rand_x)) self.parent_widget.setShapeAt(rand_x, 0, ElementType.ENEMY_DOWN) elif self.parent_widget.num_of_all_enemies == -3: self.parent_widget.advanceToNextLevel() self.chosen_enemy = self.chooseRandomEnemy() if self.chosen_enemy is not None: if self.chosen_enemy.fireBullet(): if Helper.isCollision(self.parent_widget, self.chosen_enemy.active_bullet.x, self.chosen_enemy.active_bullet.y, ElementType.BULLET): self.bulletImpactOnFire(self.chosen_enemy.active_bullet.x, self.chosen_enemy.active_bullet.y, self.chosen_enemy.active_bullet) else: self.bulletFired() self.sendUpdatedEnemies()
def bulletImpact(self, new_x, new_y, bullet, bullets_to_be_removed): if not (0 <= new_x <= self.parent_widget.BoardWidth - 1 and 0 <= new_y <= self.parent_widget.BoardHeight - 1): self.parent_widget.setShapeAt(bullet.x, bullet.y, ElementType.NONE) bullets_to_be_removed.append(bullet) return next_shape = self.parent_widget.getShapeType(new_x, new_y) levelChanged = False if next_shape == ElementType.WALL: self.parent_widget.setShapeAt(new_x, new_y, ElementType.NONE) elif next_shape == ElementType.BULLET or (ElementType.BULLET_UP <= next_shape <= ElementType.BULLET_LEFT): other_bullet = self.findBulletAt(new_x, new_y) self.parent_widget.setShapeAt(new_x, new_y, ElementType.NONE) if other_bullet is not None: # self.parent_widget.setShapeAt(other_bullet.x, other_bullet.y, ElementType.NONE) #mozda setShape na new_x, new_y? bullets_to_be_removed.append(other_bullet) print("find other bullet!") else: print("bulletImpact(): other_bullet is None") elif (next_shape == ElementType.PLAYER1 or (ElementType.PLAYER1_UP <= next_shape <= ElementType.PLAYER1_LEFT) or next_shape == ElementType.PLAYER2 or (ElementType.PLAYER2_UP <= next_shape <= ElementType.PLAYER2_LEFT)) and bullet.type == BulletType.ENEMY: if next_shape == ElementType.PLAYER1 or (ElementType.PLAYER1_UP <= next_shape <= ElementType.PLAYER1_LEFT): gb_player = self.parent_widget.player_1 starting_position = self.parent_widget.player_1_starting_position elif next_shape == ElementType.PLAYER2 or (ElementType.PLAYER2_UP <= next_shape <= ElementType.PLAYER2_LEFT): gb_player = self.parent_widget.player_2 starting_position = self.parent_widget.player_2_starting_position gb_player.lives -= 1 if gb_player.player_type == PlayerType.PLAYER_1: self.send_status_update(player_1_life=gb_player.lives) #slanje zahteva na klijent za osvezavanje stat framea elif gb_player.player_type == PlayerType.PLAYER_2: self.send_status_update(player_2_life=gb_player.lives) if gb_player.lives > 0: self.parent_widget.setShapeAt(gb_player.x, gb_player.y, ElementType.NONE) gb_player.x = starting_position[0] gb_player.y = starting_position[1] gb_player.orientation = Orientation.UP self.parent_widget.setShapeAt(gb_player.x, gb_player.y, Helper.enumFromOrientationPlayer(gb_player.player_type, Orientation.UP)) else: self.parent_widget.gameOver() elif (next_shape == ElementType.ENEMY or (ElementType.ENEMY_UP <= next_shape <= ElementType.ENEMY_LEFT)) and bullet.type == BulletType.FRIEND: self.parent_widget.setShapeAt(new_x, new_y, ElementType.NONE) for enemy in self.parent_widget.enemy_list: if new_x == enemy.x and new_y == enemy.y: self.parent_widget.enemy_list.remove(enemy) self.parent_widget.num_of_all_enemies -= 1 self.send_status_update(enemies_left=self.parent_widget.num_of_all_enemies) if self.parent_widget.num_of_all_enemies > 0: while (True): rand_x = randint(0, self.parent_widget.BoardWidth) if self.parent_widget.getShapeType(rand_x, 0) == ElementType.NONE: break self.parent_widget.enemy_list.append(EnemyTank(rand_x)) self.parent_widget.setShapeAt(rand_x, 0, ElementType.ENEMY_DOWN) elif self.parent_widget.num_of_all_enemies == -3: self.parent_widget.advanceToNextLevel() levelChanged = True break elif next_shape == ElementType.BASE and bullet.type == BulletType.ENEMY: self.parent_widget.gameOver() if not levelChanged: bullets_to_be_removed.append(bullet) self.parent_widget.setShapeAt(bullet.x, bullet.y, ElementType.NONE)