Skip to content

Instantly share code, notes, and snippets.

@barbeque
Forked from shilrobot/resistor_code.py
Created February 28, 2012 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barbeque/1929283 to your computer and use it in GitHub Desktop.
Save barbeque/1929283 to your computer and use it in GitHub Desktop.
cleaned up keyboard binding
colors = [
(0,0,0),
(150,75,0),
(255,0,0),
(255,165,0),
(255,255,0),
(154,205,50),
(100,149,237),
(238,130,238),
(160,160,160),
(255,255,255)
]
import pyglet
from pyglet.window import key
from pyglet.gl import *
import random
window = pyglet.window.Window()
currval = -1
def randomize():
global currval
while True:
new = random.randint(0,len(colors)-1)
if new != currval:
break
currval = new
randomize()
@window.event
def on_draw():
glClearColor(0,0,0,0)
glClear(GL_COLOR_BUFFER_BIT)
glDisable(GL_DEPTH_TEST)
glDisable(GL_CULL_FACE)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0,640,480,0,-10,10)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
x0 = 320-25
x1 = x0+50
y0 = 240-25
y1 = y0+50
glBegin(GL_QUADS)
r,g,b = colors[currval]
glColor3f(r/255.0, g/255.0, b/255.0)
glVertex3f(x0,y0,0)
glVertex3f(x1,y0,0)
glVertex3f(x1,y1,0)
glVertex3f(x0,y1,0)
glEnd()
@window.event
def on_key_press(symbol, modifiers):
number = -1
if symbol >= key._0 and symbol <= key._9:
number = symbol - key._0
print number
elif symbol >= key.NUM_0 and symbol <= key.NUM_9:
number = symbol - key.NUM_0
if number >= 0:
if number == currval:
print 'CORRECT!'
randomize()
else:
print 'Sorry, wrong. Your answer was: %d' % number
print 'correct was: %d' % currval
elif symbol == key.ESCAPE:
print 'Goodbye'
else:
print 'Press a number key'
pyglet.app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment