from math import radians, cos, sin
import pyglet
from pyglet.gl import *
from win32api import GetSystemMetrics
class Ball():
x = 0
y = 0
color = (0, 0, 0)
radius = 5
class PrimaryWindow(pyglet.window.Window):
FPS = 60
smoothConfig = Config(sample_buffers=1, samples=4, depth_size=16, double_buffer=True)
screenWidth = GetSystemMetrics(0)
screenHeight = GetSystemMetrics(1)
windowWidthPixels = int(screenWidth/2)
windowHeightPixels = int(screenHeight/2)
ballRadiusPixels = 20
ballVelocity = 10
ballColor = (0, 128 / 255, 255 / 255, 1)
def __init__(self):
super(PrimaryWindow, self).__init__(config=self.smoothConfig)
self.set_size(self.windowWidthPixels, self.windowHeightPixels)
self.set_location(int(self.screenWidth/2 - self.windowWidthPixels/2), int(self.screenHeight/2 - self.windowHeightPixels/2))
self.ball = Ball()
self.ball.x = self.windowWidthPixels/2
self.ball.y = self.windowHeightPixels/2
self.ball.radius = self.ballRadiusPixels
self.ball.color = self.ballColor
pyglet.clock.schedule_interval(self.update, 1.0 / self.FPS)
pyglet.app.run()
def on_draw(self):
self.clear()
verts = []
for i in range(100):
angle = radians(float(i)/100*360.0)
x = self.ball.radius*cos(angle) + self.ball.x
y = self.ball.radius*sin(angle) + self.ball.y
verts += [x, y]
circle = pyglet.graphics.vertex_list(100, ('v2f', verts))
glClear(pyglet.gl.GL_COLOR_BUFFER_BIT)
glColor3f(self.ball.color[0], self.ball.color[1], self.ball.color[2])
circle.draw(GL_LINE_LOOP)
def update(self, dt):
self.ball.x += self.ballVelocity
if self.ball.x > self.windowWidthPixels or self.ball.x < 0:
self.ballVelocity *= -1
if __name__ == "__main__":
PrimaryWindow()