forked from sugarlabs/recall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
548 lines (485 loc) · 20 KB
/
game.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# -*- coding: utf-8 -*-
#Copyright (c) 2012 Walter Bender
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# You should have received a copy of the GNU General Public License
# along with this library; if not, write to the Free Software
# Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
'''
Three different games:
(0) find the repeated image
(1) find the image not shown in the collection
(2) recall the image shown previously
'''
from gi.repository import Gtk
from gi.repository import GObject
from gi.repository import Gdk
from gi.repository import GdkPixbuf
import cairo
import os
import glob
from random import uniform
from gettext import gettext as _
from gettext import ngettext
import logging
_logger = logging.getLogger('search-activity')
try:
from sugar3.graphics import style
GRID_CELL_SIZE = style.GRID_CELL_SIZE
except ImportError:
GRID_CELL_SIZE = 0
DOT_SIZE = 40
from sprites import Sprites, Sprite
class Game():
def __init__(self, canvas, parent=None, path=None,
colors=['#A0FFA0', '#FF8080']):
self._canvas = canvas
self._parent = parent
self._parent.show_all()
self._path = path
self._colors = ['#FFFFFF']
self._colors.append(colors[0])
self._colors.append(colors[1])
self._canvas.set_can_focus(True);
self._canvas.connect("expose-event", self._expose_cb)
self._canvas.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
self._canvas.connect("button-press-event", self._button_press_cb)
self._width = Gdk.Screen.width()
self._height = Gdk.Screen.height() - (GRID_CELL_SIZE * 1.5)
self._scale = self._height / (4 * DOT_SIZE * 1.3)
self._dot_size = int(DOT_SIZE * self._scale)
self._space = int(self._dot_size / 5.)
self.we_are_sharing = False
self._start_time = 0
self._timeout_id = None
self._level = 3
self._game = 0
self._correct = 0
# Find the image files
self._PATHS = glob.glob(os.path.join(self._path, 'images', '*.png'))
self._CPATHS = glob.glob(
os.path.join(self._path, 'color-images', '*.svg'))
# Generate the sprites we'll need...
self._sprites = Sprites(self._canvas)
self._dots = []
self._opts = []
yoffset = int(self._space / 2.)
self._line = Sprite(
self._sprites, 0,
int(3 * (self._dot_size + self._space) + yoffset / 2.),
self._line(vertical=False))
for y in range(3):
for x in range(6):
xoffset = int((self._width - 6 * self._dot_size - \
5 * self._space) / 2.)
self._dots.append(
Sprite(self._sprites,
xoffset + x * (self._dot_size + self._space),
y * (self._dot_size + self._space) + yoffset,
self._new_dot_surface(color=self._colors[0])))
self._dots[-1].type = -1 # No image
self._dots[-1].set_label_attributes(72)
y = 3
for x in range(3):
self._opts.append(
Sprite(self._sprites,
xoffset + x * (self._dot_size + self._space),
y * (self._dot_size + self._space) + yoffset,
self._new_dot_surface(color=self._colors[0])))
self._opts[-1].type = -1 # No image
self._opts[-1].set_label_attributes(72)
self._opts[-1].hide()
def _all_clear(self):
''' Things to reinitialize when starting up a new game. '''
if self._timeout_id is not None:
GObject.source_remove(self._timeout_id)
# Auto advance levels
if self._correct > 3 and self._level < len(self._dots):
self._level += 3
self._correct = 0
self._set_label('')
for i in range(3):
self._opts[i].hide()
self._opts[i].type = -1
self._opts[i].set_label('')
for dot in self._dots:
dot.type = -1
if self._game == 2 or self._dots.index(dot) < self._level:
dot.set_shape(self._new_dot_surface(
self._colors[abs(dot.type)]))
dot.set_label('?')
dot.set_layer(100)
else:
dot.hide()
self._dance_counter = 0
self._dance_step()
def _dance_step(self):
''' Short animation before loading new game '''
if self._game == 2:
for i in range(len(self._dots)):
self._dots[i].set_shape(self._new_dot_surface(
self._colors[int(uniform(0, 3))]))
else:
for i in range(self._level):
self._dots[i].set_shape(self._new_dot_surface(
self._colors[int(uniform(0, 3))]))
self._dance_counter += 1
if self._dance_counter < 10:
self._timeout_id = GObject.timeout_add(500, self._dance_step)
else:
self._new_game()
def new_game(self, game=None, restart=True):
''' Start a new game. '''
if game is not None:
self._game = game
self._level = 3
self._correct = 0
if restart:
self._all_clear()
def _image_in_dots(self, n):
for i in range(self._level):
if self._dots[i].type == n:
return True
return False
def _image_in_opts(self, n):
for i in range(3):
if self._opts[i].type == n:
return True
return False
def _choose_random_images(self):
''' Choose images at random '''
if self._game == 3:
maxi = len(self._CPATHS)
else:
maxi = len(self._PATHS)
for i in range(self._level):
if self._dots[i].type == -1:
n = int(uniform(0, maxi))
while self._image_in_dots(n):
n = int(uniform(0, maxi))
self._dots[i].type = n
if self._game == 3:
self._dots[i].set_shape(self._new_dot_surface(
color_image=self._dots[i].type))
else:
self._dots[i].set_shape(self._new_dot_surface(
image=self._dots[i].type))
self._dots[i].set_layer(100)
self._dots[i].set_label('')
def _load_image_from_list(self):
if self._recall_counter == len(self._recall_list):
self._timeout_id = GObject.timeout_add(
1000, self._ask_the_question)
return
for dot in self._dots:
dot.type = self._recall_list[self._recall_counter]
dot.set_shape(self._new_dot_surface(image=dot.type))
dot.set_layer(100)
dot.set_label('')
self._recall_counter += 1
self._timeout_id = GObject.timeout_add(
1000, self._load_image_from_list)
def _find_repeat(self):
''' Find an image that repeats '''
for i in range(self._level):
for j in range(self._level - i - 1):
if self._dots[i].type == self._dots[j].type:
return i
return None
def _new_game(self, restore=False):
''' Load game images and then ask a question... '''
if self._game in [0, 1, 3]:
self._choose_random_images()
else: # game 2
# generate a random list
self._recall_list = []
for i in range(12):
n = int(uniform(0, len(self._PATHS)))
while n in self._recall_list:
n = int(uniform(0, len(self._PATHS)))
self._recall_list.append(n)
self._recall_counter = 0
self._load_image_from_list()
if self._game == 0:
if not restore:
# Repeat at least one of the images
self._repeat = int(uniform(0, self._level))
n = (self._repeat + int(uniform(1, self._level))) % self._level
_logger.debug('repeat=%d, n=%d' % (self._repeat, n))
self._dots[self._repeat].set_shape(self._new_dot_surface(
image=self._dots[n].type))
self._dots[self._repeat].type = self._dots[n].type
else: # Find repeated image, as that is the answer
self._repeat = self._find_repeat()
if self._repeat is None:
_logger.debug('could not find repeat')
self._repeat = 0
if self.we_are_sharing:
_logger.debug('sending a new game')
self._parent.send_new_game()
if self._game in [0, 1, 3]:
self._timeout_id = GObject.timeout_add(
3000, self._ask_the_question)
def _ask_the_question(self):
''' Each game has a challenge '''
self._timeout_id = None
# Hide the dots
if self._game == 2:
for dot in self._dots:
dot.hide()
else:
for i in range(self._level):
self._dots[i].hide()
if self._game == 0:
self._set_label(_('Recall which image was repeated.'))
# Show the possible solutions
for i in range(3):
n = int(uniform(0, len(self._PATHS)))
if self._level == 3:
while(n == self._dots[self._repeat].type or \
self._image_in_opts(n)):
n = int(uniform(0, len(self._PATHS)))
else:
while(n == self._dots[self._repeat].type or \
not self._image_in_dots(n) or \
self._image_in_opts(n)):
n = int(uniform(0, len(self._PATHS)))
self._opts[i].type = n
self._answer = int(uniform(0, 3))
self._opts[self._answer].type = self._dots[self._repeat].type
for i in range(3):
self._opts[i].set_shape(self._new_dot_surface(
image=self._opts[i].type))
self._opts[i].set_layer(100)
elif self._game == 1:
self._set_label(_('Recall which image was not shown.'))
# Show the possible solutions
for i in range(3):
n = int(uniform(0, len(self._PATHS)))
while(not self._image_in_dots(n) or \
self._image_in_opts(n)):
n = int(uniform(0, len(self._PATHS)))
self._opts[i].type = n
self._answer = int(uniform(0, 3))
n = int(uniform(0, len(self._PATHS)))
while(self._image_in_dots(n)):
n = int(uniform(0, len(self._PATHS)))
self._opts[self._answer].type = n
for i in range(3):
self._opts[i].set_shape(self._new_dot_surface(
image=self._opts[i].type))
self._opts[i].set_layer(100)
elif self._game == 3:
self._set_label(_('Recall which image was not shown.'))
# Show the possible solutions
for i in range(3):
n = int(uniform(0, len(self._CPATHS)))
while(not self._image_in_dots(n) or \
self._image_in_opts(n)):
n = int(uniform(0, len(self._CPATHS)))
self._opts[i].type = n
self._answer = int(uniform(0, 3))
n = int(uniform(0, len(self._CPATHS)))
while(self._image_in_dots(n)):
n = int(uniform(0, len(self._CPATHS)))
self._opts[self._answer].type = n
for i in range(3):
self._opts[i].set_shape(self._new_dot_surface(
color_image=self._opts[i].type))
self._opts[i].set_layer(100)
elif self._game == 2:
self._set_label(ngettext(
'Recall which image was displayed %d time ago',
'Recall which image was displayed %d times ago',
(int(self._level / 3))) % \
(int(self._level / 3)))
# Show the possible solutions
for i in range(3):
self._answer = len(self._recall_list) - int(self._level / 3) - 1
n = int(uniform(0, len(self._recall_list)))
while n == self._answer:
n = int(uniform(0, len(self._recall_list)))
self._opts[i].type = n
i = int(uniform(0, 3))
self._opts[i].type = self._recall_list[self._answer]
for i in range(3):
self._opts[i].set_shape(self._new_dot_surface(
image=self._opts[i].type))
self._opts[i].set_layer(100)
def restore_game(self, dot_list, correct=0, level=3, game=0):
''' Restore a game from the Journal or share '''
# TODO: Save/restore recall list for game 2
self._correct = correct
self._level = level
self._game = game
for i, dot in enumerate(dot_list):
self._dots[i].type = dot
if dot == -1:
self._dots[i].hide()
self._new_game(restore=True)
def save_game(self):
''' Return dot list for saving to Journal or sharing '''
dot_list = []
for dot in self._dots:
dot_list.append(dot.type)
return dot_list, self._correct, self._level, self._game
def _set_label(self, string):
''' Set the label in the toolbar or the window frame. '''
self._parent.status.set_label(string)
def _button_press_cb(self, win, event):
if self._timeout_id is not None:
_logger.debug('still in timeout... ignoring click')
return
win.grab_focus()
x, y = map(int, event.get_coords())
spr = self._sprites.find_sprite((x, y), inverse=True)
if spr == None:
return
if self._game in [0, 1, 3]:
for i in range(3):
if self._opts[i] == spr:
break
self._opts[i].set_shape(self._new_dot_surface(
color=self._colors[0]))
if i == self._answer:
self._opts[i].set_label('☻')
self._correct += 1
else:
self._opts[i].set_label('☹')
self._correct = 0
else:
for i in range(3):
if self._opts[i] == spr:
break
self._opts[i].set_shape(self._new_dot_surface(
color=self._colors[0]))
if self._opts[i].type == self._recall_list[self._answer]:
self._opts[i].set_label('☻')
self._correct += 1
else:
self._opts[i].set_label('☹')
self._correct = 0
if self._game in [0, 1, 3]:
for i in range(self._level):
self._dots[i].set_layer(100)
else:
for dot in self._dots:
dot.set_shape(self._new_dot_surface(
image=self._recall_list[self._answer]))
dot.set_layer(100)
if self._correct == 0:
self._timeout_id = GObject.timeout_add(5000, self.new_game)
else:
self._timeout_id = GObject.timeout_add(3000, self.new_game)
return True
def remote_button_press(self, dot, color):
''' Receive a button press from a sharer '''
self._dots[dot].type = color
self._dots[dot].set_shape(self._new_dot_surface(
color=self._colors[color]))
def set_sharing(self, share=True):
_logger.debug('enabling sharing')
self.we_are_sharing = share
def _expose_cb(self, win, event):
self.do_expose_event(event)
def do_expose_event(self, event):
''' Handle the expose-event by drawing '''
# Restrict Cairo to the exposed area
cr = self._canvas.get_window().cairo_create()
cr.rectangle(event.area.x, event.area.y,
event.area.width, event.area.height)
cr.clip()
# Refresh sprite list
self._sprites.redraw_sprites(cr=cr)
def _destroy_cb(self, win, event):
Gtk.main_quit()
def _new_dot_surface(self, color='#000000', image=None, color_image=None):
''' generate a dot of a color color '''
self._dot_cache = {}
if color_image is not None:
if color_image + 10000 in self._dot_cache:
return self._dot_cache[color_image + 10000]
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(
os.path.join(self._path, self._CPATHS[color_image]),
self._svg_width, self._svg_height)
elif image is not None:
if image in self._dot_cache:
return self._dot_cache[image]
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(
os.path.join(self._path, self._PATHS[image]),
self._svg_width, self._svg_height)
else:
if color in self._dot_cache:
return self._dot_cache[color]
self._stroke = color
self._fill = color
self._svg_width = self._dot_size
self._svg_height = self._dot_size
i = self._colors.index(color)
pixbuf = svg_str_to_pixbuf(
self._header() + \
self._circle(self._dot_size / 2., self._dot_size / 2.,
self._dot_size / 2.) + \
self._footer())
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
self._svg_width, self._svg_height)
context = cairo.Context(surface)
context = Gdk.CairoContext(context)
context.set_source_pixbuf(pixbuf, 0, 0)
context.rectangle(0, 0, self._svg_width, self._svg_height)
context.fill()
if color_image is not None:
self._dot_cache[color_image + 10000] = surface
elif image is not None:
self._dot_cache[image] = surface
else:
self._dot_cache[color] = surface
return surface
def _line(self, vertical=True):
''' Generate a center line '''
if vertical:
self._svg_width = 3
self._svg_height = self._height
return svg_str_to_pixbuf(
self._header() + \
self._rect(3, self._height, 0, 0) + \
self._footer())
else:
self._svg_width = self._width
self._svg_height = 3
return svg_str_to_pixbuf(
self._header() + \
self._rect(self._width, 3, 0, 0) + \
self._footer())
def _header(self):
return '<svg\n' + 'xmlns:svg="http://www.w3.org/2000/svg"\n' + \
'xmlns="http://www.w3.org/2000/svg"\n' + \
'xmlns:xlink="http://www.w3.org/1999/xlink"\n' + \
'version="1.1"\n' + 'width="' + str(self._svg_width) + '"\n' + \
'height="' + str(self._svg_height) + '">\n'
def _rect(self, w, h, x, y):
svg_string = ' <rect\n'
svg_string += ' width="%f"\n' % (w)
svg_string += ' height="%f"\n' % (h)
svg_string += ' rx="%f"\n' % (0)
svg_string += ' ry="%f"\n' % (0)
svg_string += ' x="%f"\n' % (x)
svg_string += ' y="%f"\n' % (y)
svg_string += 'style="fill:#000000;stroke:#000000;"/>\n'
return svg_string
def _circle(self, r, cx, cy):
return '<circle style="fill:' + str(self._fill) + ';stroke:' + \
str(self._stroke) + ';" r="' + str(r - 0.5) + '" cx="' + \
str(cx) + '" cy="' + str(cy) + '" />\n'
def _footer(self):
return '</svg>\n'
def svg_str_to_pixbuf(svg_string):
""" Load pixbuf from SVG string """
pl = GdkPixbuf.PixbufLoader('svg')
pl.write(svg_string)
pl.close()
pixbuf = pl.get_pixbuf()
return pixbuf