def __init__(self, init_state, source, space_subdivisions, init_hook=None): super().__init__(source=source) self.position = Position(self, space_subdivisions) self.position.set_x(init_state) self.line_rod = LineRod(self, space_subdivisions, init_state=init_hook) self.hook = Hook(self, self.line_rod, space_subdivisions) self.num_fishes_caught = 0
def __init__(self, boat, line_rod, space_subdivisions): super().__init__() self.boat = boat self.line_rod = line_rod self.position = Position(self, space_subdivisions) self.position.set_x(boat.position.x) self.position.set_y(line_rod.position.y)
def __init__(self, boat, space_subdivisions, init_state=None): super().__init__() self.boat = boat self.position = Position(self, space_subdivisions) self.position.set_x(boat.position.x) if init_state is None: self.position.set_y(space_subdivisions - 1) else: self.position.set_y(init_state)
class Boat(Image): has_fish = ObjectProperty(None, allownone=True) def __init__(self, init_state, source, space_subdivisions, init_hook=None): super().__init__(source=source) self.position = Position(self, space_subdivisions) self.position.set_x(init_state) self.line_rod = LineRod(self, space_subdivisions, init_state=init_hook) self.hook = Hook(self, self.line_rod, space_subdivisions) self.num_fishes_caught = 0 def on_state(self, obj, val): self.pos_hint = {"center_x": self.position.pos_x, "top": 0.98}
class Hook(Image): def __init__(self, boat, line_rod, space_subdivisions): super().__init__() self.boat = boat self.line_rod = line_rod self.position = Position(self, space_subdivisions) self.position.set_x(boat.position.x) self.position.set_y(line_rod.position.y) def on_state(self, *args, **kwargs): self.pos_hint = { "center_x": self.position.pos_x + 0.0035, "center_y": self.position.pos_y }
def __init__(self, init_state, type_fish, name, settings, observations_sequence): super().__init__() self.type_fish = type_fish self.name = name self.prev_direction = random.choice(range(8)) if self.prev_direction in [2, 4, 6]: self.orientation = -1 self.observation = None self.observations_sequence = observations_sequence self.updates_cnt = 0 self.source = 'fishing_game_core/images/fish' + str(type_fish) + '.png' self.settings = settings space_subdivisions = 20 self.position = Position(self, space_subdivisions) self.position.set_x(init_state[0]) self.position.set_y(init_state[1]) self.prev_move = None from fishing_game_core.shared import TYPE_TO_SCORE self.score = TYPE_TO_SCORE[type_fish] # was: self.score = type_fish + 1 if type_fish < 6 else -7 self.guessed = False
class LineRod(Widget): color = ListProperty([0, 0.5, 0, 1]) def __init__(self, boat, space_subdivisions, init_state=None): super().__init__() self.boat = boat self.position = Position(self, space_subdivisions) self.position.set_x(boat.position.x) if init_state is None: self.position.set_y(space_subdivisions - 1) else: self.position.set_y(init_state) def on_state(self, *args, **kwargs): self.size_hint = None, 1.075 - self.position.pos_y self.pos_hint = { "center_x": self.position.pos_x, "top": 1.1 }
class Fish(Image): orientation = NumericProperty(1.0) caught = ObjectProperty(None) def __init__(self, init_state, type_fish, name, settings, observations_sequence): super().__init__() self.type_fish = type_fish self.name = name self.prev_direction = random.choice(range(8)) if self.prev_direction in [2, 4, 6]: self.orientation = -1 self.observation = None self.observations_sequence = observations_sequence self.updates_cnt = 0 self.source = 'fishing_game_core/images/fish' + str(type_fish) + '.png' self.settings = settings space_subdivisions = 20 self.position = Position(self, space_subdivisions) self.position.set_x(init_state[0]) self.position.set_y(init_state[1]) self.prev_move = None from fishing_game_core.shared import TYPE_TO_SCORE self.score = TYPE_TO_SCORE[type_fish] # was: self.score = type_fish + 1 if type_fish < 6 else -7 self.guessed = False def next_movement_and_flip_horizontally(self): if self.caught is not None: return 0, 0 if self.observations_sequence is None: new_direction = self.model.sample( previous_state=self.prev_direction) else: new_direction = self.observations_sequence[self.updates_cnt] self.prev_move = new_direction self.observation = new_direction if new_direction in [3, 5, 7]: move_x = 1 self.orientation = move_x elif new_direction in [2, 4, 6]: move_x = -1 self.orientation = move_x else: move_x = 0 if new_direction in [0, 4, 5]: move_y = 1 elif new_direction in [1, 6, 7]: move_y = -1 else: move_y = 0 return move_x, move_y def attach_hook(self, rod): """ Enforce the center of the fish to be hooked up to the tip of the rod :return: """ self.pos_hint = {"center_x": rod.hook.center_x / self.parent.size[0], "center_y": rod.hook.pos[1] / self.parent.size[1]} def on_state(self, ins, val): self.pos_hint = {"center_x": self.position.pos_x, "center_y": self.position.pos_y} def increase_x_y(self, x, y): if self.caught is not None: self.attach_hook(self.caught) else: self.position.increase_x(x) self.position.increase_y(y)