Exemplo n.º 1
0
    def redrawAllScene2(self, screen):
        LargeTree.generate(LargeTree, screen, self.scrollX)
        Stone.generate(Stone, screen, self.width, self.height, self.scrollX,
                       self.snowed)
        Waterfall.generate(Waterfall, screen, self.scrollX)
        Bush.generate(Bush, screen, self.scrollX)
        for rabbit in Rabbit.rabbits:
            rabbit.draw(screen, self.scrollX)
        Bridge.generate(Bridge, screen, self.scrollX)
        MediumTree.generate(MediumTree, screen, self.scrollX)
        for bird in Bird.birds:
            bird.draw(screen, self.scrollX)
        if self.scrollX > -800:
            screen.blit(self.rightArrow, self.rightArrowRect)
        if self.scrollX < 800:
            screen.blit(self.leftArrow, self.leftArrowRect)
        #snow
        if self.snowed == True:
            for snowflake in self.snowflakes:
                screen.blit(self.snowflake, snowflake)
        #bloom instruction
        if self.currSeason == 0 or self.currSeason == 1:
            pixel = pygame.font.Font('Pixeled.ttf', 15)
            instructionSurf = pixel.render("CLICK ON TREES TO MAKE THEM BLOOM",
                                           True, (255, 255, 255))
            instructionRect = instructionSurf.get_rect()
            instructionRect.center = (self.width / 2, 775)
            screen.blit(instructionSurf, instructionRect)
        elif self.currSeason == 2 or self.currSeason == 3:
            pixel = pygame.font.Font('Pixeled.ttf', 15)
            instructionSurf = pixel.render(
                "CLICK ON TREES TO MAKE THE LEAVES FALL", True,
                (255, 255, 255))
            instructionRect = instructionSurf.get_rect()
            instructionRect.center = (self.width / 2, 775)
            screen.blit(instructionSurf, instructionRect)
        #menu bar
        screen.blit(self.menuBar, self.menuPos)
        if self.menuExpanded == True:
            screen.blit(self.seasonBar, self.seaPos)
            screen.blit(self.mydrawings, self.mydrawingsPos)

        if self.arrowShow == True:
            screen.blit(self.arrow, self.arrowPos)
        if self.seasonExpanded == True:
            screen.blit(self.spring, self.springPos)
            screen.blit(self.summer, self.summerPos)
            screen.blit(self.autumn, self.autumnPos)
            screen.blit(self.winter, self.winterPos)
            if self.currSeason == 0:
                screen.blit(self.spring1, self.springPos)
            elif self.currSeason == 1:
                screen.blit(self.summer1, self.summerPos)
            elif self.currSeason == 2:
                screen.blit(self.autumn1, self.autumnPos)
            elif self.currSeason == 3:
                screen.blit(self.winter1, self.winterPos)
                screen.blit(self.makeitsnow, self.makePos)
        if self.currSeason == 3:
            screen.blit(self.makeitsnow, self.makePos)
Exemplo n.º 2
0
    def getFreeStones(self):
        """return the list of the stones which can move ordered"""
        stones1 = []
        stones2 = []
        x = y = 0
        while x < self.board.cols:
            while y < self.board.rows:
                if self.board.stoneExists(x, y):
                    stone = Stone(self.board, x, y)
                    if (stone.color == self.color
                            or stone.color == utils.getSelectedColor(
                                self.color)) and stone.canMove():
                        if stone.canBeCaptured() or x == 0 or (
                                y + 1) == self.board.rows or y == 0 or (
                                    y + 1) == self.board.cols:
                            stones1.append((x, y))
                        else:
                            stones2.append((x, y))
                y += 1
            x += 1
            y = 0

        stones1 = utils.random_list(stones1)
        for a in stones2:
            stones1.append(a)

        return stones1
Exemplo n.º 3
0
 def reset_stones(self, te, flist, color):
     if color == Stone.WHITE:
         c = Stone.BLACK
     elif color == Stone.BLACK:
         c = Stone.WHITE
     self.stones[te] = Stone(te, c)
     for z in flist:
         self.stones[z] = Stone(z, c)
     self.stones[z] = Stone(z, Stone.EMPTY)
Exemplo n.º 4
0
    def __init__(self, nplay=1, show_result=False, show_board=False):

        self._nplay = nplay
        self._show_result = show_result
        self._show_board = show_board
        self.BLACK = Stone("●")
        self.WHITE = Stone("○")
        self.BLANK = Stone("×")
        self.OPPONENT = {self.BLACK: self.WHITE, self.WHITE: self.BLACK}
Exemplo n.º 5
0
 def __init__(self):
     self.BLACK = Stone("●")
     self.WHITE = Stone("○")
     self.BLANK = Stone("×")
     self.OPPONENT = {self.BLACK: self.WHITE, self.WHITE: self.BLACK}
     size = self.SIZE
     center = size // 2
     square = [[self.BLANK for y in range(size)]
               for x in range(size)]  # 最初の盤面を定義
     square[center - 1][center - 1:center + 1] = [self.WHITE, self.BLACK]
     square[center + 0][center - 1:center + 1] = [self.BLACK, self.WHITE]
     self.square = square
Exemplo n.º 6
0
    def makeMove(self, color, board):
        character = 'ABCDEFGHI'
        number = '123456789'
        while True:
            print(board.printBoard)
            move = input('\n' + color + ', insert a move\n').upper()

            if any(move.startswith(x) for x in character) and len(move) == 2:
                if any(move.endswith(y) for y in number):
                    position = {
                        'A': 0,
                        'B': 1,
                        'C': 2,
                        'D': 3,
                        'E': 4,
                        'F': 5,
                        'G': 6,
                        'H': 7,
                        'I': 8,
                    }
                    posx = position.get(move[0])
                    posy = int(move[1]) - 1
                    if board.printBoard[posx][
                            posy] != 'wh' and board.printBoard[posx][
                                posy] != 'bl':

                        Stone(posx, posy, board, color)

                        break
                    print('invalid move')
                else:
                    print('invalid position')
            else:
                print('invalid position')
Exemplo n.º 7
0
 def redrawAllScene1(self, screen):
     LargeTree.generate(LargeTree, screen, self.scrollX)
     Stone.generate(Stone, screen, self.width, self.height, self.scrollX,
                    self.snowed)
     Waterfall.generate(Waterfall, screen, self.scrollX)
     Bush.generate(Bush, screen, self.scrollX)
     Bridge.generate(Bridge, screen, self.scrollX)
     MediumTree.generate(MediumTree, screen, self.scrollX)
     #snow
     if self.snowed == True:
         for snowflake in self.snowflakes:
             screen.blit(self.snowflake, snowflake)
     if self.showInstruction == True:
         pixel = pygame.font.Font('Pixeled.ttf', 15)
         instructionSurf = pixel.render("PRESS 'SPACE' TO ADD ANIMALS",
                                        True, (255, 255, 255))
         instructionRect = instructionSurf.get_rect()
         instructionRect.center = (self.width / 2, 775)
         screen.blit(instructionSurf, instructionRect)
     if self.showNavigation == True:
         screen.blit(self.navigation, self.navRect)
     if self.scrollX > -800:
         screen.blit(self.rightArrow, self.rightArrowRect)
     if self.scrollX < 800:
         screen.blit(self.leftArrow, self.leftArrowRect)
     screen.blit(self.menuBar, self.menuPos)
     if self.menuExpanded == True:
         screen.blit(self.seasonBar, self.seaPos)
     if self.arrowShow == True:
         screen.blit(self.arrow, self.arrowPos)
     if self.seasonExpanded == True:
         screen.blit(self.spring, self.springPos)
         screen.blit(self.summer, self.summerPos)
         screen.blit(self.autumn, self.autumnPos)
         screen.blit(self.winter, self.winterPos)
         if self.currSeason == 0:
             screen.blit(self.spring1, self.springPos)
         elif self.currSeason == 1:
             screen.blit(self.summer1, self.summerPos)
         elif self.currSeason == 2:
             screen.blit(self.autumn1, self.autumnPos)
         elif self.currSeason == 3:
             screen.blit(self.winter1, self.winterPos)
     if self.currSeason == 3:
         screen.blit(self.makeitsnow, self.makePos)
Exemplo n.º 8
0
    def getSelectedStone(self):
        """return the stone selected by the AI"""
        result = []
        stones = self.getFreeStones()

        nb = 0
        for (x,y) in stones :
            stone = Stone(self.board,x,y)
            new_x , new_y , tmp_nb ,action = stone.canCaptureStones()
            if new_x >= 0 and new_y >= 0 and tmp_nb > nb :
                nb = tmp_nb
                result.insert(0, (x,y))

        if len(result) == 0 and len(stones) > 0 :
            #choose a stone randomly
            i = random.randint(0, len(stones) - 1 )
            result.append( stones[i] )

        return result[0]
Exemplo n.º 9
0
    def getSelectedStone(self):
        """return the stone selected by the AI"""
        result = []
        stones = self.getFreeStones()

        nb = 0
        for (x, y) in stones:
            stone = Stone(self.board, x, y)
            new_x, new_y, tmp_nb, action = stone.canCaptureStones()
            if new_x >= 0 and new_y >= 0 and tmp_nb > nb:
                nb = tmp_nb
                result.insert(0, (x, y))

        if len(result) == 0 and len(stones) > 0:
            #choose a stone randomly
            i = random.randint(0, len(stones) - 1)
            result.append(stones[i])

        return result[0]
Exemplo n.º 10
0
 def create_abs(self):
     # chose a stone from the objects randomly
     index = random.choice(range(self.num_objs))
     img = self.objects[index]
     # here we need a initial coordinate and mode
     init_x = 10
     init_y = 0
     init_m = Gravity()
     stone = Stone(img, init_x, init_y, init_m)
     return stone
Exemplo n.º 11
0
 def put_stone(self, te, color):
     stone = Stone(te, color)
     flist = self.flip_list(stone)
     self.set_stones(flist, color)
     self.stones[te] = stone
     if color == Stone.BLACK:
         self.nBlack = len(flist) + 1
     else:
         self.nWhite = len(flist) + 1
     return flist
Exemplo n.º 12
0
    def getFreeStones(self):
        """return the list of the stones which can move ordered"""
        stones1 = []
        stones2 = []
        x = y = 0
        while x < self.board.cols :
            while y < self.board.rows :
                if self.board.stoneExists(x,y) :
                    stone = Stone(self.board , x,y)
                    if (stone.color == self.color or stone.color == utils.getSelectedColor(self.color) ) and stone.canMove() :
                        if stone.canBeCaptured() or x == 0 or (y + 1) == self.board.rows or y == 0 or (y + 1 ) == self.board.cols :
                            stones1.append((x,y))
                        else:
                            stones2.append((x,y) )
                y+=1
            x+=1
            y=0

        stones1 = utils.random_list(stones1)
        for a in stones2:
            stones1.append(a)

        return stones1
Exemplo n.º 13
0
 def create(self, ti):
     """
     according the last_t, ti and frq decide weather to creat a new stone or not
     """
     if (ti - self.last_t) > self.frq:
         # update the clock
         self.last_t = ti
         # chose a stone from the objects randomly
         index = random.choice(range(self.num_objs))
         img = self.objects[index]
         # here we need a initial coordinate and mode
         init_x = 10
         init_y = 0
         init_m = Gravity()
         stone = Stone(img, init_x, init_y, init_m)
         return stone
     return None
Exemplo n.º 14
0
 def __init__(self, NW=4):
     self.nKoma = [0 for _ in range(9)]
     self.NW = NW
     self.NxN = NW * NW
     self.stones = [Stone(i, Stone.EMPTY) for i in range(self.NxN)]
     m = NW // 2
     n = m - 1
     self.stones[NW * n + n].setColor(Stone.WHITE)
     self.stones[NW * n + m].setColor(Stone.BLACK)
     self.stones[NW * m + n].setColor(Stone.BLACK)
     self.stones[NW * m + m].setColor(Stone.WHITE)
     nrnd = randint(0, 1)
     if nrnd == 0:
         self.stones[NW * n + n].flipColor()
         self.stones[NW * n + m].flipColor()
         self.stones[NW * m + n].flipColor()
         self.stones[NW * m + m].flipColor()
     self.nBlack = self.nWhite = 2
     self.nEmpty = self.NxN - (self.nBlack + self.nWhite)
     self.state = Board.GAME
Exemplo n.º 15
0
    fitness_values = []
    for i in check_fitness(position, population):
        fitness = move(stones, i)
        if fitness == 0:
            print("Solution: {} Generation: {}, ".format(i, generation))
            return None
        else:
            fitness_values.append({'fitness': fitness, 'chromosome': i})
    if generation < 300:
        new_population = create_generation(fitness_values)
        print("Generation: {}, Population: {}, Fitness: ".format(
            generation, len(new_population)))
        work(new_population, stones, generation + 1)


# Initial state
inital_state = [[1, 2, 3], [7, 9, 5], [4, 8, 6]]

stones = {}
# Place the stones
for i, l1 in enumerate(inital_state):
    for j, l2 in enumerate(l1):
        stones[l2] = (Stone(l2, ((3 * i) + j), connections(inital_state, i,
                                                           j)))

if __name__ == "__main__":
    print('initial state:')
    print_board(stones)
    print('----------')
    population = [random_chromosome() for _ in range(100)]
    work(population, stones, 1)
Exemplo n.º 16
0
    def initScene1(self):
        season = self.seasons[self.currSeason]
        #reset everything before changing the season
        Stone.backStones = []
        Stone.middleStones = []
        Stone.frontStones = []
        Stone.dims = []
        Stone.stoneSurface = dict()
        Stone.backSurface = dict()
        Stone.middleSurface = dict()
        Stone.frontSurface = dict()
        Tree.mediumTrees = []
        Tree.bushes = []
        Tree.largeTrees = []
        Bridge.bridges = []
        Waterfall.waterfalls = []
        #generate stones
        for corner in Stone.cornerData:
            if corner[1] <= self.height / 3:
                layer = 0
                h = random.randint(500, 800)
                w = random.randint(50, 100)
                for dim in range(random.randint(0, 4)):
                    length = random.randint(5, w // 3)
                    dimx = random.randint(corner[0], corner[0] + w * 2 // 3)
                    dimy = 790 - h
                    Stone.dims.append((length, dimx, dimy))
                Stone.backStones.append((corner[0], 800, w, h, layer))
                #add to surface dictionary
                for i in range(corner[0], corner[0] + w):
                    Stone.stoneSurface[i] = 789 - h
                    Stone.backSurface[i] = 789 - h
            elif corner[1] <= self.height * 2 / 3:
                layer = 1
                h = random.randint(100, 500)
                w = random.randint(100, 150)
                for dim in range(random.randint(0, 4)):
                    length = random.randint(5, w // 3)
                    dimx = random.randint(corner[0], corner[0] + w * 2 // 3)
                    dimy = 790 - h
                    Stone.dims.append((length, dimx, dimy))
                for i in range(corner[0], corner[0] + w):
                    Stone.stoneSurface[i] = 789 - h
                    Stone.middleSurface[i] = 789 - h
                Stone.middleStones.append((corner[0], 800, w, h, layer))

            else:
                layer = 2
                h = random.randint(20, 100)
                w = random.randint(50, 150)
                for dim in range(random.randint(0, 4)):
                    length = random.randint(5, w // 3)
                    dimx = random.randint(corner[0], corner[0] + w * 2 // 3)
                    dimy = 790 - h
                    Stone.dims.append((length, dimx, dimy))
                for i in range(corner[0], corner[0] + w):
                    Stone.stoneSurface[i] = 789 - h
                    Stone.frontSurface[i] = 789 - h
                Stone.frontStones.append((corner[0], 800, w, h, layer))

        #trees
        for tree in Tree.mediumTreeData:
            a = tree[0] + 800
            b = Stone.stoneSurface.get(a, 800)
            if b != 800:
                Tree.mediumTrees.append(MediumTree(a, b, season))
            x = tree[0]
            y = Stone.stoneSurface.get(x, 800)
            Tree.mediumTrees.append(MediumTree(x, y, season))
            c = tree[0] - 800
            d = Stone.stoneSurface.get(c, 800)
            if d != 800:
                Tree.mediumTrees.append(MediumTree(c, d, season))
        for bush in Tree.bushData:
            a = bush[0] + 800
            b = Stone.stoneSurface.get(a, 800)
            if b != 800:
                Tree.bushes.append(Bush(a, b, season))
            x = bush[0]
            y = Stone.stoneSurface.get(x, 800)
            Tree.bushes.append(Bush(x, y, season))
            c = bush[0] - 800
            d = Stone.stoneSurface.get(c, 800)
            if d != 800:
                Tree.bushes.append(Bush(c, d, season))
        for largeTree in Tree.largeTreeData:
            x = largeTree[0]
            y = 800
            height = random.randint(300, 600)
            Tree.largeTrees.append(LargeTree(x, y, height, season))
            Tree.largeTrees.append(LargeTree(x + 800, y, height, season))
            Tree.largeTrees.append(LargeTree(x - 800, y, height, season))
        #identify biggest gap
        gapOriginX,gapDestinationX,gapOriginY,gapDestinationY =\
        Stone.findBiggestGap(Stone,Stone.stoneSurface)
        #setup back stones for waterfall
        self.backStoneX = gapOriginX + (gapOriginX - gapDestinationX) / 2
        self.backStoneH = random.randint(500, 800)
        self.backStoneW = random.randint(50, 100)
        Stone.backStones.append(
            (self.backStoneX, 800, self.backStoneW, self.backStoneH, 0))
        self.middleStoneX = self.backStoneX + 30
        self.middleStoneH = random.randint(100, 500)
        self.middleStoneW = random.randint(100, 150)
        Stone.middleStones.append(
            (self.middleStoneX, 800, self.middleStoneW, self.middleStoneH, 1))
        self.frontStoneX = self.middleStoneX + 30
        self.frontStoneH = random.randint(20, 100)
        self.frontStoneW = random.randint(50, 150)
        Stone.frontStones.append(
            (self.frontStoneX, 800, self.frontStoneW, self.frontStoneH, 2))
        Waterfall.waterfalls.append(Waterfall(self.backStoneX,800-self.backStoneH,\
        self.backStoneW,self.backStoneH,self.middleStoneX,800-self.middleStoneH,\
        self.middleStoneW,self.middleStoneH,self.frontStoneX,800-self.frontStoneH,
        self.frontStoneW,self.frontStoneH))
        #identify bridge gaps
        #back
        if Stone.findBiggestGap(Stone, Stone.backSurface) != None:
            backX1,backX2,backY1,backY2 =\
            Stone.findBiggestGap(Stone,Stone.backSurface)
            Bridge.bridges.append(\
            Bridge(backX1,backX2,backY1,backY2))
        #middle
        if Stone.findBiggestGap(Stone, Stone.middleSurface) != None:
            middleX1,middleX2,middleY1,middleY2 =\
            Stone.findBiggestGap(Stone,Stone.middleSurface)
            Bridge.bridges.append(\
            Bridge(middleX1,middleX2,middleY1,middleY2))
        #front
        if Stone.findBiggestGap(Stone, Stone.frontSurface) != None:
            frontX1,frontX2,frontY1,frontY2 =\
            Stone.findBiggestGap(Stone,Stone.frontSurface)
            Bridge.bridges.append(\
            Bridge(frontX1,frontX2,frontY1,frontY2))

        #instruction
        self.showInstruction = False
        self.showNavigation = True
        self.timer = 0
        self.leftArrow = pygame.image.load('menu/leftarrow.png')
        self.rightArrow = pygame.image.load('menu/rightarrow.png')
        self.leftArrowRect = self.leftArrow.get_rect()
        self.rightArrowRect = self.rightArrow.get_rect()
        self.leftArrowRect.center = (50, self.height / 2)
        self.rightArrowRect.center = (1230, self.height / 2)
        self.navigation = pygame.image.load('menu/lookaround.png')
        self.navRect = self.navigation.get_rect()
        self.navRect.center = (self.width / 2, 30)

        #menubar
        self.menuBar = pygame.image.load('menu/menu.png')
        self.menuPos = (1150, 20)
        self.menuExpanded = False
        self.seasonBar = pygame.image.load('menu/seasons.png')
        self.seaPos = (960, 20)
        self.seasonExpanded = False
        self.arrow = pygame.image.load('menu/downarrow.png')
        self.arrowPos = (1055, 75)
        self.arrowShow = False
        self.spring = pygame.image.load('menu/spring.png')
        self.spring1 = pygame.image.load('menu/spring1.png')
        self.springPos = (960, 70)
        self.summer = pygame.image.load('menu/summer.png')
        self.summer1 = pygame.image.load('menu/summer1.png')
        self.summerPos = (960, 120)
        self.autumn = pygame.image.load('menu/autumn.png')
        self.autumn1 = pygame.image.load('menu/autumn1.png')
        self.autumnPos = (960, 170)
        self.winter = pygame.image.load('menu/winter.png')
        self.winter1 = pygame.image.load('menu/winter1.png')
        self.winterPos = (960, 220)
        self.makeitsnow = pygame.image.load('menu/makeitsnow.png')
        self.makePos = (20, 20)
        self.snowed = False
        #snow
        self.snowDensity = 30
        self.snowSpeed = 10
        self.snowflakes = []
        self.snowflake = pygame.image.load('menu/snowflake.png')
        for i in range(self.snowDensity):
            snowX = random.randint(0, 1280)
            snowY = random.randint(0, 800)
            self.snowflakes.append((snowX, snowY))
Exemplo n.º 17
0
 def set_stones(self, flist, color):
     for z in flist:
         self.stones[z] = Stone(z, color)
     self.stones[z] = Stone(z, Stone.EMPTY)
Exemplo n.º 18
0
    def run(self,timeout):
        TIMEOUT = timeout

        board = Board()
        board.populate()

        status_black = utils.load_image(STATUS_BLACK)
        status_white = utils.load_image(STATUS_WHITE)
        status_red = utils.load_image(STATUS_RED)
        status_disabled = utils.load_image(STATUS_DISABLED)
        cmd_exit = utils.load_image(EXIT)
        cmd_stop = utils.load_image(PLAYER_STOP)
        cmd_play = utils.load_image(PLAYER_PLAY)
        cmd_pause = utils.load_image(PLAYER_PAUSE)
        cmd_pass = utils.load_image(PLAYER_END)
        background = utils.load_image(BOARD_BACKGROUND)

        background.blit(cmd_exit,(0,0))
        background.blit(cmd_stop,(22,0))
        background.blit(cmd_pause,(44,0))
        background.blit(cmd_play,(66,0))
        background.blit(cmd_pass,(88,0))

        rand = random.randint(0,1000)
        selected = BLACK

        if rand % 2 == 0 :
            selected = RED

        rand = random.randint(0,1000)
        ai_color = RED

        if rand % 2 == 0 :
            ai_color = BLACK

        timeout = TIMEOUT
        time_left = TIMEOUT
        white = False
        winner = None
        status = 'stop'

        #menu
        menu = cMenu(5, 5, 2, 5, 'vertical', 100, self.screen,
               [( _('New Game'),  1, None), 
                ( _('One Player'),  2, None),
                ( _('Two Players'), 3, None),
                ( _('Return'),      4, None),
                ( _('Exit'),        5, None)])
        menu.set_center(True, True)
        menu.set_alignment('center', 'center')
        show_menu = True
        state = 0
        prev_state = 1
        rect_list = []

        while 1:
            if show_menu:
                # Check if the state has changed, if it has, then post a user event to
                # the queue to force the menu to be shown at least once
                if prev_state != state:
                    pygame.event.post(pygame.event.Event(EVENT_CHANGE_STATE, key = 0))
                    prev_state = state

                # Get the next event
                e = pygame.event.wait()

                # Update the menu, based on which "state" we are in - When using the menu
                # in a more complex program, definitely make the states global variables
                # so that you can refer to them by a name
                if e.type == pygame.KEYDOWN or e.type == EVENT_CHANGE_STATE:
                    if state == 0:
                        rect_list, state = menu.update(e, state)
                    elif state == 1:
                        print 'New Game'
                        state = 0
                        board = Board()
                        board.populate()
                        status = 'play'
                        show_menu = False
                    elif state == 2:
                        print 'One Player'
                        state = 0
                        self.player = 1
                        show_menu = False
                    elif state == 3:
                        print 'Two Players'
                        self.player = 2
                        show_menu = False
                        state = 0
                    elif state == 4:
                        print 'Return'
                        show_menu = False
                        state = 0
                    else:
                        print 'Exit!'
                        pygame.quit()
                        sys.exit()

                # Quit if the user presses the exit button
                if e.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()

                # Update the screen
                pygame.display.update(rect_list)
                
                if show_menu: 
                    continue
            
            #AI
            if self.player == 1 and ai_color == selected and status == 'play':
                p = Player(board , ai_color)
                p.play(self.screen)
                selected = utils.invert_color(selected)
                timeout = TIMEOUT
                board.unselectedAll()

            for event in pygame.event.get():
                x = y = -1
                if event.type == QUIT:
                    return
                elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                    show_menu = True
                elif event.type == MOUSEBUTTONDOWN:
                    m_pos = pygame.mouse.get_pos()

                    (x , y, pass_round) = board.getSelectedPosition(m_pos)

                    if pass_round:
                        selected = utils.invert_color(selected)
                        board.unselectedAll()
                        continue

                    if x >= 0 and y >= 0 :

                        if board.stoneExists(x,y) :
                            status = 'play'

                            oStone = Stone(board,x,y)
                            if oStone.color == selected and oStone.canMove() :
                                sStone = Stone(board,x,y)
                                sStone.selected()

                            else :
                                try:
                                    if oStone.mustChoose():
                                        timeout = TIMEOUT
                                        sStone.choose(x,y)
                                        if sStone.canMove():
                                            sStone.selected()

                                        else:
                                            selected = utils.invert_color(sStone.color)
                                            board.unselectedAll()

                                except:
                                    print "object sStone doesn't exist"

                        else:
                            (old_x,old_y,color) = board.getSelectedStone()
                            if old_x>=0 and old_y>=0 and color>0 :
                                if sStone.move(x,y) :
                                    timeout = TIMEOUT
                                    if sStone.mustChoose():
                                        sStone.selected()

                                    elif sStone.canMove() :
                                        sStone.selected()


                                    else:
                                        selected = utils.invert_color(sStone.color)
                                        sStone.unselected()
                    #action
                    else:
                        (m_x , m_y) = m_pos
                        if m_x < 22 and m_y < 22:
                            # quit
                            return
                        elif m_x > 22 and m_x < 44 and m_y < 22:
                            # stop
                            board = Board()
                            board.populate()
                            # print "stop"
                            status = 'stop'

                        elif m_x > 44 and m_x < 66 and m_y < 22:
                            # pause
                            # print "pause"
                            if status == 'pause':
                                status = 'play'
                                timeout = time_left
                                time_left = 0

                            else:
                                status = 'pause'
                                time_left = timeout

                        elif m_x > 66 and m_x < 88 and m_y < 22:
                            # play
                            # print "play"
                            if status == 'pause':
                                timeout = time_left
                                time_left = 0

                            status = 'play'
                            if winner != None:
                                board = Board()
                                board.populate()

                        elif m_x > 88 and m_x < 110 and m_y < 22:
                            # play
                            # print "pass"
                            selected=utils.invert_color(selected)
                            timeout=TIMEOUT
                            status = 'play'

            self.screen.blit(background, (0, 0))

            #check if a player wins
            winner = board.checkWinner();
            if winner != None:
                font = pygame.font.Font(pygame.font.get_default_font(), 26)
                text =  _('%s wins') % utils.getColorName(winner)
                text = font.render(text, 1, (10, 10, 10))
                textpos = text.get_rect()
                textpos.centerx = background.get_rect().centerx
                self.screen.blit(text, textpos)
                status = 'pause'

            #show timer
            font = pygame.font.Font(pygame.font.get_default_font(), 16)
            if status == 'play':
                text =  _('%d seconds left') % (timeout/1000)
                text = font.render(text, 1, (10, 10, 10))
                textpos = text.get_rect()
                textpos.centerx = background.get_rect().centerx
                self.screen.blit(text, textpos)
            elif status == 'pause' and winner == None :
                text =  _('%d seconds left') % (time_left/1000)
                text = font.render(text, 1, (10, 10, 10))
                textpos = text.get_rect()
                textpos.centerx = background.get_rect().centerx
                self.screen.blit(text, textpos)

            #blink
            if status == 'play':
                if timeout < 10000 and not white:
                    if selected == BLACK :
                        self.screen.blit(status_white, (500-40, 0))
                        self.screen.blit(status_disabled, (500-20, 0))
                    else:
                        self.screen.blit(status_disabled, (500-40, 0))
                        self.screen.blit(status_white, (500-20, 0))
                    white = True

                else :
                    if selected == BLACK :
                        self.screen.blit(status_black, (500-40, 0))
                        self.screen.blit(status_disabled, (500-20, 0))
                    else:
                        self.screen.blit(status_disabled, (500-40, 0))
                        self.screen.blit(status_red, (500-20, 0))
                    white = False

            #show
            board.populate_gui(self.screen)
            pygame.display.update()
            pygame.time.delay(DELAY)

            #time out
            timeout -= DELAY
            if status == "stop" or status =="pause":
                timeout = TIMEOUT

            if timeout < 0 :
                selected = utils.invert_color(selected)
                board.unselectedAll()
                timeout = TIMEOUT
Exemplo n.º 19
0
    def play(self, screen):
        """the AI plays here"""
        must_choose = 0
        (ai_x, ai_y) = self.getSelectedStone()
        #        print "AI x = %d y = %d " % (ai_x,ai_y)

        pStone = Stone(self.board, ai_x, ai_y)
        pStone.selected()

        (new_x, new_y, nb, action) = pStone.canCaptureStones()
        if new_x >= 0 and new_y >= 0:
            self.update(screen)

        timeout = 0
        while 1:

            if timeout < 10000:
                self.update(screen)
                timeout += 1

            (new_x, new_y, nb, action) = pStone.canCaptureStones()
            if new_x >= 0 and new_y >= 0:

                timeout = 0

                #                print "moves to (%d , %d)" % (new_x,new_y)
                if pStone.move(new_x, new_y):
                    if pStone.mustChoose():
                        pStone.chooseAction(action)
                        pStone.selected()

                    if pStone.canMove():
                        pStone.selected()

                    else:
                        #                         print "can't move and capture "
                        pStone.unselected()
                        break

            else:
                stones = self.getFreeStones()

                found = False
                for (sx, sy) in stones:
                    neib = utils.get_positions((sx, sy))

                    for (xx, yy) in neib:
                        oStone = Stone(self.board, sx, sy)

                        if xx >= 0 and xx < self.board.cols and yy >= 0 and yy < self.board.rows and oStone.legalMove(
                                xx, yy):
                            new_board = self.board.copy()
                            new_board.unselectedAll()

                            oStone = Stone(new_board, sx, sy)
                            if oStone.move(xx, yy):
                                if not oStone.canBeCaptured():
                                    found = True
                                    pStone = Stone(self.board, sx, sy)
                                    pStone.selected()
                                    pStone.move(xx, yy)
                                    break

                    if found:
                        break

                if found:
                    break

                else:
                    print "must choose one"
                    neib = utils.get_positions((ai_x, ai_y))
                    oStone = Stone(self.board, ai_x, ai_y)
                    for (xx, yy) in neib:
                        if xx >= 0 and xx < self.board.cols and yy >= 0 and yy < self.board.rows and oStone.legalMove(
                                xx, yy):
                            found = True
                            pStone = Stone(self.board, sx, sy)
                            pStone.selected()
                            pStone.move(xx, yy)
                            break
                    break

            self.board.unselectedAll()
            self.update(screen)
Exemplo n.º 20
0
def get_player(mode,name,color,education_bord=None):

    if color == 'black':
        if mode == 'NextOne':
            player =NextOne(Stone("●"),name,education_bord)
        elif mode == 'CountStone':
            player = CountStone(Stone("●"),name,education_bord)
        elif mode == 'Random':
            player = Random(Stone("●"),name,education_bord)
        elif mode == 'Q_learning':
            player = Q_learning(Stone("●"),name,education_bord)
        elif mode == 'Naive':
            player = Naive(Stone("●"),name,education_bord)
        elif mode == 'MLP':
            player = MLP_p(Stone("●"),name,education_bord)
    else:
        if mode == 'NextOne':
            player = NextOne(Stone("○"),name,education_bord)
        elif mode == 'CountStone':
            player = CountStone(Stone("○"),name,education_bord)
        elif mode == 'Random':
            player = Random(Stone("○"),name,education_bord)
        elif mode == 'Q_learning':
            player = Q_learning(Stone("○"),name,education_bord)
        elif mode == 'Naive':
            player = Naive(Stone("○"),name,education_bord)
        elif mode == 'MLP':
            player = MLP_p(Stone("○"),name,education_bord)
    return player
Exemplo n.º 21
0
 def can_put(self, te, color):
     if 0 <= te < self.NxN:
         if 0 < self.check_flip(Stone(te, color)):
             return True
     return False
Exemplo n.º 22
0
    def loop(self):
        """
        The main loop

        	@param self -- the JogoDoPiano
	    
        """
        sc = Screen()
        screen = sc.getScreen()

        statusBar = Label(10, 805, [(0, 0), (1, 1), (0, 1)])
        statusBarRight = Label(1100, 805, [(0, 0), (1, 1), (0, 1)])
        #statusBar = Label(10, 10, [(0,0), (1,1), (0,1)])

        lampCursor = Cursor("lamp", 0, 0, [(0, 0), (1, 1), (0, 1)])
        earCursor = Cursor("ear", 0, 0, [(0, 0), (1, 1), (0, 1)])
        lamp = Lamp("lamp", 825, 709, [(836, 721), (868, 721), (836, 779),
                                       (868, 779)], lampCursor)
        #lamp = Lamp("lamp", 825, 709, [(0, 0), (0, 20), (20, 20), (20, 0)], lampCursor)
        game = Game()

        buttons = [
            Key("1", -36, 507, [(28, 508), (0, 566), (0, 639), (31, 649),
                                (49, 602), (30, 595), (58, 514)], game),
            Key("2", 31, 510, [(60, 512), (30, 595), (58, 603), (89, 518)],
                game),
            Key("3", 19, 516, [(89, 518), (59, 603), (49, 602), (34, 649),
                               (88, 664), (100, 615), (84, 610), (104, 520)],
                game),
            Key("4", 85, 517, [(106, 520), (84, 610), (113, 615), (133, 524)],
                game),
            Key("5", 75, 524, [(134, 524), (113, 615), (100, 614), (87, 663),
                               (137, 672), (158, 527)], game),
            Key("6", 131, 527, [(160, 527), (138, 671), (196, 677), (200, 624),
                                (184, 621), (196, 532)], game),
            Key("7", 187, 529, [(194, 532), (184, 621), (213, 623),
                                (221, 536)], game),
            Key("8", 190, 535, [(221, 536), (215, 623), (201, 624), (196, 680),
                                (254, 677), (253, 628), (232, 626),
                                (237, 538)], game),
            Key("9", 234, 534, [(236, 536), (232, 625), (263, 626),
                                (262, 539)], game),
            Key("10", 243, 538,
                [(262, 539), (263, 626), (252, 627), (254, 676), (303, 675),
                 (296, 627), (284, 626), (279, 538)], game),
            Key("11", 282, 532, [(279, 538), (285, 625), (312, 625),
                                 (301, 538)], game),
            Key("12", 295, 540, [(302, 540), (312, 625), (297, 627),
                                 (301, 676), (344, 670), (317, 539)], game),
            Key("13", 320, 537, [(317, 539), (344, 671), (389, 662),
                                 (378, 625), (367, 624), (342, 538)], game),
            Key("14", 342, 532, [(342, 538), (367, 623), (387, 621),
                                 (358, 535)], game),
            Key("15", 358, 535,
                [(358, 535), (387, 620), (378, 625), (389, 661), (432, 651),
                 (418, 618), (407, 619), (376, 534)], game),
            Key("16", 377, 526, [(376, 534), (406, 618), (424, 614),
                                 (394, 530)], game),
            Key("17", 393, 529, [(394, 531), (424, 613), (418, 618),
                                 (431, 653), (473, 639), (411, 529)], game),
            Key("18", 413, 524, [(413, 530), (474, 639), (511, 628),
                                 (496, 603), (484, 605), (434, 525)], game),
            Key("19", 436, 516, [(435, 526), (483, 604), (503, 599),
                                 (456, 521)], game),
            Key("20", 455, 520,
                [(455, 522), (503, 599), (496, 603), (511, 629), (548, 615),
                 (532, 592), (522, 593), (474, 520)], game),
            Key("21", 475, 508, [(473, 520), (522, 592), (542, 585),
                                 (493, 514)], game),
            Key("22", 494, 512,
                [(493, 514), (541, 585), (532, 591), (548, 616), (585, 600),
                 (568, 579), (559, 580), (507, 512)], game),
            Key("23", 510, 502, [(508, 512), (559, 580), (577, 574),
                                 (526, 507)], game),
            Key("24", 526, 506, [(527, 508), (577, 574), (569, 582),
                                 (585, 602), (621, 586), (538, 504)], game),
            Key("25", 539, 496, [(538, 504), (622, 588), (661, 571),
                                 (639, 550), (626, 553), (572, 496)], game),
            Key("26", 572, 485, [(572, 496), (627, 553), (644, 546),
                                 (595, 494)], game),
            Key("27", 595, 492,
                [(596, 493), (645, 546), (639, 551), (662, 572), (698, 562),
                 (672, 538), (664, 539), (610, 491)], game),
            Key("28", 611, 481, [(610, 492), (664, 539), (679, 534),
                                 (632, 488)], game),
            Key("29", 632, 488, [(633, 489), (679, 534), (672, 539),
                                 (698, 562), (729, 553), (646, 488)], game),
            Key("30", 647, 485, [(645, 488), (731, 556), (763, 550),
                                 (739, 531), (728, 531), (671, 485)], game),
            Key("31", 672, 478, [(670, 485), (728, 530), (742, 527),
                                 (690, 484)], game),
            Key("32", 690, 483,
                [(693, 484), (742, 528), (739, 532), (763, 551), (796, 545),
                 (778, 530), (769, 529), (714, 482)], game),
            Key("33", 715, 474, [(714, 483), (770, 529), (784, 525),
                                 (734, 481)], game),
            Key("34", 735, 481,
                [(736, 481), (784, 524), (777, 529), (797, 547), (829, 543),
                 (810, 527), (802, 525), (748, 481)], game),
            Key("35", 746, 472, [(749, 481), (802, 525), (817, 521),
                                 (771, 480)], game),
            Key("36", 771, 482, [(773, 481), (818, 522), (810, 527),
                                 (830, 543), (869, 541), (793, 481)], game),
            Key("37", 794, 481, [(793, 481), (869, 540), (902, 539),
                                 (879, 520), (866, 518), (821, 480)], game),
            Key("38", 823, 472, [(820, 480), (866, 519), (881, 516),
                                 (843, 480)], game),
            Key("39", 843, 480,
                [(843, 480), (881, 518), (878, 520), (902, 539), (936, 537),
                 (909, 515), (899, 516), (852, 480)], game),
            Stick("2", _("Aleluia - Handel - 4 notes"), 892,
                  241, [(1022, 330), (1017, 325), (1011, 327), (1005, 319),
                        (1008, 315), (1010, 301), (1008, 296), (1004, 289),
                        (993, 284), (984, 277), (983, 257), (988, 248),
                        (998, 247), (1007, 247), (1013, 249), (1018, 257),
                        (1020, 269), (1016, 277), (1010, 282), (1007, 285),
                        (1009, 295), (1016, 295), (1021, 326)], game),
            Stick("1", _("Silent Night - Christmas Song - 4 notes"), 885,
                  271, [(1006, 343), (1001, 338), (1006, 330), (1000, 322),
                        (994, 325), (984, 324), (976, 319), (970, 308),
                        (973, 296), (982, 288), (992, 285), (1003, 290),
                        (1008, 299), (1009, 309), (1004, 321), (1010, 327),
                        (1017, 325), (1038, 351)], game),
            Stick("3", _("Save me - Hanson - 4 notes"), 917, 222,
                  [(1039, 351), (1027, 338), (1024, 278), (1031, 276),
                   (1030, 265), (1026, 262), (1019, 256), (1015, 246),
                   (1016, 233), (1026, 229), (1036, 226), (1046, 231),
                   (1049, 238), (1049, 252), (1044, 262), (1036, 266),
                   (1037, 276), (1046, 276), (1049, 352), (1037, 352)], game),
            Stick("4", _("Every breath you take - Sting - 5 notes"), 944, 213,
                  [(1050, 233), (1054, 223), (1063, 218), (1071, 218),
                   (1084, 223), (1087, 235), (1083, 247), (1079, 253),
                   (1070, 250), (1061, 249), (1055, 249), (1053, 248)], game),
            Stick("5", _("Aquarela - Toquinho - 6 notes"), 950, 247,
                  [(1061, 353), (1055, 302), (1063, 299), (1062, 289),
                   (1055, 283), (1048, 276), (1047, 263), (1053, 254),
                   (1059, 250), (1070, 250), (1082, 257), (1086, 271),
                   (1081, 278), (1074, 282), (1070, 289), (1070, 299),
                   (1078, 301), (1081, 351)], game),
            Stick("6", _("Wedding March - Felix Mendelssohn - 7 notes"), 969,
                  202, [(1091, 233), (1091, 239), (1094, 248), (1100, 257),
                        (1108, 257), (1116, 257), (1125, 252), (1128, 242),
                        (1129, 234), (1128, 226), (1122, 222), (1111, 219),
                        (1102, 221), (1096, 226)], game),
            Stick("7", _("Amor Maior - Jota Quest - 8 notes"), 977, 251,
                  [(1081, 352), (1079, 324), (1086, 304), (1093, 308),
                   (1097, 296), (1094, 291), (1090, 283), (1089, 275),
                   (1091, 267), (1096, 263), (1104, 261), (1109, 260),
                   (1114, 262), (1119, 266), (1126, 272), (1126, 279),
                   (1124, 290), (1119, 295), (1108, 298), (1102, 299),
                   (1101, 303), (1098, 311), (1105, 313), (1091, 351)], game),
            Stick("8", _("Greenleaves - Anonymous - 9 notes"), 1014, 211,
                  [(1108, 346), (1118, 296), (1124, 292), (1125, 286),
                   (1127, 279), (1127, 274), (1124, 266), (1129, 267),
                   (1133, 257), (1127, 252), (1128, 251), (1129, 242),
                   (1130, 238), (1130, 230), (1128, 222), (1133, 219),
                   (1136, 217), (1144, 217), (1149, 220), (1155, 226),
                   (1158, 234), (1158, 245), (1152, 252), (1143, 258),
                   (1136, 263), (1135, 267), (1133, 271), (1136, 286),
                   (1134, 278), (1138, 291), (1140, 293), (1140, 301),
                   (1132, 301), (1129, 301), (1126, 316), (1120, 328),
                   (1114, 339), (1111, 344)], game),
            Stick("9", _("Velha Infancia - Tribalistas - 5 notes"), 985, 219,
                  [(1113, 344), (1130, 303), (1139, 303), (1143, 294),
                   (1139, 288), (1136, 282), (1135, 273), (1138, 266),
                   (1140, 262), (1146, 259), (1157, 259), (1167, 260),
                   (1170, 269), (1173, 278), (1171, 286), (1165, 293),
                   (1157, 295), (1149, 297), (1148, 298), (1146, 307),
                   (1151, 309), (1139, 339), (1113, 345)], game),
            Stick("10", _("Jingle Bells - Christmas Song - 11 notes"), 883,
                  496, [(1009, 604), (1002, 560), (1009, 558), (1009, 547),
                        (1004, 545), (996, 543), (991, 533), (990, 526),
                        (995, 513), (997, 510), (1007, 507), (1016, 507),
                        (1021, 511), (1025, 518), (1027, 528), (1024, 536),
                        (1019, 545), (1014, 546), (1014, 557), (1023, 557),
                        (1028, 609)], game),
            Stick("11", _("Habanera - Georges Bizet - 8 notes"), 927, 479,
                  [(1039, 612), (1034, 536), (1040, 533), (1040, 521),
                   (1034, 519), (1029, 515), (1024, 509), (1024, 499),
                   (1026, 490), (1030, 487), (1039, 485), (1048, 484),
                   (1059, 490), (1062, 498), (1062, 509), (1056, 515),
                   (1047, 521), (1046, 523), (1047, 534), (1054, 534),
                   (1059, 612)], game),
            Stick("12", _("Yellow Submarine - Beatles - 10 notes"), 954, 497,
                  [(1060, 609), (1059, 589), (1062, 571), (1066, 552),
                   (1071, 551), (1074, 541), (1068, 537), (1062, 526),
                   (1064, 513), (1071, 504), (1085, 503), (1087, 504),
                   (1096, 512), (1099, 520), (1096, 532), (1089, 539),
                   (1080, 543), (1078, 553), (1086, 556), (1074, 614)], game),
            Stick("13",
                  _("Jesu, Joy of Man's Desiring - J. S. Bach - 12 notes"),
                  995, 495,
                  [(1087, 612), (1105, 549), (1113, 550), (1114, 538),
                   (1110, 533), (1104, 522), (1105, 515), (1112, 506),
                   (1118, 504), (1126, 501), (1134, 503), (1139, 509),
                   (1143, 519), (1139, 533), (1129, 541), (1122, 543),
                   (1117, 553), (1126, 554), (1108, 608)], game),
            Stick("14", _("Symphony No. 9 - Beethoven - 15 notes"), 1027, 515,
                  [(1113, 605), (1133, 566), (1138, 569), (1145, 560),
                   (1142, 551), (1139, 544), (1139, 536), (1145, 529),
                   (1152, 525), (1160, 526), (1169, 529), (1176, 539),
                   (1174, 553), (1169, 560), (1158, 565), (1151, 564),
                   (1146, 572), (1151, 578), (1138, 600)], game), lampCursor,
            earCursor, lamp, statusBar, statusBarRight
        ]

        helps = [
            Help("re", 370, 500, [(0, 0), (1, 1), (0, 1)]),
            Help("re_sust", 380, 458, [(0, 0), (1, 1), (0, 1)]),
            Help("mi", 415, 490, [(0, 0), (1, 1), (0, 1)]),
            Help("fa", 450, 478, [(0, 0), (1, 1), (0, 1)]),
            Help("fa_sust", 455, 435, [(0, 0), (1, 1), (0, 1)]),
            Help("sol", 490, 470, [(0, 0), (1, 1), (0, 1)]),
            Help("sol_sust", 490, 425, [(0, 0), (1, 1), (0, 1)]),
            Help("la", 525, 460, [(0, 0), (1, 1), (0, 1)]),
            Help("la_sust", 530, 417, [(0, 0), (1, 1), (0, 1)]),
            Help("si", 565, 445, [(0, 0), (1, 1), (0, 1)]),
            Help("do", 595, 430, [(0, 0), (1, 1), (0, 1)]),
            Help("do_sust", 600, 395, [(0, 0), (1, 1), (0, 1)]),
            Help("re", 640, 420, [(0, 0), (1, 1), (0, 1)]),
            Help("re_sust", 630, 380, [(0, 0), (1, 1), (0, 1)]),
            Help("mi", 675, 405, [(0, 0), (1, 1), (0, 1)]),
            Help("fa", 708, 402, [(0, 0), (1, 1), (0, 1)]),
            Help("fa_sust", 695, 370, [(0, 0), (1, 1), (0, 1)]),
            Help("sol", 742, 398, [(0, 0), (1, 1), (0, 1)])
        ]

        game.setButtons(buttons, helps)

        stones = [
            Stone("stone", 115, 187, [(116, 188), (159, 188), (159, 234),
                                      (116, 234)], game),
            Stone("stone", 163, 187, [(164, 188), (207, 188), (207, 234),
                                      (164, 234)], game),
            Stone("stone", 211, 187, [(212, 188), (255, 188), (255, 234),
                                      (212, 234)], game),
            Stone("stone", 259, 187, [(260, 188), (303, 188), (303, 234),
                                      (260, 234)], game),
            Stone("stone", 307, 187, [(308, 188), (351, 188), (351, 234),
                                      (308, 234)], game),
            Stone("stone", 355, 187, [(356, 188), (399, 188), (399, 234),
                                      (356, 234)], game),
            Stone("stone", 403, 187, [(404, 188), (447, 188), (447, 234),
                                      (404, 234)], game),
            Stone("stone", 451, 187, [(452, 188), (495, 188), (495, 234),
                                      (452, 234)], game),
            Stone("stone", 498, 187, [(500, 188), (543, 188), (543, 234),
                                      (500, 234)], game),
            Stone("stone", 546, 187, [(548, 188), (591, 188), (591, 234),
                                      (548, 234)], game),
            Stone("stone", 594, 187, [(596, 188), (639, 188), (639, 234),
                                      (596, 234)], game),
            Stone("stone", 642, 187, [(644, 188), (687, 188), (687, 234),
                                      (644, 234)], game),
            Stone("stone", 690, 187, [(692, 188), (735, 188), (735, 234),
                                      (692, 234)], game),
            Stone("stone", 738, 187, [(740, 188), (783, 188), (783, 234),
                                      (740, 234)], game),
            Stone("stone", 786, 187, [(788, 188), (831, 188), (831, 234),
                                      (788, 234)], game)
        ]

        animation = [
            Ball("bar", 80, 287, [(0, 0), (1, 1), (0, 1)], game),
            Ball("redBall", 10, 247, [(8, 247), (100, 247), (100, 338),
                                      (8, 338)], game),
            Ball("greenBall", 10, 247, [(8, 247), (100, 247), (100, 338),
                                        (8, 338)], game),
            Ball("blueBall", 10, 247, [(8, 247), (100, 247), (100, 338),
                                       (8, 338)], game),
            Ball("yellowBall", 10, 247, [(8, 247), (100, 247), (100, 338),
                                         (8, 338)], game),
            Ball("rightBall", 850, 267, [(0, 0), (1, 1), (0, 1)], game)
        ]

        game.addChallenge(
            Challenge("noitefeliz", [20, 22, 20, 17], buttons[40], stones,
                      [5, 7, 5, 2], animation[1], animation[0], animation[5],
                      "easy", game))
        game.addChallenge(
            Challenge("aleluia", [27, 22, 24, 22], buttons[39], stones,
                      [12, 7, 9, 7], animation[3], animation[0], animation[5],
                      "easy", game))
        game.addChallenge(
            Challenge("saveme", [16, 17, 24, 17], buttons[41], stones,
                      [1, 2, 9, 2], animation[2], animation[0], animation[5],
                      "easy", game))
        game.addChallenge(
            Challenge("everybreath", [26, 27, 26, 24, 22], buttons[42], stones,
                      [11, 12, 11, 9, 7], animation[3], animation[0],
                      animation[5], "easy", game))
        game.addChallenge(
            Challenge("aquarela", [15, 15, 20, 20, 19, 17], buttons[43],
                      stones, [0, 0, 5, 5, 4, 2], animation[4], animation[0],
                      animation[5], "easy", game))
        game.addChallenge(
            Challenge("marchanupcial", [25, 24, 19, 22, 20, 18, 15],
                      buttons[44], stones, [10, 9, 4, 7, 5, 3, 0],
                      animation[1], animation[0], animation[5], "easy", game))
        game.addChallenge(
            Challenge("amormaior", [22, 23, 22, 25, 27, 22, 20, 18],
                      buttons[45], stones, [7, 8, 7, 10, 12, 7, 5, 3],
                      animation[2], animation[0], animation[5], "easy", game))
        game.addChallenge(
            Challenge("greenleaves", [17, 20, 22, 24, 25, 24, 22, 19, 15],
                      buttons[46], stones, [], animation[4], animation[0],
                      animation[5], "easy", game))
        game.addChallenge(
            Challenge("velhainfancia", [19, 22, 19, 22, 29], buttons[47],
                      stones, [4, 7, 4, 7, 14], animation[1], animation[0],
                      animation[5], "easy", game))
        game.addChallenge(
            Challenge("jinglebells",
                      [24, 24, 24, 24, 24, 24, 24, 27, 20, 22, 24],
                      buttons[48], stones, [9, 9, 9, 9, 9, 9, 9, 12, 5, 7, 9],
                      animation[1], animation[0], animation[5], "difficult",
                      game))
        game.addChallenge(
            Challenge("habanera", [27, 26, 25, 25, 25, 24, 23, 22],
                      buttons[49], stones, [12, 11, 10, 10, 10, 9, 8, 7],
                      animation[2], animation[0], animation[5], "difficult",
                      game))
        game.addChallenge(
            Challenge("yellow", [27, 27, 27, 27, 29, 24, 22, 22, 22, 22],
                      buttons[50], stones, [12, 12, 12, 12, 14, 9, 7, 7, 7, 7],
                      animation[3], animation[0], animation[5], "difficult",
                      game))
        game.addChallenge(
            Challenge("jesusalegria",
                      [20, 22, 24, 27, 25, 25, 29, 27, 27, 32, 31, 32],
                      buttons[51], stones,
                      [5, 7, 9, 12, 10, 10, 14, 12, 12, 17, 16, 17],
                      animation[0], animation[4], animation[5], "difficult",
                      game))
        game.addChallenge(
            Challenge(
                "nonasinfonia",
                [19, 19, 20, 22, 22, 20, 19, 17, 15, 15, 17, 19, 19, 17, 17],
                buttons[52], stones,
                [4, 4, 5, 7, 7, 5, 4, 2, 0, 0, 2, 4, 4, 2, 2], animation[3],
                animation[0], animation[5], "difficult", game))

        keyboard = pygame.image.load("image/keyboard.png")

        bigBar = pygame.image.load("image/bigBar.png")
        bigBar.set_alpha(None)  # disable alpha.
        bigBar.convert()
        bigBar.set_colorkey((255, 0, 255))  # magenta

        frame = pygame.image.load("image/frame.png")
        frame.set_alpha(None)  # disable alpha.
        frame.convert()
        frame.set_colorkey((255, 0, 255))  # magenta

        bucket1 = pygame.image.load("image/bucket1.png")
        bucket1.set_alpha(None)  # disable alpha.
        bucket1.convert()
        bucket1.set_colorkey((255, 0, 255))  # magenta

        bucket2 = pygame.image.load("image/bucket2.png")
        bucket2.set_alpha(None)  # disable alpha.
        bucket2.convert()
        bucket2.set_colorkey((255, 0, 255))  # magenta

        clock = pygame.time.Clock()
        FPS = 20

        while self.run:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
                else:
                    for button in buttons:
                        button.parseEvent(event)
                    for stone in stones:
                        stone.parseEvent(event)
                    for help in helps:
                        help.parseEvent(event)
                    for thing in animation:
                        thing.parseEvent(event)
                    if event.type == MOUSEMOTION:
                        posicao = (event.pos[0], event.pos[1])
                        #pygame.display.set_caption(str(posicao[0])+" x "+str(posicao[1]))

            screen.blit(keyboard, (0, -1))

            for button in buttons:
                button.draw(screen)

            for stone in stones:
                stone.draw(screen)

                for thing in animation:
                    thing.draw(screen)

            screen.blit(frame, (-39, 470))
            screen.blit(bigBar, (114, 274))

            for help in helps:
                help.draw(screen)

            screen.blit(bucket1, (988, 338))
            screen.blit(bucket2, (987, 597))

            lampCursor.draw(screen)
            earCursor.draw(screen)

            #update the surface in the screen
            pygame.display.flip()

            #control number of frames per second
            clock.tick(FPS)
Exemplo n.º 23
0
    def play(self,screen):
        """the AI plays here"""
        must_choose = 0
        (ai_x, ai_y) = self.getSelectedStone()
#        print "AI x = %d y = %d " % (ai_x,ai_y)

        pStone = Stone(self.board , ai_x ,ai_y)
        pStone.selected()

        (new_x, new_y , nb , action) = pStone.canCaptureStones()
        if new_x >= 0 and new_y >= 0 :
            self.update(screen)

        timeout = 0
        while 1 :

            if timeout<10000:
                self.update(screen)
                timeout+=1

            (new_x, new_y , nb , action) = pStone.canCaptureStones()
            if new_x >= 0 and new_y >= 0 :

                timeout = 0

#                print "moves to (%d , %d)" % (new_x,new_y)
                if pStone.move(new_x,new_y) :
                    if pStone.mustChoose():
                        pStone.chooseAction(action)
                        pStone.selected()

                    if pStone.canMove() :
                        pStone.selected()

                    else :
#                         print "can't move and capture "
                        pStone.unselected()
                        break

            else :
                stones = self.getFreeStones()

                found = False
                for (sx,sy) in stones :
                    neib = utils.get_positions((sx,sy))


                    for (xx ,yy) in  neib:
                        oStone = Stone(self.board,sx,sy)

                        if xx>=0 and xx < self.board.cols and yy >=0 and yy < self.board.rows and oStone.legalMove(xx,yy) :
                            new_board = self.board.copy()
                            new_board.unselectedAll()

                            oStone = Stone(new_board,sx,sy)
                            if oStone.move(xx,yy):
                                if not oStone.canBeCaptured():
                                    found = True
                                    pStone = Stone(self.board , sx ,sy)
                                    pStone.selected()
                                    pStone.move(xx,yy)
                                    break

                    if found:
                        break

                if found:
                    break

                else:
                    print "must choose one"
                    neib = utils.get_positions((ai_x,ai_y))
                    oStone = Stone(self.board,ai_x,ai_y)
                    for (xx ,yy) in  neib:
                        if xx>=0 and xx < self.board.cols and yy >=0 and yy < self.board.rows and oStone.legalMove(xx,yy) :
                            found = True
                            pStone = Stone(self.board , sx ,sy)
                            pStone.selected()
                            pStone.move(xx,yy)
                            break
                    break


            self.board.unselectedAll()
            self.update(screen)
Exemplo n.º 24
0
screen = pygame.Surface((width, height))

food = [
    Food(*np.random.rand(2) * width, width, height)
    for _ in range(grass_amount)
]
flock = [
    Boid(*np.random.rand(2) * width, width, height)
    for _ in range(herbivores_amount)
]
carnivores = [
    WildBoid(*np.random.rand(2) * width, width, height)
    for _ in range(carnivores_amount)
]
riff = [
    Stone(*np.random.rand(2) * width, width, height)
    for _ in range(riffs_amount)
]

grass_appearance_delay = int(grass_appearance_delay * 1000)
birth_delay *= 1000
current_time = pygame.time.get_ticks()
pygame.time.set_timer(pygame.USEREVENT, grass_appearance_delay)
done = True
while done:
    pygame.time.delay(25)
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            done = False
        if e.type == pygame.MOUSEBUTTONDOWN:
            pos = pygame.mouse.get_pos()
Exemplo n.º 25
0
def get_player(mode, name, color, bord=None):
    if color == 'black':
        player = MLP_p(Stone("●"), name, bord)
    else:
        player = MLP_p(Stone("○"), name, bord)
    return player