home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 124 / cd-rom 124.iso / edu / tuxmath / tuxmathscrabble / asymptopia / tuxmathscrabble.py < prev    next >
Encoding:
Python Source  |  2003-11-11  |  25.7 KB  |  752 lines

  1. #!/usr/bin/env python
  2. """
  3. /***************************************************************************
  4.  
  5.     Author             :Charles B. Cosse 
  6.     
  7.     Email            :ccosse@asymptopia.com
  8.                     
  9.                     
  10.     Copyright        :(C) 2002,2003 Asymptopia Software.
  11.     
  12.  ***************************************************************************/
  13. /***************************************************************************
  14.                           tuxmathscrabble.py
  15.  
  16.  ***************************************************************************/
  17.  
  18. /***************************************************************************
  19.  *                                                                         *
  20.  *   This program is free software; you can redistribute it and/or modify  *
  21.  *   it under the terms of the GNU General Public License as published by  *
  22.  *   the Free Software Foundation; either version 2 of the License, or     *
  23.  *   (at your option) any later version. (Please note that if you use this *
  24.  *   code you must give credit by including the Author and Copyright       *
  25.  *   info at the top of this file).                                        *
  26.  ***************************************************************************/
  27. """
  28.  
  29.  
  30.  
  31. import os, pygame,sys
  32. from pygame.locals import *
  33. from random import random
  34. from asymptopia.myutil import load_image
  35.         
  36. from asymptopia.Board import *
  37. from asymptopia.Spot import *
  38. from asymptopia.myutil import *
  39. from asymptopia.Tile import *
  40. from asymptopia.Button import *
  41. from asymptopia.Validator import *
  42. from asymptopia.Localizer import *
  43. from asymptopia.Tux import *
  44. from asymptopia.Animator import *
  45. from asymptopia.AnimatedChar import *
  46. from asymptopia.CuckooManeuvers import CuckooManeuvers
  47. from asymptopia.TuxManeuvers import TuxManeuvers
  48.  
  49. #get path to site-packages:
  50. try:
  51.     for sitepkgdir in sys.path:
  52.         if sitepkgdir[-13:]=='site-packages':break
  53. except:pass
  54.     
  55.  
  56. if not pygame.font: print 'Warning, fonts disabled'
  57. if not pygame.mixer: print 'Warning, sound disabled'
  58. M=13
  59. N=17
  60.  
  61. W=800
  62. H=600
  63. DEFAULT_LEVEL=5
  64. #WAIT_TIME=300
  65. WAIT_TIME=100
  66.  
  67. class TuxMathScrabble:
  68.     def __init__(self,level):
  69.         pygame.init()
  70.         screen = pygame.display.set_mode((W,H))
  71.         pygame.display.set_caption('www.asymptopia.com')
  72.         pygame.mouse.set_visible(1)
  73.         
  74.         self.pyfont=pygame.font.Font(os.path.join(sitepkgdir,'asymptopia/font/freesansbold.ttf'),16)
  75.         
  76.         self.LEVEL=level
  77.         self.tux_wait_counter=0
  78.         
  79.         slide1=load_sound('asymptopia/sounds/slide01.wav')
  80.         slide2=load_sound('asymptopia/sounds/slide02.wav')
  81.         boink=load_sound('asymptopia/sounds/bong05.wav')
  82.         kerplunk=load_sound('asymptopia/sounds/beep07.wav')
  83.         win_sound_01=load_sound('asymptopia/sounds/sparcle.wav')
  84.         win_sound_02=load_sound('asymptopia/sounds/bullcall.wav')
  85.         win_sound_03=load_sound('asymptopia/sounds/aooga.wav')
  86.         lose_sound=load_sound('asymptopia/sounds/foghorn2.wav')
  87.         pop01=load_sound('asymptopia/sounds/pop01.wav')
  88.         pop02=load_sound('asymptopia/sounds/pop01.wav')
  89.         
  90.         
  91.         #_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
  92.         animator=Animator()
  93.         
  94.         self.tuxManeuvers=TuxManeuvers()
  95.         AnimatedTux=AnimatedChar(self.tuxManeuvers.getSequencesPyld(),W/2,100)
  96.         animator.add_char("Tux",AnimatedTux)
  97.         
  98.         tuxManeuversPyld=self.tuxManeuvers.getManeuversPyld()
  99.         for key in tuxManeuversPyld:
  100.             animator.define_maneuver(key,tuxManeuversPyld[key])
  101.         
  102.         self.cuckooManeuvers=CuckooManeuvers()
  103.         AnimatedTiles=[]
  104.         AnimatedTileNames=[]
  105.         for idx in range(N):
  106.             AnimatedTiles.append(AnimatedChar(self.cuckooManeuvers.getSequencesPyld(),-25,-25))#offscreen
  107.             new_name="Tile_%02d"%(idx)
  108.             AnimatedTileNames.append(new_name)
  109.             animator.add_char(AnimatedTileNames[idx],AnimatedTiles[idx])
  110.         
  111.         cuckooManeuversPyld=self.cuckooManeuvers.getManeuversPyld()
  112.         for key in cuckooManeuversPyld:
  113.             animator.define_maneuver(key,cuckooManeuversPyld[key])
  114.  
  115.         #_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
  116.         
  117.         
  118.         #onward...
  119.         help_button=Button('asymptopia/images/help.gif')
  120.         help_button.rect.center=(W-70,H-285)
  121.         credit_button=Button('asymptopia/images/credits.gif')
  122.         credit_button.rect.center=(W-70,H-235)
  123.         skip_button=Button('asymptopia/images/skip.gif')
  124.         skip_button.rect.center=(W-70,H-135)
  125.         ready_button=Button('asymptopia/images/ready.gif')
  126.         ready_button.rect.center=(W-70,H-185)
  127.  
  128.         l1_button=Button('asymptopia/images/level_one.gif')
  129.         l1_button.rect.center=(65,H-135)
  130.         l2_button=Button('asymptopia/images/level_two.gif')
  131.         l2_button.rect.center=(65,H-185)
  132.         l3_button=Button('asymptopia/images/level_three.gif')
  133.         l3_button.rect.center=(65,H-235)
  134.         l4_button=Button('asymptopia/images/level_four.gif')
  135.         l4_button.rect.center=(65,H-285)
  136.             
  137.         buttongroup=pygame.sprite.Group([help_button,ready_button,skip_button,credit_button,l1_button,l2_button,l3_button,l4_button])
  138.         buttons=pygame.sprite.RenderPlain(buttongroup)
  139.  
  140.         background = pygame.Surface(screen.get_size())
  141.         background = background.convert()
  142.         background.fill((0, 150, 100))#green
  143.         
  144.         level_indicator = pygame.Surface((10,10))
  145.         level_indicator = level_indicator.convert()
  146.         level_indicator.fill((255, 0, 0))
  147.         
  148.         #Specific Test beyond this point:
  149.         board=Board(M,N,W/2,H/2,None,'asymptopia/images/new_spot.gif')
  150.         boardspots=pygame.sprite.RenderPlain(board.get_spots())
  151.         
  152.         if self.LEVEL==3 or self.LEVEL==4:
  153.             NTRAYSPOTS=12
  154.             NNUMBERS=7
  155.         else:
  156.             NTRAYSPOTS=10
  157.             NNUMBERS=6
  158.             
  159.         tray=Board(1,NTRAYSPOTS,W/2,H-40,None,'asymptopia/images/new_spot.gif')
  160.         trayspots=pygame.sprite.RenderPlain(tray.get_spots())
  161.         
  162.         submission=Board(M,N,W/2,H/2,None,'asymptopia/images/new_spot.gif')
  163.         submissionspots=pygame.sprite.RenderPlain()
  164.         
  165.         #drag-n-drop object    
  166.         target=pygame.sprite.RenderClear()
  167.         
  168.         validator=Validator(board,self)#self passed for score update
  169.         localizer=Localizer(board,self)#self passed for score update
  170.  
  171.         self.NTRAYSPOTS=NTRAYSPOTS
  172.         self.NNUMBERS=NNUMBERS
  173.         self.localizer=localizer
  174.         self.validator=validator
  175.         self.screen=screen
  176.         self.boink=boink
  177.         self.kerplunk=kerplunk
  178.         self.slide1=slide1
  179.         self.slide2=slide2
  180.         self.win_sound_01=win_sound_01
  181.         self.win_sound_02=win_sound_02
  182.         self.win_sound_03=win_sound_03
  183.         self.lose_sound=lose_sound
  184.         self.submissionspots=submissionspots
  185.         self.submission=submission
  186.         self.board=board
  187.         self.boardspots=boardspots
  188.         self.tray=tray
  189.         self.trayspots=trayspots
  190.         self.target=target
  191.         self.background=background
  192.         self.level_indicator=level_indicator
  193.         self.buttons=buttons
  194.         self.help_button=help_button
  195.         self.ready_button=ready_button
  196.         self.skip_button=skip_button
  197.         self.credit_button=credit_button
  198.         self.l1_button=l1_button
  199.         self.l2_button=l2_button
  200.         self.l3_button=l3_button
  201.         self.l4_button=l4_button
  202.         self.animator=animator
  203.         self.AnimatedTux=AnimatedTux
  204.         self.AnimatedTiles=AnimatedTiles
  205.         self.AnimatedTileNames=AnimatedTileNames
  206.         self.num_animated_tiles=0
  207.         self.animation_in_progress=0
  208.         self.tuxturn=0
  209.     
  210.         tuxtray=Board(1,NTRAYSPOTS,W/2,70,None,'asymptopia/images/new_spot.gif')
  211.         tuxtrayspots=pygame.sprite.RenderPlain(tuxtray.get_spots())
  212.         self.tuxtrayspots=tuxtrayspots
  213.         self.tuxtray=tuxtray
  214.         tux=Tux(self)
  215.         self.tux=tux
  216.         
  217.     def draw_tiles(self,spots,who):
  218.         imgnames=[
  219.             'zero','one','two','three','four','five','six','seven','eight','nine','ten',
  220.             'eleven','twelve','thirteen','fourteen','fifteen',
  221.             #'sixteen','seventeen','eighteen','nineteen',
  222.             'twenty'
  223.         ]
  224.         for spot in spots:
  225.             if spot.occupied():continue
  226.             if (spot.getMN()[1]<self.NNUMBERS):
  227.                 if self.LEVEL==4:int_val=int(16*random())+1#1-20, no zero
  228.                 elif self.LEVEL==3:int_val=int(10*random())+1#1-20, no zero
  229.                 elif self.LEVEL==2:int_val=int(15*random())#1-15
  230.                 else:int_val=int(10*random())#1-10
  231.                 str_val=`int_val`+'.0'
  232.                 
  233.                 #special case for "20":(since skipping 16,17,18,19)
  234.                 if int_val==16:
  235.                     str_val='20.0'
  236.                 
  237.                 #print str_val,int_val
  238.                 if who=='tux':fname='asymptopia/images/tux_'+imgnames[int_val]+'.gif'
  239.                 else:fname='asymptopia/images/'+imgnames[int_val]+'.gif'
  240.             elif spot.getMN()[1]<self.NTRAYSPOTS-1:
  241.                 opnames=['new_plus','new_minus','new_mult','new_divide']
  242.                 if self.LEVEL==4:idx=int(4*random())#add,subtract,mult,divide
  243.                 elif self.LEVEL==3:idx=int(3*random())#add,subtract,mult,divide
  244.                 else:idx=int(2*random())#add,subtract only
  245.                 int_val=None
  246.                 if   opnames[idx]=='new_plus':str_val='+'
  247.                 elif opnames[idx]=='new_minus':str_val='-'
  248.                 elif opnames[idx]=='new_mult':str_val='*'
  249.                 elif opnames[idx]=='new_divide':str_val='/'
  250.                 else:pass
  251.                 if who=='tux':opnames[idx]='tux_'+opnames[idx]
  252.                 fname='asymptopia/images/'+opnames[idx]+'.gif'
  253.             else:
  254.                 if who=='tux':fname='asymptopia/images/tux_new_equal.gif'
  255.                 else:fname='asymptopia/images/new_equal.gif'
  256.                 int_val=None
  257.                 str_val='='
  258.             tile=Tile(fname,int_val,str_val)
  259.             spot.take_guest(tile,1)
  260.             tile.saved_center=spot.rect.center
  261.             
  262.         
  263.     def play(self):
  264.         
  265.         AnimatedTiles=self.AnimatedTiles
  266.         AnimatedTileNames=self.AnimatedTileNames
  267.         
  268.         
  269.         AnimatedTux=self.AnimatedTux
  270.         animator=self.animator
  271.         tux=self.tux
  272.         validator=self.validator
  273.         localizer=self.localizer
  274.         screen=self.screen
  275.         boink=self.boink
  276.         kerplunk=self.kerplunk
  277.         slide1=self.slide1
  278.         slide2=self.slide2
  279.         win_sound_01=self.win_sound_01
  280.         win_sound_02=self.win_sound_02
  281.         win_sound_03=self.win_sound_03
  282.         lose_sound=self.lose_sound
  283.         submissionspots=self.submissionspots
  284.         submission=self.submission
  285.         board=self.board
  286.         boardspots=self.boardspots
  287.         tray=self.tray
  288.         trayspots=self.trayspots
  289.         target=self.target
  290.         background=self.background
  291.         level_indicator=self.level_indicator
  292.         buttons=self.buttons
  293.         help_button=self.help_button
  294.         ready_button=self.ready_button
  295.         skip_button=self.skip_button
  296.         credit_button=self.credit_button
  297.         l1_button=self.l1_button
  298.         l2_button=self.l2_button
  299.         l3_button=self.l3_button
  300.         l4_button=self.l4_button
  301.         tuxtrayspots=self.tuxtrayspots    
  302.         tuxtray=self.tuxtray
  303.         self.draw_tiles(tray.get_spots(),'player')
  304.         self.draw_tiles(tuxtray.get_spots(),'tux')
  305.         tuxturn=self.tuxturn
  306.         num_animated_tiles=self.num_animated_tiles
  307.         animation_in_progress=self.animation_in_progress
  308.         
  309.         self.tuxscore=0
  310.         self.playerscore=0
  311.         
  312.         #
  313.         coin=0#int(random()*2)
  314.         if coin==0:animator.start_maneuver('Tux','tux_welcome_maneuver_01')
  315.         #else:animator.start_maneuver('Tux','tux_welcome_maneuver_02')
  316.         
  317.         animation_in_progress=1
  318.         tux_move_progress=0
  319.         tile_animation_in_progress=0
  320.         num_animated_tiles=0
  321.         
  322.         while(1):
  323.             num_animated_tiles=self.num_animated_tiles
  324.             animation_in_progress=self.animation_in_progress
  325.             if (tuxturn==1) and (tux_move_progress==0) and (animation_in_progress==0) and (num_animated_tiles==0):#only @beginning of Tux's turn
  326.                 #_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
  327.                 #print 'starting tux_get_up_maneuver'
  328.                 coin=int(random()*4)
  329.                 if coin==0:animator.start_maneuver('Tux','tux_get_up_maneuver_01')
  330.                 elif coin==1:animator.start_maneuver('Tux','tux_get_up_maneuver_02')
  331.                 elif coin==2:animator.start_maneuver('Tux','tux_get_up_maneuver_03')
  332.                 else:animator.start_maneuver('Tux','tux_get_up_maneuver_04')
  333.                 
  334.                 tux_move_progress=1
  335.                 animation_in_progress=1
  336.                 #_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
  337.                 
  338.             if ((num_animated_tiles==0) and (animation_in_progress==0) and (tuxturn==1)):
  339.                 
  340.                 tux.generate_expressions()
  341.                 tux_finished=0
  342.                 
  343.                 while tux_finished!=1:
  344.                     tux_submission=tux.generate_options()
  345.                     rlist=tux_submission
  346.                     #print 'tux_submission=',tux_submission
  347.                     
  348.                     #_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
  349.                     if tux_submission==None:
  350.                         tux_finished=1
  351.                         tuxturn=0
  352.                         self.tux_wait_counter=0
  353.                         #print 'Tux ran out of options!'
  354.                         animator.start_maneuver('Tux','return_stumped_maneuver')
  355.                         break
  356.                     
  357.                     #_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
  358.                     
  359.                     if rlist==None:
  360.                         #tux_finished=1
  361.                         #tuxturn=0
  362.                         #print 'Tux returned \'None\'...only handling first submission!'
  363.                         #break
  364.                         #print 'Tux returned \'None\'...trying another submission'
  365.                         pass
  366.                     
  367.                     else:
  368.                         #print rlist
  369.                         if rlist[0][0]==rlist[1][0]:exprtype='row'
  370.                         else:exprtype='col'
  371.                         try:
  372.                             for idx in range(len(rlist)):
  373.                                 tile=tuxtray.get_guest_by_str(rlist[idx][0])
  374.                                 spot=submission.take_guestMN(tile,rlist[idx][1],rlist[idx][2])
  375.                                 submissionspots.add(spot)    
  376.                         except:
  377.                             raw_input()
  378.                             
  379.                         #commit submission
  380.                         board.increment_num_commited()
  381.                         for spot in submissionspots.sprites():
  382.                             for board_spot in board.get_spots():
  383.                                 if board_spot.rect.center==spot.rect.center:# M,N-Matching
  384.                                     #print spot.guest.str_val,'len=',len(board.get_spots()),len(submissionspots.sprites())
  385.                                     tile=spot.pop_guest()
  386.                                     board_spot.take_guest(tile,1)
  387.                                     board_spot.lock()
  388.                                     submission.remove(spot)
  389.                                     
  390.                                     
  391.                         #del submissionspots            
  392.                         submissionspots.empty()
  393.                         self.draw_tiles(tuxtray.get_spots(),'tux')
  394.                         tux_finished=1
  395.                         tuxturn=0
  396.                         
  397.                         #_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
  398.                         #return to corner: 
  399.                         coin=int(random()*3)
  400.                         if coin==0:animator.start_maneuver('Tux','return_skating_maneuver')
  401.                         elif coin==1:animator.start_maneuver('Tux','tux_go_back_from_tray_maneuver_01')
  402.                         else:animator.start_maneuver('Tux','tux_go_back_from_tray_maneuver_02')
  403.                         
  404.                         #_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
  405.                         
  406.             for event in pygame.event.get():
  407.                 if event.type == QUIT:return(0)
  408.                 elif event.type == KEYDOWN and event.key == K_ESCAPE:return(0)
  409.                 elif event.type == KEYDOWN and event.key == K_F1:
  410.                         display_surface=pygame.display.get_surface()
  411.                         pygame.image.save(display_surface,os.path.join(os.environ['HOME'],'tuxmathscrabble.bmp'))
  412.                 
  413.                 elif event.type == MOUSEBUTTONDOWN:
  414.                     #we have action! reset wait_counter:
  415.                     self.tux_wait_counter=0
  416.                     
  417.                     #HELP BUTTON:
  418.                     if help_button.rect.collidepoint(pygame.mouse.get_pos()):
  419.                         help_image,rect=load_image('asymptopia/images/help_splash.gif')
  420.                         self.screen.blit(help_image,(0,0))
  421.                         pygame.display.flip()
  422.                         while 1:
  423.                             breakout=0
  424.                             for event in pygame.event.get():
  425.                                 if event.type == KEYDOWN:breakout=1
  426.                                 elif event.type == MOUSEBUTTONDOWN:breakout=1
  427.                             if breakout:break    
  428.                     
  429.                     
  430.                     #CREDIT BUTTON:
  431.                     if credit_button.rect.collidepoint(pygame.mouse.get_pos()):
  432.                         credit_image,rect=load_image('asymptopia/images/credit_splash.gif')
  433.                         self.screen.blit(credit_image,(0,0))
  434.                         pygame.display.flip()
  435.                         while 1:
  436.                             breakout=0
  437.                             for event in pygame.event.get():
  438.                                 if event.type == KEYDOWN:breakout=1
  439.                                 elif event.type == MOUSEBUTTONDOWN:breakout=1
  440.                             if breakout:break    
  441.                     
  442.                     #LEVEL BUTTONS:
  443.                     if l1_button.rect.collidepoint(pygame.mouse.get_pos()):
  444.                         self.LEVEL=1
  445.                         return(1)
  446.                     if l2_button.rect.collidepoint(pygame.mouse.get_pos()):
  447.                         self.LEVEL=2
  448.                         return(2)
  449.                     if l3_button.rect.collidepoint(pygame.mouse.get_pos()):
  450.                         self.LEVEL=3
  451.                         return(3)
  452.                     if l4_button.rect.collidepoint(pygame.mouse.get_pos()):
  453.                         self.LEVEL=4
  454.                         return(4)
  455.                     
  456.                     
  457.                     #SKIP BUTTON:
  458.                     elif skip_button.rect.collidepoint(pygame.mouse.get_pos()):
  459.                         if tuxturn==1 or AnimatedTux.rect[0]<400:continue
  460.                         else:
  461.                             for spot in submissionspots.sprites():#reset
  462.                                 tile=spot.pop_guest()
  463.                                 if tile:
  464.                                     for spot in tray.get_spots():    
  465.                                         if    tile.saved_center==spot.rect.center:
  466.                                             spot.take_guest(tile,1)
  467.                             
  468.                             #_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
  469.                             if len(board.get_listofheads())==0:
  470.                                 if not animation_in_progress:
  471.                                     animator.start_maneuver('Tux','insist_yougofirst_maneuver')
  472.                             else:
  473.                                 animator.start_maneuver('Tux','head_tilt_right_maneuver')
  474.                                 lose_sound.play()
  475.                                 animation_in_progress=1
  476.                                 tux_move_progress=0
  477.                                 submissionspots.empty()
  478.                                 tuxturn=1
  479.                                 continue
  480.                             #_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
  481.                     
  482.                     #READY BUTTON:
  483.                     elif ready_button.rect.collidepoint(pygame.mouse.get_pos()):
  484.                         if(validator.validate(submissionspots.sprites())):#ready for this now!(first player turn)
  485.                             board.increment_num_commited()
  486.                             for spot in submissionspots.sprites():
  487.                                 for board_spot in board.get_spots():
  488.                                     if board_spot.rect.center==spot.rect.center and spot.guest:
  489.                                         #validator is gonna have to fill-in AMHEAD fields, then add->board.list_of_heads.add(spot)
  490.                                         #print spot.guest.str_val,'len=',len(board.get_spots()),len(submissionspots.sprites())
  491.                                         tile=spot.pop_guest()
  492.                                         
  493.                                         #while we've got spot center info set_center of cuckoo_tile:
  494.                                         rect=board_spot.rect
  495.                                         xc=rect[0]+rect[2]/2
  496.                                         yc=rect[1]+rect[3]/2
  497.                                         cuckoo_tile_center=(xc,yc)
  498.                                         AnimatedTiles[num_animated_tiles].set_center(cuckoo_tile_center)
  499.                                         
  500.                                         #Validator sets submission_spot and game(this) xfers->board_spot; they work together!
  501.                                         if spot.AMHEAD==1:
  502.                                             board_spot.AMHEAD=1
  503.                                             if spot.AMROWEXPR==1:
  504.                                                 board_spot.AMROWEXPR=1
  505.                                                 board_spot.ROWEXPRLENGTH=spot.ROWEXPRLENGTH
  506.                                                 
  507.                                             if spot.AMCOLEXPR==1:
  508.                                                 board_spot.AMCOLEXPR=1
  509.                                                 board_spot.COLEXPRLENGTH=spot.COLEXPRLENGTH
  510.                                                 
  511.                                             #board.list_of_heads.add(board_spot)#sprites know not to add selves twice; relying on that here.
  512.                                         
  513.                                         board_spot.take_guest(tile,1)
  514.                                         board_spot.lock()
  515.                                         submission.remove(spot)
  516.                                         
  517.                                 
  518.                                 num_animated_tiles=num_animated_tiles+1#HACKHACKHACK!
  519.                                 
  520.                             
  521.                             #_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
  522.                             coin=int(random()*2)
  523.                             if coin==0:win_sound_01.play()
  524.                             else:win_sound_02.play()
  525.                             #else:win_sound_03.play()
  526.                             
  527.                             #_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
  528.                             #TRIGGER ANIMATION:
  529.                             #
  530.                             coin=int(random()*2)
  531.                             if coin==0:animator.start_maneuver('Tux','forehead_slap_maneuver')
  532.                             else:animator.start_maneuver('Tux','neck_stretch_maneuver')
  533.                             tux_move_progress=0
  534.                             animation_in_progress=1
  535.                             
  536.                             cuckoo_maneuver_names=self.cuckooManeuvers.getManeuverNames()
  537.                             for idx in range(num_animated_tiles):
  538.                                 maneuver_idx=int(random()*(len(cuckoo_maneuver_names)))
  539.                                 animator.start_maneuver(AnimatedTileNames[idx],cuckoo_maneuver_names[maneuver_idx])
  540.                             self.num_animated_tiles=num_animated_tiles
  541.                             
  542.                             #_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
  543.                             
  544.                             self.draw_tiles(tray.get_spots(),'player')
  545.                             #print 'SCORE: Tux=',self.tuxscore,' PLAYER=',self.playerscore
  546.                             tuxturn=1
  547.                         else:#put back and play lose sound(not4tux)
  548.                             for spot in submissionspots.sprites():#reset
  549.                                 tile=spot.pop_guest()
  550.                                 if tile:
  551.                                     for spot in tray.get_spots():    
  552.                                         if    tile.saved_center==spot.rect.center:
  553.                                             spot.take_guest(tile,1)
  554.                             
  555.                             if len(self.board.get_listofheads())>0:
  556.                                 lose_sound.play()
  557.                                 animator.start_maneuver('Tux','head_tilt_left_maneuver')
  558.                                 tux_move_progress=0
  559.                                 animation_in_progress=1
  560.                             
  561.                         submissionspots.empty()
  562.                         continue
  563.                     
  564.                     #check tray for new target:
  565.                     for spot in tray.get_spots():
  566.                         if not spot.guest:continue
  567.                         if spot.rect.collidepoint(pygame.mouse.get_pos()):
  568.                             slide1.play()
  569.                             target.add(spot.pop_guest())
  570.                     
  571.                     #check the board for new target:
  572.                     for spot in board.get_spots():
  573.                         if spot.rect.collidepoint(pygame.mouse.get_pos()) and spot.islocked():
  574.                             boink.play()
  575.                         
  576.                     #check the submission for new target:
  577.                     #print 'down1:len(submissionspots.sprites())=',len(submissionspots.sprites()),
  578.                     for spot in submission.get_spots():
  579.                         if not spot.occupied():continue
  580.                         if spot.rect.collidepoint(pygame.mouse.get_pos()):
  581.                             guest=spot.pop_guest()
  582.                             submissionspots.remove(spot)
  583.                             slide1.play()
  584.                             target.add(guest)
  585.                     #print 'down2:len(submissionspots.sprites())=',len(submissionspots.sprites())
  586.                             
  587.                 elif event.type == MOUSEBUTTONUP:
  588.                     if len(target.sprites())>0:
  589.                         for spot in submission.get_spots():
  590.                             if spot.rect.collidepoint(target.sprites()[0].rect.center):#welcome to new home
  591.                                 #print 'up1:len(submissionspots.sprites())=',len(submissionspots.sprites()),
  592.                                 if spot.occupied():continue#part of submission
  593.                                 #elif spot.islocked():continue#part of board
  594.                                 spot.take_guest(target.sprites()[0],1)
  595.                                 kerplunk.play()
  596.                                 submissionspots.add(spot)
  597.                                 target.empty()
  598.                                 #print 'up2:len=',len(board.get_spots()),len(submissionspots.sprites())
  599.                                 break
  600.                         
  601.                         if len(target.sprites())==0:continue
  602.                         
  603.                         for spot in board.get_spots():
  604.                             if spot.rect.collidepoint(target.sprites()[0].rect.center):    
  605.                                 boink.play()
  606.                                 break
  607.                                 
  608.                         if len(target.sprites())>0:#didn't drop? revert!
  609.                             #print 'cleanup'
  610.                             tile=target.sprites()[0]
  611.                             target.remove(tile)
  612.                             for spot in tray.get_spots():    
  613.                                 if    tile.saved_center==spot.rect.center:
  614.                                     spot.take_guest(tile,1)
  615.                                     break
  616.             
  617.             if not tuxturn:
  618.                 self.tux_wait_counter=self.tux_wait_counter+1
  619.                 if self.tux_wait_counter>WAIT_TIME and self.animation_in_progress==0:self.tux_wait_maneuver()
  620.                 else:tux_wait_counter=0
  621.             self.update()
  622.             self.tuxturn=tuxturn
  623.     
  624.     def do_one_scratch(self):
  625.             coin=int(random()*2)
  626.             if coin==0:self.animator.start_maneuver('Tux','one_scratchl_maneuver')
  627.             else:self.animator.start_maneuver('Tux','one_scratchr_maneuver')
  628.             self.tux_move_progress=0
  629.             self.update()
  630.             self.update()
  631.             self.update()
  632.             self.update()
  633.             
  634.     def tux_wait_maneuver(self):
  635.             idx=int(random()*9)
  636.             if idx==0:self.animator.start_maneuver('Tux','head_tilt_left_maneuver')
  637.             if idx==1:self.animator.start_maneuver('Tux','head_tilt_right_maneuver')
  638.             if idx==2:self.animator.start_maneuver('Tux','neck_stretch_maneuver')
  639.             if idx==3:self.animator.start_maneuver('Tux','tux_flapper_maneuver')
  640.             #if idx==4:self.animator.start_maneuver('Tux','hopflap_leftright_maneuver')#can get away if allowed to go left!
  641.             if idx==5:self.animator.start_maneuver('Tux','hop_in_place_maneuver')
  642.             if idx==6:self.animator.start_maneuver('Tux','walkoff_stage_right_maneuver')
  643.             if idx==7:self.animator.start_maneuver('Tux','neck_squash_tiltleftright_maneuver')
  644.             if idx==8:self.animator.start_maneuver('Tux','neck_squash_hop_in_place_maneuver')
  645.             
  646.             self.tux_move_progress=0
  647.             self.tux_wait_counter=0
  648.             self.update()
  649.                                 
  650.     
  651.     def update(self):
  652.             screen=self.screen
  653.             background=self.background
  654.             buttons=self.buttons
  655.             boardspots=self.boardspots
  656.             trayspots=self.trayspots
  657.             submissionspots=self.submissionspots
  658.             target=self.target
  659.             AnimatedTux=self.AnimatedTux
  660.             animation_in_progress=self.animation_in_progress
  661.             AnimatedTiles=self.AnimatedTiles
  662.             tuxtrayspots=self.tuxtrayspots
  663.             
  664.             for event in pygame.event.get():
  665.                 if event.type == QUIT:return(0)
  666.                 elif event.type == KEYDOWN and event.key == K_ESCAPE:return(0)
  667.                             
  668.             screen.blit(background,(0,0))
  669.             #for button in buttons.sprites():
  670.             #    
  671.             if self.LEVEL==1:b=self.l1_button
  672.             elif self.LEVEL==2:b=self.l2_button
  673.             elif self.LEVEL==3:b=self.l3_button
  674.             elif self.LEVEL==4:b=self.l4_button
  675.             screen.blit(self.level_indicator,(10,b.rect[1]+b.rect[3]/3))
  676.             
  677.             #Scoreboard:
  678.             tuxscorestring=" Tux\'s Score: %03d"%self.tuxscore
  679.             screen.blit(self.pyfont.render(tuxscorestring,1,(255,255,0,0),(0, 150, 100)),(20,30))
  680.             playerscorestring=" Your Score : %03d"%self.playerscore
  681.             screen.blit(self.pyfont.render(playerscorestring,1,(255,255,0,0),(0, 150, 100)),(20,60))
  682.             
  683.             #Who's Turn-Indicator:
  684.             if self.LEVEL==3:
  685.                 if self.tuxturn==1:
  686.                     tuxturnstring= '<-- Tux\'s Turn'
  687.                     screen.blit(self.pyfont.render(tuxturnstring,1,(255,255,0,0),(0, 150, 100)),(W-180,45))
  688.                 else:
  689.                     yourturnstring='<-- Your Turn!'
  690.                     screen.blit(self.pyfont.render(yourturnstring,1,(255,255,0,0),(0, 150, 100)),(W-180,530))
  691.             else:
  692.                 if self.tuxturn==1:
  693.                     tuxturnstring= '<-- Tux\'s Turn'
  694.                     screen.blit(self.pyfont.render(tuxturnstring,1,(255,255,0,0),(0, 150, 100)),(W-200,45))
  695.                 else:
  696.                     yourturnstring='<-- Your Turn!'
  697.                     screen.blit(self.pyfont.render(yourturnstring,1,(255,255,0,0),(0, 150, 100)),(W-200,530))
  698.             
  699.             #
  700.             buttons.draw(screen)
  701.             
  702.             boardspots.draw(screen)
  703.             submissionspots.draw(screen)
  704.             #
  705.             trayspots.draw(screen)
  706.             tuxtrayspots.draw(screen)
  707.             if len(target.sprites())>0:
  708.                 target.sprites()[0].update()
  709.                 target.draw(screen)
  710.             
  711.             #_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
  712.             #ANIMATED TUX UPDATE:
  713.             #
  714.             rval=AnimatedTux.update()
  715.             screen.blit(AnimatedTux.image,(AnimatedTux.rect[0],AnimatedTux.rect[1]))
  716.             
  717.             sound=AnimatedTux.get_sound()
  718.             if sound:sound.play()
  719.             
  720.             #okay, this is now official location of decision:
  721.             if AnimatedTux.get_dest():
  722.                 self.animation_in_progress=1
  723.             else:
  724.                 self.animation_in_progress=0
  725.                 
  726.                 
  727.             #_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
  728.             #ANIMATED TILE UPDATE:
  729.             #
  730.             #sound=AnimatedTiles[0].get_sound()
  731.             #if sound:sound.play()
  732.             if self.num_animated_tiles>0:
  733.                 #time.sleep(.1)
  734.                 self.num_animated_tiles=0
  735.                 for tile in AnimatedTiles:
  736.                     if not tile.get_dest():
  737.                         tile.set_center((0,0))
  738.                     else:
  739.                         self.num_animated_tiles=self.num_animated_tiles+1
  740.                         rval=tile.update()
  741.                         screen.blit(tile.image,(tile.rect[0],tile.rect[1]))
  742.                 
  743.             #_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
  744.             
  745.             pygame.display.flip()
  746.             
  747.             
  748.             if AnimatedTux.get_dest():
  749.                 if AnimatedTux.rect[0]<0:AnimatedTux.set_center((730,100))
  750.                 time.sleep(AnimatedTux.get_timeout())
  751.             
  752.