Merge pull request #710 from sheenarbw/mouse_sprites

Added sprite that uses mouse as a target
This commit is contained in:
Piper Thunstrom 2024-04-08 16:49:43 -07:00 committed by GitHub
commit 108cef5f58
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 0 deletions

View file

@ -11,6 +11,7 @@ import math
from ppb import keycodes
from ppb.events import KeyPressed, KeyReleased
from dataclasses import dataclass
import ppb.events as events
class TargetSprite(ppb.Sprite):
@ -131,3 +132,15 @@ class KeyBoardMovementSprite(ppb.Sprite):
self.direction += ppb.Vector(0, -1)
if key_event.key == self.key_bindings.down:
self.direction += ppb.Vector(0, 1)
class MouseTargetSprite(TargetSprite):
"""Sprite that treats your mouse as a moving target.
:param speed: Distance per second that the sprite travels with linear motion.
If you set the speed to a high number (eg 100) then the sprite can act as a cursor. And if you set it to a lower number then it will chase the mouse menacingly
"""
def on_mouse_motion(self, event: events.MouseMotion, signal):
self.target = event.position

View file

@ -4,7 +4,9 @@ from ppb.features.default_sprites import (
TargetSprite,
wasd_direction_key_bindings,
KeyBoardMovementSprite,
MouseTargetSprite
)
import ppb.events as events
def test_target_sprite_linear():
@ -134,3 +136,13 @@ def test_keyboard_movement_sprite_move_down_left_wasd():
assert keyboard_sprite.direction.isclose((-1, -1))
assert keyboard_sprite.position.isclose((-1, -1))
def test_mouse_target_sprite():
sprite = MouseTargetSprite()
mouse_position = Vector(123,456)
sprite.on_mouse_motion(events.MouseMotion(mouse_position,Vector(0,0),buttons=[]), lambda x:None)
assert sprite.target == mouse_position