# Copyright 2013 Paolo Monsalvo # # 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 import gtk import logging import gobject import random from ConfigParser import SafeConfigParser from subprocess import Popen 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 JugandoAprendesActivity(activity.Activity): """HelloWorldActivity class as specified in activity.info""" def __init__(self, handle): """Set up the HelloWorld activity.""" activity.Activity.__init__(self, handle) # 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() 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() self.load_data() self.cargar_ui() self.flag=0 self.patron_nuevo='' # label with the text, make the string translatable def cargar_ui(self): vbox = gtk.VBox() self.set_canvas(vbox) self.hbox = gtk.HBox() label = gtk.Label() self.connect('key-press-event', self.__key_press_cb, label) vbox.add(self.hbox) self.image1=gtk.Image() self.image1.set_from_file('imagenes/derecha.png') self.image1.show() self.hbox.pack_start(self.image1) self.image2=gtk.Image() self.image2.set_from_file('imagenes/izquierda.png') self.image2.show() self.hbox.pack_start(self.image2) self.image3=gtk.Image() self.image3.set_from_file('imagenes/arriba.png') self.image3.show() self.hbox.pack_start(self.image3) self.image4=gtk.Image() self.image4.set_from_file('imagenes/abajo.png') self.image4.show() self.hbox.pack_start(self.image4) vbox.show_all() def load_data(self): parser=SafeConfigParser() parser.read('config.ini') words= parser.get('inicio','patron') self.say (words) def controlar_patrones(self): while(self.i < 4): if (self.patron[self.i]==self.key_name): self.say(self.patron[self.i]+'Correcto') self.i+=1 self.flag=1 break else: self.say(self.patron[self.i]+'Incorrecto Vuelva a escuchar los patrones e intentelo de nuevo'+self.patron_nuevo) self.i+=0 break def leer_patrones(self): self.say(self.patron_nuevo) self.i=0 self.flag=1 def generar_patrones(self): if (self.flag==0): self.patron=['arriba','abajo','izquierda', 'derecha'] random.shuffle(self.patron) self.patron_nuevo=" ".join(self.patron) self.hbox.remove(self.image1) self.hbox.remove(self.image2) self.hbox.remove(self.image3) self.hbox.remove(self.image4) self.image1=gtk.Image() self.image1.set_from_file('imagenes/'+self.patron[0]+'.png') self.image1.show() self.hbox.pack_start(self.image1) self.image2=gtk.Image() self.image2.set_from_file('imagenes/'+self.patron[1]+'.png') self.image2.show() self.hbox.pack_start(self.image2) self.image3=gtk.Image() self.image3.set_from_file('imagenes/'+self.patron[2]+'.png') self.image3.show() self.hbox.pack_start(self.image3) self.image4=gtk.Image() self.image4.set_from_file('imagenes/'+self.patron[3]+'.png') self.image4.show() self.hbox.pack_start(self.image4) self.leer_patrones() def __key_press_cb(self, window, event, label): self.key_name = gtk.gdk.keyval_name(event.keyval) if (self.key_name=='Up'): self.say ('Arriba') self.key_name='arriba' if(self.patron_nuevo != ''): self.controlar_patrones() elif (self.key_name=='Down'): self.say ('Abajo') self.key_name='abajo' if(self.patron_nuevo != ''): self.controlar_patrones() elif (self.key_name=='Left'): self.say ('Izquierda') self.key_name='izquierda' if(self.patron_nuevo != ''): self.controlar_patrones() elif (self.key_name=='Right'): self.say ('Derecha') self.key_name='derecha' if(self.patron_nuevo != ''): self.controlar_patrones() elif (self.key_name=='space'): if(self.flag==0): self.generar_patrones() else: self.say(self.patron_nuevo) else: self.say('Tecla incorrecta') def say(self, text): Popen(['espeak', '-v', 'es', text]) def read_file(self, file_name): self.patron=self.metadata['juego'] def write_file(self, file_name): self.metadata['juego']=self.patron