def puntaje(self, sprite, player): puntaje1, puntaje2 = 0, 0 for i in self.interruptores: if i.player == 1: puntaje1 += 1 elif i.player == 2: puntaje2 += 1 self.rojo.puntos = puntaje1 self.verde.puntos = puntaje2 if not puntaje1: spyral.event.queue("carrito.gana", spyral.Event(player=2)) elif not puntaje2: spyral.event.queue("carrito.gana", spyral.Event(player=1))
def update(self, delta): self.x += delta * self.vel_x self.y += delta * self.vel_y r = self.rect if r.top < 0: r.top = 0 self.vel_y = -self.vel_y if r.bottom > HEIGHT: r.bottom = HEIGHT self.vel_y = -self.vel_y if r.left < 0: spyral.event.handle("pong_score", spyral.Event(side='left')) if r.right > WIDTH: spyral.event.handle("pong_score", spyral.Event(side='right'))
def _remove_char(self, position, end=None): """ Remove the characters from *position* to *end* within the text. If *end* is None, it removes only a single character. Also triggers a form.<name>.<widget>.changed event. """ if end is None: end = position + 1 if position == len(self._value): pass else: self._value = self._value[:position] + self._value[end:] self._compute_letter_widths() self._render_text() self._render_cursor() e = spyral.Event(name="changed", widget=self, form=self.form, value=self._value) self.scene._queue_event( "form.%(form_name)s.%(widget)s.changed" % { "form_name": self.form.__class__.__name__, "widget": self.name }, e)
def animate(self, animation): """ Animates this sprite given an animation. Read more about :class:`animation <spyral.animation>`. :param animation: The animation to run. :type animation: :class:`Animation <spyral.Animation>` """ for a in self._animations: repeats = a.properties.intersection(animation.properties) if repeats: # Loop over all repeats raise ValueError("Cannot animate on propies %s twice" % (str(repeats),)) if len(self._animations) == 0: spyral.event.register('director.update', self._run_animations, ('delta', ), scene=self._scene()) self._animations.append(animation) self._progress[animation] = 0 self._evaluate(animation, 0.0) e = spyral.Event(animation=animation, sprite=self) # Loop over all possible properties for property in animation.properties: spyral.event.handle("%s.%s.animation.start" % (self.__class__.__name__, property), e)
def focus(self, widget=None): """ Sets the focus to be on a specific widget. Focus by default goes to the first widget added to the form. :param widget: The widget that is gaining focus; if None, then the first widget gains focus. :type widget: :ref:`Widget <api.widgets>` """ # By default, we focus on the first widget added to the form if widget is None: if not self._widgets: return widget = min(self._tab_orders.iteritems(), key=operator.itemgetter(1))[0] # If we'd focused on something before, we blur it if self._current_focus is not None: self._blur(self._current_focus) # We keep track of our newly focused thing self._current_focus = widget # Make and send the "focused" event e = spyral.Event(name="focused", widget=widget, form=self) self.scene._queue_event( "form.%(form_name)s.%(widget)s.focused" % { "form_name": self.__class__.__name__, "widget": widget.name }, e) widget._handle_focus(e) return
def _handle_mouse_up(self, event): """ The function called when the mouse is released while on this widget. """ if self.state.startswith('down'): self.state = self.state.replace('down', 'up') e = spyral.Event(name="clicked", widget=self, form=self.form, value=self._get_value()) self.scene._queue_event("form.%(form_name)s.%(widget)s.clicked" % {"form_name": self.form.__class__.__name__, "widget": self.name}, e)
def move_player(self, direction): if not self.player_animation_lock.acquire(False): return #self.player_animation_lock.acquire() #self.player_sprite.stop_all_animations() pos = self.player_sprite.pos tile_height = int(self.renderer.tmx_data.tileheight * self.scale_height) tile_width = int(self.renderer.tmx_data.tilewidth * self.scale_width) if direction == 'down': move_animation = spyral.Animation('y', spyral.easing.Linear(pos.y, pos.y + tile_height), STEP_INTERVAL) new_pos = spyral.Vec2D(pos.x, pos.y + tile_height) elif direction == 'up': move_animation = spyral.Animation('y', spyral.easing.Linear(pos.y, pos.y - tile_height), STEP_INTERVAL) new_pos = spyral.Vec2D(pos.x, pos.y - tile_height) elif direction == 'left': move_animation = spyral.Animation('x', spyral.easing.Linear(pos.x, pos.x - tile_width), STEP_INTERVAL) new_pos = spyral.Vec2D(pos.x - tile_width, pos.y) elif direction == 'right': move_animation = spyral.Animation('x', spyral.easing.Linear(pos.x, pos.x + tile_width), STEP_INTERVAL) new_pos = spyral.Vec2D(pos.x + tile_width, pos.y) try: assert(new_pos.x % self.renderer.tmx_data.tilewidth == 0) assert(new_pos.y % self.renderer.tmx_data.tileheight == 0) except AssertionError: import pdb; pdb.set_trace() properties = self.get_renderer_tile_properties(new_pos) walking_animation = load_walking_animation(self.sprite_file, direction, self.sprite_offset) if self.position_in_scene(new_pos): if properties.get('collision'): collision_event = spyral.Event(pos = pos, new_pos = new_pos) spyral.event.handle('rpg.map.collision', event = collision_event) else: walking_animation = (walking_animation & move_animation) event_name = None if 'x' in walking_animation.properties: event_name = self.player_sprite.__class__.__name__ + '.x.animation.end' elif 'y' in walking_animation.properties: event_name = self.player_sprite.__class__.__name__ + '.y.animation.end' if event_name: def test_function(*args, **kwargs): self.player_animation_lock.release() spyral.event.unregister(event_name, test_function) spyral.event.register(event_name, test_function) else: self.player_animation_lock.release() try: self.player_sprite.animate(walking_animation) except ValueError: if event_name: self.player_animation_lock.release() spyral.event.unregister(event_name, test_function)
def _changed(self): """ Called when this View has changed a visual property, ensuring that the offset and collision box are recomputed; also triggers a `spyral.internal.view.changed` event. """ self._recalculate_offset() self._set_collision_box_tree() # Notify any listeners (probably children) that I have changed changed_event = spyral.Event(name="changed", view=self) spyral.event.handle("spyral.internal.view.changed", changed_event, self.scene)
def _set_state(self, state): old_value = self.value self._state = state if self.value != old_value: e = spyral.Event(name="changed", widget=self, form=self.form, value=self._get_value()) self.scene._queue_event("form.%(form_name)s.%(widget)s.changed" % {"form_name": self.form.__class__.__name__, "widget": self.name}, e) self.button.image = self._images[state] self.mask = spyral.Rect(self.pos, self.button.size) self._on_state_change()
def _set_value(self, value): if self._value is not None: e = spyral.Event(name="changed", widget=self, form=self.form, value=value) self.scene._queue_event("form.%(form_name)s.%(widget)s.changed" % {"form_name": self.form.__class__.__name__, "widget": self.name}, e) self._value = value self._compute_letter_widths() self._cursor_pos = 0#len(value) self._render_text() self._render_cursor()
def _blur(self, widget): """ Queues an event indicating that a widget has lost focus. :param widget: The widget that is losing focus. :type widget: :ref:`Widget <api.widgets>` """ e = spyral.Event(name="blurred", widget=widget, form=self) self.scene._queue_event( "form.%(form_name)s.%(widget)s.blurred" % { "form_name": self.__class__.__name__, "widget": widget.name }, e) widget._handle_blur(e)
def chequea_choque(self, event): if self.x > self.scene.width: self.x = 0 if self.y > self.scene.height: self.y = 0 if self.x < 0: self.x = self.scene.width if self.y < 0: self.y = self.scene.height for i in self.scene.interruptores: if self.collide_sprite(i): if not i.player == self.player: spyral.event.queue( "carrito.choca", spyral.Event(sprite=i, player=self.player)) self.bonus.play()
def stop_animation(self, animation): """ Stops a given animation currently running on this sprite. :param animation: The animation to stop. :type animation: :class:`Animation <spyral.Animation>` """ if animation in self._animations: self._animations.remove(animation) del self._progress[animation] e = spyral.Event(animation=animation, sprite=self) spyral.event.handle( "%s.%s.animation.end" % (self.__class__.__name__, animation.property), e) if len(self._animations) == 0: spyral.event.unregister('director.update', self._run_animations, scene=self._scene())
def _insert_char(self, position, char): """ Insert the given *char* into the text at *position*. Also triggers a form.<name>.<widget>.changed event. """ if position == len(self._value): self._value += char new_width= self.font.get_size(self._value)[0] self._letter_widths.append(new_width) else: self._value = self._value[:position] + char + self._value[position:] self._compute_letter_widths() self._render_text() e = spyral.Event(name="changed", widget=self, form=self.form, value=self._value) self.scene._queue_event("form.%(form_name)s.%(widget)s.changed" % {"form_name": self.form.__class__.__name__, "widget": self.name}, e)
def update_callback(delta): """ A closure for handling events, which includes firing the update related events (e.g., pre_update, update, and post_update). """ global _tick if sugar: while gtk.events_pending(): gtk.main_iteration() if len(pygame.event.get([pygame.VIDEOEXPOSE])) > 0: scene.redraw() scene._handle_event("director.redraw") scene._event_source.tick() events = scene._event_source.get() for event in events: scene._queue_event(*spyral.event._pygame_to_spyral(event)) scene._handle_event("director.pre_update") scene._handle_event("director.update", spyral.Event(delta=delta)) _tick += 1 scene._handle_event("director.post_update")
def replace(scene): """ Replace the currently running scene on the stack with *scene*. Execution will continue after this is called, so make sure you return; otherwise you may find unexpected behavior:: spyral.director.replace(Scene()) print "This will be printed!" return :param scene: The new scene. :type scene: :class:`Scene <spyral.Scene>` """ if _stack: spyral.event.handle('director.scene.exit', scene=_stack[-1]) old = _stack.pop() spyral.sprite._switch_scene() _stack.append(scene) spyral.event.handle('director.scene.enter', event=spyral.Event(scene=scene), scene=scene) # Empty all events! pygame.event.get()
def trigger_fake_mouse_click(pos): ''' Build up a fake mouse event and then send it to be handled. ''' mouse_press_event = spyral.Event(pos=pos, button=1) spyral.event.handle('input.mouse.down', mouse_press_event, my_scene)