Example #1
0
    def take_shot(self, angle, power):
        move_done = False

        #Gets the ball's positions
        white_ball_pos = self.white_ball_pos
        other_ball_pos = self.other_ball_pos

        #Sets the balls and takes the shot
        self.game.start_pool_mlp(white_ball_pos, other_ball_pos, angle, power)

        while not move_done:  #Lets the balls do their thing according to the game rules
            collisions.resolve_all_collisions(self.game.balls, self.game.holes,
                                              self.game.table_sides)

            for ball in self.game.balls:
                ball.update()

            if self.game.all_not_moving():
                move_done = True

        #Shot is over at this point

        #Gathers end ball positions
        for ball in self.game.balls:
            ball_pos = ball.ball.pos

            if int(ball.number) == 0:
                white_ball_end_pos = ball_pos
            else:
                other_ball_end_pos = ball_pos

        #Checks for pocketed balls (0 means not pocketed)
        white_ball_pocket = 0
        other_ball_pocket = 0

        for hole in config.potted.values():
            if int(hole[0]) == 0:
                white_ball_end_pos = hole[1]
                white_ball_pocket = self.get_hole_num(hole[2])
            elif int(hole[0]) == 1:
                other_ball_end_pos = hole[1]
                other_ball_pocket = self.get_hole_num(hole[2])

        #Resets the potted config file for the next shot
        config.potted = {}

        #Assembles the output
        data = np.array([
            white_ball_pos[0], white_ball_pos[1], other_ball_pos[0],
            other_ball_pos[1], angle, power, white_ball_end_pos[0],
            white_ball_end_pos[1], other_ball_end_pos[0],
            other_ball_end_pos[1], white_ball_pocket, other_ball_pocket
        ])

        output_data = copy.deepcopy(data)

        self.random_reset_balls()

        return output_data
Example #2
0
def play_game(game_idx):
    was_closed = False
    step = 0
    if not os.path.isdir(str(game_idx)):
        os.mkdir(str(game_idx))
    while not was_closed:
        game = gamestate.GameState()
        button_pressed = config.play_game_button  #graphics.draw_main_menu(game)

        if button_pressed == config.play_game_button:
            game.start_pool()
            events = event.events()
            while not (events["closed"] or game.is_game_over
                       or events["quit_to_main_menu"] or step == 10):
                print(step)
                events = event.events()
                collisions.resolve_all_collisions(game.balls, game.holes,
                                                  game.table_sides)
                game.redraw_all()

                table = np.zeros((config.resolution[0], config.resolution[1]))
                for i in range(len(list(game.balls))):
                    pos = list(game.balls)[i].ball.pos
                    table[[
                        int(pos[0] + cos(i * 2 * pi / 360) * 12.5)
                        for i in range(360)
                    ], [
                        int(pos[1] + sin(i * 2 * pi / 360) * 12.5)
                        for i in range(360)
                    ]] = 1

                plt.imshow(table, cmap='gray')
                plt.savefig(str(game_idx) + '/' + str(step) + '.png')
                step += 1

                if game.all_not_moving():
                    game.check_pool_rules()
                    game.cue.make_visible(game.current_player)
                    while not (
                        (events["closed"] or events["quit_to_main_menu"])
                            or game.is_game_over) and game.all_not_moving():
                        game.redraw_all()
                        events = event.events()
                        if True:  #game.cue.is_clicked(events):
                            game.cue.cue_is_active(game, events)
                        elif game.can_move_white_ball and game.white_ball.is_clicked(
                                events):
                            game.white_ball.is_active(
                                game, game.is_behind_line_break())
            was_closed = True  #events["closed"]

        if button_pressed == config.exit_button:
            was_closed = True

    print('closing')
    pygame.quit()
    print('done')
Example #3
0
    def play(self,x,y):
        self.was_closed = False
        while not self.was_closed:
            game = gamestate.GameState()
            button_pressed = config.play_game_button
            if button_pressed == config.play_game_button:
                game.start_pool()
                events = event.events()
                self.reward=0
                while not (events["closed"] or game.is_game_over or events["quit_to_main_menu"]):
                    events = event.events()
                    self.reward=collisions.resolve_all_collisions(game.balls, game.holes, game.table_sides,self.reward)
                    
                    game.redraw_all()
                    store_ball_list=dict()
                    if game.all_not_moving():
                        
                        print("\n*****************************")
                        
                        for ball in game.balls:
                            store_ball_list[ball.number]=ball.ball.pos
                            #print("ball {} not moving : {}".format(ball.number,ball.ball.pos))
                        
                        game.check_pool_rules()
                        
                        game.cue.make_visible(game.current_player)

                        if 0 not in store_ball_list:
                            store_ball_list[0]=game.give_value()
                        #print("如果陣列中沒有白球位置請看這裡 : {}".format(game.give_value()))

                        print("all balls : ",store_ball_list)
                        print("reward : {}".format(self.reward))

                        
                        while not (
                            (events["closed"] or events["quit_to_main_menu"]) or game.is_game_over) and game.all_not_moving():
                            game.redraw_all()
                            events = event.events()
                            if 1:
                                # x=int(input("x : "))
                                # y=int(input("y : "))
                                game.cue.cue_is_active(game, events,(x,y))
                            elif game.can_move_white_ball and game.white_ball.is_clicked(events):
                                game.white_ball.is_active(game, game.is_behind_line_break())
                        self.return_command=str(self.reward)+"*"+str(self.was_closed)
                self.was_closed = True
Example #4
0
import graphics
import config

was_closed = False

while not was_closed:
    game = gamestate.GameState()
    button_pressed = graphics.draw_main_menu(game)

    if button_pressed == config.play_game_button:
        game.start_pool()
        events = event.events()
        while not (events["closed"] or game.is_game_over
                   or events["quit_to_main_menu"]):
            events = event.events()
            collisions.resolve_all_collisions(game.balls, game.holes,
                                              game.table_sides)
            game.redraw_all()

            if game.all_not_moving():
                game.check_pool_rules()
                game.cue.make_visible(game.current_player)
                while not ((events["closed"] or events["quit_to_main_menu"])
                           or game.is_game_over) and game.all_not_moving():
                    game.redraw_all()
                    events = event.events()
                    if game.cue.is_clicked(events):
                        game.cue.cue_is_active(game, events)
                    elif game.can_move_white_ball and game.white_ball.is_clicked(
                            events):
                        game.white_ball.is_active(game,
                                                  game.is_behind_line_break())
Example #5
0
def play_game(game_idx):
    was_closed = False
    global_step = 0
    local_step = 0
    turn = 0
    tmp_list = [[]]
    """fig = plt.figure(figsize=(config.resolution[1]/mydpi, config.resolution[0]/mydpi), dpi=mydpi)
    ax = plt.Axes(fig, [0., 0., 1., 1.])"""

    if not os.path.isdir('dataset/' + str(game_idx)):
        os.mkdir('dataset/' + str(game_idx))
        #os.mkdir('dataset/'+ str(game_idx) + '/' + str(turn))
    while not was_closed:
        game = gamestate.GameState()
        button_pressed = config.play_game_button  #graphics.draw_main_menu(game)

        if button_pressed == config.play_game_button:
            game.start_pool()
            events = event.events()
            while not (events["closed"] or game.is_game_over
                       or events["quit_to_main_menu"] or global_step == 50000):
                events = event.events()
                collisions.resolve_all_collisions(game.balls, game.holes,
                                                  game.table_sides)
                game.redraw_all()
                #table = np.zeros((config.resolution[0], config.resolution[1]))
                for i in range(len(list(game.balls))):
                    pos = list(game.balls)[i].ball.pos
                    tmp_list[-1].append(pos.copy())
                    """table[
                        [int(pos[0] + cos(i*2*pi/360)*12.5) for i in range(360)],
                        [int(pos[1] + sin(i*2*pi/360)*12.5) for i in range(360)]
                    ] = 1"""

                tmp_list.append([])
                """ax.set_axis_off()
                fig.add_axes(ax)
                ax.imshow(table, cmap='gray')
                plt.savefig('dataset/'+ str(game_idx) + '/' + str(turn) + '/'+str(local_step)+'.png')
                plt.cla()
                plt.clf()"""
                global_step += 1
                local_step += 1

                if game.all_not_moving():
                    if global_step != 1:
                        with open(
                                'dataset/' + str(game_idx) + '/' + str(turn) +
                                '_' + str(local_step) + '.pickle', "wb") as f:
                            pickle.dump(tmp_list[:-1], f)
                            tmp_list = [[]]
                        turn += 1
                        #os.mkdir('dataset/'+ str(game_idx) + '/' + str(turn))
                    local_step = 0
                    game.check_pool_rules()
                    game.cue.make_visible(game.current_player)
                    while not (
                        (events["closed"] or events["quit_to_main_menu"])
                            or game.is_game_over) and game.all_not_moving():
                        game.redraw_all()
                        events = event.events()
                        if True:  #game.cue.is_clicked(events):
                            game.cue.cue_is_active(game, events,
                                                   global_step == 1)
                        elif game.can_move_white_ball and game.white_ball.is_clicked(
                                events):
                            game.white_ball.is_active(
                                game, game.is_behind_line_break())
            was_closed = True  #events["closed"]

        if button_pressed == config.exit_button:
            was_closed = True

    print('closing')
    pygame.quit()
    print('done')