예제 #1
0
def main():
    global FPSCLOCK, DISPLAYSURF, BASICFONT, SCORE_SURF, SCORE_RECT
    global walls, capsulePos, game, pacman, score, scoreText
    PACMANPLAYER = True

    filename = ".\\layouts\\ASTinyMaze2.lay"
    #filename = ".\\layouts\\ADSmallClassic.lay"
    # filename = ".\\layouts\\ASMediumClassic.lay"
    # filename = ".\\layouts\\ASminimaxClassic.lay"
    # filename = ".\\layouts\\ADSmallClassic.lay"
    # filename = ".\\layouts\\smallDottedMaze.lay"

    scores = 0
    if (len(sys.argv) > 1):
        print(sys.argv[1])
        gAgent = sys.argv[1]
    else:
        gAgent = "random"
    game = PacGame(filename)
    pygame.init()
    FPSCLOCK = pygame.time.Clock()
    DISPLAYSURF = pygame.display.set_mode(
        (game.WINDOWWIDTH, game.WINDOWHEIGHT))

    strS = game.strS
    pygame.display.set_caption('PacMan')
    BASICFONT = pygame.font.Font('freesansbold.ttf', BASICFONTSIZE)
    BASICFONT = pygame.font.Font('freesansbold.ttf', BASICFONTSIZE)

    # Store the option buttons and their rectangles in OPTIONS.
    scoreText = 'SCORE:' + str(scores)
    SCORE_SURF, SCORE_RECT = makeText(scoreText, TEXTCOLOR, BGCOLOR, 10,
                                      game.WINDOWHEIGHT - 40)
    DISPLAYSURF.blit(SCORE_SURF, SCORE_RECT)
    game.genMaze()
    walls = game.walls
    game.drawWall(DISPLAYSURF)
    pacman = Pacman(game.pacmanPos, 0, PACCOLOR, PAC_SIZE, 0, walls,
                    game.MAZE_WIDTH, game.MAZE_HEIGHT)
    pacman.drawPacman(DISPLAYSURF)
    game.drawCapsule(DISPLAYSURF)
    game.drawFoods(DISPLAYSURF)
    ghost = Ghost(game, PINK, 0)
    ghost.drawGhost(DISPLAYSURF)
    problem = PacmanAdversarialGameProblem(game)
    pacmanAdvAgent = PacmanAdvGameAgent(problem)
    ghostAgent = GhostGameAgent(problem)
    pygame.time.wait(1000)
    slideTo = None
    stop = False
    while not stop:  # main game loop
        for event in pygame.event.get():  # event handling loop
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

        while (game.capsulePos or game.foodPos):
            for event in pygame.event.get():  # event handling loop
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
            if PACMANPLAYER:
                (collision, pac_action, new_pos) = pacmanAdvAgent.get_action()
                if collision:
                    stop = True
                    break
                currentPos = game.pacmanPos.pop(0)
                game.pacmanPos.append(new_pos)
                if new_pos in game.capsulePos:
                    index = game.capsulePos.index(new_pos)
                    game.capsulePos.pop(index)
                    t = pygame.time.get_ticks()
                    scores += int(t / 10000) + 1
                elif new_pos in game.foodPos:
                    index = game.foodPos.index(new_pos)
                    game.foodPos.pop(index)
                slideTo = pac_action
                if slideTo:
                    slideAnimation(pacman, currentPos, slideTo, "Ok", 8)  #

                scores += 1
                pygame.display.update()
                FPSCLOCK.tick(FPS)
            else:  #ghost play
                (collision, action, new_pos) = ghostAgent.get_action(gAgent)
                if collision:
                    stop = True
                    break
                currentPos = game.ghostPos.pop(0)
                game.ghostPos.append(new_pos)
                slideTo = action
                if slideTo:
                    slideAnimation(ghost, currentPos, slideTo, "Ok", 8)  #

                pygame.display.update()
                FPSCLOCK.tick(FPS)
            scoreText = 'SCORE:' + str(scores)
            SCORE_SURF, SCORE_RECT = makeText(scoreText, TEXTCOLOR, BGCOLOR,
                                              10, game.WINDOWHEIGHT - 40)
            DISPLAYSURF.blit(SCORE_SURF, SCORE_RECT)
            PACMANPLAYER = (not PACMANPLAYER)
        pygame.display.update()
        FPSCLOCK.tick(FPS)
예제 #2
0
def main():
    global FPSCLOCK, DISPLAYSURF, BASICFONT, SCORE_SURF, SCORE_RECT, SOLVE_SURF, SOLVE_RECT
    global walls, pacmanPos, capsulePos, game, pacman, score, scoreText
    score = 0
    filename = ".\\layouts\\smallestMaze.lay"
    game = PacGame(filename)

    pygame.init()
    FPSCLOCK = pygame.time.Clock()
    DISPLAYSURF = pygame.display.set_mode(
        (game.WINDOWWIDTH, game.WINDOWHEIGHT))
    pygame.display.set_caption('PacMan')
    BASICFONT = pygame.font.Font('freesansbold.ttf', BASICFONTSIZE)
    BASICFONT = pygame.font.Font('freesansbold.ttf', BASICFONTSIZE)

    # Store the option buttons and their rectangles in OPTIONS.
    scoreText = 'SCORE:' + str(score)
    SCORE_SURF, SCORE_RECT = makeText(scoreText, TEXTCOLOR, BGCOLOR, 10,
                                      game.WINDOWHEIGHT - 40)
    DISPLAYSURF.blit(SCORE_SURF, SCORE_RECT)
    game.genMaze()
    pacmanPos = game.pacmanPos

    walls = game.walls
    allMoves = []  # list of moves made from the solved configuration
    game.drawWall(DISPLAYSURF)
    pacman = Pacman(pacmanPos, 0, PACCOLOR, PAC_SIZE, 0, walls,
                    game.MAZE_WIDTH, game.MAZE_HEIGHT)
    pacman.drawPacman(DISPLAYSURF)
    game.drawCapsule(DISPLAYSURF)
    p_graph = build_graph(game)
    pygame.time.wait(2000)
    slideTo = None  # the direction, if any, a tile should slide
    scores = 0
    pygame.display.update()

    if (len(sys.argv) <= 1):
        while True:  # main game loop
            for event in pygame.event.get():  # event handling loop
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == KEYUP:
                    # check if the user pressed a key to slide a tile
                    if event.key in (K_LEFT, K_a):
                        slideTo = 'West'
                    elif event.key in (K_RIGHT, K_d):
                        slideTo = 'East'
                    elif event.key in (K_UP, K_w):
                        slideTo = 'North'
                    elif event.key in (K_DOWN, K_s):
                        slideTo = 'South'
                    if slideTo and len(game.capsulePos) > 0:
                        slideAnimation(slideTo, "Ok", 8)  #
                        scores += 1
                        pygame.display.update()
                        FPSCLOCK.tick(FPS)
                        scoreText = 'SCORE:' + str(scores)
                        SCORE_SURF, SCORE_RECT = makeText(
                            scoreText, TEXTCOLOR, BGCOLOR, 10,
                            game.WINDOWHEIGHT - 40)
                        DISPLAYSURF.blit(SCORE_SURF, SCORE_RECT)
                    pygame.display.update()
                    FPSCLOCK.tick(FPS)
    else:
        if (sys.argv[1] == 'auto'):
            choices = ['East', 'West', 'North', 'South']
            while True:  # main game loop
                for event in pygame.event.get():  # event handling loop
                    if event.type == QUIT:
                        pygame.quit()
                        sys.exit()
                #scores=0
                while (game.capsulePos):
                    pac_actions = []
                    #Q6) add a simple reflex agent:It randomly choose an action,
                    # and append it to the list pac_actions
                    # you may need nextDirectionIsValid(direction, pacman.pos[0]) in pacmanGame.py

                    pac_actions = find_solution(p_graph)
                    game.pacmanPos.pop(0)
                    game.pacmanPos.append(game.capsulePos[0])
                    while (pac_actions):
                        slideTo = pac_actions.pop(0)
                        if slideTo:
                            slideAnimation(slideTo, "Ok", 8)  #
                        scores += 1
                        FPSCLOCK.tick(FPS)
                        scoreText = 'SCORE:' + str(scores)
                        SCORE_SURF, SCORE_RECT = makeText(
                            scoreText, TEXTCOLOR, BGCOLOR, 10,
                            game.WINDOWHEIGHT - 40)
                        DISPLAYSURF.blit(SCORE_SURF, SCORE_RECT)

                pygame.display.update()
                FPSCLOCK.tick(FPS)