Back to index

gcompris  8.2.2
watercycle.py
Go to the documentation of this file.
00001 #  gcompris - watercycle
00002 #
00003 # Time-stamp: <2001/08/20 00:54:45 bruno>
00004 #
00005 # Copyright (C) 2003 Bruno Coudoin
00006 #
00007 #   This program is free software; you can redistribute it and/or modify
00008 #   it under the terms of the GNU General Public License as published by
00009 #   the Free Software Foundation; either version 2 of the License, or
00010 #   (at your option) any later version.
00011 #
00012 #   This program is distributed in the hope that it will be useful,
00013 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 #   GNU General Public License for more details.
00016 #
00017 #   You should have received a copy of the GNU General Public License
00018 #   along with this program; if not, write to the Free Software
00019 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 #
00021 import gnomecanvas
00022 import gcompris
00023 import gcompris.utils
00024 import gcompris.anim
00025 import gcompris.skin
00026 import gtk
00027 import gtk.gdk
00028 
00029 class Gcompris_watercycle:
00030   """The Cycle Water activity"""
00031 
00032 
00033   def __init__(self, gcomprisBoard):
00034     self.gcomprisBoard = gcomprisBoard
00035 
00036     print("Gcompris_watercycle __init__.")
00037 
00038 
00039   def start(self):
00040     self.gcomprisBoard.level=1
00041     self.gcomprisBoard.maxlevel=1
00042     self.gcomprisBoard.sublevel=1
00043     self.gcomprisBoard.number_of_sublevel=1
00044 
00045     # The basic tick for object moves
00046     self.timerinc = 50
00047 
00048     # The tick for the boat is variable
00049     self.boat_timerinc = self.timerinc
00050 
00051     # Need to manage the timers to quit properly
00052     self.boat_timer = 0
00053     self.sun_timer = 0
00054     self.vapor_timer = 0
00055     self.cloud_timer = 0
00056     self.waterlevel_timer = 0
00057 
00058     gcompris.bar_set(0)
00059     gcompris.set_background(self.gcomprisBoard.canvas.root(),
00060                             "watercycle/background.png")
00061     gcompris.bar_set_level(self.gcomprisBoard)
00062 
00063     # Create our rootitem. We put each canvas item in it so at the end we
00064     # only have to kill it. The canvas deletes all the items it contains automaticaly.
00065     self.rootitem = self.gcomprisBoard.canvas.root().add(
00066       gnomecanvas.CanvasGroup,
00067       x=0.0,
00068       y=0.0
00069       )
00070 
00071     # Take care, the items are stacked on each other in the order you add them.
00072     # If you need, you can reorder them later with raise and lower functions.
00073 
00074     # The River
00075     self.riveritem = self.rootitem.add(
00076       gnomecanvas.CanvasPixbuf,
00077       pixbuf = gcompris.utils.load_pixmap("watercycle/riverempty.png"),
00078       x=150.0,
00079       y=50.0
00080       )
00081     self.riverfull = 0
00082 
00083     # The bad water
00084     self.badwateritem = self.rootitem.add(
00085       gnomecanvas.CanvasPixbuf,
00086       pixbuf = gcompris.utils.load_pixmap("watercycle/badwater_off.png"),
00087       x=360.0,
00088       y=292.0
00089       )
00090 
00091     # The clean water
00092     self.cleanwateritem = self.rootitem.add(
00093       gnomecanvas.CanvasPixbuf,
00094       pixbuf = gcompris.utils.load_pixmap("watercycle/cleanwater_off.png"),
00095       x=470.0,
00096       y=130.0
00097       )
00098     self.cleanwaterstatus = 0
00099 
00100     # The Sun
00101     self.sunitem = self.rootitem.add(
00102       gnomecanvas.CanvasPixbuf,
00103       pixbuf = gcompris.utils.load_pixmap("watercycle/sun.png"),
00104       x=10.0,
00105       y=70.0
00106       )
00107     self.sunitem.connect("event", self.sun_item_event)
00108     # This item is clickeable and it must be seen
00109     self.sunitem.connect("event", gcompris.utils.item_event_focus)
00110     self.sun_direction = -1
00111     self.sun_on = 0
00112 
00113     # The sun mask object to simulate the see
00114     self.rootitem.add(
00115       gnomecanvas.CanvasRect,
00116       x1=10.0,
00117       y1=89.0,
00118       x2=90.0,
00119       y2=155.0,
00120       fill_color_rgba=0x0099FFFFL,
00121       width_units=0.0
00122       )
00123 
00124     # The Snow
00125     self.snowitem = self.rootitem.add(
00126       gnomecanvas.CanvasPixbuf,
00127       pixbuf = gcompris.utils.load_pixmap("watercycle/snow.png"),
00128       x=180.0,
00129       y=3.0
00130       )
00131     self.snowitem.hide()
00132 
00133     # The rain
00134     self.rainitem = self.rootitem.add(
00135       gnomecanvas.CanvasPixbuf,
00136       pixbuf = gcompris.utils.load_pixmap("watercycle/rain.png"),
00137       x=40.0,
00138       y=70.0
00139       )
00140     self.rainitem.hide()
00141     self.rain_on = 0
00142 
00143     # The cloud
00144     self.clouditem = self.rootitem.add(
00145       gnomecanvas.CanvasPixbuf,
00146       pixbuf = gcompris.utils.load_pixmap("gcompris/misc/cloud.png"),
00147       x=10.0,
00148       y=10.0
00149       )
00150     self.clouditem.hide()
00151     self.clouditem.connect("event", self.cloud_item_event)
00152     # This item is clickeable and it must be seen
00153     self.clouditem.connect("event", gcompris.utils.item_event_focus)
00154     self.cloud_on = 0
00155 
00156     # The vapor
00157     self.vaporitem = self.rootitem.add(
00158       gnomecanvas.CanvasPixbuf,
00159       pixbuf = gcompris.utils.load_pixmap("watercycle/vapor.png"),
00160       x=35.0,
00161       y=150.0
00162       )
00163     self.vaporitem.hide()
00164 
00165     # The Waterpump
00166     self.waterpumpitem = self.rootitem.add(
00167       gnomecanvas.CanvasPixbuf,
00168       pixbuf = gcompris.utils.load_pixmap("watercycle/waterpump.png"),
00169       x=165.0,
00170       y=120.0
00171       )
00172     self.waterpumpitem.connect("event", self.waterpump_item_event)
00173     # This item is clickeable and it must be seen
00174     self.waterpumpitem.connect("event", gcompris.utils.item_event_focus)
00175     self.waterpump_on = 0
00176 
00177     # The pump water
00178     self.pumpwateritem = self.rootitem.add(
00179       gnomecanvas.CanvasPixbuf,
00180       pixbuf = gcompris.utils.load_pixmap("watercycle/pumpwater_off.png"),
00181       x=270.0,
00182       y=133.0
00183       )
00184 
00185     # The WaterCleaning
00186     self.watercleaningitem = self.rootitem.add(
00187       gnomecanvas.CanvasPixbuf,
00188       pixbuf = gcompris.utils.load_pixmap("watercycle/watercleaning.png"),
00189       x=520.0,
00190       y=380.0
00191       )
00192     self.watercleaningitem.connect("event", self.watercleaning_item_event)
00193     # This item is clickeable and it must be seen
00194     self.watercleaningitem.connect("event", gcompris.utils.item_event_focus)
00195     self.watercleaning_on = 0
00196 
00197     # The tuxboat
00198     self.tuxboatitem = gcompris.anim.CanvasItem( gcompris.anim.Animation("watercycle/tuxboat.txt"),
00199                                                  self.rootitem );
00200     numstates = self.tuxboatitem.num_states
00201 
00202     self.tuxboatcanvas = self.tuxboatitem.gnomecanvas
00203     self.tuxboatcanvas.set( x=-100.0, y=430.0 )
00204 
00205     # Tux in the shower (without water)
00206     self.tuxshoweritem = self.rootitem.add(
00207       gnomecanvas.CanvasPixbuf,
00208       pixbuf = gcompris.utils.load_pixmap("gcompris/misc/minitux.png"),
00209       x=569.0,
00210       y=239.0
00211       )
00212     self.tuxshoweritem.hide()
00213     self.tuxisinshower = 0
00214 
00215     # Tux in the shower with the water
00216     self.tuxshowerwateritem = self.rootitem.add(
00217       gnomecanvas.CanvasPixbuf,
00218       pixbuf = gcompris.utils.load_pixmap("watercycle/showerwater.png"),
00219       x=561.0,
00220       y=231.0
00221       )
00222     self.tuxshowerwateritem.hide()
00223 
00224     # The shower itself
00225     self.rootitem.add(
00226       gnomecanvas.CanvasPixbuf,
00227       pixbuf = gcompris.utils.load_pixmap("watercycle/shower.png"),
00228       x=560.0,
00229       y=283.0
00230       )
00231 
00232     # The shower on/off button (I need to get the 2 buttons to manage the focus)
00233     self.showerbuttonitem_on = self.rootitem.add(
00234       gnomecanvas.CanvasPixbuf,
00235       pixbuf = gcompris.utils.load_pixmap("watercycle/shower_on.png"),
00236       x=622.0,
00237       y=260.0
00238       )
00239     self.showerbuttonitem_on.connect("event", self.showerbutton_item_event)
00240     # This item is clickeable and it must be seen
00241     self.showerbuttonitem_on.connect("event", gcompris.utils.item_event_focus)
00242     self.showerbuttonitem_on.hide()
00243     self.showerbutton = 0
00244 
00245     self.showerbuttonitem_off = self.rootitem.add(
00246       gnomecanvas.CanvasPixbuf,
00247       pixbuf = gcompris.utils.load_pixmap("watercycle/shower_off.png"),
00248       x=622.0,
00249       y=260.0
00250       )
00251     self.showerbuttonitem_off.connect("event", self.showerbutton_item_event)
00252     # This item is clickeable and it must be seen
00253     self.showerbuttonitem_off.connect("event", gcompris.utils.item_event_focus)
00254 
00255     # The level of water in the castle
00256     self.waterlevel_max = 65
00257     self.waterlevel_min = 85
00258     self.waterlevel     = self.waterlevel_min
00259     self.waterlevel_timer = gtk.timeout_add(1000, self.update_waterlevel)
00260 
00261     self.waterlevel_item = self.rootitem.add(
00262           gnomecanvas.CanvasLine,
00263           points = (655 , self.waterlevel,
00264                     655 , self.waterlevel_min),
00265           fill_color_rgba = 0x0033FFFFL,
00266           width_units = 20.0
00267           )
00268 
00269     # Some item ordering
00270     self.rainitem.raise_to_top()
00271     self.clouditem.raise_to_top()
00272 
00273     # Ready GO
00274     self.move_boat()
00275 
00276     print("Gcompris_watercycle start.")
00277 
00278 
00279   def end(self):
00280     # Remove all the timer first
00281     if self.boat_timer :
00282       gtk.timeout_remove(self.boat_timer)
00283     if self.sun_timer :
00284       gtk.timeout_remove(self.sun_timer)
00285     if self.vapor_timer :
00286       gtk.timeout_remove(self.vapor_timer)
00287     if self.cloud_timer :
00288       gtk.timeout_remove(self.cloud_timer)
00289     if self.waterlevel_timer :
00290       gtk.timeout_remove(self.waterlevel_timer)
00291 
00292     # Remove the root item removes all the others inside it
00293     self.rootitem.destroy()
00294 
00295     print("Gcompris_watercycle end.")
00296 
00297 
00298   def ok(self):
00299     print("Gcompris_watercycle ok.")
00300 
00301 
00302   def repeat(self):
00303     print("Gcompris_watercycle repeat.")
00304 
00305 
00306   def config(self):
00307     print("Gcompris_watercycle config.")
00308 
00309   def key_press(self, keyval, commit_str, preedit_str):
00310     print("got key %i" % keyval)
00311     return False
00312 
00313   # This is called each second to update to castle water level
00314   def update_waterlevel(self):
00315     old_waterlevel = self.waterlevel
00316 
00317     if self.waterpump_on and self.waterlevel > self.waterlevel_max :
00318       self.waterlevel -= 1
00319 
00320     # It tux is in the shower and it works, then remove some water
00321     if (self.tuxisinshower and self.cleanwaterstatus and self.showerbutton
00322         and self.waterlevel < self.waterlevel_min) :
00323       self.waterlevel += 2
00324 
00325     # Redisplay the level of water if needed
00326     if old_waterlevel != self.waterlevel :
00327       self.waterlevel_item.set(
00328         points = (655, self.waterlevel,
00329                   655, self.waterlevel_min),
00330         )
00331 
00332     # Update the clean water tubes
00333     if self.waterlevel < self.waterlevel_min - 5 :
00334       self.set_cleanwater(1)
00335     else:
00336       self.set_cleanwater(0)
00337       # In this case, de activate the shower to avoid blinking tubes
00338       self.showerbuttonitem_on.hide()
00339       self.showerbuttonitem_off.show()
00340       self.showerbutton = 0
00341       self.shower_water_update()
00342 
00343     self.waterlevel_timer = gtk.timeout_add(1000, self.update_waterlevel)
00344 
00345 
00346   def set_cleanwater(self, status):
00347     if(status == self.cleanwaterstatus):
00348       return
00349 
00350     if status:
00351       self.cleanwateritem.set(pixbuf = gcompris.utils.load_pixmap("watercycle/cleanwater.png"));
00352     else:
00353       self.cleanwateritem.set(pixbuf = gcompris.utils.load_pixmap("watercycle/cleanwater_off.png"));
00354     self.cleanwaterstatus = status
00355 
00356 
00357   def move_boat(self):
00358     if( self.tuxboatcanvas.get_bounds()[2] < 770 ) :
00359 
00360       # Make the boat slow down when arriving
00361       if(self.tuxboatcanvas.get_bounds()[2]==700 or self.tuxboatcanvas.get_bounds()[2]==701):
00362         self.boat_timerinc+=50
00363 
00364       self.tuxboatcanvas.move(2, 0)
00365       self.boat_timer = gtk.timeout_add(self.boat_timerinc, self.move_boat)
00366     else:
00367       if self.tuxboatcanvas.get_bounds()[2] < 800 :
00368         # Park the boat
00369         self.tuxboatcanvas.move(0.7, -0.7)
00370         self.tuxboatitem.setState(1)
00371         self.boat_timer = gtk.timeout_add(self.timerinc, self.move_boat)
00372       else :
00373         # We are parked, change the boat to remove tux
00374         #self.tuxboatcanvas.set(
00375         #  pixbuf = gcompris.utils.load_pixmap("gcompris/misc/fishingboat.png"),
00376         #  width = 100.0,
00377         #  height = 48.0,
00378         #  width_set = 1,
00379         #  height_set = 1,
00380         #  )
00381         self.tuxboatitem.setState(2)
00382 
00383         # Now display tux in the shower
00384         self.tuxshoweritem.show()
00385         self.tuxisinshower = 1
00386 
00387 
00388   def move_cloud(self):
00389     if(self.cloud_on):
00390       self.clouditem.show()
00391     else:
00392       self.clouditem.hide()
00393 
00394     if(self.rain_on):
00395       self.rainitem.show()
00396       # The snow appear if we are close to the left mountain
00397       if( self.clouditem.get_bounds()[0] < 250):
00398           self.snowitem.show()
00399       # The river
00400       if(not self.riverfull):
00401         self.riveritem.set(pixbuf = gcompris.utils.load_pixmap("watercycle/riverfull.png"));
00402         self.riverfull = 1
00403 
00404     else:
00405       self.rainitem.hide()
00406 
00407     self.clouditem.move(1, 0);
00408     self.rainitem.move(1, 0);
00409     if( self.clouditem.get_bounds()[0] > 800 ) :
00410       self.clouditem.move(-800, 0);
00411       self.rainitem.move(-800, 0);
00412       self.cloud_on = 0
00413       self.rain_on = 0
00414       self.clouditem.hide()
00415       self.rainitem.hide()
00416     else:
00417       self.cloud_timer = gtk.timeout_add(self.timerinc, self.move_cloud)
00418 
00419   def init_vapor(self):
00420     self.vapor_on = 1
00421     self.vaporitem.show()
00422     self.move_vapor()
00423 
00424   def move_vapor(self):
00425     self.vaporitem.move(0, -1);
00426     if( self.vaporitem.get_bounds()[1] < 20 ) :
00427       self.vaporitem.move(0, +100);
00428 
00429     self.vapor_timer = gtk.timeout_add(self.timerinc, self.move_vapor)
00430 
00431   def move_sun(self):
00432     self.sunitem.move(0, self.sun_direction);
00433     if( (self.sunitem.get_bounds()[1] > 0 and
00434          self.sunitem.get_bounds()[1] < 70 ) ) :
00435       self.sun_timer = gtk.timeout_add(self.timerinc, self.move_sun)
00436     else :
00437       # The sun is at is top
00438       if(self.sun_direction < 0) :
00439         # Stop the sun
00440         self.sun_timer = gtk.timeout_add(15000, self.move_sun)
00441         # Start the vapor
00442         self.vapor_timer = gtk.timeout_add(5000 , self.init_vapor)
00443         self.vapor_on = 1
00444         # Start the cloud
00445         if(not self.cloud_on):
00446           self.cloud_timer = gtk.timeout_add(10000, self.move_cloud)
00447           self.cloud_on = 1
00448         # Remove the snow
00449         self.snowitem.hide()
00450 
00451       else :
00452         # Stop the vapor
00453         self.vapor_on = 0
00454         self.vaporitem.hide()
00455         # Stop the sun
00456         self.sun_on = 0
00457 
00458       self.sun_direction = self.sun_direction * -1
00459 
00460   def pause(self, pause):
00461     print("Gcompris_watercycle pause. %i" % pause)
00462 
00463   def set_level(self, level):
00464     print("Gcompris_watercycle set level. %i" % level)
00465 
00466   def sun_item_event(self, widget, event=None):
00467     if event.type == gtk.gdk.BUTTON_PRESS:
00468       if event.button == 1:
00469         if not self.sun_on :
00470           self.sun_timer = gtk.timeout_add(self.timerinc, self.move_sun)
00471           self.sun_on = 1
00472         return True
00473     return False
00474 
00475   def cloud_item_event(self, widget, event=None):
00476     if event.type == gtk.gdk.BUTTON_PRESS:
00477       if event.button == 1:
00478         self.rain_on = 1
00479         return True
00480     return False
00481 
00482   def waterpump_item_event(self, widget, event=None):
00483     if event.type == gtk.gdk.BUTTON_PRESS:
00484       if event.button == 1:
00485         if self.riverfull:
00486           self.waterpump_on = 1
00487           self.pumpwateritem.set(pixbuf = gcompris.utils.load_pixmap("watercycle/pumpwater.png"));
00488         return True
00489     return False
00490 
00491   def watercleaning_item_event(self, widget, event=None):
00492     if event.type == gtk.gdk.BUTTON_PRESS:
00493       if event.button == 1:
00494         if self.waterpump_on:
00495           self.watercleaning_on = 1
00496           self.badwateritem.set(pixbuf = gcompris.utils.load_pixmap("watercycle/badwater.png"));
00497         return True
00498     return False
00499 
00500 
00501   # If Tux is in the shower, we must display the water if needed
00502   def shower_water_update(self):
00503 
00504     if not self.tuxisinshower:
00505       return
00506 
00507     if self.cleanwaterstatus and self.showerbutton:
00508       self.tuxshoweritem.hide()
00509       self.tuxshowerwateritem.show()
00510     else:
00511       self.tuxshoweritem.show()
00512       self.tuxshowerwateritem.hide()
00513 
00514 
00515   def showerbutton_item_event(self, widget, event=None):
00516 
00517     # Not active until tux is in the shower and the watercleaning station is running
00518     if not self.tuxisinshower:
00519       return
00520 
00521     if not self.watercleaning_on:
00522       return
00523 
00524     if event.type == gtk.gdk.BUTTON_PRESS:
00525       if event.button == 1:
00526         if self.showerbutton:
00527           self.showerbuttonitem_on.hide()
00528           self.showerbuttonitem_off.show()
00529         else:
00530           self.showerbuttonitem_on.show()
00531           self.showerbuttonitem_off.hide()
00532 
00533         self.showerbutton = not self.showerbutton
00534 
00535         self.shower_water_update()