Created
October 8, 2013 03:57
-
-
Save davepape/6879258 to your computer and use it in GitHub Desktop.
fading an object in and out by changing its alpha value over time
This file contains hidden or 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 time, math | |
| from pyglet.gl import * | |
| window = pyglet.window.Window() | |
| class AlphaSquare: | |
| def __init__(self, color): | |
| self.xpos = 0 | |
| self.ypos = 0 | |
| self.color = color | |
| self.vlist = pyglet.graphics.vertex_list(4, ('v2f', [-128,-128, 128,-128, -128,128, 128,128])) | |
| def draw(self): | |
| glEnable(GL_BLEND) | |
| glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) | |
| glColor4f(self.color[0],self.color[1],self.color[2],self.color[3]) | |
| glPushMatrix() | |
| glTranslatef(self.xpos, self.ypos, 0) | |
| self.vlist.draw(GL_TRIANGLE_STRIP) | |
| glPopMatrix() | |
| glDisable(GL_BLEND) | |
| solidSquare = AlphaSquare([1, 1, 1, 1]) | |
| solidSquare.xpos = 320 | |
| solidSquare.ypos = 240 | |
| transpSquare = AlphaSquare([1, 0, 0, 0.5]) | |
| transpSquare.xpos = 320 | |
| transpSquare.ypos = 240 | |
| @window.event | |
| def on_draw(): | |
| global transpSquare, solidSquare | |
| glClearColor(0, 0.3, 0.5, 0) | |
| glClear(GL_COLOR_BUFFER_BIT) | |
| glLoadIdentity() | |
| solidSquare.draw() | |
| transpSquare.draw() | |
| def update(dt): | |
| global transpSquare | |
| transpSquare.xpos = 320 + 128 * math.sin(time.time()) | |
| transpSquare.ypos = 240 + 128 * math.cos(time.time()) | |
| transpSquare.color[3] = (1+math.sin(time.time()))/2.0 | |
| pyglet.clock.schedule_interval(update,1/60.0) | |
| pyglet.app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment