Example #1
0
    def __init__(self, screen, settings): #serveur_hostname,serveur_port,single_player,fullscreen):
        #pygame.init()
        self.settings = settings # server_ip,server_port,fullscreen,solo
        self.TEST = self.settings.get("test", False)
        #print self.settings
        self.mapw       =   80
        self.maph       =   60
        self.mapdata    =   [1 for i in range(self.mapw*self.maph)]      # only used for obstacles
        self.entries = [(0,0),(0,self.maph - 1),(self.mapw - 1,0),(self.mapw - 1,self.maph - 1)]
        self.castlex = 40
        self.castley = 30
        self.last_bg_sent = 0
        self.last_wave_sent = 0
        #for x in range(37,44):
            #for y in range(27,34):
                #self.mapdata[x + y * self.mapw] = 9
        now = pygame.time.get_ticks
        self.single_player = settings["solo"]
        self.enemy_info = {}
        self.level = 1
        self.current_tower_creation_mode = objects.basic_tower
        self.cm        = cm.cm (self.settings["keys"],self.settings["touches_boutons"])            # create a new control manager
        self.cm.register_game (self)                # register self to cm        
        self.cm.register(screen)
        #self.dm        = pygame_dm.pygame_dm (self.cm, settings["fullscreen"], self.single_player)        # create a new display manager
        self.dm = screen
        self.castle = castle.castle(self.cm)
        self.astar_cache = {}
        if self.single_player:
            self.castle.update_boutons_text()
            self.dm.update_bb(message = "Bienvenue dans le mode solo")
        self.nm = None
        if self.settings["server_ip"] and not self.single_player:
            print "Connecting to serveur",self.settings["server_ip"],"on port",self.settings["server_port"]
            self.nm        = nm.nm (self.cm,self.settings["server_ip"],self.settings["server_port"])
            self.nm.start ()
            while not self.nm.connected:
                print "waiting for the connexion"
                time.sleep(1)
        self.last_cputick = 0

        self.waves = []
        self.colors = [ (255,128,128), (128,255,128), (128,128,255) ]

        self.towers    = []
        self.badguys    = []
        self.objects    = [self.towers , self.badguys]
                
        self.current_wave = {
            "number":   0,
            "life"  :   None,
            "speed" :   None,
            "special" : None,
            "coord" : (0,0),
            }
        self.update_road = False
        self.cm.post(["mode_change","SELECT"])
Example #2
0
File: player.py Project: kyuf/Chess
    def move(self, notation, spaces, opponent):
        #vulnerable pawns can be captured en passant
        #clear any previously vulnerable pawns
        self.vulSpace = None

        #castle if O-O or O-O-O
        if notation == 'O-O' or notation == 'O-O-O':
            castling = True
            spaces, newSpaces, oldSpaces, pieceType = castle.castle(
                    notation, spaces, self)
        #no castle move
        elif notation in self.moveset:
            castling = False
            #determine new space
            if notation[-2] != '=':
                newSpace = notation[-2:]
                promoting = False
            else:
                #pawn promotion has '=X' at end of notation where X is new
                #piece type
                newSpace = notation[-4:-2]
                promoting = True

            #check for capture
            if 'x' in notation:
                if spaces[newSpace] != '  ':
                    capturedPiece = spaces[newSpace]
                #if captured space is empty en passant
                else:
                    capturedPiece = spaces[opponent.vulSpace]
            else:
                capturedPiece = None
            
            #determine piece being moved
            #pawn if first letter is lowercase
            if notation[0] in 'abcdefgh':
                pieceType = 'P'
            #rook, knight, bishop, queen, king
            elif notation[0] in 'RNBQK':
                pieceType = notation[0]      
            #no piece found
            else:
                raise RuntimeError('Incorrect piece notation')
            
            #find correct piece to move
            oldSpace = self.moveset[notation]

            #special considerations for moving pawns
            if pieceType == 'P':
                #check if pawn was made vulnerable
                if abs(ord(newSpace[1])-ord(oldSpace[1])) == 2:
                    self.vulSpace = newSpace
                    spaces[oldSpace].makeVul()
                #check if pawn is being promoted
                elif promoting:
                    newPiece = notation[-1]
                    if newPiece in 'RNBQ':
                        self.pieces['P'].remove(oldSpace)
                        self.pieces[newPiece].add(oldSpace)
                        if newPiece == 'R':
                            spaces[oldSpace] = Rook(
                                    self.color, oldSpace, 'R')
                            #disable new rook's ability to castle
                            spaces[oldSpace].disableCastle()
                        elif newPiece == 'N':
                            spaces[oldSpace] = Knight(
                                    self.color, oldSpace, 'N')
                        elif newPiece == 'B':
                            spaces[oldSpace] = Bishop(
                                    self.color, oldSpace, 'B')
                        else:
                            spaces[oldSpace] = Queen(
                                    self.color, oldSpace, 'Q')
                        pieceType = newPiece
                    else:
                        raise RuntimeError('Invalid promotion')

            #remove captured piece from opponent's pieces
            if capturedPiece:
                spaces[capturedPiece.space] = '  '
                opponent.pieces[capturedPiece.note].remove(
                        capturedPiece.space) 

            #reformat newSpace and oldSpace
            newSpaces, oldSpaces = [newSpace], [oldSpace]

        else:
            raise RuntimeError('Invalid notation')

        #update spaces and pieces
        for n, o, p in zip(newSpaces, oldSpaces, pieceType):
            #set piece at new space
            spaces[n] = spaces[o]
            spaces[n].updatePieceSpace(n)
            #clear old space
            spaces[o] = '  '
            #update player piece sets
            self.pieces[p].remove(o)
            self.pieces[p].add(n)   

        #disable castling for piece if rook or king
        if not castling and pieceType in 'KR':
            spaces[newSpace].disableCastle()

        #update spaces and opponent
        return spaces, opponent