/usr/share/gcompris/python/hexagon.py is in gcompris-data 15.02-1.1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # gcompris - Hexagon
#
# Copyright (C) 2004, 2008 Christof Petig and Ingo Konrad
#
# 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.
#
# 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, see <http://www.gnu.org/licenses/>.
#
import goocanvas
import gcompris
import gcompris.utils
import gcompris.bonus
import gcompris.skin
import gcompris.sound
import gtk
import gtk.gdk
import random
import math
class Gcompris_hexagon:
"""The hexagon activity"""
def __init__(self, gcomprisBoard):
self.gcomprisBoard = gcomprisBoard
self.rootitem = None
# These are used to let us restart only after the bonus is displayed.
# When the bonus is displayed, it call us first with pause(1) and then with pause(0)
self.board_paused = 0;
self.gamewon = False;
def start(self):
self.gcomprisBoard.level=1
self.gcomprisBoard.maxlevel=9
self.gcomprisBoard.sublevel=1
self.gcomprisBoard.number_of_sublevel=1
gcompris.bar_set(gcompris.BAR_LEVEL)
gcompris.set_background(self.gcomprisBoard.canvas.get_root_item(),
"hexagon/background.svgz")
gcompris.bar_set_level(self.gcomprisBoard)
gcompris.bar_location(-1, -1, 0.8)
self.set_level(1)
self.game_start()
def end(self):
self.cleanup()
def ok(self):
pass
def key_press(self, keyval, commit_str, preedit_str):
return False
# Called by gcompris core
def pause(self, pause):
self.board_paused = pause
# When the bonus is displayed, it call us first with pause(1) and then with pause(0)
# the game is won
if(pause == 0 and self.gamewon):
self.gamewon = 0
self.set_level(self.gcomprisBoard.level)
self.game_start()
return
# Called by gcompris when the user click on the level icon
def set_level(self, level):
self.gcomprisBoard.level=level
self.gcomprisBoard.sublevel=1
# Set the level in the control bar
gcompris.bar_set_level(self.gcomprisBoard);
if(self.gcomprisBoard.level == 1):
self.r = 30
self.nbx = 15
self.nby = 10
elif(self.gcomprisBoard.level == 2):
self.r = 28
self.nbx = 16
self.nby = 10
elif(self.gcomprisBoard.level == 3):
self.r = 27
self.nbx = 16
self.nby = 11
elif(self.gcomprisBoard.level == 4):
self.r = 25
self.nbx = 18
self.nby = 12
elif(self.gcomprisBoard.level == 5):
self.r = 23
self.nbx = 19
self.nby = 13
elif(self.gcomprisBoard.level == 6):
self.r = 22
self.nbx = 20
self.nby = 13
elif(self.gcomprisBoard.level == 7):
self.r = 21
self.nbx = 21
self.nby = 14
elif(self.gcomprisBoard.level == 8):
self.r = 19
self.nbx = 23
self.nby = 15
elif(self.gcomprisBoard.level == 9):
self.r = 17
self.nbx = 26
self.nby = 17
self.game_start()
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
def game_start(self):
self.cleanup()
self.rootitem = goocanvas.Group(parent = self.gcomprisBoard.canvas.get_root_item())
self.paint_skin()
self.random_catx = random.randrange(self.nbx)
self.random_caty = random.randrange(self.nby)
def cleanup(self):
self.gamewon = False;
# Remove the root item removes all the others inside it
if self.rootitem != None:
self.rootitem.remove()
self.rootitem = None
def paint_hex(self, x, y, color=0x0099FFCCL):
ax = 30+self.sqrt3*self.r*x
ay = 30+1.5*self.r*y
if y&1 :
ax+=self.sqrt3/2*self.r
pts = []
for i in range (len(self.cx)):
pts.append ((ax+self.cx [i], ay+self.cy [i]))
s = goocanvas.Polyline (parent = self.rootitem,
points = goocanvas.Points(pts),
close_path = True,
stroke_color = "black",
fill_color_rgba = color,
line_width = 2.5)
return s
def paint_skin(self):
self.cx = []
self.cy = []
self.sqrt3 = math.sqrt(3)
for i in range (6):
self.cx.append(int(self.r*math.cos(math.pi/6+i*math.pi/3)))
self.cy.append(int(self.r*math.sin(math.pi/6+i*math.pi/3)))
for x in range (self.nbx):
for y in range (self.nby):
s = self.paint_hex(x, y)
s.connect ("button_press_event", self.on_click, x-int(y/2), y)
def coloring(self,dist):
r=b=g=0
if dist <0.25:
r = 0xFF
g = long(4*0xFF*dist)
elif dist <0.5:
g = 0xFF
r = 0xFF-long(4*(dist-0.25)*0xFF)
elif dist <0.75:
g = 0xFF-long(4*(dist-0.5)*0xFF)
b = long(0xFF*4*(dist-0.5))
elif dist <=1:
b = 0xFF
r = long(0xFF*4*(dist-0.75))
else :
pass
color = r*0x1000000+g*0x10000+b*0x100+0xFF
return color
def on_click (self, widget, target, event=None, x=0, y=0):
if event.type == gtk.gdk.BUTTON_PRESS and event.button == 1 :
catdistance = self.distance_cat(x,y)
#print self.random_catx, self.random_caty,x,y,catdistance
if catdistance<0.1:
gcompris.sound.play_ogg("sounds/smudge.wav")
self.paint_cat()
self.increment_level()
self.gamewon = True;
gcompris.bonus.display(gcompris.bonus.WIN, gcompris.bonus.GNU)
else:
gcompris.sound.play_ogg("sounds/bleep.wav")
color = self.coloring (catdistance/30.0)
widget.props.fill_color_rgba = color;
def paint_cat(self):
position = 19 + self.sqrt3 * self.r * self.random_catx
if self.random_caty % 2:
position += self.sqrt3/2 * self.r
pixbuf2 = gcompris.utils.load_pixmap \
("hexagon/strawberry.svg")
h2 = 30
w2 = pixbuf2.get_width() * h2/pixbuf2.get_height()
img = goocanvas.Image (parent = self.rootitem,
pixbuf = pixbuf2.scale_simple(w2, h2,
gtk.gdk.INTERP_BILINEAR),
x = position - 5,
y = 14+1.5*self.random_caty*self.r)
def distance_cat (self,x,y):
dx = self.random_catx-x-int(self.random_caty/2)
dy = self.random_caty-y
if dx*dy >=0:
return abs(dx)+abs(dy)
if dx*dy<0:
return max(abs(dx),abs(dy))
# Code that increments the sublevel and level
# And bail out if no more levels are available
# return 1 if continue, 0 if bail out
def increment_level(self):
self.gcomprisBoard.sublevel += 1
if(self.gcomprisBoard.sublevel>self.gcomprisBoard.number_of_sublevel):
# Try the next level
self.gcomprisBoard.sublevel=1
self.gcomprisBoard.level += 1
if(self.gcomprisBoard.level > self.gcomprisBoard.maxlevel):
self.gcomprisBoard.level = self.gcomprisBoard.maxlevel
return 1
|