def clock_shown(self, show): if self._show != show: if show: self.do(AccelDeccel(MoveTo((0, 0), duration=1))) else: self.do(AccelDeccel(MoveTo((0, -100), duration=1))) self._show=show
def ping(self, delete_method): c = objects.Objects.get_controller() if self.curVertex.borderVertices: curVertex = self.curVertex for asID in self.curVertex.borderVertices.keys(): destVertexList = self.curVertex.borderVertices[asID] for destVertex in destVertexList: if destVertex.asID not in c.visibleASes: self.isSelectable = False self.isDestroyed = True pingAction = MoveTo(self.curVertex.position, 0.6) pingAction += MoveTo(destVertex.position, 2) pingAction += MoveTo(self.curVertex.position, 2) pingAction += CallFunc(c._show_as, asID) # pingAction += CallFunc(utils.play_sound, "Visible.wav") pingAction += CallFunc(delete_method, self) pingAction += CallFunc(curVertex._update_visibility) self.do(pingAction) self.dispatch_event("Ping") utils.play_sound("ping.wav") return True # We successfully pinged. Return True. return False
def attack(self, my_pokemon, opponent_pokemon): """ :param my_pokemon: :type my_pokemon: pokemon.Pokemon :param opponent_pokemon: :type opponent_pokemon: pokemon.Pokemon :return: """ if not self.current_battle: raise RuntimeError("Trying to attack pokemon when there is no battle active!") elif self.current_battle.animation_locked: return opponent_pokemon.decrement_current_health(my_pokemon.get_damage_to_target(opponent_pokemon)) my_sprite = my_pokemon.get_sprite_for_orientation(self.orientation) opponent_sprite = opponent_pokemon.get_sprite_for_orientation(constants.opposite_orientation(self.orientation)) original_position = my_sprite.position attack = MoveTo(opponent_sprite.position, duration=0.3) move_back = MoveTo(original_position, duration=0.25) my_sprite.do(attack + move_back + self.current_battle.toggle_animation_lock) self.current_battle.enable_animation_lock()
def switch_pokemon(self, source_index, target_index): if not self.current_battle: raise RuntimeError("Trying to switch active pokemon when there is no battle active!") elif self.current_battle.animation_locked: return source_pokemon = self.pokemon[source_index] source_sprite = source_pokemon.get_sprite_for_orientation(self.orientation) destination_pokemon = self.pokemon[target_index] destination_sprite = destination_pokemon.get_sprite_for_orientation(self.orientation) original_position = source_sprite.position if self.orientation == constants.BACK_FACING: switch_out = MoveTo((0, 0), duration=0.25) else: destination_sprite.position = (640, 480) switch_out = MoveTo((640, 480), duration=0.25) def switch_in(): destination_sprite.do(self.add_sprite + MoveTo(original_position, duration=0.25) + self.current_battle.toggle_animation_lock) switch_in = CallFunc(switch_in) source_sprite.do(switch_out + self.remove_sprite + switch_in) self.current_battle.enable_animation_lock() self.pokemon[target_index] = source_pokemon self.pokemon[source_index] = destination_pokemon self.current_battle.hud.update_health_bars()
def _replace_node(self, new_node): """ Move a new node into display, slow animation. """ if not self.is_running: if debug_mode: print("Not running") if self._current_node: self._current_node.kill() self._current_node = None return if self._current_node: self._current_node.do( MoveTo(self._point_offscreen_left, 0.5 * self._move_time) + CallFunc(self._current_node.kill)) self._current_node = UpdatableNode(new_node, self._point_offscreen_right) self._current_node.do( Delay(0.5 * self._move_time) + MoveTo(self._point_onscreen, 0.5 * self._move_time)) self.add(self._current_node)
def __init__(self): super().__init__("images/Audi.png", scale=.33) self.position = 400, 360 self.cshape = cm.AARectShape(eu.Vector2(*self.position), self.width / 4, self.height / 1.9) self.do( Repeat( MoveTo((random.randint(50, 1200), random.randint(50, 670)), 2) + MoveTo((random.randint(50, 1200), random.randint(50, 670)), 2)))
def check_kill(self, snake): if (not self.killed and not snake.is_dead) and (abs(snake.x - self.x) < 32 and abs(snake.y - self.y) < 32): self.killed = True self.killer = snake if not self.killer.is_enemy: are = self.parent.parent.parent s = chr(self.num + 96) # 判断当前吃掉的字母 if are.word[are.order] == s: # 如果和当前要吃的字母相同 则指针右移继续判断下一个字母 are.order += 1 are.score.element.text += s if are.order == are.length: #已经吃完当前单词 吃下一个 are.i = random.randint(0, are.num) are.j += 1 are.order = 0 are.word = are.wordlist[are.i][0] ss = unicode(are.wordlist[are.i][1], 'utf-8') are.chinese.element.text = ss are.length = len(are.word) are.score.element.text = '' for c in are.word: s = ord(c) - 96 print(s) arena = self.parent.parent arena.batch.add(Dot2(s)) else: if are.chance > 0: are.chance -= 1 are.life.element.text = str(are.chance) else: self.killer.crash() self.do(MoveTo(snake.position, 0.1) + CallFuncS(kill))
def on_mouse_press(self, x, y, buttons, modifier): p = self.players.get('Hero') if not self.action == None: p.remove_action(self.action) self.action = p.do(MoveTo((x, y), duration=sqrt(x**2 + y**2) / 32.0))
def check_kill(self, snake): if (not self.killed and not snake.is_dead) and (abs(snake.x - self.x) < 32 and abs(snake.y - self.y) < 32): self.killed = True self.killer = snake self.do(MoveTo(snake.position, 0.1) + CallFuncS(kill))
def __init__(self): super(LoadingScene, self).__init__() # 开启事件处理层 有事件发生 就要写 cocos.layer.Layer.is_event_handler = True d_width, d_height = cocos.director.director.get_window_size() # 创建背景精灵 background = cocos.sprite.Sprite('Resource//loading_scene//background.png') background.position = d_width // 2, d_height // 2 # 创建标题精灵 title = cocos.sprite.Sprite('Resource//loading_scene//title.png') title.position = d_width // 2, d_height // 2 delay_move_to = Delay(1.0) move_to = MoveTo((d_width // 2, d_height // 5 * 3), 2) title.do(delay_move_to + move_to) sub_title = cocos.sprite.Sprite('Resource//loading_scene//sub_title.png') sub_title.position = d_width // 2, d_height // 7 * 3 sub_title.opacity = 0 fade_in = FadeIn(2.0) delay_fade_in = Delay(2) sub_title.do(delay_fade_in + fade_in) self.add(background) self.add(title) self.add(sub_title)
def do_draw(self): """Draw a screen.""" self.main_character.x = 0 self.main_character.y = 0 self.main_character.do( MoveTo((self.interface.width, self.main_character.y), 10)) for character in self.characters: with suppress(Exception): self.remove(character.sprite) for explosion in self.explosions: with suppress(Exception): self.remove(explosion) self.explosions = [] self.collision_manager.clear() if self.toaster: self.remove(self.toaster) self.toaster = CollidableSprite("toaster00.svg") self.toaster.scale = 0.1 self.toaster.position = (2 * random.randint(0, self.width) / 3 + self.width / 3), (random.randint( 0, round(2 * self.height / 3))) self.add(self.toaster) self.collision_manager.add(self.toaster) self.current_screen += 1 for character in self.characters: self.sprites_by_id[id(character.sprite)] = character self.collision_manager.add(character.sprite) self.add(character.sprite) self.draw()
def attack(self, target, gameMap): from maps import Vertex if type(target) == Vertex or ( (issubclass(type(target), Troop) or type(target) == Firewall) and target.pid != self.pid): # Clicked a vertex or an enemy troop or an enemy Firewall if type(target) != Vertex: target = target.curVertex path = gameMap.get_path(self.curVertex, target, self.pid, self, "attack") if path and len(path) <= self.attackRange + 1: self.attackingPath = path self.is_attacking = True # Move the Ping Of Death to the dest vertex pingOfDeathAction = Delay(0) for vertexID in path: vertex = gameMap.vertices[vertexID] pingOfDeathAction += MoveTo(vertex.position, self.speed) # Execute Ping Of Death! pingOfDeathAction += CallFunc(self.POD, target) controller = objects.Objects.get_controller() pingOfDeathAction += CallFunc(controller.remove_unit, self) self.do(pingOfDeathAction) return True return False
def __init__(self): super(TestLayer, self).__init__() x, y = director.get_window_size() self.sprite = Sprite('grossini.png') self.add(self.sprite) self.sprite.do(MoveTo((x, y), 6))
def step(self, dt): super(RohinianAction, self).step(dt) if self.target.position[1] < 0: self.target.position = (random.randint(0, WIDTH), HEIGHT) self.target.do(MoveTo((random.randint(0, WIDTH), - self.target.height), random.randint(2, 6))) self.target.cshape.center = self.target.position
def check_eaten(self, snake): if (not self.eaten and not snake.is_dead) and ( abs(snake.x - self.x) < 32 and abs(snake.y - self.y) < 32): # 当食物未被吃掉,而 self.eaten = True self.killer = snake self.do(MoveTo(snake.position, 0.1) + CallFuncS(eat)) # MoveTo函数实现了当蛇靠近食物时,食物会自动被“吸引”到蛇头的效果
def check_kill(self, snake): # 检查是否被snake杀死 if (not self.killed and not snake.is_dead) and ( abs(snake.x - self.x) < 32 and abs(snake.y - self.y) < 32): # 当自己没有被吃掉且snake活着且距离足够小才满足被吃掉 self.killed = True # 改变状态 self.killer = snake # 记录击杀者 self.do(MoveTo(snake.position, 0.1) + CallFuncS(kill)) # 用0.1秒移动自己的位置到snake的位置
def on_enter(self): super(Bg, self).on_enter() sprite = Sprite(self.image) self.add(sprite) sprite.position = 320, 240 sprite.do(ScaleTo(4,0)) action = MoveTo((640, 480), 4) | ( ScaleTo(2,2) + ScaleTo(4,2) ) sprite.do(action)
def __init__(self): super(TestLayer, self).__init__() x, y = director.get_window_size() self.sprite = Sprite('grossini.png', (x // 4, y // 2)) self.add(self.sprite) shake_part = MoveBy((-4.0, 0.0), 0.05) shake = shake_part + Reverse(shake_part) * 2 + shake_part self.sprite.do(MoveTo((x // 2, y // 2), 1) + Repeat(shake))
def __init__(self): super(TestLayer, self).__init__() x, y = director.get_window_size() self.sprite = Sprite('grossini.png') self.add(self.sprite) self.sprite.do(MoveTo((x, y), 10)) if autotest: self.do(Delay(1) + CallFunc(self.resize))
def animate_packets(self, dt, target, gameMap): #### packet logic ### if self.is_attacking: if self.packets: p = self.packets.pop() p.position = self.position if not p.onMap: p.onMap = True gameMap.add(p, z=PACKET_Z) action = Show() action += MoveTo( gameMap.vertices[self.attackingPath[0]].position, 0.2) for v in self.attackingPath[1:]: vertex = gameMap.vertices[v] action += MoveTo(vertex.position, float(1 / p.speed)) action += MoveTo(target.position, 0.2) action += Hide() p.do(action) self.packets.insert(0, p)
def _add_animation(self, node, drink): """ Construct an animation path for the label. """ node.scale = 0.5 content_offset = (node.scale * node.label.element.content_width + self._drink_distance) / 2 # Make sure only one item has focus minimum_offset = ( (self._focus_length + self._focus_ramp) * self._screen_width) / 2 if content_offset < minimum_offset: content_offset = minimum_offset content_time = float(content_offset) / self._scroll_speed # Set start position node.position = (self._screen_width + content_offset, self._ticker_y) # Construct the path # Move to beginning of screen (coordinates, _) = self._path[0] move_actions = MoveTo(coordinates, content_time) # Move to ramp up (coordinates, time) = self._path[1] move_actions += MoveTo(coordinates, time) # Do the ramp (coordinates, time) = self._path[2] move_actions += (MoveTo(coordinates, time) | (Delay(time / 2) + ScaleTo(1, time / 2)) | CallFunc(self._set_focused_drink, drink)) # Move in focus (coordinates, time) = self._path[3] move_actions += MoveTo(coordinates, time) # Ramp down (coordinates, time) = self._path[4] move_actions += MoveTo(coordinates, time) | (ScaleTo(0.5, time / 2)) # Move to end of screen (coordinates, time) = self._path[5] move_actions += MoveTo(coordinates, time) # Move out of sight move_actions += MoveTo((0 - content_offset, self._ticker_y), content_time) # Prepare spawn point spawn_actions = Delay(content_time * 2) + CallFunc(self._next_drink) self.do(spawn_actions) # Start animation node.do(move_actions + CallFunc(self._safe_kill, node))
def get_move_action(self, action=None): x, y = self.position y = y + 40 theLocation = euclid.Vector2(x, y) if action == None: moveAction = Show() else: moveAction = action + Show() moveAction += MoveTo(theLocation, self.duration / 4) x = x + 40 theLocation = euclid.Vector2(x, y) moveAction += MoveTo(theLocation, self.duration / 4) y = y - 40 theLocation = euclid.Vector2(x, y) moveAction += MoveTo(theLocation, self.duration / 4) x = x - 40 theLocation = euclid.Vector2(x, y) moveAction += MoveTo(theLocation, self.duration / 4) moveAction += Hide() return moveAction
def start(self): arena = self.target.parent #type:Arena map = arena.map map.turn += 1 if map.turn <= 5: if map.check_states(): self.target.do( sequence(MoveTo(coordinate(map.i, map.j, arena.size), 2), Check_state(1))) else: self.target.do(Check_state(1)) else: print('gg')
def add_new_infos(self, content, color=(0, 255, 0, 255)): if self.info.full(): obj_leave = self.info.get() obj_leave.do( MoveTo((self.width // 2, self.height), 1) | FadeOut(1)) for obj in self.info.queue: obj.do(MoveBy((0, 40), 1)) obj = Text(text=content, position=(self.width // 2, self.height // 2 + 50), color=color, font_size=30) self.add(obj) self.info.put(obj) obj.do(FadeIn(1.5) + CallFunc(self.get_next_action))
def new_game(self): """ Create a new game scene, and add some elements in scene, like the rohenians, aerolites and spaceship. """ self.add(self.background, z=0) self.add(self.spaceship, z=3) self.add(self.bullets_string, z=4) self.add(self.hp_string, z=4) for aero in self.aerolites: # Set a randomic initial position to aerolites width = random.randint(0, WIDTH) aero.do(MoveTo((width, -aero.image.height), random.randint(7, 15))) self.add(aero, z=1) for rohenian in self.rohenians: # Set a randomic initial position to rohinians width = random.randint(-WIDTH, 2 * WIDTH) rohenian.do( MoveTo((width, -rohenian.image.height), random.randint(5, 8))) self.add(rohenian, z=1) return self
def _sequential_move(self, person, dst): #传入人物对象和移动轨迹 map = self.map i, j = dst[-1] id = person.pid map.person_container.position[id] = i, j map.person_container.movable[id] = False self.mapstate = self.map.send_mapstate() if map.person_container.controller[id] == 0: self.person[id].state = 'moved' self.is_event_handler = False self.map2per[dst[-1]] = person action = Delay(0.1) for x,y in dst: action = action + MoveTo(coordinate(x, y, self.size), 0.5) return action
def _handle_shoot(self, dt): if self.last_shoot_dt != 0: self.last_shoot_dt += dt keyboard = self.key_handler if keyboard[key.SPACE]: if self.last_shoot_dt == 0: self.last_shoot_dt += dt bear_actor = None objs = self.cm.ranked_objs_near(self, self.domain.shoot_distance) # reduce(lambda x, acc: [...acc, x] if isinstance(x[0], Bear) else acc, objs, []) for obj, distance in objs: if isinstance(obj, BearActor): bear_actor = obj # create bullet # set move action for it's. Sprite with cshape. When it's move check bullet_speed = 1000 x = bear_actor.position[0] - self.position[0] y = bear_actor.position[1] - self.position[1] distance = sqrt(x * x + y * y) # calculate position direction = ((x / distance, y / distance)) print(f"direction {direction}, xy: {x},{y}") offset = (TILE_WIDTH * direction[0], TILE_WIDTH * direction[1]) bullet_pos = (self.position[0] + offset[0], self.position[1] + offset[1]) bullet = Actor('assets/bullet.png', position=bullet_pos, domain=Bullet()) self.layer.addCollidable(bullet) bullet_fly_duration = distance / bullet_speed action = MoveTo(bear_actor.position, bullet_fly_duration) bullet.do(action) # TODO: collisions with map(trees) break # shoot only one bear if self.last_shoot_dt >= self.shoot_rate: self.last_shoot_dt = 0
def move(self, direct): pos = self.map.get_neighbor(self.cell(), direct).center self.do(MoveTo(pos, 1))
def switch_in(): destination_sprite.do(self.add_sprite + MoveTo(original_position, duration=0.25) + self.current_battle.toggle_animation_lock)
def to(self, point=(0, 0)): self.sprite.do(MoveTo(point, 1))