-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathactivity.py
201 lines (166 loc) · 6.53 KB
/
activity.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
# activity.py
# my standard link between sugar and my activity
"""
Copyright (C) 2018 Rahul Bothra
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.
"""
from gettext import gettext as _
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GdkPixbuf
from sugar3.activity import activity
from sugar3.graphics.toolbarbox import ToolbarBox
from sugar3.activity.widgets import ActivityToolbarButton, StopButton
from sugar3.graphics.combobox import ComboBox
from sugar3.graphics.toolbutton import ToolButton
from sugar3.graphics.style import GRID_CELL_SIZE
import pygame
import sugargame.canvas
import CowBulls
class CowBullsActivtiy(activity.Activity):
def __init__(self, handle):
activity.Activity.__init__(self, handle)
# No sharing (Future Improvement)
self.max_participants = 1
# Build the activity toolbar.
toolbox = ToolbarBox()
activity_button = ActivityToolbarButton(self)
toolbox.toolbar.insert(activity_button, 0)
activity_button.show()
comboLabel1 = Gtk.ToolItem()
label1 = Gtk.Label()
label1.set_text(_('Level:') + ' ')
comboLabel1.add(label1)
toolbox.toolbar.insert(comboLabel1, -1)
comboLabel1.show()
label1.show()
comboField = Gtk.ToolItem()
combo = ComboBox()
combo.set_wrap_width(3)
combo.append_item(0, _('Easy'))
combo.append_item(1, _('Moderate'))
combo.append_item(2, _('Hard'))
combo.set_active(0)
comboField.add(combo)
combo.connect('changed', self.change_combo)
toolbox.toolbar.insert(comboField, -1)
comboField.show()
combo.show()
separator = Gtk.SeparatorToolItem()
separator.props.draw = True
separator.set_expand(False)
toolbox.toolbar.insert(separator, -1)
separator.show()
restart = ToolButton('new-game')
toolbox.toolbar.insert(restart, -1)
restart.set_tooltip(_('Restart'))
restart.connect('clicked', self._restart_button_cb)
restart.show()
next_bt = ToolButton('next')
toolbox.toolbar.insert(next_bt, -1)
next_bt.set_tooltip(_('Next Number'))
next_bt.connect('clicked', self._next_button_cb)
next_bt.set_sensitive(False)
next_bt.show()
separator2 = Gtk.SeparatorToolItem()
separator2.props.draw = True
separator2.set_expand(False)
toolbox.toolbar.insert(separator2, -1)
separator2.show()
comboLabel2 = Gtk.ToolItem()
label1 = Gtk.Label()
label1.set_text(_('Score:') + ' ')
comboLabel2.add(label1)
toolbox.toolbar.insert(comboLabel2, -1)
comboLabel2.show()
label1.show()
self._score_image = Gtk.Image()
item = Gtk.ToolItem()
item.add(self._score_image)
toolbox.toolbar.insert(item, -1)
item.show()
separator2 = Gtk.SeparatorToolItem()
separator2.props.draw = False
separator2.set_expand(True)
toolbox.toolbar.insert(separator2, -1)
separator2.show()
stop_button = StopButton(self)
toolbox.toolbar.insert(stop_button, -1)
stop_button.show()
stop_button.connect('clicked', self._stop_cb)
toolbox.show()
self.set_toolbar_box(toolbox)
# Create the game instance.
self.game = CowBulls.CowBulls(parent=self)
# Build the Pygame canvas.
self.game.canvas = self._pygamecanvas = sugargame.canvas.PygameCanvas(
self, main=self.game.run, modules=[pygame.display, pygame.font])
# Note that set_canvas implicitly calls
# read_file when resuming from the Journal.
self.game.set_next_button(next_bt)
self.set_canvas(self._pygamecanvas)
Gdk.Screen.get_default().connect('size-changed',
self.__configure_cb)
def _stop_cb(self, button):
self.game.going = False
def change_combo(self, combo):
level = combo.get_value()
self.game.change_level(int(level) + 3)
def get_preview(self):
return self._pygamecanvas.get_preview()
def __configure_cb(self, event):
''' Screen size has changed '''
pygame.display.set_mode((Gdk.Screen.width(),
Gdk.Screen.height() - GRID_CELL_SIZE),
pygame.RESIZABLE)
self.game.g_init()
self.game.run()
def update_score(self, score):
pixbuf = self._svg_str_to_pixbuf(self._score_icon(score))
self._score_image.set_from_pixbuf(pixbuf)
self._score_image.show()
def _svg_str_to_pixbuf(self, svg_string):
''' Load pixbuf from SVG string '''
pl = GdkPixbuf.PixbufLoader.new_with_type('svg')
pl.write(svg_string.encode())
pl.close()
pixbuf = pl.get_pixbuf()
return pixbuf
def _score_icon(self, score):
return \
'<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n' + \
'<svg\n' + \
'xmlns:dc="http://purl.org/dc/elements/1.1/"\n' + \
'xmlns:cc="http://creativecommons.org/ns#"\n' + \
'xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"\n' + \
'xmlns:svg="http://www.w3.org/2000/svg"\n' + \
'xmlns="http://www.w3.org/2000/svg"\n' + \
'version="1.1"\n' + \
'width="55"\n' + \
'height="55"\n' + \
'viewBox="0 0 55 55">\n' + \
'<path\n' + \
'd="M 27.497,50.004 C 39.927,50.004 50,39.937 50,27.508 50,'\
'15.076 39.927,4.997 27.497,4.997 15.071,4.997 5, \
15.076 5,27.508 '\
'5,39.937 15.071,50.004 27.497,50.004 z"\n' + \
'style="fill:#ffffff;fill-opacity:1" /><text\n' + \
'style=" \
fill:#000000;fill-opacity:1;stroke:none;font-family:Sans">'\
'<tspan\n' + \
'x="27.5"\n' + \
'y="37.3"\n' + \
'style="font-size:24px;text-align:center;text-anchor:middle">'\
'%d' % score + \
'</tspan></text>\n' + \
'</svg>'
def _restart_button_cb(self, event):
self.game.restart()
def _next_button_cb(self, event):
self.game.nextRound()