def fire(self): if not self.primary_time() or self.battery <= self.pEnergy: return # Drain battery self.battery_cost(self.pEnergy) # Create body and shape bodydef = Body() bodydef.ccd = True bodydef.angle = self.body.angle bodydef.position = self.body.get_world_point(Vec2(0, -5)) shell = self.melee.world.append_body(bodydef) angle = vforangle(self.body.angle) velocity = rotate(angle, (0.0, -100.0)) vb = self.body.linear_velocity shell.linear_velocity = Vec2(velocity[0]+vb.x, velocity[1]+vb.y) polydef = Polygon() verts = [Vec2(0.5, 0.8), Vec2(-0.5, 0.8), Vec2(-0.5, -0.8), Vec2(0.5, -0.8)] polydef.vertices = verts polydef.density = 5 shell.append_shape(polydef) shell.set_mass_from_shapes() # Create projectile projectile = PrimaryWeapon(self, self.melee, shell) projectile.group = self.group projectile.lifetime = 2.5 projectile.damage = 10 projectile.health = 5 projectile.shapes = verts
def choose_move(game, piece): rotations = [0, 1, 2, 3] search_tree = [] for y_coordinate, row in enumerate(game.board): for x_coordinate, item in enumerate(row): for rotation in rotations: rotated_piece = engine.rotate(piece, rotation) candidate_move = x_coordinate, y_coordinate, rotated_piece if engine.move_is_legal(game, candidate_move): search_tree.append(candidate_move) if len(search_tree) == 0: return None, None values = [] for move in search_tree: x, y, piece = move game_copy = deepcopy(game) game_copy.apply_move(piece, (x, y)) value = evaluator.evaluate(game_copy)[0] values.append(value) max_value = max(values) if len(values) > 0: if random.random() > 0.95: log.debug("Exploring a random option") max_value = random.choice(values) move_valuations = list( filter(lambda move_value: move_value[1] == max_value, list(zip(search_tree, values)))) return random.choice(move_valuations)
def test_attenuate_fitness(self): game = Game() piece = engine.rotate(tetrominos.I, 1) game.apply_move(piece, (1, 17)) chapter = Chapter(game, 2, 2) fitness = chapter.attenuate_fitness(204) self.assertEqual(51, fitness)
def test_calculate_line_height_to_be_four_when_theres_a_standing_I_on_the_bottom( self): game = Game() piece = engine.rotate(tetrominos.I, 1) game.apply_move(piece, (1, 17)) chapter = Chapter(game, 4, 2) height = chapter.calculate_height() self.assertEqual(4, height)
def debug_draw(self): pos = self.body.position angle = vforangle(self.body.angle) for s in self.body.shapes: verts = [] if isinstance(s, BoundPolygon): # Draw polygon for v in s.vertices: p = rotate(angle, (v.x, v.y)) px = pos.x + p[0] py = pos.y + p[1] verts.append((px, py)) draw_solid_polygon(verts, self.fill, self.outline) else: # Draw circle v = s.local_position p = rotate(angle, (v.x, v.y)) c1x = pos.x + p[0] c1y = pos.y + p[1] draw_solid_circle((c1x, c1y), s.radius, self.fill, self.outline) # Draw center of mass #red = 255, 0, 0 #draw_circle((pos.x, pos.y), 0.5, red)
def has_path_recursive(game_board, piece, position, target, last_move, attempted_moves): move = get_move(piece, position) if move in attempted_moves: return False, attempted_moves attempted_moves.append(move) target_x, target_y, target_piece = target if piece == target_piece and position == (target_x, target_y): return True, attempted_moves has_found_path = False for move in valid_moves: if not move == get_opposite(last_move): new_position = deepcopy(position) new_piece = deepcopy(piece) if move == Move.LEFT: new_position = move_left(position) if move == Move.RIGHT: new_position = move_right(position) if move == Move.DOWN: new_position = move_down(position) if move == Move.ROTATE_COUNTERCLOCKWISE: new_piece = engine.rotate(piece, 3) if move == Move.ROTATE_CLOCKWISE: new_piece = engine.rotate(piece, 1) if not engine.piece_has_collided(game_board, new_piece, new_position): has_found_path, attempted_moves = has_path_recursive( game_board, new_piece, new_position, target, move, attempted_moves) if has_found_path: return True, attempted_moves return has_found_path, attempted_moves
def forward(self): a = vforangle(self.body.angle) return rotate(self.engine_force, a)