def process_right_panel(self, evt): px = evt.pos[0] - self.right_panel_position.x - 20 py = evt.pos[1] - self.right_panel_position.y if self.enter_button.rect.collidepoint((px, py)) and not self.enter_button_disabled: #print "enter button pressed!" if not self.castle_active: GI.game_core.enter_town() return GI.game_core.enter_castle() return if self.leave_button.rect.collidepoint((px, py)) and not self.leave_button_disabled: #print "leave button pressed!" GI.game_core.leave_town() return k = 0 for x in self.btn_array: px, py = x.rect.x + self.right_panel_position.x + 20, x.rect.y + self.right_panel_position.y + 20 tmp = pygame.Rect((px, py), (x.rect.width, x.rect.height)) if tmp.collidepoint(evt.pos): break k += 1 #print "collide with button #", if k < len(self.btn_array): if GI.game_core.recruit(self.unit_array[k]): play(self.trumpet_sound)
def show_animation(self): screen = pygame.display.get_surface() window = screen.copy() play(self.battle_sound, True) pygame.time.delay(500) while not self.missile.colliderect(self.target): self.missile = self.missile.move(self.speed) screen.blit(window,(0,0)) screen.blit(self.image, self.missile) pygame.time.delay(50) pygame.display.flip()
def change_frames(self, every_redraw=False): if self.playing_animation: #print "we're using this frames to update: %s" % self.frame_names self.time_passed += GI.time_passed if not GI.ai.working: play(self.walk_sound) #print self.time_passed if self.time_passed > self.needed_time or every_redraw: #print "time to change frame!" self.time_passed = 0 self.current_frame += 1 if self.current_frame == len(self.frames): self.current_frame = 0 self.image = pygame.Surface((32, 32), flags=pygame.SRCALPHA) #print "frame # ", self.current_frame self.image.blit(self.frames[self.current_frame], (0, 0))
def show_unit_panel(self, unit): if not isinstance(unit,Unit): return play(self.yes_sir_sound) self.unit_panel = pygame.Surface((400, 100), flags=pygame.SRCALPHA) self.unit_panel.fill(Palette.black_alpha) image = pygame.image.load(unit.link) image = pygame.transform.scale(image, (64,64)) self.unit_panel.blit(image, (18, 18)) pygame.draw.rect(self.unit_panel, Palette.dark_yellow, pygame.Rect(0, 0, 400, 99), 2) #create text unit_attack = pygame.font.SysFont(None, 22).render("Hit points: %s" % unit.attack, 3, Palette.dark_yellow) self.unit_panel.blit(unit_attack, (110, 15)) unit_defense = pygame.font.SysFont(None, 22).render("Defense: %s" % unit.defense, 3, Palette.dark_yellow) self.unit_panel.blit(unit_defense, (110, 45)) unit_health = pygame.font.SysFont(None, 22).render("Health: %s/%s" % (unit.actual_health,unit.health), 3, Palette.dark_yellow) self.unit_panel.blit(unit_health, (110, 75)) unit_movement = pygame.font.SysFont(None, 22).render("Movements: %s" % unit.movement, 3, Palette.dark_yellow) self.unit_panel.blit(unit_movement, (250, 15)) unit_attack_range = pygame.font.SysFont(None, 22).render("Attack Range: %s" % unit.attack_range, 3, Palette.dark_yellow) self.unit_panel.blit(unit_attack_range, (250, 45)) unit_level = pygame.font.SysFont(None, 22).render("Level: %s" % unit.level, 3, Palette.dark_yellow) self.unit_panel.blit(unit_level, (250, 75)) self.unit_panel_state = 1
def attack_block(self, target_pos): target = GI.worldGrid.get_block(target_pos[1], target_pos[0]) if not isinstance(target, GameUI.ActiveTile.ActiveTile): #print "wow, you're suddenly attacking Tile! o_O" return False if target.is_friendly in [1, 2]: return False if GI.selected_unit.attack_range <= 0: return False if isinstance(target, GameUI.Placeholder.Placeholder): target = target.link_to_obj if isinstance(GI.selected_unit, GameUI.Archer.Archer) and not GI.ai.working: missile = Missile("arrow",GI.selected_unit.get_rect(), target_pos, GI.worldGrid.world_position) missile.show_animation() elif isinstance(GI.selected_unit, GameUI.Wizard.Wizard) and not GI.ai.working: missile = Missile("blast",GI.selected_unit.get_rect(), target_pos, GI.worldGrid.world_position) missile.show_animation() elif not GI.ai.working: play(self.battle_sound, True) if isinstance(target, GameUI.Unit.Unit): #print "attacking!" target.damage(GI.selected_unit.attack - target.defense) if target.actual_health <= 0: GI.worldGrid.update_from_source(target.position) if GI.ai.working: GI.selected_unit.level_up(5) else: GI.selected_unit.level_up() tpx, tpy = target.position blk = GI.worldGrid.get_block(tpy, tpx) blk.set_highlighted("skull") GI.worldGrid.need_to_redraw = True if isinstance(target, GameUI.Castle.Castle) and not target.crashed: #print "attacking!" target.damage(GI.selected_unit.attack * 5) if target.toughness <= 0: target.set_crashed(True) GI.worldGrid.update_from_source(target.position) px, py = target.position GI.worldGrid.update_placeholders(px, py, None) #print "units inside: %s" % str(target.get_units()) for r in target.get_units(False): if isinstance(r, GameUI.King.King): #print "leaving King from castle!" r.hidden = False r.actual_health = r.health / 2 r.position = target.position #print "castle of player #%d destroyed!" % target.player_index GI.worldGrid.redraw(clearing=True) GI.worldGrid.draw_active_data(GI.all_active_data) if isinstance(target, GameUI.Town.Town): play(self.fanfare_sound, True) #print "attacking town!" target.mod_loyalty(-GI.selected_unit.attack * 1.5) if target.loyalty <= 0: #just notifying pl_num = GI.active_player + 1 if pl_num >= self.total_players: pl_num = 0 user_data = GI.player_private_data[pl_num] old_txt = "" if "message" in user_data: old_txt = user_data["message"] val = {"message": old_txt + "One of your towns was lost!\n"} user_data.update(val) # and now updating! target.loyalty = 70 target.set_friendly(2) target.unit_inside = None target.player_index = GI.active_player GI.worldGrid.redraw(clearing=True) GI.worldGrid.draw_active_data(GI.all_active_data) GI.selected_unit.attack_range -= 1 #GI.selected_unit.movement -= 1 self.calc_all_areas() #GI.worldGrid.redraw() return True
def die(self): play(self.die_sound, True) #print "dying :(" self.hidden = True self.player_index = -1