Ejemplo n.º 1
0
def ml_loop():
    filename = 'ian.sav'
    filepath = os.path.join(os.path.dirname(__file__), filename)
    model = pickle.load(open(filepath, 'rb'))
    comm.ml_ready()

    ballspeedx = 0
    ballspeedy = 0
    ballx = 102.5
    bally = 102.5
    predictx = 100

    scene_info = comm.get_scene_info()
    comm.send_instruction(scene_info.frame, PlatformAction.SERVE_TO_LEFT)

    while True:
        scene_info = comm.get_scene_info()

        if scene_info.status == GameStatus.GAME_OVER:
            scene_info = comm.get_scene_info()
            break
        elif scene_info.status == GameStatus.GAME_PASS:
            scene_info = comm.get_scene_info()

        ballspeedx = scene_info.ball[0] + 2.5 - ballx
        ballspeedy = scene_info.ball[1] + 2.5 - bally
        ballx = scene_info.ball[0] + 2.5
        bally = scene_info.ball[1] + 2.5
        platformx = scene_info.platform[0] + 20
        predictx = model.predict([[ballspeedx, ballspeedy, ballx, bally]])[0]

        if predictx - 3 > platformx:
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
        elif predictx + 3 < platformx:
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
Ejemplo n.º 2
0
def ml_loop():
    """The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    filename = 'model.sav'
    modelfile = path.join(path.dirname(__file__), filename)
    model = pickle.load(open(modelfile, 'rb'))
    ball_last = [101, 101]
    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()
        ball = np.array(scene_info.ball)
        vector = ball_last - ball
        vector = vector[0]/vector[1]

        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
                scene_info.status == GameStatus.GAME_PASS:
            comm.ml_ready()

            scene_info = comm.get_scene_info()
            # Do some stuff if needed

        # 3.3. Put the code here to handle the scene information
        # print(ball,vector)
        input = np.hstack((ball,vector))
        input = input.reshape((1,3))

        print('Actual value = ',compute_x_end(ball,ball_last))
        print(input)
        ball_x_end = model.predict(input)
        print("predict value = ", ball_x_end)
        # 3.4. Send the instruction for this frame to the game process

        ball_last = ball
        move = (ball_x_end) - (scene_info.platform[0])

        if move > 0:
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
        elif move < 0:
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
        else:
            comm.send_instruction(scene_info.frame, PlatformAction.NONE)
Ejemplo n.º 3
0
def ml_loop():
    """The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    # 2. Inform the game process that ml process is ready before start the loop.
    filename = 'model.sav'
    model = pickle.load(open(filename, 'rb'))
    comm.ml_ready()
    last_x = 0
    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()
        direction = 1 if scene_info.ball[0] > last_x else -1

        inp_temp = np.array([
            scene_info.ball[0], scene_info.ball[1], scene_info.platform[0],
            direction
        ])
        input = inp_temp[np.newaxis, :]

        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
                scene_info.status == GameStatus.GAME_PASS:
            comm.ml_ready()

            scene_info = comm.get_scene_info()
            # Do some stuff if needed

        # 3.3. Put the code here to handle the scene information
        move = model.predict(input)
        # motion direction of ball
        # compute the location of falling
        print(move)
        # 3.4. Send the instruction for this frame to the game process
        if move > 0:
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
        elif move < 0:
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
        else:
            comm.send_instruction(scene_info.frame, PlatformAction.NONE)
def ml_loop():
    comm.ml_ready()

    ball_postition_history=[]
    final_x=75
    while True:
        scene_info = comm.get_scene_info()
        platform_center_x=scene_info.platform[0]
        ball_postition_history.append(scene_info.ball)
        if len(ball_postition_history) > 1 :
            ball_x_move = ball_postition_history[-1][0] - ball_postition_history[-2][0]
            ball_y_move = ball_postition_history[-1][1] - ball_postition_history[-2][1]
            ball_platform_high = 400 - ball_postition_history[-1][1]
            if ball_y_move>0 :
                if  ball_platform_high <196 :
                    if ball_x_move > 0 : #right
                        final_x = ball_postition_history[-1][0] + ball_platform_high
                    else : #left
                        final_x = ball_postition_history[-1][0] - ball_platform_high
                else:
                    final_x = 100
            else :
                final_x = 100
            print(str(ball_postition_history[-1][0])+" ; "+str(ball_postition_history[-1][1])+" ; "+str(final_x))
            if  platform_center_x +20 >  final_x:
                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
            if  platform_center_x+20 <  final_x:
                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)    
Ejemplo n.º 5
0
def ml_loop():
    """The main loop of the machine learning process"""


    # Here is the execution order of the loop === #
    ball_position_history=[]
    filename='C:\\Users\\user\\Desktop\\MLGame\\games\\arkanoid\\knn.sav'
    model=pickle.load(open(filename, 'rb'))
    comm.ml_ready()

    # Start an endless loop.
    while True:
        scene_info = comm.get_scene_info()
        ball_position_history.append(scene_info.ball)
        if len(ball_position_history) > 2:
            inp_temp=np.array([ball_position_history[-2][0], ball_position_history[-2][1], ball_position_history[-1][0], ball_position_history[-1][1], scene_info.platform[0]])
            input=inp_temp[np.newaxis, :]
            print(input)
        
            if scene_info.status == GameStatus.GAME_OVER or \
                scene_info.status == GameStatus.GAME_PASS:
                comm.ml_ready()
                continue
        
            move=model.predict(input)
            print(move)
            if move > 0:
                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
            elif move < 0:
                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
            else:
                comm.send_instruction(scene_info.frame, PlatformAction.NONE)
Ejemplo n.º 6
0
def ml_loop():
    """The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_position_history = []
    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()
        ball_position_history.append(scene_info.ball)
        platform_center_x = scene_info.platform[0] + 20
        if len(ball_position_history) == 1:
            ball_going_down = 0
        elif ball_position_history[-1][1] - ball_position_history[-2][1] > 0:
            ball_going_down = 1
            vy = ball_position_history[-1][1] - ball_position_history[-2][1]
            vx = ball_position_history[-1][0] - ball_position_history[-2][0]
        else:
            ball_going_down = 0
        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            #    scene_info = comm.get_scene_info()
            #    ball_going_down=0
            # Do some stuff if needed
            comm.ml_ready()
            continue
        # 3.3. Put the code here to handle the scene information
        if ball_going_down == 1 and ball_position_history[-1][1] >= 0:
            ball_destination = ball_position_history[-1][0] + (
                (395 - ball_position_history[-1][1]) / vy) * vx
            if ball_destination >= 195:
                ball_destination = 195 - (ball_destination - 195)
            elif ball_destination <= 0:
                ball_destination = -ball_destination
        else:
            ball_destination = platform_center_x
            # 3.2.1. Inform the game process that ml process is ready

        # 3.4. Send the instruction for this frame to the game process
        #comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
        if platform_center_x >= ball_destination:
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
        elif platform_center_x <= ball_destination:
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
        else:
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_NONE)
Ejemplo n.º 7
0
def ml_loop():
    comm.ml_ready()

    last_ball_x = last_ball_y = 0
    flag = 1
    while True:
        scene_info = comm.get_scene_info()

        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            comm.ml_ready()
            continue

        ball_x, ball_y = scene_info.ball[0], scene_info.ball[1]

        if ball_y > 100 and ball_y - last_ball_y > 0:
            if flag:
                next_ball_x = get_next_ball_x(ball_x, ball_y,
                                              (ball_x - last_ball_x) > 0)
                flag = 0
            if (scene_info.platform[0] + 20) - (next_ball_x + 2.5) > 5:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_LEFT)
            elif (scene_info.platform[0] + 20) - (next_ball_x + 2.5) < -5:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_RIGHT)
            else:
                comm.send_instruction(scene_info.frame, PlatformAction.NONE)
        else:
            flag = 1
            comm.send_instruction(scene_info.frame, PlatformAction.NONE)

        last_ball_x, last_ball_y = ball_x, ball_y
Ejemplo n.º 8
0
def ml_loop():   
    comm.ml_ready()   
    while True:        
        scene_info = comm.get_scene_info()
        platform_center = scene_info.platform[0]+20
        ball_center = scene_info.ball[0]+2.5
        ball_Y = scene_info.ball[1]
        BIG_Y = 325#球會彈兩次的界線
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            comm.ml_ready()
            continue
        
        if ball_Y<= BIG_Y:
            if ball_center > 100:
                if platform_center < 20	:
                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                elif platform_center > 20	:
                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
            elif ball_center < 100: 
                if platform_center < 180	:
                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                elif platform_center > 180	:
                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
        elif ball_Y>= BIG_Y:
            if platform_center < ball_center:
                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
            elif platform_center > ball_center:
                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
Ejemplo n.º 9
0
def ml_loop():
    """
    The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_served = False
    pre_ball = (0,0)

    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()
        new_ball = scene_info.ball

        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed
            ball_served = False

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information

        # 3.4. Send the instruction for this frame to the game process
        if not ball_served:
            comm.send_instruction(scene_info.frame, PlatformAction.SERVE_TO_LEFT)
            ball_served = True
        else:
            if (new_ball[1] - pre_ball[1]) > 0:
                slope = (new_ball[0] - pre_ball[0]) / (new_ball[1] - pre_ball[1])
                fall_point = new_ball[0] + (400 - new_ball[1]) * slope
                if fall_point > 200:
                    fall_point = 200 - (fall_point - 200)
                if fall_point < 0:
                    fall_point = 0 - fall_point

                if fall_point > (scene_info.platform[0]+30):
                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)#此function告訴platform他在這個frame應該要做什麼動作
                elif fall_point < (scene_info.platform[0]+10):
                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
            else:
                comm.send_instruction(scene_info.frame, PlatformAction.NONE)    
            
            pre_ball = scene_info.ball
Ejemplo n.º 10
0
def ml_loop():
    comm.ml_ready()

    last_ball_x = 0
    filepath = os.path.join(os.path.dirname(__file__), "model.pickle")
    clf = pickle.load(open(filepath, 'rb'))
    while True:
        scene_info = comm.get_scene_info()

        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            comm.ml_ready()
            continue

        pred = clf.predict([[
            scene_info.ball[0], scene_info.ball[1],
            (scene_info.ball[0] - last_ball_x) > 0, scene_info.ball[1] > 100
        ]])
        if pred == 1:
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
        elif pred == 2:
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
        else:
            comm.send_instruction(scene_info.frame, PlatformAction.NONE)

        last_ball_x = scene_info.ball[0]
Ejemplo n.º 11
0
def ml_loop():

    filename = "D:\\MLGame-master\\knn.sav"
    model = pickle.load(open(filename, 'rb'))
    ball_position_history = []
    comm.ml_ready()

    while True:
        scene_info = comm.get_scene_info()
        ball_position = scene_info.ball

        if len(ball_position_history) != 0:
            inp_temp = np.array([
                ball_position_history[0], ball_position_history[1],
                ball_position[0], ball_position[1], scene_info.platform[0]
            ])
            input = inp_temp[np.newaxis, :]

            if scene_info.status == GameStatus.GAME_OVER or \
                scene_info.status == GameStatus.GAME_PASS:
                comm.ml_ready()
                continue
            move = model.predict(input)

            if (move < 0):
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_LEFT)
            elif (move > 0):
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_RIGHT)
            else:
                comm.send_instruction(scene_info.frame, PlatformAction.NONE)
        ball_position_history = ball_position
Ejemplo n.º 12
0
def ml_loop():
    """The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_last = [101,101]
    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()

        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
                scene_info.status == GameStatus.GAME_PASS:
            comm.ml_ready()

            scene_info = comm.get_scene_info()
            # Do some stuff if needed

        # 3.3. Put the code here to handle the scene information

#         ball_x_end = compute_x_end(scene_info.ball, ball_last)
#         ball_last = scene_info.ball
#         move  = (ball_x_end) - (scene_info.platform[0]+20)
        # motion direction of ball
        # compute the location of falling

        # 3.4. Send the instruction for this frame to the game process
#         if move > 0:
#             comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
#         elif move < 0:
#             comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
#         else:
#             comm.send_instruction(scene_info.frame, PlatformAction.NONE)
        comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
Ejemplo n.º 13
0
def ml_loop():
    """The main loop of the machine learning process
    This loop is run in a separate process, and communicates with the game process.
    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_position_history = []
    # 2. Inform the game process that ml process is ready before start the loop.
    import pickle
    import numpy as np
    filename = 'games\\arkanoid\\ml\\knn_example1.sav'
    model = pickle.load(open(filename, 'rb'))
    comm.ml_ready()
    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()
        platform_center_x = scene_info.platform[0]
        ball_position_history.append(scene_info.ball)
        if (len(ball_position_history) > 1):
            vy = ball_position_history[-1][1] - ball_position_history[-2][1]
            vx = ball_position_history[-1][0] - ball_position_history[-2][0]
            inp_temp = np.array([
                scene_info.ball[0], scene_info.ball[1], scene_info.platform[0],
                vx, vy
            ])
            input = inp_temp[np.newaxis, :]
        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER:
            print("game over")
            comm.ml_ready()
            continue
        elif scene_info.status == GameStatus.GAME_PASS:
            #scene_info = comm.get_scene_info()
            # Do some stuff if needed

            # 3.2.1. Inform the game process that ml process is ready
            print("game pass")
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information
        if (len(ball_position_history) > 1):
            move = model.predict(input)
        else:
            move = 0
        # 3.4. Send the instruction for this frame to the game process
        if move < 0:
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
        elif move > 0:
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
        else:
            comm.send_instruction(scene_info.frame, PlatformAction.NONE)
Ejemplo n.º 14
0
def ml_loop():
    """The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """
    import pickle
    import numpy as np

    vx = 0
    vy = 0
    filename = "C:\\Users\\user\\Downloads\\MLGame-master\\games\\arkanoid\\ml\\knn_example.sav"
    model = pickle.load(open(filename, 'rb'))
    comm.ml_ready()
    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.

    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()
    ball_position_history = []

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()
        ball_position_history.append(scene_info.ball)
        Platform_center_x = scene_info.platform[0] + 20
        if (len(ball_position_history) > 1):
            vx = ball_position_history[-1][0] - ball_position_history[-2][0]
            vy = ball_position_history[-1][1] - ball_position_history[-2][1]
            inp_temp = np.array([
                scene_info.ball[0], scene_info.ball[1], scene_info.platform[0],
                vx, vy
            ])
            input = inp_temp[np.newaxis, :]

        if scene_info.status == GameStatus.GAME_OVER or scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed
            #scene_info = comm.get_scene_infso()
            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        if (len(ball_position_history) > 1):
            move = model.predict(input)
        else:
            move = 0
        print(move)

        if move < 0:
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
        elif move > 0:
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
        else:
            comm.send_instruction(scene_info.frame, PlatformAction.NONE)
Ejemplo n.º 15
0
def ml_loop():
    comm.ml_ready()
    comm.send_instruction(comm.get_scene_info().frame, PlatformAction.SERVE_TO_LEFT)

    predictx = 95
    ballx, bally = 95, 400
    while True:
        scene_info = comm.get_scene_info()

        if scene_info.status == GameStatus.GAME_OVER:
            break

        ballspeedx = scene_info.ball[0] + 2.5 - ballx
        ballspeedy = scene_info.ball[1] + 2.5 - bally
        ballx = scene_info.ball[0] + 2.5
        bally = scene_info.ball[1] + 2.5
        platformx = scene_info.platform[0] + 20

        dir = 1
        for i in range(30):
            if ballx+i*ballspeedx < 0 or bally+i*ballspeedy < 0 or \
            ballx+i*ballspeedx > 200 or bally+i*ballspeedy > 400:
                break
            for j in range(-30, 30):
                if (int(ballx+i*ballspeedx+j), int(bally+i*ballspeedy)) in scene_info.bricks:
                    dir = -1
                    break

        if (ballspeedy > 0) and (bally > 257):
            predictx = ballx + dir*(ballspeedx * (400 - bally)/ballspeedy) + randint(-3, 3)
        elif (ballspeedy < 0) and (bally > 257):
            predictx = ballx + dir*(ballspeedx * (400 - bally)/ballspeedy) + randint(-7, 7)
        elif ballspeedy == 0:
            predictx = 95 + 2.2*randint(-11, 11)
        else:
            predictx = 95


        if platformx == predictx:
            comm.send_instruction(scene_info.frame, PlatformAction.NONE)
        elif platformx > predictx:
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
        elif platformx < predictx:
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
Ejemplo n.º 16
0
def ml_loop():
    """The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_position_history = []
    filename = 'SVC_example.sav'
    model = pickle.load(open(filename, 'rb'))
    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()
        ball_position_history.append(scene_info.ball)
        if len(ball_position_history) > 2:
            inp_temp = np.array([
                ball_position_history[-2][0], ball_position_history[-2][1],
                ball_position_history[-1][0], ball_position_history[-1][1],
                scene_info.platform[0]
            ])
            input = inp_temp[np.newaxis, :]
            #print(input)
            """if scene_info.status == GameStatus.GAME_OVER or \
                scene_info.status == GameStatus.GAME_PASS:
                comm.ml_ready()
                continue"""
            if scene_info.status == GameStatus.GAME_OVER:
                print("game over")
                comm.ml_ready()
                continue
            elif scene_info.status == GameStatus.GAME_PASS:
                print("game pass")
                comm.ml_ready()
                continue

            move = model.predict(input)
            #print(move)
            if move > 0:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_RIGHT)
            elif move < 0:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_LEFT)
            else:
                comm.send_instruction(scene_info.frame, PlatformAction.NONE)
Ejemplo n.º 17
0
def ml_loop():
    """
    The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_served = False

    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()  #一定要保留這行 跟遊戲核心的通道

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()  #取得遊戲場景資訊

        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed
            ball_served = False

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information

        # 3.4. Send the instruction for this frame to the game process
        if not ball_served:
            comm.send_instruction(scene_info.frame,
                                  PlatformAction.SERVE_TO_LEFT)
            ball_served = True
        else:
            ball_x = scene_info.ball[0]
            platform_x = scene_info.platform[0]

            if (platform_x > ball_x):
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_LEFT)
            elif platform_x < ball_x:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_RIGHT)
            else:
                comm.send_instruction(scene_info.frame, PlatformAction.NONE)
Ejemplo n.º 18
0
def ml_loop():
    """The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_position_history = []
    ball_center_record = []
    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()
        ball_position_history.append(scene_info.ball)
        platform_center_x = scene_info.platform[0] + 20
        platform_center_y = scene_info.platform[1]
        ball_center = scene_info.ball[0] + 2.5  #球X位點左上角+2.5為正中央
        ball_height = scene_info.ball[1]  #球Y位點
        if platform_center_y - ball_height >= 200:
            vx = ball_center
            if platform_center_x < 100:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_RIGHT)
            elif platform_center_x > 100:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_LEFT)
            pass
        else:
            if platform_center_x > abs(vx - 200):
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_LEFT)
            else:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_RIGHT)
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue
Ejemplo n.º 19
0
def ml_loop():
    """
    The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_served = False
    filename = 'your_file_name.pickle'
    filename = path.join(path.dirname(__file__), filename)
    log = pickle.load(open(filename, 'rb'))

    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()

        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed
            ball_served = False

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information

        # 3.4. Send the instruction for this frame to the game process
        if not ball_served:
            comm.send_instruction(scene_info.frame,
                                  PlatformAction.SERVE_TO_RIGHT)
            ball_served = True
        else:
            history_log = log[scene_info.frame]
            action = history_log.command
            comm.send_instruction(scene_info.frame, action)
Ejemplo n.º 20
0
def ml_loop():
    """The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_last = [101,101]
    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()

        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()

        # 3.3. Put the code here to handle the scene information
        #1.算出下降速率
        targetX=getTargetX(scene_info.ball,ball_last)
        ball_last=scene_info.ball
        platform_X=scene_info.platform[0]
        if (platform_X+18) > (targetX) :
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
        elif (platform_X+18) < (targetX) :
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
        else:
            comm.send_instruction(scene_info.frame, PlatformAction.NONE)
Ejemplo n.º 21
0
def ml_loop():
    """The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.

    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()

        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information
        ball_x = scene_info.ball[0]
        ball_y = scene_info.ball[1]
        platform_x = scene_info.platform[0]
        platform_y = scene_info.platform[1]
        print(ball_x, ball_y, platform_x, platform_y)

        # 3.4. Send the instruction for tcomm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)his frame to the game process
        comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
Ejemplo n.º 22
0
def ml_loop():
    filename = "F:/Documents/MachineLearning/EX/Arkanoid-master/knn.sav"
    # filename="F:/Documents/MachineLearning/MLGame-master/knn_test_2.sav"
    model = pickle.load(open(filename, 'rb'))
    comm.ml_ready()
    ball_postition_history = []
    while True:

        scene_info = comm.get_scene_info()
        ball_postition_history.append(scene_info.ball)

        if (len(ball_postition_history) > 1):
            vx = ball_postition_history[-1][0] - ball_postition_history[-2][0]
            vy = ball_postition_history[-1][1] - ball_postition_history[-2][1]

            temp = np.array([
                scene_info.ball[0], scene_info.ball[1], scene_info.platform[0],
                vx, vy
            ])
            Result = temp[np.newaxis, :]

        if scene_info.status == GameStatus.GAME_OVER:
            print("Lose", end='\n')
            comm.ml_ready()
            continue
        elif scene_info.status == GameStatus.GAME_PASS:
            print("Win", end='\n')
            comm.ml_ready()
            continue

        if (len(ball_postition_history) > 1):
            Prediction = model.predict(Result)
        else:
            Prediction = 0
        if Prediction < 0:
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
        elif Prediction > 0:
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
        else:
            comm.send_instruction(scene_info.frame, PlatformAction.NONE)
Ejemplo n.º 23
0
def ml_loop():
    """The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()
    ball_position_history=[]
    need_to_go=0.00
    M=0.00
    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()
        ball_position_history.append(scene_info.ball)
        #球和平板的X位置
        ball_posistion_x = scene_info.ball[0]+2.5
        platform_center_x = scene_info.platform[0]+20
        #球是否下墜
        if (len(ball_position_history))==1:
            ball_going_down=0
        elif ball_position_history[-1][1]-ball_position_history[-2][1] > 0:
            Dx = ball_position_history[-1][0]-ball_position_history[-2][0]
            Dy = ball_position_history[-1][1]-ball_position_history[-2][1]
            if Dy == 0:
                Dy = 0.000001
            elif Dx == 0:
                Dx = 0.000001
            M=Dx/Dy
            ball_going_down = 1
        else:
            ball_going_down = 0
        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information
        if ball_going_down == 1 and ball_position_history[-1][1]>=0:
            need_to_go = ball_position_history[-1][0] + (((400-ball_position_history[-1][1])/Dy)*Dx)
            if need_to_go >= 200:
                need_to_go = 400 - need_to_go
            elif need_to_go <= 0:
                need_to_go = 0 - need_to_go
                 
        # 3.4. Send the instruction for this frame to the game process
        if ball_going_down == 1 and ball_position_history[-1][1]>=0:
            if platform_center_x < need_to_go:
                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
            elif platform_center_x > need_to_go:
                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)        
Ejemplo n.º 24
0
ball_position_history=[]
   # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.

    # 2. Inform the game process that ml process is ready before start the loop.
    
filename='svc_example.sav'
model=pickle.load(open(filename, 'rb'))

comm.ml_ready()
    

    # 3. Start an endless loop.
while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()

        ball_position_history.append(scene_info.ball)
        if(len(ball_position_history) > 2):
            vx1=ball_position_history[-1][0]
            vx2=ball_position_history[-2][0]
            vy1=ball_position_history[-1][1]
            vy2=ball_position_history[-2][1]
            inp_temp=np.array([vx2, vy2, vx1, vy1, scene_info.platform[0]])
            input=inp_temp[np.newaxis, :]
            print(input)
        # 3.2. If the game is over or passed, the game process will reset
            if scene_info.status == GameStatus.GAME_OVER or \
                scene_info.status == GameStatus.GAME_PASS:
                comm.ml_ready()
                continue
Ejemplo n.º 25
0
def ml_loop():
    """
    The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_served = False
    filename = path.join(path.dirname(__file__),
                         "save\clf_KMeans_BallAndDirection.pickle")
    with open(filename, 'rb') as file:
        clf = pickle.load(file)

    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()

    s = [93, 93]

    def get_direction(ball_x, ball_y, ball_pre_x, ball_pre_y):
        VectorX = ball_x - ball_pre_x
        VectorY = ball_y - ball_pre_y
        if (VectorX >= 0 and VectorY >= 0):
            return 0
        elif (VectorX > 0 and VectorY < 0):
            return 1
        elif (VectorX < 0 and VectorY > 0):
            return 2
        elif (VectorX < 0 and VectorY < 0):
            return 3

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()
        feature = []
        feature.append(scene_info.ball[0])
        feature.append(scene_info.ball[1])
        feature.append(scene_info.platform[0])

        feature.append(get_direction(feature[0], feature[1], s[0], s[1]))
        s = [feature[0], feature[1]]
        feature = np.array(feature)
        feature = feature.reshape((-1, 4))
        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed
            ball_served = False

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information

        # 3.4. Send the instruction for this frame to the game process
        if not ball_served:
            comm.send_instruction(scene_info.frame,
                                  PlatformAction.SERVE_TO_LEFT)
            ball_served = True
        else:

            y = clf.predict(feature)

            if y == 0:
                comm.send_instruction(scene_info.frame, PlatformAction.NONE)
                print('NONE')
            elif y == 1:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_LEFT)
                print('LEFT')
            elif y == 2:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_RIGHT)
                print('RIGHT')
Ejemplo n.º 26
0
def ml_loop():
    """The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.

    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()
    ball_position_histroy = []

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()
        ball_position_histroy.append(scene_info.ball)
        platform_center_x = scene_info.platform[0] + 20
        if (len(ball_position_histroy)) == 1:
            ball_going_down = 0
        elif ball_position_histroy[-1][1] - ball_position_histroy[-2][1] > 0:
            ball_going_down = 1

            vx = ball_position_histroy[-1][0] - ball_position_histroy[-2][0]
            vy = ball_position_histroy[-1][1] - ball_position_histroy[-2][1]
            ball_position_pridictonplateform = ball_position_histroy[-1][0] + (
                400 - ball_position_histroy[-1][1]) * vx / vy
            while ball_position_pridictonplateform < 0 or ball_position_pridictonplateform > 200:
                if ball_position_pridictonplateform > 200:
                    ball_position_pridictonplateform = 400 - ball_position_pridictonplateform
                else:
                    ball_position_pridictonplateform = -ball_position_pridictonplateform

            if platform_center_x < ball_position_pridictonplateform:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_RIGHT)
            elif platform_center_x > ball_position_pridictonplateform:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_LEFT)
            else:
                continue

        else:
            ball_going_down = 0

            if platform_center_x < 100:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_RIGHT)
            elif platform_center_x > 100:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_LEFT)
            else:
                continue

        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue
Ejemplo n.º 27
0
def ml_loop():
    """
    The main loop of the machine learning process
    This loop is run in a separate process, and communicates with the game process.
    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_served = False
    final_x = 100
    s = [93,93]
    def get_direction(ball_x,ball_y,ball_pre_x,ball_pre_y):
        VectorX = ball_x - ball_pre_x
        VectorY = ball_y - ball_pre_y
        if(VectorX>=0 and VectorY>=0):
            return 0
        elif(VectorX>0 and VectorY<0):
            return 1
        elif(VectorX<0 and VectorY>0):
            return 2
        elif(VectorX<0 and VectorY<0):
            return 3

    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()
    
    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()

        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed
            ball_served = False

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information
        direction = get_direction(scene_info.ball[0],scene_info.ball[1],s[0],s[1])
        if s[0] != scene_info.ball[0]:
            gradient = (s[1] - scene_info.ball[1])/(s[0] - scene_info.ball[0])

            if gradient != 0:
                final_x = scene_info.ball[0] + (400 - scene_info.ball[1])/gradient
                if final_x < 0 or final_x > 200:
                    if direction == 0:
                        wall_y = gradient*(200 - scene_info.ball[0]) + scene_info.ball[1]
                        final_x = 200 - (400 - wall_y)/gradient
                    elif direction == 2:
                        wall_y = gradient*(0 - scene_info.ball[0]) + scene_info.ball[1]
                        final_x = 0 - (400 - wall_y)/gradient
        
        s = [scene_info.ball[0],scene_info.ball[1]]

        # 3.4. Send the instruction for this frame to the game process
        if not ball_served:
            comm.send_instruction(scene_info.frame, PlatformAction.SERVE_TO_LEFT)
            ball_served = True
        elif direction == 1 or direction == 3:
            if scene_info.platform[0] < 79:
                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
            elif scene_info.platform[0] > 81:
                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
            else:
                comm.send_instruction(scene_info.frame, PlatformAction.NONE)
        elif direction == 2 or direction == 0:
            if scene_info.platform[0] > final_x - 16:
                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
            elif scene_info.platform[0] < final_x - 24:
                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
            else:
                comm.send_instruction(scene_info.frame, PlatformAction.NONE)
Ejemplo n.º 28
0
def ml_loop():
    """
    The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_served = False
    pre_Ball_x = 95
    pre_Ball_y = 400
    m = 0
    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()

        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed
            ball_served = False
            pre_Ball_x = scene_info.ball[0]
            pre_ball_y = scene_info.ball[1]
            print(pre_Ball_x, pre_Ball_y)
            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information

        ball_x = scene_info.ball[0]
        ball_y = scene_info.ball[1]
        platform_x = scene_info.platform[0]
        Vx = ball_x - pre_Ball_x
        Vy = ball_y - pre_Ball_y

        # 3.4. Send the instruction for this frame to the game process
        if not ball_served:
            #comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
            #comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
            comm.send_instruction(scene_info.frame,
                                  PlatformAction.SERVE_TO_RIGHT)
            ball_served = True
        else:
            #comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
            """
            ball_x=scene_info.ball[0]
            ball_y=scene_info.ball[1]
            platform_x=scene_info.platform[0]
            Vx=ball_x-pre_Ball_x
            Vy=ball_y-pre_Ball_y
            """
            """
            if Vx==0:
                Vx=0.001
            m=Vy/Vx
            if m==0:
                m=0.001
            
            new_x=((400-ball_y)/m)+ball_x
            while new_x<0 or new_x>200 :
                if new_x<0 :
                    new_x=-new_x
                elif new_x>200:
                    new_x=400-new_x
            new_x-=25
            if new_x>platform_x :
                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
            elif new_x<=platform_x:
                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
            else:
                comm.send_instruction(scene_info.frame, PlatformAction.NONE)
            """

            if Vy > 0:
                newp = down(ball_x, ball_y, Vx, scene_info)
                if platform_x + 10 > newp:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_LEFT)
                elif platform_x + 30 < newp:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_RIGHT)

            else:
                newp = up(ball_x, ball_y, Vx, scene_info)
                if platform_x + 10 > newp:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_LEFT)
                elif platform_x + 30 < newp:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_RIGHT)

            pre_Ball_x = ball_x
            pre_Ball_y = ball_y
Ejemplo n.º 29
0
def ml_loop():
    """
    The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_served = False
    finalx = 100
    lastframey = 0
    velocity = 0
    NEWX = 0
    lastframex = 0
    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()

        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed
            ball_served = False

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information

        # 3.4. Send the instruction for this frame to the game process
        if not ball_served:
            comm.send_instruction(scene_info.frame,
                                  PlatformAction.SERVE_TO_LEFT)
            ball_served = True
        else:

            if scene_info.ball[1] > 100 and (lastframey - scene_info.ball[1] <
                                             0):  #往下掉

                velocity = scene_info.ball[0] - lastframex
                finalx = scene_info.ball[0] + velocity * (
                    395 - scene_info.ball[1]) / 7

            else:
                velocity = 0
                finalx = 100
            #撞左邊
            if lastframey - scene_info.ball[1] > 0:
                finalx = 100
            while (finalx < 0 or finalx > 195):
                if finalx > 195:
                    finalx = 390 - finalx
                elif finalx < 0:
                    finalx = -finalx
            NEWX = finalx - 20
            if scene_info.platform[0] < NEWX:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_RIGHT)
            if scene_info.platform[0] > NEWX:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_LEFT)

            lastframey = scene_info.ball[1]
            lastframex = scene_info.ball[0]
Ejemplo n.º 30
0
def ml_loop():
    """The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.

    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()

        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information
        ball_x = scene_info.ball[0]
        ball_y = scene_info.ball[1]
        platform = scene_info.platform[0] + 20

        #print(ball_y)
        #print(platform_C)
        #print(i)
        if ball_y <= 260 or ball_ey > ball_y:
            u = 0
            if platform > 100:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_LEFT)
            if platform < 100:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_RIGHT)
        else:
            if u != 1:
                if ball_ex > ball_x:
                    if ball_x - 140 > 0:
                        i = ball_x - 140
                    else:
                        i = -(ball_x - 140)
                else:
                    if ball_x + 140 < 200:
                        i = i = ball_x + 140
                    else:
                        i = 400 - (ball_x + 140)
                u = 1

            if platform > i:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_LEFT)
            elif platform < i:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_RIGHT)
            else:
                comm.send_instruction(scene_info.frame, PlatformAction.NONE)

        ball_ey = ball_y
        ball_ex = ball_x