I'm trying to create a square that forms around the drag of a mouse (like the one that appears when dragging on the desktop, for example.) THis is the code I've tried:
import pyglet
from pyglet.window import mouse
window = pyglet.window.Window()
@window.event
def on_draw():
window.clear()
@window.event
def on_mouse_drag(x, y, dx, dy, buttons, modifiers):
pyglet.graphics.draw(4, pyglet.gl.GL_QUADS, ('v2f', [x, y, dx, y, dx, dy, x, dy]))
pyglet.app.run()
However, it's not working, and I can't see why. Any advice?
So, as there hasn't been an answer, this is how I solved the problem:
import pyglet
from pyglet.window import mouse
window = pyglet.window.Window()
@window.event
def on_draw():
pass
@window.event
def on_mouse_drag(x, y, dx, dy, buttons, modifiers):
pyglet.graphics.draw(4, pyglet.gl.GL_QUADS, ('v2f', [x, y, x-dx, y, x-dx, y-dy, x, y-dy]))
print x, y, dx, y, dx, dy, x, dy
pyglet.app.run()
Now I just need to work out how to destroy the rectangles...
I started with your solution and improved it a bit. The rectangle artifacting problem is fixed by adding an on_draw function, and in that function you clear the window and then re-draw every object. Pyglet (OpenGL) uses double-buffering, so this works a lot faster than it sounds.
import pyglet
# rectangle class
class Rect:
def __init__(self, x, y, w, h):
self.set(x, y, w, h)
def draw(self):
pyglet.graphics.draw(4, pyglet.gl.GL_QUADS, self._quad)
def set(self, x=None, y=None, w=None, h=None):
self._x = self._x if x is None else x
self._y = self._y if y is None else y
self._w = self._w if w is None else w
self._h = self._h if h is None else h
self._quad = ('v2f', (self._x, self._y,
self._x + self._w, self._y,
self._x + self._w, self._y + self._h,
self._x, self._y + self._h))
def __repr__(self):
return f"Rect(x={self._x}, y={self._y}, w={self._w}, h={self._h})"
# main function
def main():
r1 = Rect(10, 10, 100, 100)
window = pyglet.window.Window()
@window.event
def on_draw():
window.clear()
r1.draw()
@window.event
def on_mouse_press(x, y, button, modifiers):
r1.set(x=x, y=y)
print(r1)
@window.event
def on_mouse_drag(x, y, dx, dy, buttons, modifiers):
r1.set(x=x, y=y)
print(r1)
pyglet.app.run()
if __name__ == '__main__':
main()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With