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

  1. """
  2. /***************************************************************************
  3.  
  4.     Author             :Charles B. Cosse 
  5.     
  6.     Email            :ccosse@asymptopia.com
  7.                     
  8.                     
  9.     Copyright        :(C) 2002,2003 Asymptopia Software.
  10.     
  11.  ***************************************************************************/
  12. /***************************************************************************
  13.                           Board.py
  14.  
  15.     Description:
  16.  
  17.     A Board is a group of generalized spots.
  18.     This class should be used as a base class for various types of
  19.     boards -- blackjack tables,cw puzzles,checkers -- any thing with
  20.     fixed spots. The game pieces are attirbutes of the board.
  21.     If you want a background image, blit that as background from the
  22.     outermost game class, not this class, as this is a Group.
  23.     
  24.     Group base class provides: Add,copy,empty,has,sprites,remove,update
  25.     Should not give both empty_spot_image and background_image; either/or.
  26.  
  27.     If background image, then we break-up the background image into default 
  28.     spot images. If neither, of course, then it's an *invisible* spot.
  29.     *Derived* classes set the location of the self.spots.
  30.     The way the images are passed to __init__, the look of the board(ie spot
  31.     images) is still controlled from outside. No need to manaully
  32.     do anything unless get the spots through the get_spots() method.
  33.     XC,YC are center of board -- boards all assumed array like
  34.     
  35.     Latest: submission is M*N array of invisible spots; validate_submission(self.submission);
  36.  
  37.  ***************************************************************************/
  38.  
  39. /***************************************************************************
  40.  *                                                                         *
  41.  *   This program is free software; you can redistribute it and/or modify  *
  42.  *   it under the terms of the GNU General Public License as published by  *
  43.  *   the Free Software Foundation; either version 2 of the License, or     *
  44.  *   (at your option) any later version. (Please note that if you use this *
  45.  *   code you must give credit by including the Author and Copyright       *
  46.  *   info at the top of this file).                                        *
  47.  ***************************************************************************/
  48.  
  49. """
  50.  
  51. import pygame
  52. from pygame.locals import *
  53. from Spot import Spot
  54.  
  55.  
  56. class Board(pygame.sprite.Group):
  57.     
  58.     def __init__(self,M,N,XC,YC,background_image,default_spot_image):
  59.         pygame.sprite.Group.__init__(self)
  60.         self.M=M
  61.         self.N=N
  62.         self.XC=XC#this is 974/2
  63.         self.YC=YC
  64.         self.default_spot_image=default_spot_image
  65.         self.map=None
  66.         self.num_commited=0
  67.         
  68.         if background_image and not default_spot_image:self.make_background_spots(background_image)
  69.         elif default_spot_image and not background_image:self.make_default_spots(default_spot_image)
  70.         else:self.make_invisible_spots(None)
  71.     
  72.     def get_map(self):
  73.         m=[]
  74.         for midx in range(self.M):
  75.             m.append([])
  76.             for nidx in range(self.N):
  77.                 for spot in self.sprites():
  78.                     if spot.getMN()[0]==midx and spot.getMN()[1]==nidx:
  79.                         if spot.guest:m[midx].append(spot.guest.str_val)
  80.                         else:m[midx].append('')
  81.         return(m)
  82.     
  83.             
  84.     def check4guest(self,m,n):
  85.         #print 'check4guest:',m,n
  86.         if m<0 or m>self.M-1 or n<0 or n>self.N-1:return(0)
  87.         spot=self.get_spotMN(m,n)
  88.         if spot.guest==None:return(0)
  89.         else:return(1)
  90.         
  91.     def get_listofheads(self):
  92.         heads=[]
  93.         for spot in self.sprites():
  94.             if spot.guest:
  95.                 if spot.AMHEAD:heads.append(spot)
  96.         return(heads)            
  97.     
  98.     def clear_spots(self):
  99.         for spot in self.sprites():
  100.             spot.remove(self)
  101.     
  102.     def get_spotMN(self,m,n):
  103.         for spot in self.sprites():
  104.             MN=spot.getMN()
  105.             if MN[0]==m and MN[1]==n:
  106.                 return(spot)
  107.     
  108.     def take_guestMN(self,tile,m,n):
  109.         #print 'take_guestMN:',tile,m,n
  110.         for spot in self.sprites():
  111.             MN=spot.getMN()
  112.             if MN[0]==m and MN[1]==n:
  113.                 spot.take_guest(tile,1)
  114.                 return(spot)
  115.     
  116.     def get_num_commited(self):
  117.         return(self.num_commited)
  118.     
  119.     def increment_num_commited(self):
  120.         self.num_commited=self.num_commited+1
  121.     
  122.     def get_guest_by_str(self,str_val):
  123.         #this function TuxTray->Submission ('getting' from Tux Tray)
  124.         for spot in self.sprites():
  125.             if spot.guest and spot.guest.str_val==str_val:
  126.                 return spot.pop_guest()
  127.         return(None)
  128.             
  129.     def get_spots(self):
  130.         #this function is boardspots
  131.         return(self.sprites())
  132.     
  133.     #SPOT MAKERS:
  134.     def make_background_spots(self,background_image):
  135.         #print 'make_background_spots'
  136.         for midx in range(self.M):
  137.             for nidx in range(self.N):
  138.                 #print 'need to break-up background image!'
  139.                 self.add(Spot(default_spot_image,midx,nidx))#change to background_tile
  140.     
  141.     def make_default_spots(self,default_spot_image):
  142.         #print 'make_default_spots'
  143.         XC=self.XC
  144.         YC=self.YC
  145.         M=self.M
  146.         N=self.N
  147.         
  148.         for midx in range(0,M):
  149.             for nidx in range(0,N):
  150.                 if self.M==1:spot=Spot(default_spot_image,midx,nidx,'REG')
  151.                 ####
  152.                 elif abs(M/2-midx)==abs(N/2-nidx):
  153.                     img=default_spot_image[:-4]+'_2XL.gif'
  154.                     spot=Spot(img,midx,nidx,'2XL')
  155.                 ####
  156.                 elif (midx)==(-nidx+(N/2-4)):#top left
  157.                     img=default_spot_image[:-4]+'_3XL.gif'
  158.                     spot=Spot(img,midx,nidx,'3XL')
  159.                 elif (midx)==(-nidx+(N/2+16)):#bot right
  160.                     img=default_spot_image[:-4]+'_3XL.gif'
  161.                     spot=Spot(img,midx,nidx,'3XL')
  162.                 elif (midx)==(+nidx-(N/2+4)):#top right
  163.                     img=default_spot_image[:-4]+'_3XL.gif'
  164.                     spot=Spot(img,midx,nidx,'3XL')
  165.                 elif (midx)==(+nidx+(8)):#bot left
  166.                     img=default_spot_image[:-4]+'_3XL.gif'
  167.                     spot=Spot(img,midx,nidx,'3XL')
  168.                 ####
  169.                 elif (midx==0 and nidx==0) or (nidx==N-1 and midx==M-1) or (nidx==0 and midx==M-1) or (nidx==N-1 and midx==0):
  170.                     img=default_spot_image[:-4]+'_2XW.gif'
  171.                     spot=Spot(img,midx,nidx,'2XW')
  172.                 elif (midx==M/2 and nidx==N/4) or (nidx==N/2 and midx==M/4) or (nidx==N/2 and midx==3*M/4) or (nidx==3*N/4 and midx==M/2):
  173.                     img=default_spot_image[:-4]+'_2XW.gif'
  174.                     spot=Spot(img,midx,nidx,'2XW')
  175.                 ####
  176.                 elif (nidx==N/2 and midx==0):
  177.                     img=default_spot_image[:-4]+'_3XW.gif'
  178.                     spot=Spot(img,midx,nidx,'3XW')
  179.                 elif (nidx==N/2 and midx==M-1):
  180.                     img=default_spot_image[:-4]+'_3XW.gif'
  181.                     spot=Spot(img,midx,nidx,'3XW')
  182.                 elif (nidx==0 and midx==M/2):
  183.                     img=default_spot_image[:-4]+'_3XW.gif'
  184.                     spot=Spot(img,midx,nidx,'3XW')
  185.                 elif (nidx==N-1 and midx==M/2):
  186.                     img=default_spot_image[:-4]+'_3XW.gif'
  187.                     spot=Spot(img,midx,nidx,'3XW')
  188.                 ####
  189.                 else:
  190.                     spot=Spot(default_spot_image,midx,nidx,'REG')
  191.                 w=spot.image.get_width()
  192.                 h=spot.image.get_height()
  193.                 spot.rect.center=(    XC+((-N/2+nidx+.5)*w),YC+(    (-M/2+midx+.5)*h)    )
  194.                 self.add(spot)
  195.     
  196.     def make_invisible_spots(self,invisible):#instantiate with invisible="None"
  197.         print 'make_invisible_spots'
  198.         for midx in range(self.M):
  199.             for nidx in range(self.N):
  200.                 self.add(Spot(invisible,midx,nidx))
  201.     
  202.     
  203.     #def localize(self,submission):
  204.     #    print 'board.localize:',submission
  205.     #    return(1)
  206.