Created
May 6, 2015 14:11
-
-
Save ccanepa/6749680b7fd8f87bddec to your computer and use it in GitHub Desktop.
simplified pyglet's noisy.py for testing purposes
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
'''Simplified noisy.py without sound, text or events to diagnose basic problems with sprites, | |
shows ten bouncing balls | |
''' | |
import os | |
import random | |
import sys | |
from pyglet.gl import * | |
import pyglet | |
BALL_IMAGE = 'ball.png' | |
class Ball(pyglet.sprite.Sprite): | |
ball_image = pyglet.resource.image(BALL_IMAGE) | |
width = ball_image.width | |
height = ball_image.height | |
def __init__(self): | |
x = random.random() * (window.width - self.width) | |
y = random.random() * (window.height - self.height) | |
super(Ball, self).__init__(self.ball_image, x, y, batch=balls_batch) | |
self.dx = (random.random() - 0.5) * 1000 | |
self.dy = (random.random() - 0.5) * 1000 | |
def update(self, dt): | |
if self.x <= 0 or self.x + self.width >= window.width: | |
self.dx *= -1 | |
if self.y <= 0 or self.y + self.height >= window.height: | |
self.dy *= -1 | |
self.x += self.dx * dt | |
self.y += self.dy * dt | |
self.x = min(max(self.x, 0), window.width - self.width) | |
self.y = min(max(self.y, 0), window.height - self.height) | |
window = pyglet.window.Window(640, 480) | |
@window.event | |
def on_draw(): | |
window.clear() | |
balls_batch.draw() | |
def update(dt): | |
for ball in balls: | |
ball.update(dt) | |
pyglet.clock.schedule_interval(update, 1/30.) | |
balls_batch = pyglet.graphics.Batch() | |
balls = [Ball() for i in range(10)] | |
if __name__ == '__main__': | |
pyglet.app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment