home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 124 / cd-rom 124.iso / edu / tuxmath / tuxmathscrabble / asymptopia / Board.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-11-16  |  10.1 KB  |  236 lines

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