Back to index

gcompris  8.2.2
Public Member Functions | Public Attributes
searace.Gcompris_searace Class Reference

List of all members.

Public Member Functions

def __init__
def start
def end
def pause
def ok
def set_level
def repeat
def config
def key_press
def init_boats
def display_sea_area
def display_weather
def display_condition
def get_weather_condition
def get_absolute_weather_condition
def get_wind_score
def cmd_forward
def cmd_turn_left
def race_one_command
def tux_move
def ruler_item_event

Public Attributes

 gcomprisBoard
 board_paused
 border_x
 sea_area
 weather
 left_boat
 right_boat
 left_initial_boat_y
 right_initial_boat_y
 timerinc
 timer_turn
 timer1
 timer2
 sea_ratio
 statusitem
 rootitem
 root_weather_item
 pos_x
 pos_y
 ruleritem

Detailed Description

The Boat Racing activity

Definition at line 65 of file searace.py.


Constructor & Destructor Documentation

def searace.Gcompris_searace.__init__ (   self,
  gcomprisBoard 
)

Definition at line 69 of file searace.py.

00069 
00070   def __init__(self, gcomprisBoard):
00071     self.gcomprisBoard = gcomprisBoard
00072 
00073     self.board_paused = False
00074 
00075     # Some constants
00076     self.border_x  = 30
00077     self.sea_area = (self.border_x , 30, gcompris.BOARD_WIDTH-self.border_x , 350)
00078     self.weather   = []
00079 
00080     self.left_boat  = Boat()
00081     self.left_boat.player = 0
00082 
00083     self.right_boat = Boat()
00084     self.left_boat.player = 1
00085 
00086     self.left_initial_boat_y  = 150
00087     self.right_initial_boat_y = 150 + 28
00088 
00089     # The basic tick for object moves
00090     self.timerinc = 40
00091     self.timer_turn = 15
00092     self.timer1 = 0
00093     self.timer2 = 0
00094 
00095     # How to transform user visible sea size to pixels (calculated later)
00096     self.sea_ratio = 1
00097 
00098     # We display what's going on here
00099     self.statusitem = []
00100 
00101     #print("Gcompris_searace __init__.")
00102 


Member Function Documentation

def searace.Gcompris_searace.cmd_forward (   self,
  boat,
  value 
)

Definition at line 714 of file searace.py.

00714 
00715   def cmd_forward(self, boat, value):
00716     #    print "Player " + str(boat.player) + " cmd_forward " + str(value) + " dx=" + str(boat.dx) + " dy=" + str(boat.dy)
00717 
00718     if(self.board_paused):
00719       boat.timer = 0
00720       return
00721 
00722     value -= 1
00723     if value <= 0:
00724       # Process next command
00725       self.race_one_command(boat)
00726       boat.timer = 0
00727       return
00728 
00729     # Move it
00730     boat.x += 1
00731     boat.y += 0
00732 
00733     # We need to convert the coord to the rootitem coordinate to check limits
00734     (x, y)= boat.item.i2w( boat.x, boat.y)
00735 
00736     # Manage the wrapping
00737     if(y<self.sea_area[1]):
00738       y = self.sea_area[3]
00739       (boat.x, boat.y)= boat.item.w2i( x, y)
00740     elif(y>self.sea_area[3]):
00741       y = self.sea_area[1]
00742       (boat.x, boat.y)= boat.item.w2i( x, y)
00743     elif(x>self.sea_area[2]):
00744       boat.arrived     = True
00745       boat.finish_time = time.time()
00746       print "self.left_boat.finish_time" + str(self.left_boat.finish_time)
00747       print "self.right_boat.finish_time" + str(self.right_boat.finish_time)
00748       if(not self.left_boat.won and not self.right_boat.won):
00749         boat.won = True
00750       elif(abs(self.left_boat.finish_time - self.right_boat.finish_time) < 1):
00751         # The two boat arrived in a close time frame (1s), it's a draw
00752         self.statusitem.set(text=_("This is a draw"))
00753         self.left_boat.won  = False
00754         self.right_boat.won = False
00755         boat.speeditem.set(text="")
00756         boat.speeditem.set(text="")
00757 
00758       if(self.left_boat.won):
00759         self.statusitem.set(text=_("The Red boat has won"))
00760         boat.speeditem.set(text="")
00761       elif(self.right_boat.won):
00762         self.statusitem.set(text=_("The Green boat has won"))
00763         boat.speeditem.set(text="")
00764 
00765       boat.timer = 0
00766       return
00767 
00768     condition = self.get_weather_condition(boat)
00769 
00770     boat.item.set(x = boat.x,
00771                   y = boat.y)
00772 
00773 
00774     wind = self.get_wind_score(boat.angle, condition)
00775 
00776     #print "Player " + str(boat.player) + "  wind_angle=" + str(abs(wind_angle)) + " condition=" + str(condition[1]) + " cx=" + str(cx) + "     wind=" + str(wind)
00777     angle = condition[0]
00778     if(angle>180):
00779       angle = abs(angle-360)
00780     boat.speeditem.set(text = _("Angle:") + str(angle) + " " + _("Wind:") + str(int(wind)*-1))
00781     boat.timer = gtk.timeout_add(int(self.timerinc+wind), self.cmd_forward, boat, value)
00782 
00783 
00784 

Here is the call graph for this function:

Here is the caller graph for this function:

def searace.Gcompris_searace.cmd_turn_left (   self,
  boat,
  value 
)

Definition at line 786 of file searace.py.

00786 
00787   def cmd_turn_left(self, boat, value):
00788     #    print "Player " + str(boat.player) + " turn left " + str(value)
00789 
00790     if(self.board_paused):
00791       boat.timer = 0
00792       return
00793 
00794     if value == 0:
00795       # Process next command
00796       self.race_one_command(boat)
00797       boat.timer = 0
00798       return
00799 
00800     turn = 1
00801     if value > 0:
00802       turn = -1
00803 
00804     boat.angle += turn
00805     if(boat.angle>360):
00806       boat.angle-=360
00807     elif(boat.angle<360):
00808       boat.angle+=360
00809 
00810     value    += turn
00811 
00812     gcompris.utils.item_rotate_relative(boat.item, turn);
00813     boat.timer = gtk.timeout_add(self.timer_turn, self.cmd_turn_left, boat, value)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 236 of file searace.py.

00236 
00237   def config(self):
00238     #print("Gcompris_searace config.")
00239     return

def searace.Gcompris_searace.display_condition (   self,
  condition 
)

Definition at line 611 of file searace.py.

00611 
00612   def display_condition(self, condition):
00613 
00614     #print condition
00615     # Calc the center
00616     cx = condition[0][0]+(condition[0][2]-condition[0][0])/2
00617     cy = condition[0][1]+(condition[0][3]-condition[0][1])/2
00618 
00619     pixmap = gcompris.utils.load_pixmap("images/arrow.png")
00620     item = self.root_weather_item.add(
00621       gnomecanvas.CanvasPixbuf,
00622       pixbuf = pixmap,
00623       x=cx,
00624       y=cy,
00625       anchor=gtk.ANCHOR_CENTER
00626       )
00627     gcompris.utils.item_rotate_relative(item, condition[1][0]);
00628     item.connect("event", self.ruler_item_event)
00629 
00630     # Text number Shadow
00631     item = self.root_weather_item.add (
00632       gnomecanvas.CanvasText,
00633       text=condition[1][1],
00634       font=gcompris.skin.get_font("gcompris/content"),
00635       x=cx+1+pixmap.get_width()/2,
00636       y=cy+1+pixmap.get_height()/2,
00637       fill_color_rgba=0x000000FFL
00638       )
00639     item.connect("event", self.ruler_item_event)
00640 
00641     # Text number
00642     item = self.root_weather_item.add (
00643       gnomecanvas.CanvasText,
00644       text=condition[1][1],
00645       font=gcompris.skin.get_font("gcompris/content"),
00646       x=cx+pixmap.get_width()/2,
00647       y=cy+pixmap.get_height()/2,
00648       fill_color_rgba=0xFFFFFFFFL
00649       )
00650     item.connect("event", self.ruler_item_event)
00651 
00652     return

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 309 of file searace.py.

00309 
00310   def display_sea_area(self):
00311     # Some constant to define the sea area
00312     # The sea area is defined in the global self.sea_area
00313     step_x    = (self.sea_area[2]-self.sea_area[0])/20
00314     step_y    = (self.sea_area[3]-self.sea_area[1])/10
00315     self.sea_ratio = step_x
00316 
00317     text_x    = self.sea_area[0] - 15
00318     text_y    = self.sea_area[1] - 15
00319 
00320     # We manage a 2 colors grid
00321     ci = 0
00322     ca = 0xAACCFFFFL
00323     cb = 0x1D0DFFFFL
00324 
00325     for y in range (self.sea_area[1], self.sea_area[3]+1, int(step_y)):
00326       if(ci%2):
00327         color = ca
00328       else:
00329         color = cb
00330       ci += 1
00331 
00332       # Shadow for text number
00333       item = self.rootitem.add (
00334         gnomecanvas.CanvasText,
00335         text=int(ci),
00336         font=gcompris.skin.get_font("gcompris/content"),
00337         x=text_x+1,
00338         y=y+1,
00339         fill_color_rgba=0x000000FFL
00340         )
00341       item.connect("event", self.ruler_item_event)
00342 
00343       # Text number
00344       item = self.rootitem.add (
00345         gnomecanvas.CanvasText,
00346         text=int(ci),
00347         font=gcompris.skin.get_font("gcompris/content"),
00348         x=text_x,
00349         y=y,
00350         fill_color_rgba=cb
00351         )
00352       item.connect("event", self.ruler_item_event)
00353 
00354       item = self.rootitem.add(
00355         gnomecanvas.CanvasLine,
00356         points=(self.sea_area[0], y, self.sea_area[2], y),
00357         fill_color_rgba = color,
00358          width_units=1.0
00359         )
00360       item.connect("event", self.ruler_item_event)
00361 
00362 
00363     ci = 0
00364     for x in range (self.sea_area[0], self.sea_area[2]+1, int(step_x)):
00365       if(ci%2):
00366         color = ca
00367       else:
00368         color = cb
00369       ci += 1
00370 
00371       # Shadow for text number
00372       item = self.rootitem.add (
00373         gnomecanvas.CanvasText,
00374         text=int(ci),
00375         font=gcompris.skin.get_font("gcompris/content"),
00376         x=x+1,
00377         y=text_y+1,
00378         fill_color_rgba=0x000000FFL
00379         )
00380       item.connect("event", self.ruler_item_event)
00381 
00382       # Text number
00383       item = self.rootitem.add (
00384         gnomecanvas.CanvasText,
00385         text=int(ci),
00386         font=gcompris.skin.get_font("gcompris/content"),
00387         x=x,
00388         y=text_y,
00389         fill_color_rgba=cb
00390         )
00391       item.connect("event", self.ruler_item_event)
00392 
00393       item = self.rootitem.add(
00394         gnomecanvas.CanvasLine,
00395         points=(x, self.sea_area[1], x, self.sea_area[3]),
00396         fill_color_rgba = color,
00397          width_units=1.0
00398         )
00399       item.connect("event", self.ruler_item_event)
00400 
00401 
00402     # The ARRIVAL LINE
00403     item = self.rootitem.add(
00404       gnomecanvas.CanvasLine,
00405       points=(self.sea_area[2], self.sea_area[1]-5, self.sea_area[2], self.sea_area[3]+5),
00406       fill_color_rgba = 0xFF0000FFL,
00407       width_units=5.0
00408       )
00409     item.connect("event", self.ruler_item_event)
00410 
00411     # The grid is done
00412     # ----------------
00413 
00414     # The Programming input area LEFT
00415     self.left_boat.sw = gtk.ScrolledWindow()
00416     self.left_boat.sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
00417     self.left_boat.sw.set_shadow_type(gtk.SHADOW_ETCHED_OUT)
00418 
00419     w = 250.0
00420     h = 100.0
00421     y = 400.0 # The upper limit of the text boxes
00422     x_left  = gcompris.BOARD_WIDTH/4 - 30
00423     x_right = (gcompris.BOARD_WIDTH/4)*3 + 30
00424 
00425     self.left_boat.tb = gtk.TextBuffer()
00426     self.left_boat.tv = gtk.TextView(self.left_boat.tb)
00427     self.left_boat.sw.add(self.left_boat.tv)
00428 
00429     command_example = _("right") + " 45\n" + _("forward") + " 5\n" + _("left") + " 45"
00430     self.left_boat.tb.set_text(command_example)
00431 
00432     self.left_boat.tv.set_wrap_mode(gtk.WRAP_CHAR)
00433     self.rootitem.add(
00434       gnomecanvas.CanvasWidget,
00435       widget=self.left_boat.sw,
00436       x=x_left,
00437       y=y,
00438       width=w,
00439       height= h,
00440       anchor=gtk.ANCHOR_N,
00441       size_pixels=False)
00442     self.left_boat.tv.show()
00443     self.left_boat.sw.show()
00444 
00445     # The Programming input area RIGHT
00446     self.right_boat.sw = gtk.ScrolledWindow()
00447     self.right_boat.sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
00448     self.right_boat.sw.set_shadow_type(gtk.SHADOW_ETCHED_OUT)
00449 
00450     self.right_boat.tb = gtk.TextBuffer()
00451     self.right_boat.tv = gtk.TextView(self.right_boat.tb)
00452     self.right_boat.sw.add(self.right_boat.tv)
00453 
00454     command_example = _("left") + " 45\n" + _("forward") + " 5\n" + _("right") + " 45"
00455     self.right_boat.tb.set_text(command_example)
00456 
00457     self.right_boat.tv.set_wrap_mode(gtk.WRAP_CHAR)
00458     self.rootitem.add(
00459       gnomecanvas.CanvasWidget,
00460       widget=self.right_boat.sw,
00461       x=x_right,
00462       y=y,
00463       width=w,
00464       height= h,
00465       anchor=gtk.ANCHOR_N,
00466       size_pixels=False)
00467     self.right_boat.tv.show()
00468     self.right_boat.sw.show()
00469 
00470     # Text Labels
00471     self.left_boat.speeditem = self.rootitem.add (
00472       gnomecanvas.CanvasText,
00473       text="",
00474       font=gcompris.skin.get_font("gcompris/content"),
00475       x=x_left,
00476       y=y-20,
00477       fill_color_rgba=0xFF0000FFL
00478       )
00479 
00480     self.right_boat.speeditem = self.rootitem.add (
00481       gnomecanvas.CanvasText,
00482       text="",
00483       font=gcompris.skin.get_font("gcompris/content"),
00484       x=x_right,
00485       y=y-20,
00486       fill_color_rgba=0X027308FFL
00487       )
00488 
00489     # The status area
00490     self.statusitem = self.rootitem.add (
00491       gnomecanvas.CanvasText,
00492       text="",
00493       font=gcompris.skin.get_font("gcompris/content"),
00494       x=gcompris.BOARD_WIDTH/2,
00495       y=y-40,
00496       fill_color_rgba=0X000a89FFL
00497       )
00498 
00499     # The decoration boats
00500     pixmap = gcompris.utils.load_pixmap("images/top_boat_red.png")
00501     item = self.rootitem.add(
00502       gnomecanvas.CanvasPixbuf,
00503       pixbuf = pixmap,
00504       x=25,
00505       y=y+40,
00506       anchor=gtk.ANCHOR_CENTER,
00507       )
00508     gcompris.utils.item_rotate_relative(item, -90);
00509 
00510     pixmap = gcompris.utils.load_pixmap("images/top_boat_green.png")
00511     item = self.rootitem.add(
00512       gnomecanvas.CanvasPixbuf,
00513       pixbuf = pixmap,
00514       x=gcompris.BOARD_WIDTH-25,
00515       y=y+40,
00516       anchor=gtk.ANCHOR_CENTER,
00517       )
00518     gcompris.utils.item_rotate_relative(item, -90);
00519 
00520     # The commands
00521     hl = 18
00522     y += 7
00523     text_color = 0x0000FFFFL
00524     self.rootitem.add (
00525       gnomecanvas.CanvasText,
00526       text=_("COMMANDS ARE"),
00527       font=gcompris.skin.get_font("gcompris/content"),
00528       x=gcompris.BOARD_WIDTH/2,
00529       y=y,
00530       fill_color_rgba=text_color
00531       )
00532 
00533     self.rootitem.add (
00534       gnomecanvas.CanvasText,
00535       text=_("forward"),
00536       font=gcompris.skin.get_font("gcompris/content"),
00537       x=gcompris.BOARD_WIDTH/2,
00538       y=y+hl,
00539       fill_color_rgba=text_color
00540       )
00541 
00542     self.rootitem.add (
00543       gnomecanvas.CanvasText,
00544       text=_("left"),
00545       font=gcompris.skin.get_font("gcompris/content"),
00546       x=gcompris.BOARD_WIDTH/2,
00547       y=y+hl*2,
00548       fill_color_rgba=text_color
00549       )
00550 
00551     self.rootitem.add (
00552       gnomecanvas.CanvasText,
00553       text=_("right"),
00554       font=gcompris.skin.get_font("gcompris/content"),
00555       x=gcompris.BOARD_WIDTH/2,
00556       y=y+hl*3,
00557       fill_color_rgba=text_color
00558       )
00559 

Here is the call graph for this function:

Definition at line 563 of file searace.py.

00563 
00564   def display_weather(self):
00565 
00566     # Reset the weather list
00567     self.weather   = []
00568 
00569     # Some constant to define the sea area
00570     # The sea area is defined in the global self.sea_area
00571     slice_x = 5 + self.gcomprisBoard.level
00572     slice_y = 3 + self.gcomprisBoard.level
00573 
00574     step_x  = (self.sea_area[2]-self.sea_area[0])/slice_x
00575     step_y  = (self.sea_area[3]-self.sea_area[1])/slice_y
00576 
00577     stop_x  = self.sea_area[0]+step_x*slice_x
00578     stop_y  = self.sea_area[1]+step_y*slice_y
00579 
00580     for x in range (self.sea_area[0], stop_x, int(step_x)):
00581       for y in range (self.sea_area[1], stop_y, int(step_y)):
00582         #print x, step_x, self.sea_area[2]
00583         angle = 0
00584         if(self.left_initial_boat_y>y and self.left_initial_boat_y<y+step_y):
00585           # Bad weather condition on the straigh line
00586           direction = random.randint(5,7)
00587         elif(self.right_initial_boat_y>y and self.right_initial_boat_y<y+step_y):
00588           # Bad weather condition on the straigh line
00589           direction = random.randint(5,7)
00590         else:
00591           direction = random.randint(0,7)
00592 
00593         if(direction < 4):
00594           # There is more chance to go forward than others
00595           angle = random.randint(-45,45)
00596         elif(direction < 5):
00597           angle = random.randint(135,225)
00598         elif(direction == 6):
00599           angle = random.randint(80, 110)
00600         elif(direction == 7):
00601           angle = random.randint(260, 280)
00602 
00603         speed = random.randint(1,10)
00604         condition = [ (x, y, x+step_x, y+step_y), (angle, speed) ]
00605         self.display_condition(condition)
00606         self.weather.append(condition)
00607 
00608 
00609     return

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 155 of file searace.py.

00155 
00156   def end(self):
00157     # Remove all the timer first
00158     if self.timer1 :
00159       gtk.timeout_remove(self.timer1)
00160 
00161     if self.timer2 :
00162       gtk.timeout_remove(self.timer2)
00163 
00164     if self.left_boat.timer :
00165       gtk.timeout_remove(self.left_boat.timer)
00166 
00167     if self.right_boat.timer :
00168       gtk.timeout_remove(self.right_boat.timer)
00169 
00170     # Remove the root item removes all the others inside it
00171     self.rootitem.destroy()
00172 
00173     #print("Gcompris_searace end.")
00174 

Definition at line 677 of file searace.py.

00677 
00678   def get_absolute_weather_condition(self, x, y):
00679     for conditions in self.weather:
00680       coord = conditions[0]
00681       condition = conditions[1]
00682       #print "Testing coord="+str(coord)+" Boat coord x="+str(x)+" y="+str(y)
00683       if(x >= coord[0] and x <= coord[2] and y >= coord[1] and y <= coord[3]):
00684         #print "Found: x="+str(x)+" y="+str(y)+" coord="+str(coord)
00685         #print "  angle=" + str(int(condition[0])) + "  speed=" + str(condition[1])
00686         return(condition)
00687     # Should not happen, return a normal condition anyway
00688     return(0,1)

Here is the caller graph for this function:

Definition at line 654 of file searace.py.

00654 
00655   def get_weather_condition(self, boat):
00656     (x, y)= boat.item.i2w( boat.x, boat.y)
00657 
00658     # Look in the cache to speed the process
00659     if(boat.condition):
00660       coord = boat.condition[0]
00661       condition = boat.condition[1]
00662       if(boat.x >= coord[0] and boat.x <= coord[2] and boat.y >= coord[1] and boat.y <= coord[3]):
00663         return(condition)
00664 
00665     for conditions in self.weather:
00666       coord = conditions[0]
00667       condition = conditions[1]
00668       #print "Testing coord="+str(coord)+" Boat coord x="+str(x)+" y="+str(y)
00669       if(x >= coord[0] and x <= coord[2] and y >= coord[1] and y <= coord[3]):
00670         boat.condition = conditions
00671         #print "Found: x="+str(x)+" y="+str(y)+" coord="+str(coord)
00672         #print "  angle=" + str(int(condition[0])) + "  speed=" + str(condition[1])
00673         return(condition)
00674     # Should not happen, return a normal condition anyway
00675     return(0,1)

Here is the caller graph for this function:

def searace.Gcompris_searace.get_wind_score (   self,
  boat_angle,
  condition 
)

Definition at line 690 of file searace.py.

00690 
00691   def get_wind_score(self, boat_angle, condition):
00692     # Calculate the timer inc depending on the wind angle + speed
00693     wind_angle = condition[0] - boat_angle
00694     if(wind_angle>360):
00695       wind_angle-=360
00696     elif(wind_angle<-360):
00697       wind_angle+=360
00698 
00699     if(abs(wind_angle)>180):
00700       wind_angle=180-(abs(wind_angle)-180)
00701 
00702     # Increase the timer depending on wind force and direction
00703     angle_pi = wind_angle*math.pi/180
00704 
00705     cx = math.cos(angle_pi)
00706     penalty=3
00707     if(cx<0):
00708       penalty*=2
00709 
00710     return(cx*condition[1]*-1*penalty)

Here is the caller graph for this function:

Definition at line 249 of file searace.py.

00249 
00250   def init_boats(self):
00251 
00252     self.left_boat.x      = self.border_x
00253     self.left_boat.y      = self.left_initial_boat_y
00254     self.right_boat.x     = self.left_boat.x
00255     self.right_boat.y     = self.right_initial_boat_y
00256     self.left_boat.angle  = 0
00257     self.right_boat.angle = 0
00258 
00259     # Display the player boats
00260     if(self.left_boat.item):
00261       self.left_boat.item.destroy()
00262 
00263     pixmap = gcompris.utils.load_pixmap("images/top_boat_red.png")
00264     self.left_boat.item = self.rootitem.add(
00265       gnomecanvas.CanvasPixbuf,
00266       pixbuf = pixmap,
00267       x=self.left_boat.x,
00268       y=self.left_boat.y,
00269       anchor=gtk.ANCHOR_CENTER,
00270       )
00271     self.left_boat.item.raise_to_top()
00272     self.left_boat.item.connect("event", self.ruler_item_event)
00273 
00274     if(self.right_boat.item):
00275       self.right_boat.item.destroy()
00276 
00277     pixmap = gcompris.utils.load_pixmap("images/top_boat_green.png")
00278     self.right_boat.item = self.rootitem.add(
00279       gnomecanvas.CanvasPixbuf,
00280       pixbuf = pixmap,
00281       x=self.right_boat.x,
00282       y=self.right_boat.y,
00283       anchor=gtk.ANCHOR_CENTER,
00284       )
00285     self.right_boat.item.raise_to_top()
00286     self.right_boat.item.connect("event", self.ruler_item_event)
00287 
00288 
00289     # Reset command line processing as well.
00290     self.left_boat.line     = 0
00291     self.right_boat.line    = 0
00292     self.left_boat.arrived  = False
00293     self.right_boat.arrived = False
00294     self.left_boat.won      = False
00295     self.right_boat.won     = False
00296     self.statusitem.set(text="")
00297 
00298     # Let the user enter comands
00299     self.left_boat.tv.set_editable(True)
00300 
00301     if self.gcomprisBoard.mode == '1player':
00302       self.tux_move()
00303     else:
00304       self.right_boat.tv.set_editable(True)
00305 

Here is the call graph for this function:

Here is the caller graph for this function:

def searace.Gcompris_searace.key_press (   self,
  keyval,
  commit_str,
  preedit_str 
)

Definition at line 240 of file searace.py.

00240 
00241   def key_press(self, keyval, commit_str, preedit_str):
00242     #print("got key %i" % keyval)
00243     return False

Definition at line 191 of file searace.py.

00191 
00192   def ok(self):
00193     # This is a real go
00194     # We set a timer. At each tick an entry in each user box is read analysed and run
00195     if(not self.left_boat.timer and not self.right_boat.timer):
00196       self.left_boat.tv.set_editable(False)
00197       self.right_boat.tv.set_editable(False)
00198       self.race_one_command(self.left_boat)
00199       self.race_one_command(self.right_boat)
00200     else:
00201       self.statusitem.set(text=_("The race is already being run"))

Here is the call graph for this function:

def searace.Gcompris_searace.pause (   self,
  pause 
)

Definition at line 175 of file searace.py.

00175 
00176   def pause(self, pause):
00177 
00178     self.board_paused = pause
00179 
00180     # There is a problem with GTK widgets, they are not covered by the help
00181     # We hide/show them here
00182     if(pause):
00183       self.left_boat.sw.hide()
00184       self.right_boat.sw.hide()
00185     else:
00186       self.left_boat.sw.show()
00187       self.right_boat.sw.show()
00188       self.repeat()
00189 
00190     return

Here is the call graph for this function:

def searace.Gcompris_searace.race_one_command (   self,
  boat 
)

Definition at line 815 of file searace.py.

00815 
00816   def race_one_command(self, boat):
00817 
00818     if(self.board_paused):
00819       # Let the user enter commands
00820       boat.tv.set_editable(True)
00821 
00822       boat.line = 0
00823       boat.timer = 0
00824       return
00825 
00826     valid_cmd = False
00827     cmd = ""
00828     while(boat.line < boat.tb.get_line_count() and not valid_cmd):
00829       a = boat.tb.get_iter_at_line(boat.line)
00830       b = boat.tb.get_iter_at_line(boat.line)
00831       b.forward_to_line_end()
00832       cmd   = boat.tb.get_text(a, b, False)
00833       boat.line+=1
00834       if(cmd and cmd[0] == "\n"):
00835         boat.line+=1
00836       print "Processing cmd = " + cmd
00837       cmd   = cmd.strip("\n\t ")
00838       if(cmd != "" and cmd[0] != "#"):
00839         valid_cmd = True
00840 
00841     # No more commands to process for this player
00842     if (boat.line > boat.tb.get_line_count() or not valid_cmd):
00843       # Let the user enter commands
00844       boat.tv.set_editable(True)
00845       # Ready to Restart
00846       boat.line = 0
00847       boat.timer = 0
00848       return
00849 
00850     #print "boat.line=" + str(boat.line)
00851     #print "Parsing command = " + cmd + "<<"
00852     cmds  = cmd.split()
00853     # Manage default cases (no params given)
00854     if ( len(cmds) == 1 and cmd.startswith(_("forward")) ):
00855       cmd += " 1"
00856     elif ( len(cmds) == 1 and cmd.startswith(_("left")) ):
00857       cmd += " 45"
00858     elif ( len(cmds) == 1 and cmd.startswith(_("right")) ):
00859       cmd += " 45"
00860     elif ( len(cmds) > 2):
00861       boat.speeditem.set(text=_("Syntax error at line") + " " + str(boat.line) + "\n(" + cmd + ")")
00862 
00863       # Let the user enter commands
00864       boat.tv.set_editable(True)
00865 
00866       boat.line = 0
00867       boat.timer = 0
00868       return
00869 
00870     value = 0
00871     if(len(cmds) == 2):
00872       try:
00873         value = int(cmd.split()[1])
00874       except ValueError:
00875         # Let the user enter commands
00876         boat.tv.set_editable(True)
00877 
00878         boat.timer = 0
00879         boat.speeditem.set(text=_("The command") + " '" + cmd.split()[0] + "' " + "at line" + " " + str(boat.line) + "\n" + "requires a number parameter")
00880         boat.line = 0
00881         return
00882 
00883     if( cmd.startswith(_("forward"))):
00884       # Transform the value from user visible sea size to pixels
00885       value *= self.sea_ratio
00886 
00887       # Initialize the move
00888       boat.timer = gtk.timeout_add(self.timerinc, self.cmd_forward, boat, value)
00889     elif( cmd.startswith(_("left"))):
00890       boat.timer = gtk.timeout_add(self.timerinc, self.cmd_turn_left, boat, value)
00891     elif( cmd.startswith(_("right"))):
00892       boat.timer = gtk.timeout_add(self.timerinc, self.cmd_turn_left, boat, value*-1)
00893     else:
00894       # Let the user enter commands
00895       boat.tv.set_editable(True)
00896 
00897       boat.timer = 0
00898       boat.speeditem.set(text=_("Unknown command at line") + " " + str(boat.line) + "\n(" +  cmd.split()[0] + ")")
00899       boat.line = 0

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 228 of file searace.py.

00228 
00229   def repeat(self):
00230     # Want to rerun it
00231     if(self.left_boat.timer or self.right_boat.timer):
00232       self.statusitem.set(text=_("The race is already being run"))
00233     else:
00234       self.init_boats()
00235 

Here is the call graph for this function:

Here is the caller graph for this function:

def searace.Gcompris_searace.ruler_item_event (   self,
  widget,
  event = None 
)

Definition at line 1000 of file searace.py.

01000 
01001   def ruler_item_event(self, widget, event=None):
01002     if event.type == gtk.gdk.BUTTON_PRESS:
01003       if event.button == 1:
01004         self.pos_x = event.x
01005         self.pos_y = event.y
01006         self.ruleritem = self.rootitem.add(
01007           gnomecanvas.CanvasLine,
01008           points=( self.pos_x, self.pos_y, event.x, event.y),
01009           fill_color_rgba=0xFF0000FFL,
01010           width_units=2.0
01011           )
01012         return True
01013     if event.type == gtk.gdk.MOTION_NOTIFY:
01014       if event.state & gtk.gdk.BUTTON1_MASK:
01015         # Calc the angle and distance and display them in the status bar
01016         distance = math.sqrt((self.pos_x-event.x)*(self.pos_x-event.x)+(self.pos_y-event.y)*(self.pos_y-event.y))
01017         distance = int(distance/self.sea_ratio)
01018 
01019         angle = math.atan2(abs(self.pos_x-event.x), abs(self.pos_y-event.y))
01020         angle = int(angle*180/math.pi)
01021         angle = abs(angle - 90)
01022         self.statusitem.set(text=_("Distance:") + " " + str(distance) + " " + _("Angle:") + " " + str(angle))
01023         self.ruleritem.set(
01024           points=( self.pos_x, self.pos_y, event.x, event.y)
01025           )
01026     if event.type == gtk.gdk.BUTTON_RELEASE:
01027       if event.button == 1:
01028         self.ruleritem.destroy()
01029         self.statusitem.set(text="")
01030         return True
01031     return False
01032 

Here is the caller graph for this function:

def searace.Gcompris_searace.set_level (   self,
  level 
)

Definition at line 203 of file searace.py.

00203 
00204   def set_level(self, level):
00205     if(self.left_boat.timer or self.right_boat.timer):
00206       self.statusitem.set(text=_("The race is already being run"))
00207     else:
00208       self.gcomprisBoard.level=level;
00209       self.gcomprisBoard.sublevel=1;
00210 
00211       # Set the level in the control bar
00212       gcompris.bar_set_level(self.gcomprisBoard);
00213 
00214       # Remove the root item removes all the others inside it
00215       self.root_weather_item.destroy()
00216 
00217       self.root_weather_item = self.rootitem.add(
00218         gnomecanvas.CanvasGroup,
00219         x=0.0,
00220         y=0.0
00221         )
00222 
00223       # Display the weather now
00224       self.display_weather()
00225 
00226       self.init_boats()
00227 

Here is the call graph for this function:

Definition at line 103 of file searace.py.

00103 
00104   def start(self):
00105     self.gcomprisBoard.level=1
00106     self.gcomprisBoard.maxlevel=4
00107     self.gcomprisBoard.sublevel=1
00108     self.gcomprisBoard.number_of_sublevel=1
00109 
00110     self.board_paused = False
00111 
00112     pixmap = gcompris.utils.load_pixmap(gcompris.skin.image_to_skin("button_reload.png"))
00113     if(pixmap):
00114       gcompris.bar_set_repeat_icon(pixmap)
00115       gcompris.bar_set(gcompris.BAR_OK|gcompris.BAR_LEVEL|gcompris.BAR_REPEAT_ICON)
00116     else:
00117       gcompris.bar_set(gcompris.BAR_OK|gcompris.BAR_LEVEL|gcompris.BAR_REPEAT);
00118 
00119 
00120     gcompris.bar_set_level(self.gcomprisBoard)
00121 
00122     # Create our rootitem. We put each canvas item in it so at the end we
00123     # only have to kill it. The canvas deletes all the items it contains automaticaly.
00124     self.rootitem = self.gcomprisBoard.canvas.root().add(
00125       gnomecanvas.CanvasGroup,
00126       x=0.0,
00127       y=0.0
00128       )
00129 
00130     pixmap = gcompris.utils.load_pixmap(gcompris.skin.image_to_skin("gcompris-bg.jpg"))
00131     item = self.rootitem.add(
00132       gnomecanvas.CanvasPixbuf,
00133       pixbuf = pixmap,
00134       x=0,
00135       y=0,
00136       )
00137     item.connect("event", self.ruler_item_event)
00138 
00139     self.display_sea_area()
00140 
00141     self.root_weather_item = self.rootitem.add(
00142       gnomecanvas.CanvasGroup,
00143       x=0.0,
00144       y=0.0
00145       )
00146 
00147     # Display the weather now
00148     self.display_weather()
00149 
00150     # And finaly the players boats
00151     self.init_boats()
00152 
00153     #print("Gcompris_searace start.")
00154 

Definition at line 901 of file searace.py.

00901 
00902   def tux_move(self):
00903 
00904     # The sea area is defined in the global self.sea_area
00905     step_x    = (self.sea_area[2]-self.sea_area[0])/20*2
00906     step_y    = (self.sea_area[3]-self.sea_area[1])/10
00907 
00908     # Original boat position
00909     bx     = self.right_boat.x
00910     by     = self.right_boat.y
00911     ba     = 0
00912     one_path = []
00913 
00914     # X loop
00915     while (bx <= self.sea_area[2] and bx >= self.sea_area[0]):
00916       score = 10000
00917       # By default, go straight
00918       coord = (bx, by, ba, int(step_x))
00919 
00920       # angle loop
00921       for boat_angle in [ -45, 0, 45 ]:
00922         a_pi = boat_angle * math.pi/180
00923         # Find the point from current boat position with angle a and distance step_x
00924         x = bx + math.cos(a_pi)*step_x
00925         y = by + math.sin(a_pi)*step_x
00926 
00927         # Manage the wrapping
00928         line_style = gtk.gdk.LINE_SOLID
00929         if(y<self.sea_area[1]):
00930           y = self.sea_area[3] - (self.sea_area[1]-y)
00931           line_style = gtk.gdk.LINE_DOUBLE_DASH
00932         elif(y>self.sea_area[3]):
00933           y = self.sea_area[1] + (y - self.sea_area[3])
00934           line_style = gtk.gdk.LINE_DOUBLE_DASH
00935 
00936         # Find shortest path to previous calculated point
00937         condition = self.get_absolute_weather_condition(x, y)
00938         wind = self.get_wind_score(boat_angle, condition)
00939         if(wind<score):
00940           score = wind
00941           coord = (x, y, boat_angle, step_x, line_style) # x y angle distance line_style
00942 
00943         self.root_weather_item.add(
00944           gnomecanvas.CanvasText, text=int(wind),
00945           y=y, x=x,
00946           fill_color_rgba=0xFF0000FFL,
00947           )
00948 
00949       # ----------
00950       self.root_weather_item.add(
00951         gnomecanvas.CanvasLine,
00952         points=(bx, by, coord[0], coord[1]),
00953         fill_color_rgba=0x00CC00FFL,
00954         width_units=2.0,
00955         line_style=coord[4]
00956         )
00957       bx = coord[0]
00958       by = coord[1]
00959       ba = coord[2]
00960       one_path.append(coord)
00961 
00962     # --------------------------------------------------------------
00963     # Translate the previous calculation in a string for Tux program
00964     # --------------------------------------------------------------
00965 
00966     ba = 0                              # Boat angle
00967     cumulative_distance = 0
00968     tux_move = ""
00969     for c in one_path:
00970       #print "X,Y,A,D=" + str(c)
00971       x=c[0]
00972       y=c[1]
00973       a=c[2]                            # angle
00974       d=c[3]                            # distance
00975 
00976       if(ba-a<0):
00977         if(cumulative_distance):
00978           tux_move += _("forward") + " " + str(cumulative_distance) + "\n"
00979           cumulative_distance=0
00980         tux_move += _("right") + " " + str(abs(int(ba-a))) + "\n"
00981         ba += abs(int(ba-a))
00982       elif(ba-a>0):
00983         if(cumulative_distance):
00984           tux_move += _("forward") + " " + str(cumulative_distance) + "\n"
00985           cumulative_distance=0
00986         tux_move += _("left") + " " + str(abs(int(ba-a))) + "\n"
00987         ba -= abs(int(ba-a))
00988 
00989       cumulative_distance += int(d/self.sea_ratio)
00990 
00991     # Final move, add an ofset because we loose space in abs()
00992     tux_move += _("forward") + " " + str(cumulative_distance+2) + "\n"
00993 
00994     self.right_boat.tb.set_text(tux_move)
00995     self.right_boat.tv.set_editable(False)
00996 

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 72 of file searace.py.

Definition at line 75 of file searace.py.

Definition at line 70 of file searace.py.

Definition at line 79 of file searace.py.

Definition at line 85 of file searace.py.

Definition at line 1003 of file searace.py.

Definition at line 1004 of file searace.py.

Definition at line 82 of file searace.py.

Definition at line 86 of file searace.py.

Definition at line 140 of file searace.py.

Definition at line 123 of file searace.py.

Definition at line 1005 of file searace.py.

Definition at line 76 of file searace.py.

Definition at line 95 of file searace.py.

Definition at line 98 of file searace.py.

Definition at line 91 of file searace.py.

Definition at line 92 of file searace.py.

Definition at line 90 of file searace.py.

Definition at line 89 of file searace.py.

Definition at line 77 of file searace.py.


The documentation for this class was generated from the following file: