Created
May 27, 2019 17:15
-
-
Save bunyk/5f423ac35ed8039421170e489895eed0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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