Skip to content

Instantly share code, notes, and snippets.

@bunyk
Created May 27, 2019 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bunyk/5f423ac35ed8039421170e489895eed0 to your computer and use it in GitHub Desktop.
Save bunyk/5f423ac35ed8039421170e489895eed0 to your computer and use it in GitHub Desktop.
import pyglet
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
window = pyglet.window.Window(
width=WINDOW_WIDTH, height=WINDOW_HEIGHT, caption="Pong!"
)
class Paddle:
def __init__(self, width, height, y):
self.width = width
self.height = height
self.y = y
self.x = 0
def position(self, mouse_x):
x = mouse_x - self.width / 2
x = max(x, 0)
x = min(x, WINDOW_WIDTH - self.width)
self.x = x
def draw(self):
pyglet.graphics.draw(4, pyglet.gl.GL_QUADS, ('v2f', [
self.x, self.y,
self.x + self.width, self.y,
self.x + self.width, self.y + self.height,
self.x, self.y + self.height,
]))
paddle = Paddle(width=100, height=10, y=10)
@window.event
def on_draw():
window.clear()
paddle.draw()
@window.event
def on_mouse_motion(x, y, d, dy):
paddle.position(x)
if __name__ == '__main__':
pyglet.app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment