Skip to content

Instantly share code, notes, and snippets.

@shilrobot
Created February 28, 2012 03:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shilrobot/1929281 to your computer and use it in GitHub Desktop.
Save shilrobot/1929281 to your computer and use it in GitHub Desktop.
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()
keynums = [
key._0,
key._1,
key._2,
key._3,
key._4,
key._5,
key._6,
key._7,
key._8,
key._9,
]
numpad = [
key.NUM_0,
key.NUM_1,
key.NUM_2,
key.NUM_3,
key.NUM_4,
key.NUM_5,
key.NUM_6,
key.NUM_7,
key.NUM_8,
key.NUM_9,
]
@window.event
def on_key_press(symbol, modifiers):
number = -1
if symbol in keynums:
number = keynums.index(symbol)
elif symbol in numpad:
number = numpad.index(symbol)
if number >= 0:
if number == currval:
print 'CORRECT!'
randomize()
else:
print 'Sorry, wrong. Your answer was: %d' % number
print 'correct was: %d' % currval
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