def teste_adicionar_obstaculo(self): fase = Phase() self.assertListEqual([], fase._obstacles) obstaculo = Obstacle() fase.add_obstacles(obstaculo) self.assertListEqual([obstaculo], fase._obstacles) obstaculo1, obstaculo2 = Obstacle(), Obstacle() fase.add_obstacles(obstaculo1, obstaculo2) self.assertListEqual([obstaculo, obstaculo1, obstaculo2], fase._obstacles)
def test_add_obstacles(self): phase = Phase() self.assertListEqual([], phase._obstacles) obstacle = Obstacle() phase.add_obstacles(obstacle) self.assertListEqual([obstacle], phase._obstacles) obstacle1, obstacle2 = Obstacle(), Obstacle() phase.add_obstacles(obstacle1, obstacle2) self.assertListEqual([obstacle, obstacle1, obstacle2], phase._obstacles)
def teste_status(self): fase = Phase() porcos = [Pig(1, 1) for i in range(2)] passaros = [YellowBird(1, 1) for i in range(2)] fase.add_pigs(*porcos) fase.add_birds(*passaros) self.assertEqual(ON_GOING, fase.status()) for passaro, porco in zip(passaros, porcos): passaro.clash(porco, 3) self.assertEqual(VICTORY, fase.status(), 'Sem porcos ativos o jogo deveria terminar com vitória') fase.add_obstacles(Obstacle()) self.assertEqual(VICTORY, fase.status(), 'Obstáculo não interfere para definir vitória') porco = Pig() fase.add_pigs(porco) self.assertEqual(DEFEAT, fase.status(), 'Com Pig ativo e sem pássaro para lançar, o jogo deveria acabar em derrota') fase.add_birds(YellowBird()) self.assertEqual(ON_GOING, fase.status(), 'Com Pig ativo e com pássaro para lançar, o jogo não deveria acabar') porco.clash(porco, 3) self.assertEqual(VICTORY, fase.status(), 'Sem porco ativo, o jogo deveria acabar com vitória')
def test_status(self): phase = Phase() pigs = [Pig(1, 1) for i in range(2)] birds = [YellowBird(1, 1) for i in range(2)] phase.add_pigs(*pigs) phase.add_birds(*birds) self.assertEqual(ON_GOING, phase.status()) for bird, pig in zip(birds, pigs): bird.clash(pig, 3) self.assertEqual(VICTORY, phase.status(), 'Without active pigs game should end with victory') phase.add_obstacles(Obstacle()) self.assertEqual(VICTORY, phase.status(), 'Obstacle must not interfere on game result') pig = Pig() phase.add_pigs(pig) self.assertEqual( DEFEAT, phase.status(), 'With Active Pig and with no Active bird, game should end with defeat' ) phase.add_birds(YellowBird()) self.assertEqual(ON_GOING, phase.status(), 'With active pig and birds, game should not end') pig.clash(pig, 3) self.assertEqual(VICTORY, phase.status(), 'Without active pigs game should end with victory')
def test_game_over_with_pigs_and_birds(self): phase = Phase() pigs = [Pig(1, 1) for i in range(2)] # creating 2 pigs birds = [YellowBird(1, 1) for i in range(2)] # criating 2 birds phase.add_pigs(*pigs) phase.add_birds(*birds) self.assertEqual(ON_GOING, phase.status()) # clashing bird against pig on time 3 for bird, pig in zip(birds, pigs): bird.clash(pig, 3) self.assertEqual(VICTORY, phase.status()) phase.add_obstacles(Obstacle()) self.assertEqual(VICTORY, phase.status(), 'Obstacle must not interfere on game result') phase.add_pigs(Pig()) self.assertEqual( DEFEAT, phase.status(), 'With no active birds and one Pig active, player should lose') phase.add_birds(YellowBird()) self.assertEqual( ON_GOING, phase.status(), 'With one pig and bird both active, game should still going on')
def main(): global fase, passaros, porcos, obstaculos fase = Phase(clash_interval=32) passaros = [RedBird(30, 30), YellowBird(30, 30), YellowBird(30, 30)] porcos = [Pig(750, 1), Pig(700, 1)] obstaculos = [Obstacle(310, 100)] fase.add_obstacles(*obstaculos) fase.add_birds(*passaros) fase.add_pigs(*porcos) rodar_fase(fase)
def criar_fase_exemplo(multiplicador=1): fase_exemplo = Phase(1 if multiplicador == 1 else 32) passaros = [RedBird(3 * multiplicador, 3 * multiplicador), YellowBird(3 * multiplicador, 3 * multiplicador), YellowBird(3 * multiplicador, 3 * multiplicador)] porcos = [Pig(78 * multiplicador, multiplicador), Pig(70 * multiplicador, multiplicador)] obstaculos = [Obstacle(31 * multiplicador, 10 * multiplicador)] fase_exemplo.add_birds(*passaros) fase_exemplo.add_pigs(*porcos) fase_exemplo.add_obstacles(*obstaculos) return fase_exemplo
def create_phase(multplier=1): example_phase = Phase(1 if multplier == 1 else 32) birds = [ RedBird(3 * multplier, 3 * multplier), YellowBird(3 * multplier, 3 * multplier), YellowBird(3 * multplier, 3 * multplier) ] pigs = [Pig(78 * multplier, multplier), Pig(70 * multplier, multplier)] obstacles = [Obstacle(31 * multplier, 10 * multplier)] example_phase.add_birds(*birds) example_phase.add_pigs(*pigs) example_phase.add_obstacles(*obstacles) return example_phase
def teste_acabou_com_porcos_e_passaros(self): fase = Phase() porcos = [Pig(1, 1) for i in range(2)] # criando 2 porcos passaros = [YellowBird(1, 1) for i in range(2)] # criando 2 pássaros fase.add_pigs(*porcos) fase.add_birds(*passaros) self.assertEqual(ON_GOING, fase.status()) # colidindo cada passaro com um porco no tempo 3 for passaro, porco in zip(passaros, porcos): passaro.clash(porco, 3) self.assertEqual(VICTORY, fase.status()) fase.add_obstacles(Obstacle()) self.assertEqual(VICTORY, fase.status(), 'Obstáculo não interfere no fim do jogo') fase.add_pigs(Pig()) self.assertEqual(DEFEAT, fase.status(), 'Com Pig ativo e sem pássaro para lançar, o jogo deveria acabar') fase.add_birds(YellowBird()) self.assertEqual(ON_GOING, fase.status(), 'Com Pig ativo e com pássaro para lançar, o jogo não deveria acabar')
def teste_status(self): obstacle = Obstacle() self.assertEqual('O', obstacle.character()) actor_on_same_position = Actor() obstacle.clash(actor_on_same_position) self.assertEqual(' ', obstacle.character())
import sys project_dir = path.dirname(__file__) project_dir = path.join('..') sys.path.append(project_dir) from actors import YellowBird, RedBird, Obstacle, Pig from phase import Phase from graphics_tk import run_phase if __name__ == '__main__': fase = Phase(intervalo_de_colisao=32) # Adicionar Pássaros Vermelhos for i in range(5): fase.add_birds(RedBird(30, 30)) # Adicionar Pássaros Amarelos for i in range(30): fase.add_birds(YellowBird(30, 30)) # Obstaculos for i in range(30, 480, 32): fase.add_obstacles(Obstacle(300, i)) # Porcos for i in range(30, 300, 32): fase.add_pigs(Pig(600, i)) run_phase(fase)
def teste_status(self): obstaculo = Obstacle() self.assertEqual('O', obstaculo.character()) outro_ator_na_mesma_posicao = Actor() obstaculo.clash(outro_ator_na_mesma_posicao) self.assertEqual(' ', obstaculo.character())
from phase import Phase from graphics_tk import run_phase from random import randint if __name__ == '__main__': fase = Phase(intervalo_de_colisao=32) # Adicionar Pássaros Amarelos for i in range(80): fase.add_birds(YellowBird(30, 30)) # Obstaculos theta = 270 h = 12 k = 7 step = 32 r = 50 while theta < 480: x = 600 + (h + r * math.cos(theta)) y = (k + r * math.sin(theta)) fase.add_obstacles(Obstacle(x, y)) theta += 32 # Porcos for i in range(30, 300, 32): x = randint(590, 631) y = randint(0, 21) fase.add_pigs(Pig(x, y)) run_phase(fase)
# -*- coding: utf-8 -*- from actors import RedBird, YellowBird, Pig, Obstacle from phase import Phase import placa_grafica fase_exemplo = Phase() passaros = [RedBird(3, 3), YellowBird(3, 3), YellowBird(3, 3)] porcos = [Pig(78, 1), Pig(70, 1)] obstaculos = [Obstacle(31, 10)] fase_exemplo.add_birds(*passaros) fase_exemplo.add_pigs(*porcos) fase_exemplo.add_obstacles(*obstaculos) # Solução para ganhar # fase_exemplo.lancar(45, 1) # fase_exemplo.lancar(63, 3) # fase_exemplo.lancar(23, 4) if __name__ == '__main__': placa_grafica.animar(fase_exemplo)
def __init__(self, height, width, robot_info, drone_info, num_obstacles, drone_latency): super(PMGridEnv, self).__init__() # Define action and observation space self.action_space = spaces.Discrete(N_DISCRETE_ACTIONS) # using image as input (can be channel-first or channel-last): self.observation_space = spaces.Box(low=0, high=255, shape=(height, width, 3), dtype=np.uint8) # To convert integer actions into string self.action_list = ['up', 'down', 'left', 'right'] ## Canvas is the grid we are going to use self.canvas = Canvas(height, width) self.latency_factor = drone_latency ## Create the robots self.playerList = [] for i in range(len(robot_info) // 3): x_loc, y_loc, size = robot_info[3 * i:3 * (i + 1)] self.playerList.append( Player(pos=[x_loc, y_loc], color='g', size=size)) ### Add drone to the environememnt self.droneList = [] for i in range(len(drone_info) // 3): x_loc, y_loc, size = drone_info[3 * i:3 * (i + 1)] self.droneList.append( Drone(pos=[x_loc, y_loc], color='b', size=size)) ## Create the obstacle at random locations self.n_obj = num_obstacles # number of objects self.obstacleList = [] r_coords = np.random.randint(0, self.canvas.height, (self.n_obj)) # random rows c_coords = np.random.randint(0, self.canvas.width, (self.n_obj)) # random columns # Width and height would be chosen from 10,15,20,25,30 randomly for i in range(len(r_coords)): self.obstacleList.append( Obstacle(pos=[r_coords[i], c_coords[i]], size=[ np.random.choice([10, 15, 20, 25, 30]), np.random.choice([10, 15, 20, 25, 30]) ])) ### Environement image as a numpy array self.env_img = np.zeros(self.canvas.grid.shape + (3, ), dtype=np.uint8) ### Coverage information self.coverage = np.zeros(self.canvas.grid.shape, dtype=np.uint8) ### Obstacle map information self.obstacle_map = np.zeros(self.canvas.grid.shape, dtype=np.uint8) #### Drone Map self.drone_map = np.zeros(self.canvas.grid.shape + (3, ), dtype=np.uint8) #### Saving initial state for resets self.inital_state = [ self.playerList.copy(), self.droneList.copy(), self.obstacleList.copy() ] #### Initializing locaal info for each robot for player in self.playerList: player.info = self.env_img.copy() ### Update entites in screen self.update_all() self.process_screen()