class EffectDrawer(EntityDrawer): __slots__ = "anim_surface", "tracker", "effect_type" DEBUG_HITBOXES = False EFFECT_ANIMS = { EffectType.SMOKE: (("smoke", 6, 1), ), EffectType.SMALL_GROUND_DUST: (("small_ground_dust", 8, 1), ), EffectType.BIG_GROUND_DUST: (("big_ground_dust", 8, 1), ), EffectType.SLEEPING: (("sleeping_start", 3, 1), ("sleeping_idle", 3, -1)), } def __init__(self, entity: Effect, view: 'InGameView'): super().__init__(entity, view, (InGameView.EFFECT_SIZE, InGameView.EFFECT_SIZE)) self.anim_surface = view.get_effect_anim_surface() self.tracker = AnimTracker() self.effect_type = entity.get_effect_type() if self.effect_type in self.EFFECT_ANIMS: self.tracker.set_anim(*self.EFFECT_ANIMS[self.effect_type]) def draw(self, surface: Surface): self.anim_surface.blit_on(surface, self._get_draw_pos(), self.tracker)
def __init__(self): super().__init__() self._winner_button: Optional[ViewButton] = None self._play_again_button: Optional[ViewButton] = None self._quit_button: Optional[ViewButton] = None self._credits_button: Optional[ViewButton] = None self._player_anim_surface: Optional[AnimSurfaceColored] = None self._player_anim_tracker = AnimTracker() self._player_anim_tracker.set_anim(("idle", 4, -1)) self._ko_text: Optional[Surface] = None self._plants_collected_text: Optional[Surface] = None self._damage_dealt_text: Optional[Surface] = None self._damage_taken_text: Optional[Surface] = None self._ko_p1_text: Optional[Surface] = None self._plants_collected_p1_text: Optional[Surface] = None self._damage_dealt_p1_text: Optional[Surface] = None self._damage_taken_p1_text: Optional[Surface] = None self._ko_p2_text: Optional[Surface] = None self._plants_collected_p2_text: Optional[Surface] = None self._damage_dealt_p2_text: Optional[Surface] = None self._damage_taken_p2_text: Optional[Surface] = None self._ko_p3_text: Optional[Surface] = None self._plants_collected_p3_text: Optional[Surface] = None self._damage_dealt_p3_text: Optional[Surface] = None self._damage_taken_p3_text: Optional[Surface] = None self._ko_p4_text: Optional[Surface] = None self._plants_collected_p4_text: Optional[Surface] = None self._damage_dealt_p4_text: Optional[Surface] = None self._damage_taken_p4_text: Optional[Surface] = None
def __init__(self, entity: Effect, view: 'InGameView'): super().__init__(entity, view, (InGameView.EFFECT_SIZE, InGameView.EFFECT_SIZE)) self.anim_surface = view.get_effect_anim_surface() self.tracker = AnimTracker() self.effect_type = entity.get_effect_type() if self.effect_type in self.EFFECT_ANIMS: self.tracker.set_anim(*self.EFFECT_ANIMS[self.effect_type])
def __init__(self, entity: Player, view: 'InGameView'): super().__init__(entity, view, (InGameView.PLAYER_SIZE, InGameView.PLAYER_SIZE)) self.color = get_player_color(entity.get_color()) self.tracker = AnimTracker() self.phase_shift = random.random() * math.pi self.camera_x = entity.get_x() self.state = self.STATE_UNINIT
class BulletDrawer(EntityDrawer): def __init__(self, entity: Entity, view: 'InGameView'): super().__init__(entity, view, (InGameView.EFFECT_SIZE, InGameView.EFFECT_SIZE)) self.anim_surface = view.get_effect_anim_surface() self.tracker = AnimTracker() self.tracker.set_anim(("corn_bullet", 14, -1)) def _calc_offsets(self, size: Tuple[int, int]) -> Tuple[int, int]: return -int(size[0] / 2), -int(size[1] / 2) def draw(self, surface: Surface): self.anim_surface.blit_on(surface, self._get_draw_pos(), self.tracker)
def __init__(self): super().__init__() self._player_anim_surface: Optional[AnimSurfaceColored] = None self._player_anim_tracker = AnimTracker() self._player_anim_tracker.set_anim(("idle", 4, -1)) self._title: Optional[ViewButton] = None self._color_grid: Optional['ViewColorGrid'] = None self._players_slots: List[Tuple[ViewPlayerSlot, int]] = [] self._players_slots_width = 0 self._return_button: Optional[ViewButton] = None self._how_to_play_button: Optional[ViewButton] = None self._start_button: Optional[ViewButton] = None
class EndView(View): def __init__(self): super().__init__() self._winner_button: Optional[ViewButton] = None self._play_again_button: Optional[ViewButton] = None self._quit_button: Optional[ViewButton] = None self._credits_button: Optional[ViewButton] = None self._player_anim_surface: Optional[AnimSurfaceColored] = None self._player_anim_tracker = AnimTracker() self._player_anim_tracker.set_anim(("idle", 4, -1)) self._ko_text: Optional[Surface] = None self._plants_collected_text: Optional[Surface] = None self._damage_dealt_text: Optional[Surface] = None self._damage_taken_text: Optional[Surface] = None self._ko_p1_text: Optional[Surface] = None self._plants_collected_p1_text: Optional[Surface] = None self._damage_dealt_p1_text: Optional[Surface] = None self._damage_taken_p1_text: Optional[Surface] = None self._ko_p2_text: Optional[Surface] = None self._plants_collected_p2_text: Optional[Surface] = None self._damage_dealt_p2_text: Optional[Surface] = None self._damage_taken_p2_text: Optional[Surface] = None self._ko_p3_text: Optional[Surface] = None self._plants_collected_p3_text: Optional[Surface] = None self._damage_dealt_p3_text: Optional[Surface] = None self._damage_taken_p3_text: Optional[Surface] = None self._ko_p4_text: Optional[Surface] = None self._plants_collected_p4_text: Optional[Surface] = None self._damage_dealt_p4_text: Optional[Surface] = None self._damage_taken_p4_text: Optional[Surface] = None def _inner_init(self): self._winner_button = ViewButton(45, "P1 Win", disabled=True) self._winner_button.set_size(300, 100) self.add_child(self._winner_button) self._play_again_button = ViewButton(45, "Play Again!") self._play_again_button.set_size(200, 70) self._play_again_button.set_action_callback( self._shared_data.get_show_view_callback("color_select")) self.add_child(self._play_again_button) self._quit_button = ViewButton(20, "Quit") self._quit_button.set_size(100, 30) self._quit_button.set_action_callback( self._shared_data.get_show_view_callback("title")) self.add_child(self._quit_button) self._ko_text = self._shared_data.get_font(32).render( "KOs", True, self.TEXT_COLOR) self._plants_collected_text = self._shared_data.get_font(32).render( "Plants collected", True, self.TEXT_COLOR) self._damage_dealt_text = self._shared_data.get_font(32).render( "Damage dealt", True, self.TEXT_COLOR) self._damage_taken_text = self._shared_data.get_font(32).render( "Damage taken", True, self.TEXT_COLOR) self._ko_p1_text = self._shared_data.get_font(32).render( "0", True, self.TEXT_COLOR) self._plants_collected_p1_text = self._shared_data.get_font(32).render( "0", True, self.TEXT_COLOR) self._damage_dealt_p1_text = self._shared_data.get_font(32).render( "0", True, self.TEXT_COLOR) self._damage_taken_p1_text = self._shared_data.get_font(32).render( "0", True, self.TEXT_COLOR) self._ko_p2_text = self._shared_data.get_font(32).render( "0", True, self.TEXT_COLOR) self._plants_collected_p2_text = self._shared_data.get_font(32).render( "0", True, self.TEXT_COLOR) self._damage_dealt_p2_text = self._shared_data.get_font(32).render( "0", True, self.TEXT_COLOR) self._damage_taken_p2_text = self._shared_data.get_font(32).render( "0", True, self.TEXT_COLOR) self._ko_p3_text = self._shared_data.get_font(32).render( "0", True, self.TEXT_COLOR) self._plants_collected_p3_text = self._shared_data.get_font(32).render( "0", True, self.TEXT_COLOR) self._damage_dealt_p3_text = self._shared_data.get_font(32).render( "0", True, self.TEXT_COLOR) self._damage_taken_p3_text = self._shared_data.get_font(32).render( "0", True, self.TEXT_COLOR) self._ko_p4_text = self._shared_data.get_font(32).render( "0", True, self.TEXT_COLOR) self._plants_collected_p4_text = self._shared_data.get_font(32).render( "0", True, self.TEXT_COLOR) self._damage_dealt_p4_text = self._shared_data.get_font(32).render( "0", True, self.TEXT_COLOR) self._damage_taken_p4_text = self._shared_data.get_font(32).render( "0", True, self.TEXT_COLOR) self._player_anim_surface = self._shared_data.new_anim_colored( "farmer", FARMER_ANIMATION, 210, 210) def _inner_pre_draw(self, surface: Surface): stage = self._shared_data.get_game().get_stage() players = stage.get_players() main_group_x = surface.get_width() / 2 surface_width, surface_height = surface.get_size() self._winner_button.set_position_centered(main_group_x, 75) self._play_again_button.set_position_centered(main_group_x, 700) self._quit_button.set_position_centered(80, 730) if len(players) >= 2: # tableau 1 pygame.draw.rect(surface, self.BUTTON_NORMAL_COLOR, (20, 150, 229, 252)) pygame.draw.line(surface, self.TEXT_COLOR, (20, 150), (248, 150), 1) pygame.draw.line(surface, self.TEXT_COLOR, (20, 213), (248, 213), 1) pygame.draw.line(surface, self.TEXT_COLOR, (20, 276), (248, 276), 1) pygame.draw.line(surface, self.TEXT_COLOR, (20, 339), (248, 339), 1) pygame.draw.line(surface, self.TEXT_COLOR, (20, 402), (248, 402), 1) surface.blit(self._ko_text, (27, 171)) surface.blit(self._plants_collected_text, (27, 234)) surface.blit(self._damage_dealt_text, (27, 297)) surface.blit(self._damage_taken_text, (27, 360)) surface.blit(self._ko_p1_text, (210, 171)) surface.blit(self._plants_collected_p1_text, (210, 234)) surface.blit(self._damage_dealt_p1_text, (210, 297)) surface.blit(self._damage_taken_p1_text, (210, 360)) self._player_anim_surface.blit_color_on(surface, (25, 425), self._player_anim_tracker, (255, 0, 0)) # tableau 2 pygame.draw.rect(surface, self.BUTTON_NORMAL_COLOR, (270, 150, 229, 252)) pygame.draw.line(surface, self.TEXT_COLOR, (270, 150), (498, 150), 1) pygame.draw.line(surface, self.TEXT_COLOR, (270, 213), (498, 213), 1) pygame.draw.line(surface, self.TEXT_COLOR, (270, 276), (498, 276), 1) pygame.draw.line(surface, self.TEXT_COLOR, (270, 339), (498, 339), 1) pygame.draw.line(surface, self.TEXT_COLOR, (270, 402), (498, 402), 1) surface.blit(self._ko_text, (274, 171)) surface.blit(self._plants_collected_text, (274, 234)) surface.blit(self._damage_dealt_text, (274, 297)) surface.blit(self._damage_taken_text, (274, 360)) surface.blit(self._ko_p2_text, (454, 171)) surface.blit(self._plants_collected_p2_text, (454, 234)) surface.blit(self._damage_dealt_p2_text, (454, 297)) surface.blit(self._damage_taken_p2_text, (454, 360)) self._player_anim_surface.blit_color_on(surface, (280, 425), self._player_anim_tracker, (255, 0, 0)) if len(players) >= 3: # tableau 3 pygame.draw.rect(surface, self.BUTTON_NORMAL_COLOR, (520, 150, 229, 252)) pygame.draw.line(surface, self.TEXT_COLOR, (520, 150), (748, 150), 1) pygame.draw.line(surface, self.TEXT_COLOR, (520, 213), (748, 213), 1) pygame.draw.line(surface, self.TEXT_COLOR, (520, 276), (748, 276), 1) pygame.draw.line(surface, self.TEXT_COLOR, (520, 339), (748, 339), 1) pygame.draw.line(surface, self.TEXT_COLOR, (520, 402), (748, 402), 1) surface.blit(self._ko_text, (527, 171)) surface.blit(self._plants_collected_text, (527, 234)) surface.blit(self._damage_dealt_text, (527, 297)) surface.blit(self._damage_taken_text, (527, 360)) surface.blit(self._ko_p3_text, (713, 171)) surface.blit(self._plants_collected_p3_text, (713, 234)) surface.blit(self._damage_dealt_p3_text, (713, 297)) surface.blit(self._damage_taken_p3_text, (713, 360)) self._player_anim_surface.blit_color_on( surface, (535, 425), self._player_anim_tracker, (255, 0, 0)) if len(players) >= 4: # tableau 4 pygame.draw.rect(surface, self.BUTTON_NORMAL_COLOR, (770, 150, 229, 252)) pygame.draw.line(surface, self.TEXT_COLOR, (770, 150), (998, 150), 1) pygame.draw.line(surface, self.TEXT_COLOR, (770, 213), (998, 213), 1) pygame.draw.line(surface, self.TEXT_COLOR, (770, 276), (998, 276), 1) pygame.draw.line(surface, self.TEXT_COLOR, (770, 339), (998, 339), 1) pygame.draw.line(surface, self.TEXT_COLOR, (770, 402), (998, 402), 1) surface.blit(self._ko_text, (777, 171)) surface.blit(self._plants_collected_text, (777, 234)) surface.blit(self._damage_dealt_text, (777, 297)) surface.blit(self._damage_taken_text, (777, 360)) surface.blit(self._ko_p4_text, (963, 171)) surface.blit(self._plants_collected_p4_text, (963, 234)) surface.blit(self._damage_dealt_p4_text, (963, 297)) surface.blit(self._damage_taken_p4_text, (963, 360)) self._player_anim_surface.blit_color_on( surface, (790, 425), self._player_anim_tracker, (255, 0, 0)) cmpt = 0 for idx, player in players.items(): if cmpt == 0: self._player_anim_surface.blit_color_on( surface, (25, 425), self._player_anim_tracker, (get_player_color(player.get_color()))) cmpt = cmpt + 1 elif cmpt == 1: self._player_anim_surface.blit_color_on( surface, (280, 425), self._player_anim_tracker, (get_player_color(player.get_color()))) cmpt = cmpt + 1 elif cmpt == 2: self._player_anim_surface.blit_color_on( surface, (535, 425), self._player_anim_tracker, (get_player_color(player.get_color()))) cmpt = cmpt + 1 elif cmpt == 3: self._player_anim_surface.blit_color_on( surface, (790, 425), self._player_anim_tracker, (get_player_color(player.get_color()))) cmpt = cmpt + 1 pass def on_enter(self): self._shared_data.play_music("sounds/victory.ogg", 0, 0, 0) # self._shared_data.play_music("musics/menumusic.ogg") self._winner_button.set_text("P" + str(self._shared_data.get_game( ).get_stage().get_winner().get_player_index() + 1) + " Win!") stage = self._shared_data.get_game().get_stage() players = stage.get_players() cmpt = 0 for idx, player in players.items(): if cmpt == 0: self._ko_p1_text = self._shared_data.get_font(32).render( str(player.get_statistics().get_kos()), True, self.TEXT_COLOR) self._plants_collected_p1_text = self._shared_data.get_font( 32).render( str(player.get_statistics().get_plants_collected()), True, self.TEXT_COLOR) self._damage_dealt_p1_text = self._shared_data.get_font( 32).render(str(player.get_statistics().get_damage_dealt()), True, self.TEXT_COLOR) self._damage_taken_p1_text = self._shared_data.get_font( 32).render(str(player.get_statistics().get_damage_taken()), True, self.TEXT_COLOR) cmpt = cmpt + 1 elif cmpt == 1: self._ko_p2_text = self._shared_data.get_font(32).render( str(player.get_statistics().get_kos()), True, self.TEXT_COLOR) self._plants_collected_p2_text = self._shared_data.get_font( 32).render( str(player.get_statistics().get_plants_collected()), True, self.TEXT_COLOR) self._damage_dealt_p2_text = self._shared_data.get_font( 32).render(str(player.get_statistics().get_damage_dealt()), True, self.TEXT_COLOR) self._damage_taken_p2_text = self._shared_data.get_font( 32).render(str(player.get_statistics().get_damage_taken()), True, self.TEXT_COLOR) cmpt = cmpt + 1 elif cmpt == 2: self._ko_p3_text = self._shared_data.get_font(32).render( str(player.get_statistics().get_kos()), True, self.TEXT_COLOR) self._plants_collected_p3_text = self._shared_data.get_font( 32).render( str(player.get_statistics().get_plants_collected()), True, self.TEXT_COLOR) self._damage_dealt_p3_text = self._shared_data.get_font( 32).render(str(player.get_statistics().get_damage_dealt()), True, self.TEXT_COLOR) self._damage_taken_p3_text = self._shared_data.get_font( 32).render(str(player.get_statistics().get_damage_taken()), True, self.TEXT_COLOR) cmpt = cmpt + 1 elif cmpt == 3: self._ko_p4_text = self._shared_data.get_font(32).render( str(player.get_statistics().get_kos()), True, self.TEXT_COLOR) self._plants_collected_p4_text = self._shared_data.get_font( 32).render( str(player.get_statistics().get_plants_collected()), True, self.TEXT_COLOR) self._damage_dealt_p4_text = self._shared_data.get_font( 32).render(str(player.get_statistics().get_damage_dealt()), True, self.TEXT_COLOR) self._damage_taken_p4_text = self._shared_data.get_font( 32).render(str(player.get_statistics().get_damage_taken()), True, self.TEXT_COLOR) cmpt = cmpt + 1
class PlayerDrawer(EntityDrawer): __slots__ = "color", "tracker", "phase_shift", "camera_x", "state" BAR_WIDTH, BAR_HEIGHT = 100, 10 BAR_OFFSET = 40 BAR_SLEEPING_OFFSET = 25 HALF_BAR_WIDTH = BAR_WIDTH // 2 HEALTH_BACKG_COLOR = 16, 26, 11 MAX_HEALTH_COLOR = 74, 201, 20 MIN_HEALTH_COLOR = 201, 20, 20 INCARNATION_COLOR = 47, 82, 212 INCARNATION_BACK_COLOR = 23, 34, 74 CAMERA_SPEED = 0.1 STATE_UNINIT = 0 STATE_IDLE = 1 STATE_RUNNING = 2 STATE_MUTATING = 3 # STATE_ROLLING = 4 STATE_MISC_ANIM = 5 STATE_SLEEPING = 6 # STATE_SHOOTING = 7 STATE_SPECIAL = 8 NO_COLOR = 255, 255, 255 MISC_ANIMATIONS = { "farmer:rake_attack": (("attack_side", 40, 1), ), "farmer:spinning_attack": (("attack_down", 40, 2), ), "potato:punch": (("attack_side", 20, 1), ), "corn:shot": (("attack_side", 20, 1), ), "carrot:strike": (("attack_side", 20, 1), ), "hit": (("hit", 20, 1), ), "grabing": (("grab", 20, 1), ), "player:mutation": (("pick", 10, 1), ) } MISC_SOUNDS = { "farmer:rake_attack": "sounds/farmerpunch.ogg", "farmer:spinning_attack": "sounds/farmerspin.ogg", "hit": "sounds/hit.ogg", "potato:punch": "sounds/potatopunch.ogg", "potato:roll": "sounds/potatoroll.ogg", "corn:shot": "sounds/cornshot.ogg", "corn:gatling": "sounds/corngatling.ogg", "carrot:strike": "sounds/carrotstrike.ogg", "carrot:thrust": "sounds/carrotthrust.ogg" } def __init__(self, entity: Player, view: 'InGameView'): super().__init__(entity, view, (InGameView.PLAYER_SIZE, InGameView.PLAYER_SIZE)) self.color = get_player_color(entity.get_color()) self.tracker = AnimTracker() self.phase_shift = random.random() * math.pi self.camera_x = entity.get_x() self.state = self.STATE_UNINIT def get_camera_x(self) -> Optional[float]: return self.camera_x def draw(self, surface: Surface): player = cast(Player, self.entity) self.tracker.set_all_reversed(player.get_turned_to_left()) can_run = player.get_vel_x() != 0 and player.is_on_ground() incarnation_type = player.get_incarnation_type() is_potato = incarnation_type == IncarnationType.POTATO is_corn = incarnation_type == IncarnationType.CORN is_carrot = incarnation_type == IncarnationType.CARROT for animation in player.foreach_animation(): if self.state in (self.STATE_IDLE, self.STATE_RUNNING ) and animation in self.MISC_ANIMATIONS: self.tracker.set_anim(*self.MISC_ANIMATIONS[animation]) self.state = self.STATE_MISC_ANIM if animation == "player:mutation": self.state = self.STATE_MUTATING elif animation == "player:unmutation": self.state = self.STATE_UNINIT if animation in self.MISC_SOUNDS: self.view.get_shared_data().play_sound( self.MISC_SOUNDS[animation]) if self.state in (self.STATE_MUTATING, self.STATE_MISC_ANIM ) and self.tracker.get_anim_name() is None: self.state = self.STATE_UNINIT elif self.state == self.STATE_SPECIAL and not player.is_in_special_action( ): if is_potato: self.tracker.set_anim(("attack_roll_end", 14, 1)) elif is_corn: self.tracker.set_anim(("attack_gun_end", 14, 1)) elif is_carrot: self.tracker.set_anim(("attack_sword_end", 14, 1)) self.state = self.STATE_MISC_ANIM elif self.state == self.STATE_SLEEPING and not player.is_sleeping(): self.tracker.set_anim(("unsleep", 14, 1)) self.state = self.STATE_MISC_ANIM if self.state != self.STATE_SPECIAL and player.is_in_special_action(): if is_potato: self.tracker.set_anim(("attack_roll_start", 14, 1), ("attack_roll_idle", 14, -1)) elif is_corn: self.tracker.set_anim(("attack_gun_start", 14, 1), ("attack_gun_idle", 14, -1)) elif is_carrot: self.tracker.set_anim(("attack_sword_start", 14, 1), ("attack_sword_idle", 14, -1)) self.state = self.STATE_SPECIAL elif self.state != self.STATE_SLEEPING and player.is_sleeping( ) and incarnation_type is not None: self.tracker.set_anim(("sleep", 14, 1), pause_at_end=True) self.state = self.STATE_SLEEPING elif self.state in (self.STATE_UNINIT, self.STATE_IDLE) and can_run: self.tracker.set_anim(("run", 14, -1)) self.state = self.STATE_RUNNING elif self.state in (self.STATE_UNINIT, self.STATE_RUNNING) and not can_run: self.tracker.set_anim(("idle", 7, -1)) self.state = self.STATE_IDLE anim_surface = self.view.get_player_anim_surface() if self.state != self.STATE_MUTATING: if is_potato: anim_surface = self.view.get_potato_anim_surface() elif is_corn: anim_surface = self.view.get_corn_anim_surface() elif is_carrot: anim_surface = self.view.get_carrot_anim_surface() anim_surface.blit_color_on(surface, self._get_draw_pos(), self.tracker, self.color) health_ratio = player.get_hp_ratio() health_color = _lerp_color(self.MIN_HEALTH_COLOR, self.MAX_HEALTH_COLOR, health_ratio) health_bar_x, health_bar_y = self.view.get_screen_pos( self.entity.get_x(), self.entity.get_y()) if not player.is_sleeping(): health_bar_y -= InGameView.PLAYER_SIZE + self.BAR_OFFSET else: health_bar_y += self.BAR_SLEEPING_OFFSET health_bar_y -= math.cos(time.monotonic() * 5 + self.phase_shift) * 4 self._draw_bar(surface, health_bar_x, health_bar_y, health_ratio, health_color, self.HEALTH_BACKG_COLOR) if incarnation_type is not None: ratio = player.get_incarnation_duration_ratio() self._draw_bar(surface, health_bar_x, health_bar_y + self.BAR_HEIGHT, ratio, self.INCARNATION_COLOR, self.INCARNATION_BACK_COLOR) self.camera_x += (player.get_x() - self.camera_x) * self.CAMERA_SPEED def _draw_bar(self, surface: Surface, x: int, y: int, ratio: float, color: Tuple[int, int, int], back_color: Tuple[int, int, int]): x -= self.HALF_BAR_WIDTH health_bar_width = int(self.BAR_WIDTH * ratio) pygame.draw.rect(surface, back_color, (x + health_bar_width, y, self.BAR_WIDTH - health_bar_width, self.BAR_HEIGHT)) pygame.draw.rect(surface, color, (x, y, health_bar_width, self.BAR_HEIGHT))
def __init__(self, entity: Entity, view: 'InGameView'): super().__init__(entity, view, (InGameView.EFFECT_SIZE, InGameView.EFFECT_SIZE)) self.anim_surface = view.get_effect_anim_surface() self.tracker = AnimTracker() self.tracker.set_anim(("corn_bullet", 14, -1))
class ColorSelectView(View): BACKGROUND_COLOR = 133, 43, 24 def __init__(self): super().__init__() self._player_anim_surface: Optional[AnimSurfaceColored] = None self._player_anim_tracker = AnimTracker() self._player_anim_tracker.set_anim(("idle", 4, -1)) self._title: Optional[ViewButton] = None self._color_grid: Optional['ViewColorGrid'] = None self._players_slots: List[Tuple[ViewPlayerSlot, int]] = [] self._players_slots_width = 0 self._return_button: Optional[ViewButton] = None self._how_to_play_button: Optional[ViewButton] = None self._start_button: Optional[ViewButton] = None def _inner_init(self): self._player_anim_surface = self._shared_data.new_anim_colored("farmer", FARMER_ANIMATION, 210, 210) self._title = ViewButton(35, "Select your color", disabled=True) self.add_child(self._title) self._color_grid = ViewColorGrid(100, 2, 7) self._color_grid.set_change_callback(self._grid_player_changed) self.add_child(self._color_grid) self._players_slots_width = 0 for i, _ in enumerate(controls.PLAYERS_KEYS): slot = ViewPlayerSlot(i, self._player_anim_surface, self._player_anim_tracker) self.add_child(slot) self._players_slots.append((slot, self._players_slots_width)) self._players_slots_width += slot.get_width() self._return_button = ViewButton(35, "Return") self._return_button.set_action_callback(self._shared_data.get_show_view_callback("title")) self._how_to_play_button = ViewButton(35, "How To Play") self._start_button = ViewButton(35, "Start") self._start_button.set_visible(False) self._start_button.set_action_callback(self._on_start_action) self.add_child(self._return_button) self.add_child(self._start_button) def _inner_pre_draw(self, surface: Surface): height = surface.get_height() width = surface.get_width() x_mid = width / 2 self._color_grid.set_position_centered(x_mid, 225) self._title.set_position_centered(x_mid, 55) players_slots_x = x_mid - self._players_slots_width / 2 for slot, offset in self._players_slots: slot.set_position(players_slots_x + offset, 360) self._return_button.set_position(20, height - 70) self._start_button.set_position(width - self._start_button.get_width() - 20, height - 70) def _grid_player_changed(self, player_idx: int, player_color: Optional[PlayerColor]): self._players_slots[player_idx][0].set_player_color(player_color) self._start_button.set_visible(self._color_grid.get_selections_count() > 1) def _on_start_action(self, _button): stage = Stage.new_example_stage() for player_idx, player_color in self._color_grid.get_selections().items(): stage.add_player(player_idx, player_color) self._shared_data.get_game().set_stage(stage) self._shared_data.get_game().show_view("in_game")