Back to index

gcompris  8.2.2
hexagon.py
Go to the documentation of this file.
00001 #  gcompris - Hexagon
00002 #
00003 # Time-stamp: <2004/31/03 15:10:00 Ingo Konrad>
00004 #
00005 # Copyright (C) 2004  Christof Petig and Ingo Konrad
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.bonus
00025 import gcompris.skin
00026 import gtk
00027 import gtk.gdk
00028 import random
00029 import math
00030 
00031 class Gcompris_hexagon:
00032   """The minouche activity"""
00033 
00034   def __init__(self, gcomprisBoard):
00035     self.gcomprisBoard = gcomprisBoard
00036     self.rootitem = None
00037 
00038     # These are used to let us restart only after the bonus is displayed.
00039     # When the bonus is displayed, it call us first with pause(1) and then with pause(0)
00040     self.board_paused  = 0;
00041     self.gamewon       = False;
00042 
00043 
00044   def start(self):
00045     gcompris.bar_set (0)
00046     gcompris.set_background(self.gcomprisBoard.canvas.root(),
00047                             gcompris.skin.image_to_skin("gcompris-bg.jpg"))
00048     self.rootitem = self.gcomprisBoard.canvas.root().add(
00049      gnomecanvas.CanvasGroup,
00050      x=0.0,
00051      y=0.0
00052      )
00053     self.paint_skin()
00054     self.random_catx = random.randrange(21)
00055     self.random_caty = random.randrange(15)
00056 
00057 
00058   def end(self):
00059     self.cleanup()
00060 
00061   def ok(self):
00062     print("Gcompris_minouche ok.")
00063 
00064   def key_press(self, keyval, commit_str, preedit_str):
00065     #print("got key %i" % keyval)
00066     return False
00067 
00068   # Called by gcompris core
00069   def pause(self, pause):
00070 
00071     self.board_paused = pause
00072 
00073     # When the bonus is displayed, it call us first with pause(1) and then with pause(0)
00074     # the game is won
00075     if(pause == 0 and self.gamewon):
00076       self.finished()
00077       self.gamewon = 0
00078 
00079     return
00080 
00081 
00082   # ----------------------------------------------------------------------
00083   # ----------------------------------------------------------------------
00084   # ----------------------------------------------------------------------
00085 
00086 
00087   def cleanup(self):
00088 
00089     self.gamewon       = False;
00090     # Remove the root item removes all the others inside it
00091     if self.rootitem != None:
00092      self.rootitem.destroy()
00093      self.rootitem = None
00094 
00095   def paint_hex(self, x, y, color=0x0099FFCCL):
00096     ax = 30+self.sqrt3*self.r*x
00097     ay = 30+1.5*self.r*y
00098 
00099     if y&1 :
00100      ax+=self.sqrt3/2*self.r
00101     self.pts = []
00102     for i in range (len(self.cx)):
00103      self.pts.append (ax+self.cx [i])
00104      self.pts.append (ay+self.cy [i])
00105 
00106     s = self.rootitem.add(gnomecanvas.CanvasPolygon, points = self.pts,
00107          fill_color_rgba = color, outline_color = "black", width_units =
00108          2.5)
00109     return s
00110 
00111   def paint_skin(self):
00112     self.r = 20
00113     self.cx = []
00114     self.cy = []
00115     self.sqrt3 = math.sqrt(3)
00116     for i in range (6):
00117      self.cx.append(int(self.r*math.cos(math.pi/6+i*math.pi/3)))
00118      self.cy.append(int(self.r*math.sin(math.pi/6+i*math.pi/3)))
00119     for x in range (22):
00120      for y in range (16):
00121       s = self.paint_hex(x, y)
00122 
00123       s.connect ("event", self.on_click, x-int(y/2), y)
00124 
00125   def coloring(self,dist):
00126     r=b=g=0
00127     if dist <0.25:
00128      r = 0xFF
00129      g = long(4*0xFF*dist)
00130     elif dist <0.5:
00131      g = 0xFF
00132      r = 0xFF-long(4*(dist-0.25)*0xFF)
00133     elif dist <0.75:
00134      g = 0xFF-long(4*(dist-0.5)*0xFF)
00135      b = long(0xFF*4*(dist-0.5))
00136     elif dist <=1:
00137      b = 0xFF
00138      r = long(0xFF*4*(dist-0.75))
00139     else :
00140      pass
00141     color = r*0x1000000+g*0x10000+b*0x100+0xFF
00142     return color
00143 
00144   def finished(self):
00145     gcompris.bonus.board_finished(gcompris.bonus.FINISHED_RANDOM)
00146 
00147   def on_click (self, widget, event=None, x=0, y=0):
00148     if event.type == gtk.gdk.BUTTON_PRESS and event.button == 1 :
00149 
00150      catdistance = self.distance_cat(x,y)
00151      #print self.random_catx, self.random_caty,x,y,catdistance
00152 
00153      if catdistance<0.1:
00154       self.paint_cat()
00155       self.gamewon       = True;
00156       gcompris.bonus.display(gcompris.bonus.WIN, gcompris.bonus.GNU)
00157      else:
00158        color = self.coloring (catdistance/30.0)
00159        widget.set(fill_color_rgba=color);
00160 
00161 
00162   def paint_cat(self):
00163     position =19+self.sqrt3*self.r*self.random_catx
00164     if self.random_caty%2:
00165      position +=self.sqrt3/2*self.r
00166     pixbuf2 = gcompris.utils.load_pixmap \
00167     ("gcompris/misc/strawberry.png")
00168     h2 = 30
00169     w2 = pixbuf2.get_width()*h2/pixbuf2.get_height()
00170     self.rootitem.add(gnomecanvas.CanvasPixbuf,
00171                       pixbuf=pixbuf2.scale_simple(w2, h2,
00172                                                   gtk.gdk.INTERP_BILINEAR),
00173                       x=position,
00174                       y=14+1.5*self.random_caty*self.r)
00175 
00176   def distance_cat (self,x,y):
00177     dx = self.random_catx-x-int(self.random_caty/2)
00178     dy = self.random_caty-y
00179 
00180     if dx*dy >=0:
00181      return abs(dx)+abs(dy)
00182     if dx*dy<0:
00183      return max(abs(dx),abs(dy))