Exemple #1
0
def game_loop(win, mapa, neural_net, train):
    pacman = Pacman(mapa, neural_net)
    blinky = Blinky(mapa, pacman)
    pinky = Pinky(mapa, pacman)
    clyde = Clyde(mapa, pacman)
    inky = Inky(mapa, pacman)
    clock = pygame.time.Clock()

    while pacman.isAlive:
        events = pygame.event.get()
        handle_quit(events)

        win.fill((0, 0, 0))

        up = 0
        down = 0
        left = 0
        right = 0

        input_data = np.array([[calcDist(pacman.getPos(), blinky.getPos()), calcDist(pacman.getPos(), pinky.getPos()), calcDist(pacman.getPos(), inky.getPos()), calcDist(pacman.getPos(), clyde.getPos())]])

        np.interp(input_data, (0, 36), (-1, +1))

        if(mapa.map[pacman.getPos()[1] - 1][pacman.getPos()[0]] != 1):
            up = 1
        if(mapa.map[pacman.getPos()[1] + 1][pacman.getPos()[0]] != 1):
            down = 1
        if(mapa.map[pacman.getPos()[1]][pacman.getPos()[0] + 1] != 1):
            right = 1
        if(mapa.map[pacman.getPos()[1]][pacman.getPos()[0] - 1] != 1):
            left = 1
        
        input_data = np.append(np.array([[up, right, down, left]]), input_data, axis=1)

        pacman.update(input_data)
        blinky.update()
        pinky.update()
        clyde.update()
        inky.update(blinky.getPos())

        if train == False: 
            mapa.draw(win) 
            pacman.draw(win)
            blinky.draw(win)
            pinky.draw(win)
            clyde.draw(win)
            inky.draw(win)
            textsurface = pygame.font.SysFont('Comic Sans MS', 25).render('Score: {}'.format(pacman.score), False, (255, 255, 255))
            win.blit(textsurface,(240,300))
            pygame.display.update()
            clock.tick(FPS)
    return (pacman.score, pacman.count2)
Exemple #2
0
def gameloop():
    """
    Description: the main loop that will run forever while the game is on.
    Returns: play(bool): return False if the quit button is pressed by the
    user, return True otherwise.
    """
    global pacman_map, pacman, pyman, won, counter, end
    # load the map and get the RGB values of the wall colour generated randomly
    wall_colour = map_load()

    # Create the pacman(and pyman, for mode 3) object(s).
    pacman = Pacman(9, 16, pacman_map, "right", "pacman")
    if gamemode == 3:
        pyman = Pacman(9, 18, pacman_map, "right", "pyman")

    # initialize the timer to tick every 1000ms(1s)
    pygame.time.set_timer(pygame.USEREVENT, 1000)
    end = False
    play = True

    # intialize the ghost objects
    ghost_list = ghost_init()

    while not won and not end:
        # control the pacman with 4 move keys
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    pacman.Left()
                elif event.key == pygame.K_RIGHT:
                    pacman.Right()
                elif event.key == pygame.K_UP:
                    pacman.Up()
                elif event.key == pygame.K_DOWN:
                    pacman.Down()
                pacman.eat_pacdot()

                if gamemode == 3:
                    # add another 4 keys for pyman in mode 3
                    if event.key == pygame.K_a:
                        pyman.Left()
                    elif event.key == pygame.K_d:
                        pyman.Right()
                    elif event.key == pygame.K_w:
                        pyman.Up()
                    elif event.key == pygame.K_s:
                        pyman.Down()
                    pyman.eat_pacdot()

            # tracks if the user pressed the quit button
            if event.type == pygame.QUIT:
                play = False
                end = True

            if gamemode != 1:
                # set time count down for mode 2 and 3
                if event.type == pygame.USEREVENT:
                    counter -= 1

        # determine the end conditions for the game(different end conditions
        # apply to each mode)
        if pacman.score <= 0 or counter <= 0 or \
           (eaten[0] and eaten[1] and eaten[2] and eaten[3]):
            screen.fill((0, 0, 0))
            end = True
        if gamemode == 3:
            if pyman.score <= 0:
                screen.fill((0, 0, 0))
                end = True

        screen.fill((0, 0, 0))
        # draw the map with the correspond random colour
        map_draw(wall_colour)

        # the loop part is different for each mode
        if gamemode == 1:
            gm1_loop(ghost_list)
        elif gamemode == 2:
            gm2_loop(ghost_list)
        elif gamemode == 3:
            gm3_loop(ghost_list)

        pacman.draw(screen)
        # set the 4 ghosts as different modes
        modes = [1, 2, 3, 2]

        for i in range(0, 4):
            if gamemode == 2:
                # all ghosts for mode 2 move randomly
                ghost_list[i].move(pacman, 2)
            else:
                # each ghost will follow their own algorithm based their
                # given mode defined in the mode list(this mode is different
                # from gamemode)
                ghost_list[i].move(pacman, modes[i])

        # calls the display function to display texts if certain
        # condition is satisfied
        display()

        # update the screen
        pygame.display.flip()

        # set the frame rate per second(fps)
        clock = pygame.time.Clock()
        clock.tick(66)

    return(play)