-
Notifications
You must be signed in to change notification settings - Fork 3
/
braingames.py
executable file
·147 lines (123 loc) · 4.73 KB
/
braingames.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python3
import pyglet
from classifier.game.colormatch import ColorMatchGame
from classifier.game.mathgame import MathGame
from classifier.game.calcgame import CalcGame
from classifier.game.scrabble import Scrabble
from classifier.game.colormeaning import ColorMeaningGame
from classifier.game.dictionarygame import DictionaryGame
from classifier.features import settings, gameover
from classifier.utils.draw import rectangle, color2Array
gms = [ColorMatchGame, ColorMeaningGame,
MathGame, CalcGame, Scrabble, DictionaryGame]
GAMES = []
for i in gms:
GAMES += [{
'name': i.title,
'gameid': i.gameid,
'func': i,
'desc': i.descriptiontext,
'color': i.color
}]
class BrainGames():
'''
The Brain Games home page
'''
def __init__(self):
width = 700
height = 550
self.window = pyglet.window.Window(
width, height, caption='Brain Games')
self.heading = pyglet.text.Label(
'Brain Games', font_size=30, x=width // 2, y=height - 60, anchor_x='center')
self.card0 = Card(GAMES[0]['name'], 0, 30, self.heading.y -
60, GAMES[0]['color'], desc=GAMES[0]['desc'])
self.card1 = Card(GAMES[1]['name'], 1, 30 + self.card0.w +
40, self.card0.y, GAMES[1]['color'], desc=GAMES[1]['desc'])
self.card2 = Card(GAMES[2]['name'], 2, 30, self.card0.y -
self.card0.h - 20, GAMES[2]['color'], desc=GAMES[2]['desc'])
self.card3 = Card(GAMES[3]['name'], 3, 30 + self.card2.w +
40, self.card2.y, GAMES[3]['color'], desc=GAMES[3]['desc'])
self.card4 = Card(GAMES[4]['name'], 4, 30, self.card2.y -
self.card2.h - 20, GAMES[4]['color'], desc=GAMES[4]['desc'])
self.card5 = Card(GAMES[5]['name'], 5, 30 + self.card4.w +
40, self.card4.y, GAMES[5]['color'], desc=GAMES[5]['desc'])
self.option = 'colormatch'
self.clickables = [self.card0, self.card1,
self.card2, self.card3, self.card4, self.card5]
@self.window.event
def on_draw():
self.window.clear()
self.heading.draw()
for i in self.clickables:
i.draw()
@self.window.event
def on_mouse_release(x, y, button, modifiers):
if button != pyglet.window.mouse.LEFT:
return
for i in self.clickables:
if x < i.x or x > (i.x + i.w):
continue
if y > i.y or y < (i.y - i.h):
continue
self.click_event(i.value)
break
@self.window.event
def on_close():
self.selected = -1
def start(self):
pyglet.app.run()
return self.selected
def click_event(self, value):
self.selected = value
self.window.pop_handlers()
self.window.close()
pyglet.app.exit()
class Card():
'''
Card element used in BrainGames homepage
'''
w = 300
h = 100
header_size = 16
def __init__(self, heading, value, x, y, color, desc=''):
self.value = value
self.x = x
self.y = y
if str(type(color)).find('str') > -1:
color = color2Array(color)
self.color = color
self.heading = pyglet.text.Label(heading, x=x + self.w // 2, y=y - 10, anchor_x='center',
anchor_y='top', font_size=self.header_size, font_name='Verdana')
self.desc = pyglet.text.Label(
desc, x=x + 10, y=self.heading.y - 50, width=self.w - 20, multiline=True)
def draw(self):
self.figure = rectangle(self.x, self.y, self.w,
self.h, filled=True, color=self.color)
self.figure.draw()
self.heading.draw()
self.desc.draw()
def getGame(gameid):
'''
Get the game from GAMES array searching through game id
'''
for i in GAMES:
if i['gameid'] == gameid:
return i
if __name__ == '__main__':
while True:
x = BrainGames()
option = x.start()
if option > -1:
while True:
newgame = GAMES[option]['func']()
result = newgame.start()
settings.saveScore(result['gameid'], result['score'])
game = getGame(result['gameid'])
n = gameover.GameOver(game['name'], result['score'], width=result['width'], height=result['height'],
highscore=settings.getHighScore(result['gameid']))
result = n.start()
if result == 0:
break
else:
break