Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/activity.py
blob: b680d95a6ddb63d881ed590118f1bf8685796f69 (plain)
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
# Copyright 2013 Carlos Carvallo
#
# 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 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

"""Chutar Activity: A case study for developing an activity."""

import gtk, gobject, random, pango, logging

DELAY = 8
WIDTH = 766
HEIGHT = 418

from gettext import gettext as _

from sugar.activity import activity
from sugar.graphics.toolbarbox import ToolbarBox
from sugar.activity.widgets import ActivityButton
from sugar.activity.widgets import ActivityToolbox
from sugar.activity.widgets import TitleEntry
from sugar.activity.widgets import StopButton
from sugar.activity.widgets import ShareButton

class ChutarActivity(activity.Activity):
    """ChutarActivity class as specified in activity.info"""

    def __init__(self, handle):
        """Set up the Chutar activity."""
        activity.Activity.__init__(self, handle)

        # we do not have collaboration features
        # make the share option insensitive
        self.max_participants = 1

        # toolbar with the new toolbar redesign
        toolbar_box = ToolbarBox()

        activity_button = ActivityButton(self)
        toolbar_box.toolbar.insert(activity_button, 0)
        activity_button.show()

        title_entry = TitleEntry(self)
        toolbar_box.toolbar.insert(title_entry, -1)
        title_entry.show()

        share_button = ShareButton(self)
        toolbar_box.toolbar.insert(share_button, -1)
        share_button.show()
        
        separator = gtk.SeparatorToolItem()
        separator.props.draw = False
        separator.set_expand(True)
        toolbar_box.toolbar.insert(separator, -1)
        separator.show()

        stop_button = StopButton(self)
        toolbar_box.toolbar.insert(stop_button, -1)
        stop_button.show()

        self.set_toolbar_box(toolbar_box)
        toolbar_box.show()

        hbox = gtk.HBox()
        vbox = gtk.VBox()
        
        #se usaran para el marcador
        self.label = gtk.Label()
        self.label.modify_font(pango.FontDescription("Purisa 14"))
        frame = gtk.Frame("Marcador")
       
        #asignacion de variables a widgest de gtk
        self.vadjustment = gtk.Adjustment()
        self.hadjustment = gtk.Adjustment()
        
        #asignacion de valores por defecto del adjustment
        self.vadjustment.set_upper(100)
        self.vadjustment.set_value(0)
        self.hadjustment.set_upper(100)
        self.hadjustment.set_value(0)
        
        #asignacion de propiedades de widgets
        self.vslide = gtk.VScale(self.vadjustment)
        self.hslide = gtk.HScale(self.hadjustment)
        
        #propiedad para que no se muestre valor del slide
        self.vslide.set_draw_value(False)
        self.hslide.set_draw_value(False)
        
        #variables globales que se utilizan en los slides
        self.increment_x = self.increment_y = 1
        self.indicator_x = self.indicator_y = -1
        self.goles = 0
        
        #asignacion de variables de widgets
        self.drawing_area = gtk.DrawingArea()
        self.drawing_area.set_size_request(WIDTH, HEIGHT)
            
        #asignando cancha y arquero
        self.cancha = gtk.gdk.pixbuf_new_from_file('images/arcosugar.jpg')
        self.portero = gtk.gdk.pixbuf_new_from_file('images/goalkeeper_downcenter.png')
        
        #window.connect("destroy", lambda w: gtk.main_quit())
        
        #callbacks al invocar el area de dibujo
        self.drawing_area.connect('configure_event', self.__configure_cb)
        self.drawing_area.connect('expose-event', self.__expose_cb)
        
        #evento que invoca funcion al presionar cualquier tecla
        self.connect('key-press-event', self.__key_press_event_cb)
        
        #anhadiendo widgets dentro del contenedor principal
        self.set_canvas(hbox)
        hbox.add(vbox)
        frame.add(self.label)
        vbox.add(frame)
        vbox.add(self.drawing_area)
        vbox.add(self.hslide)
    	hbox.add(self.vslide)
        
        #mostrando los widgets
        self.show_all()
        
        #invocacion de la funcion que realiza loop del slider horizontal
        gobject.timeout_add(DELAY, self.__hslider_move_cb)

    def dibujar_portero_y_cancha(self):
        self.pixmap.draw_pixbuf(None, self.cancha, 0, 0, 0, 0, -1, -1,
                                gtk.gdk.RGB_DITHER_NONE, 0, 0)
        self.pixmap.draw_pixbuf(None, self.portero, 0, 0, 100, 110, -1, -1,
                                gtk.gdk.RGB_DITHER_NONE, 0, 0)
                                
    #funcion que se activa al presionar cualquier tecla y pregunta sobre el estado de los indicadores
    def __key_press_event_cb(self, window, event):
        if self.indicator_x == -1 and self.indicator_y == -1:
        
            self.capturar_valor_hslider()
            gobject.timeout_add(DELAY, self.__vslider_move_cb)
            
        elif self.indicator_y == -1:
    
            self.capturar_valor_vslider()
            
            #lanzar portero
            self.goal_keeper()
            
            #invoca a la funcion que dibuja la pelota de acuerdo a los valores de los sliders
            self.draw_ball(self.cv_x,self.cv_y)
            
            #metodo que refresca el drawing_area para actualizar posicion del portero y pelota
            self.drawing_area.queue_draw()
    
        else:
        
            #llamado a la funcion que restaura el juego
            self.restart_game()
            gobject.timeout_add(DELAY, self.__hslider_move_cb)
            
    #funcion que restaura el juego        
    def restart_game(self):
    
        self.dibujar_portero_y_cancha()
        
        #metodo que refresca el drawing_area
        self.drawing_area.queue_draw()
        
        self.increment_x = self.increment_y = 1
        self.indicator_x = self.indicator_y = -1
        self.hadjustment.value = self.vadjustment.value = 0
        
        #llamado a la funcion que actualiza el marcador
        self.marcador()
    
    #funcion que actualiza el marcador
    def marcador(self):
        if self.goles == 1:
            self.label.set_text(str(self.goles)+' '+'Gol hasta ahora!')
        elif self.goles > 1:
            self.label.set_text(str(self.goles)+' '+'Goles hasta ahora!')
    
    #funcion que dibuja y lanza al arquero en alguna posicion      
    def goal_keeper(self):

        self.pixmap.draw_pixbuf(None, self.cancha, 0, 0, 0, 0, -1, -1,
                                gtk.gdk.RGB_DITHER_NONE, 0, 0)
                                
        pixbufkeeper_DC = gtk.gdk.pixbuf_new_from_file('images/goalkeeper_downcenter.png')
        pixbufkeeper_DL = gtk.gdk.pixbuf_new_from_file('images/goalkeeper_downleft.png')
        pixbufkeeper_DR = gtk.gdk.pixbuf_new_from_file('images/goalkeeper_downright.png')
        pixbufkeeper_UC = gtk.gdk.pixbuf_new_from_file('images/goalkeeper_upcenter.png')
        pixbufkeeper_UL = gtk.gdk.pixbuf_new_from_file('images/goalkeeper_upleft.png')
        pixbufkeeper_UR = gtk.gdk.pixbuf_new_from_file('images/goalkeeper_upright.png')
        
        pos_portero = [pixbufkeeper_DC, pixbufkeeper_DL, pixbufkeeper_DR,
                       pixbufkeeper_UC, pixbufkeeper_UL, pixbufkeeper_UR]
        self.r = random.randint(0,5)
        #invocacion de forma random al pixbuf del arquero para que se lance en una posicion
        self.portero_ran = pos_portero[self.r]
        self.pixmap.draw_pixbuf(None, self.portero_ran, 0, 0, 100, 110, -1, -1,
                                gtk.gdk.RGB_DITHER_NONE, 0, 0)

    #funcion que hace el loop de los valores del slider horizontal   
    def __hslider_move_cb(self):
        if self.indicator_x < 0:
            self.hadjustment.value += self.increment_x
        
            if self.hadjustment.value >= 100:
    	        self.increment_x *= -1
            elif self.hadjustment.value <= 0:
    	        self.increment_x *= -1
            return True
        else:
            return False
    
    #funcion que hace el loop de los valores del slider vertical
    def __vslider_move_cb(self):
        if self.indicator_y < 0:
            self.vadjustment.value += self.increment_y
        
            if self.vadjustment.value >= 100:
    	        self.increment_y *= -1
            elif self.vadjustment.value <= 0:
    	        self.increment_y *= -1
            return True
        else:
            return False

    #funcion que captura el valor del slider horizontal despues de pulsar una tecla    
    def capturar_valor_hslider(self):
    
        #variable en donde se captura el valor al presionar un tecla
        self.hslide_value = self.hslide.get_value()

        #convierte el valor de changed_value a int
        self.cv_x = (((int(self.hslide_value)) * WIDTH) / 100)
        
        #detiene el slider
        self.indicator_x = 0
    
    #funcion que captura el valor del slider vertical despues de pulsar una tecla    
    def capturar_valor_vslider(self):
    
        #variable en donde se captura el valor al presionar un tecla
        self.vslide_value = self.vslide.get_value()
        
        #convierte el valor de changed_value a int
        self.cv_y = (((int(self.vslide_value)) * HEIGHT) / 100)
        
        #detiene el slider
        self.indicator_y = 0
        
    #funcion que dibuja la pelota
    def draw_ball(self, x, y):
    
        #variables de las dimensiones de la pelota
        self.c_x = x
        self.c_y = y
        self.c_a = 110
        self.c_h = 99
        
        #calculo de posicion de valor exacto de la pelota con relacion a los slides
        Xm = self.c_x - (self.c_a / 2)
        Ym = self.c_y - (self.c_h / 2)
        
        #dibujando la pelota con un pixbuf de acuerdo a valores de los slides
        self.pelota = gtk.gdk.pixbuf_new_from_file('images/ball.png')
        self.pixmap.draw_pixbuf(self.context, self.pelota, 0, 0, Xm, Ym, -1, -1, 
                                gtk.gdk.RGB_DITHER_NONE, 0, 0)
        
        #calcular el gol con las coordenadas de la pelota
        self.calculo_gol(Xm, Ym)
    
    def calculo_gol(self, c_x, c_y):
        #Coordenadas del rectangulo interior del arco
        i_x = 157
        i_y = 72
        i_a = 484
        i_h = 324
        
        if c_x > i_x and c_y > i_y and c_x < i_x+i_a and c_y < i_y+i_h:
            
            if self.r == 0:
                if c_x > 299 and c_x < 475 :
                    if c_y > 127 and c_y < 392:
                        self.label.set_text('EL ARQUERO ATAJA!!!')
                    else:
                        self.label.set_text('GOOOL!!!')
                        self.goles = self.goles + 1
                else:
                    self.label.set_text('GOOOL!!!')
                    self.goles = self.goles + 1                    
            elif self.r == 1:
                if c_x > 238 and c_x < 537:
                    if c_y > 242 and c_y < 396:
                        self.label.set_text('EL ARQUERO ATAJA!!!')
                    else:
                        self.label.set_text('GOOOL!!!')
                        self.goles = self.goles + 1
                else:
                    self.label.set_text('GOOOL!!!')
                    self.goles = self.goles + 1
            elif self.r == 2:
                if c_x > 236 and c_x < 532:
                    if c_y > 240 and c_y < 396:
                        self.label.set_text('EL ARQUERO ATAJA!!!')
                    else:
                        self.label.set_text('GOOOL!!!')
                        self.goles = self.goles + 1
                else:
                    self.label.set_text('GOOOL!!!')
                    self.goles = self.goles + 1
            elif self.r == 3:
                if c_x > 252 and c_x < 518:
                    if c_y > 142 and c_y < 345:
                        self.label.set_text('EL ARQUERO ATAJA!!!')
                    else:
                        self.label.set_text('GOOOL!!!')
                        self.goles = self.goles + 1
                else:
                    self.label.set_text('GOOOL!!!')
                    self.goles = self.goles + 1
            elif self.r == 4:
                if c_x > 265 and c_x < 503:
                    if c_y > 135 and c_y < 367:
                        self.label.set_text('EL ARQUERO ATAJA!!!')
                    else:
                        self.label.set_text('GOOOL!!!')
                        self.goles = self.goles + 1
                else:
                    self.label.set_text('GOOOL!!!')
                    self.goles = self.goles + 1
            elif self.r == 5:
                if c_x > 269 and c_x < 507:
                    if c_y > 151 and c_y < 380:
                        self.label.set_text('EL ARQUERO ATAJA!!!')
                    else:
                        self.label.set_text('GOOOL!!!')
                        self.goles = self.goles + 1
                else:
                    self.label.set_text('GOOOL!!!')
                    self.goles = self.goles + 1
        else:
            print self.label.set_text('UFF... EL BALON VA AFUERA')
            
    def __configure_cb(self, drawing_area, data=None):
        x, y, width, height = drawing_area.get_allocation()

        canvas = drawing_area.window
        self.pixmap = gtk.gdk.Pixmap(canvas, width, height)
        self.dibujar_portero_y_cancha()
        
        return True

    def __expose_cb(self, drawing_area, data=None):
        x, y, width, height = data.area
        style = drawing_area.get_style()
        self.context = style.fg_gc[gtk.STATE_NORMAL]

        canvas = drawing_area.window
        canvas.draw_drawable(self.context, self.pixmap, x, y, x, y, width, height)

        return False