Search code examples
pythongeometrycursorpyglet

PyGlet Shape Following The Mouse


So I try to make a "game" with PyGlet:

This is my code:

import pyglet
from pyglet import shapes

window = pyglet.window.Window(800, 600, "PyGlet Window")
circle = shapes.Circle(x = 100, y = 100, radius = 13, color=(255, 255, 255))

def callback(dt):
    pass

pyglet.clock.schedule_interval(callback, 0.5)

@window.event
def on_draw():
    window.clear()
    circle.draw()

pyglet.app.run()

How to make the circle follow the mouse? Thanks!


Solution

  • Implement the on_mouse_motion event (see Working with the mouse) and change the position of the shape (see pyglet.shapes):

    @window.event
    def on_mouse_motion(x, y, dx, dy):
        circle.x = x
        circle.y = y