This repository has been archived by the owner on Jul 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
beer_goggles.py
131 lines (99 loc) · 4.14 KB
/
beer_goggles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python
'''This module provides a number of unscientific tests that can be used to
determine if a user is under the influence of mind-effecting substances
such as alcohol.
The first is simple math.
The second is more complex math.
The third is shape recognition.
The fourth is hand-eye coordination. (Clicking on moving objects.)
'''
from __future__ import division
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import SIGNAL, SLOT
import random
import time
class BeerGogglesMath(QtGui.QWidget):
def __init__(self, parent = None, simple=False, complex=False):
QtGui.QWidget.__init__(self, parent)
self.setLayout(QtGui.QVBoxLayout())
self.equationlabel = QtGui.QWidget()
self.equationlabel.setLayout(QtGui.QHBoxLayout())
self.layout().addWidget(self.equationlabel)
self.solvebutton = QtGui.QPushButton("Solve!")
self.layout().addWidget(self.solvebutton)
self.solvebutton.connect(self.solvebutton, SIGNAL("clicked()"), self.solve)
if simple:
self.build_equation(self.simple_equation())
elif complex:
self.build_equation(self.complex_equation())
self.starttime = time.time()
self.finishtime = False
self.correct = None
def showEvent(self, event):
self.equationanswer.setFocus()
event.ignore()
def solve(self):
self.finishtime = time.time()
self.solvebutton.setEnabled(False)
givenanswer = str(self.equationanswer.text())
if (givenanswer) == "":
givenanswer = "0"
givenanswer = eval(givenanswer)
realanswer = eval(self.equation)
if givenanswer != realanswer:
print "WRONG", realanswer
self.emit(SIGNAL("BeerGogglesWrongAnswer"), self.finishtime, givenanswer, realanswer)
self.correct = False
self.solvebutton.setText("Wrong! D:")
else:
self.emit(SIGNAL("BeerGogglesRightAnswer"), self.finishtime, realanswer)
print "RIGHT :D"
self.correct = True
self.solvebutton.setText("Right! :D")
print "Answered in", self.finishtime-self.starttime, "seconds."
def build_equation(self, equation):
self.equation = equation
#TODO: Move the font size to an external stylesheet
f = QtGui.QFont()
f.setPointSize(60)
equation = equation.replace("*", u"\u00D7")
equation = equation.replace("/", u"\u00F7")
equatlabel = QtGui.QLabel(equation+"=")
equatlabel.setFont(f)
self.equationlabel.layout().addWidget(equatlabel)
self.equationanswer = QtGui.QLineEdit()
self.equationanswer.connect(self.equationanswer, SIGNAL("returnPressed()"), self.solve)
answerlen = len(str(eval(self.equation)))
self.equationanswer.setMaxLength(answerlen)
#self.equationanswer.setInputMask("#"*answerlen)
self.equationanswer.setFont(f)
fm = QtGui.QFontMetrics(f)
pixelWidth = fm.width("0"*answerlen)
self.equationanswer.setFixedWidth(pixelWidth + 10)
self.equationlabel.layout().addWidget(self.equationanswer)
def simple_equation(self):
x = random.randint(1, 9)
y = random.randint(1, 9)
op = random.choice(("+", "-"))
equation = "%d%s%d" % (x, op, y)
return equation
def complex_equation(self):
equation = "0.1"
while type(eval(equation)) is float:
x = random.randint(1, 9)
y = random.randint(1, 9)
op = "*" #Would have / here as a choice, but there are only a few combinations that are whole numbers
equation = "%d%s%d" % (x, op, y)
return equation
def main():
import signal
import sys
signal.signal(signal.SIGINT, signal.SIG_DFL)
app = QtGui.QApplication(sys.argv)
s = BeerGogglesMath(simple=True)
s.show()
c = BeerGogglesMath(complex=True)
c.show()
app.exec_()
if __name__ == '__main__':
main()