I want to implement a real-time movement where player moves towards mouse position. I managed to accomplish it via screens, but it got a bit laggy (sometimes player image doesn't respond to mouse click )
Anyway, I stumbled upon UDD and it seems like a better way for dealing with anything real-time and should be less laggy.
Here's what I've got so far:
Code: Select all
class PlayerWalking(renpy.Displayable): def __init__(self): renpy.Displayable.__init__(self) self.height = 20 self.width = 20 self.sprite = Solid("#ffffff", xsize=self.width, ysize=self.height) self.x = 960 self.y = 540 self.targetx = 0 self.targety = 0 def render(self, width, height, st, at): t = Transform(child=self.sprite, xpos=self.x, ypos=self.y) render = renpy.Render(1920, 1080) child_render = renpy.render(self.sprite, width, height, st, at) x, y = self.x, self.y x -= self.height / 2 y -= self.width / 2 render.blit(child_render, (t.xpos, t.ypos)) return render def event(self, ev, x, y, st): if renpy.map_event(ev, 'mousedown_1'): self.targetx=renpy.get_mouse_pos() self.targety=renpy.get_mouse_pos() distancex = self.x - self.targetx distancey = self.y - self.targety dist = (distancex**2)+(distancey**2) dist = math.sqrt(dist) if dist != 0: speed = dist/5 newx = (self.x - (distancex/dist) * speed) newy = (self.y - (distancey/dist) * speed) self.x = newx self.y = newy renpy.redraw(self, 0) def visit(self): return [ self.sprite ]
I'm definitely doing something wrong here & I'm guessing the correct way to do it is by modifying st / at in render() (?) but I have no idea how since sample codes for UDD are very rare
Can anyone please point me to the right direction?
It's my first time using UDD and hoping to learn, so please bear with me!