# Bouncing example from class 5, now with sound import sys, random, time, math from OpenGL.GLUT import * from OpenGL.GL import * from OpenGL.GLU import * import os startDir = os.getcwd() + os.sep class Triangle: def __init__(self): self.startTime = time.time() self.x = random.uniform(-1,1) self.y = -1 self.rgb = [random.uniform(0,1), random.uniform(0,1), random.uniform(0,1)] self.boinged = False def update(self, t): self.y = abs(math.sin(t - self.startTime)) * 2 - 1 if self.y < -0.97 and not self.boinged: boingSound.play() self.boinged = True elif self.y > -0.97: self.boinged = False def draw(self): glColor3fv(self.rgb) glBegin(GL_TRIANGLES) glVertex2f(self.x-0.1, self.y-0.1) glVertex2f(self.x+0.1, self.y-0.1) glVertex2f(self.x, self.y+0.1) glEnd() triangles = [Triangle()] def draw(): glClearColor(0,0,0,0) glClear(GL_COLOR_BUFFER_BIT) for t in triangles: t.draw() glutSwapBuffers() def keyboard(key, x, y): if key == chr(27): sys.exit(0) elif key == ' ': triangles.append(Triangle()) def update(v): glutTimerFunc(16, update, 0) currentTime = time.time() for t in triangles: t.update(currentTime) glutPostRedisplay() glutInit([]) glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB) glutInitWindowSize(500, 500) glutInitWindowPosition(0,0) glutCreateWindow(sys.argv[0]) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) glutTimerFunc(0, update, 0) import pygame pygame.mixer.init() boingSound = pygame.mixer.Sound(startDir + "boing.wav") pygame.mixer.music.load("octafish.ogg") pygame.mixer.music.play() pygame.mixer.music.set_volume(0.15) glutMainLoop()