def ml_loop(): #function的開頭 """ 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 # 一個frame只能傳一個指令!! if not ball_served: #遊戲開始 comm.send_instruction(scene_info.frame, PlatformAction.SERVE_TO_RIGHT) ball_served = True ball_last_y = scene_info.ball[1] ball_y_dir = "up" first_down = False final_x = 0 else: #遊戲中 #直接用球第一次向下碰到牆壁的位置來預測最後球落在的位置 ball_x = scene_info.ball[0] ball_y = scene_info.ball[1] platfrom_x = scene_info.platform[0] #判斷方向 if ball_last_y < ball_y: #方向向下 ball_y_dir = "down" else: ball_y_dir = "up" ball_last_y = ball_y if ball_y_dir == "down": if ball_x == 0: print(ball_y) gogo = (400 - ball_y) // 7 #print(gogo) if gogo > 28: gogo = gogo - 28 final_x = 195 - gogo * 7 else: final_x = gogo * 7 if ball_x == 195: print(ball_y) gogo = (400 - ball_y) // 7 #print(gogo) if gogo > 28: gogo = gogo - 28 final_x = gogo * 7 else: final_x = 195 - gogo * 7 #send instruction if ball_y <= 205: #讓板子回歸中間才來的及接球 if platfrom_x <= 80 and platfrom_x >= 100: comm.send_instruction(scene_info.frame, PlatformAction.NONE) else: if platfrom_x < 80: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) else: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) else: if platfrom_x + 18 <= final_x and platfrom_x + 22 >= final_x: comm.send_instruction(scene_info.frame, PlatformAction.NONE) else: if platfrom_x + 18 < final_x: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) else: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
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() plat = scene_info.platform[0] try: vX = scene_info.ball[0] - bX vY = scene_info.ball[1] - bY except: vX = 0 vY = 0 bX = scene_info.ball[0] bY = scene_info.ball[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: # 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: #print(vX,end=",") #print(vY) if vY > 0: dX = 10 + bX + ((400 - bY) / vY) * vX if dX > 200: dX = abs(400 - dX) elif dX < 0 and dX > -200: dX = -dX elif dX < -200: dX = 400 + dX #print(dX,end=",") #print(scene_info.platform) if plat + 40 == 200: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif plat == 0: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif plat + 30 < dX: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif plat + 30 > dX: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) else: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) else: dX = (bX + vX * 10) % 200 if plat + 30 < dX: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif plat + 30 > dX: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) else: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
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 = "C:\\Users\\fghj8\\MLGame-master\\games\\arkanoid\\svr_9class.sav" model = pickle.load(open(filename,'rb')) last_ball_x = 0 last_ball_y = 0 games = 0 gameover = 0 inputx = 0 inputy = 0 hit = False LRUP = 0 ball_to_200 = 0 comm.ml_ready() bricks = [] # 3. Start an endless loop. scene_info = comm.get_scene_info() while True: # 3.1. Receive the scene information sent from the game process. table = np.array(np.zeros((208,408))) last_ball_x = scene_info.ball[0] last_ball_y = scene_info.ball[1] scene_info = comm.get_scene_info() bricks = scene_info.bricks for k in range(len(bricks)): for p in range(0,25): for j in range(0,10): table[bricks[k][0] + p][bricks[k][1] + j ] = 1 if(last_ball_x - scene_info.ball[0] > 0): if(last_ball_y - scene_info.ball[1] > 0): #going up inputx = scene_info.ball[0] inputy = scene_info.ball[1] LRUP = 3 #U.L j = scene_info.ball[0] k = scene_info.ball[1] while(j > 0 and k > 0): if(table[j][k] == 1): hit = True break j = j-1 k = k-1 hit = False if(hit==False): j = scene_info.ball[0] k = scene_info.ball[1]+3 while(j > 0 and k > 0): if(table[j][k] == 1): hit = True break j = j-1 k = k-1 hit = False else: #going down #DL LRUP = 1 inputx = scene_info.ball[0] inputy = scene_info.ball[1] j = scene_info.ball[0] k = scene_info.ball[1] while(j > 0 and k < 400): if(table[j][k] == 1): hit = True break j = j-1 k = k+1 hit = False if(hit==False): j = scene_info.ball[0] k = scene_info.ball[1]+3 while(j > 0 and k < 400): if(table[j][k] == 1): hit = True break j = j-1 k = k+1 hit = False else: if(last_ball_y - scene_info.ball[1] > 0): #going up LRUP = 4 inputx = scene_info.ball[0]+3 inputy = scene_info.ball[1] #U.R j = scene_info.ball[0] k = scene_info.ball[1] while(j < 200 and k > 0): if(table[j][k] == 1): hit = True break j = j+1 k = k-1 hit = False if(hit==False): j = scene_info.ball[0]+3 k = scene_info.ball[1]+3 while(j < 200 and k > 0): if(table[j][k] == 1): hit = True break j = j+1 k = k-1 hit = False else: #going down inputx = scene_info.ball[0]+3 inputy = scene_info.ball[1] LRUP = 2 #D.R. j =scene_info.ball[0]+3 k = scene_info.ball[1] while(j < 200 and k < 400): if(table[j][k] == 1): hit = True break j = j+1 k = k+1 hit = False if(hit==False): j = scene_info.ball[0]+3 k = scene_info.ball[1]+3 while(j < 200 and k < 400): if(table[j][k] == 1): hit = True break j = j+1 k = k+1 hit = False ballx_to_platmid = np.abs((scene_info.ball[0]-scene_info.platform[0])) bally_to_platmid = 400-scene_info.ball[1] # print(j,k) inp_temp = np.array([inputx,inputy,j,k,LRUP,(200 - int(scene_info.ball[0])),ballx_to_platmid,bally_to_platmid,scene_info.platform[0]+20]) input = inp_temp[np.newaxis,:] if scene_info.status == GameStatus.GAME_OVER or \ scene_info.status == GameStatus.GAME_PASS: games = games + 1 # print(games) if(scene_info.status == GameStatus.GAME_OVER): # print('game over') # gameover = gameover + 1 # print((games - gameover) / games) pass # Do some stuff if needed # 3.2.1. Inform the game process that ml process is ready comm.ml_ready() continue move = model.predict(np.around(input)) # 3.3. Put the code here to handle the scene information # 3.4. Send the instruction for this frame to the game process if(move < -0.1): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif(move > 0.1): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE)
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 last_ball_position = [0, 0] final_x = 0 # speed = 7 pad = 0 lastbrick = 0 is_calcu = False # 2. Inform the game process that ml process is ready before start the loop. comm.ml_ready() random.seed(datetime.now()) # 3. Start an endless loop. while True: # 3.1. Receive the scene information sent from the game process. scene_info = comm.get_scene_info() #print(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 delta_x = 0 # 0:left, 1:right, 2:none platform_direction = 0 # delta_y = 0 """ if scene_info.ball[1] >= 393 : if final_x < (scene_info.platform[0] + 20) : platform_direction = 0 elif final_x > (scene_info.platform[0] + 20) : platform_direction = 1 else : platform_direction = 2 """ """if scene_info.ball[1] > 230 : if is_speed_up and final_x % 5 != 0 : speed += 1 is_speed_up = False else : is_speed_up = True""" #print(lastbrick) if scene_info.frame == 0: if (not scene_info.bricks) and (not scene_info.hard_bricks): lastbrick = 0 elif not scene_info.hard_bricks: lastbrick = scene_info.bricks[-1][1] elif not scene_info.bricks: lastbrick = scene_info.hard_bricks[-1][1] else: if scene_info.bricks[-1][1] > scene_info.hard_bricks[-1][1]: lastbrick = scene_info.bricks[-1][1] else: lastbrick = scene_info.hard_bricks[-1][1] if scene_info.ball[1] < lastbrick + 20: is_calcu = False pad = 0 if last_ball_position[1] < scene_info.ball[1]: if (is_calcu == False) and (scene_info.ball[1] > lastbrick + 30): delta_x = scene_info.ball[0] - last_ball_position[0] if delta_x == -10: final_x = calculate_position(2, scene_info.ball[0], scene_info.ball[1]) is_calcu = True #print(1) pad = 0 elif delta_x == 10: final_x = calculate_position(3, scene_info.ball[0], scene_info.ball[1]) #print(2) is_calcu = True pad = 0 elif delta_x == -7: pad += 1 if pad == 3: #print(3) final_x = calculate_position(0, scene_info.ball[0], scene_info.ball[1]) is_calcu = True pad = 0 elif delta_x == 7: pad += 1 if pad == 3: #print(4) final_x = calculate_position(1, scene_info.ball[0], scene_info.ball[1]) is_calcu = True pad = 0 """ elif scene_info.ball[0] < 100 : final_x = calculate_position(1, scene_info.ball[0], scene_info.ball[1]) is_calcu = True pad = 0 else : final_x = calculate_position(0, scene_info.ball[0], scene_info.ball[1]) is_calcu = True pad = 0 """ # print(final_x) """ if final_x < (scene_info.platform[0] + 20) : platform_direction = 0 elif final_x > (scene_info.platform[0] + 20) : platform_direction = 1 else : platform_direction = 2 """ # print(final_x) else: final_x = 100 # final_x = scene_info.ball[0] """ if scene_info.platform[0] > 80 : platform_direction = 0 elif scene_info.platform[0] < 80: platform_direction = 1 else : platform_direction = 2 """ if final_x < (scene_info.platform[0] + 20): platform_direction = 0 elif final_x > (scene_info.platform[0] + 20): platform_direction = 1 else: platform_direction = 2 last_ball_position = scene_info.ball """print(scene_info.bricks[-1], scene_info.hard_bricks[-1]) if (not scene_info.bricks) and (not scene_info.hard_bricks): lastbrick = 400 elif not scene_info.hard_bricks: lastbrick = scene_info.bricks[-1][1]; elif not scene_info.bricks: lastbrick = scene_info.hard_bricks[-1][1]; else: if scene_info.bricks[-1][1] > scene_info.hard_bricks[-1][1]: lastbrick = scene_info.bricks[-1][1] else: lastbrick = scene_info.hard_bricks[-1][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 else: if platform_direction == 0: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif platform_direction == 1: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_direction == 2: comm.send_instruction(scene_info.frame, PlatformAction.NONE)
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. ball_location = [0, 0] plat_location = [0, 0] bricks = [] hit = False 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: if scene_info.status == GameStatus.GAME_OVER: m = 10 / 0 # Do some stuff if needed # 3.2.1. Inform the game process that ml process is ready comm.ml_ready() continue last_ball_location = ball_location ball_location = scene_info.ball plat_location = scene_info.platform bricks = scene_info.bricks table = np.array(np.zeros((208, 408))) for k in range(len(bricks)): for p in range(0, 25): for j in range(0, 10): table[bricks[k][0] + p][bricks[k][1] + j] = 1 delta_x = int(ball_location[0]) - int(last_ball_location[0]) delta_y = int(ball_location[1]) - int(last_ball_location[1]) #get Slope try: m = delta_y / delta_x except: m = 0.0001 if (int(ball_location[1]) - int(last_ball_location[1]) > 0): # dowing next_x = (400 - int(ball_location[1])) / m + int(ball_location[0]) if (next_x > 200): next_x = 400 - next_x if (next_x < 0): if (next_x > -200): next_x = np.abs(next_x) else: next_x = next_x + 400 if (int(ball_location[0]) > int(last_ball_location[0])): #D.R. j = ball_location[0] + 3 k = ball_location[1] while (j < 200 and k < 400): if (table[j][k] == 1): hit = True break j = j + 1 k = k + 1 hit = False if (hit == False): j = ball_location[0] + 3 k = ball_location[1] + 3 while (j < 200 and k < 400): if (table[j][k] == 1): hit = True break j = j + 1 k = k + 1 hit = False if (hit): print('DR') print(j, k) if (table[j - 1][k] == 0): if (j + k < 400): next_x = k - j else: next_x = j + k - 400 else: #D.L j = ball_location[0] k = ball_location[1] while (j > 0 and k < 400): if (table[j][k] == 1): hit = True break j = j - 1 k = k + 1 hit = False if (hit == False): j = ball_location[0] k = ball_location[1] + 3 while (j > 0 and k < 400): if (table[j][k] == 1): hit = True break j = j - 1 k = k + 1 hit = False if (hit): print('DL') print(j, k) if (table[j + 1][k] == 0): if (200 - j + k < 400): next_x = k - j else: next_x = 400 - k + j if (next_x > 200): next_x = next_x - 200 if (next_x < 0): next_x = next_x * -1 else: if (int(ball_location[0]) > int(last_ball_location[0])): #U.R j = ball_location[0] + 3 k = ball_location[1] while (j < 200 and k > 0): if (table[j][k] == 1): hit = True break j = j + 1 k = k - 1 hit = False if (hit == False): j = ball_location[0] + 3 k = ball_location[1] + 3 while (j < 200 and k > 0): if (table[j][k] == 1): hit = True break j = j + 1 k = k - 1 hit = False if (hit): print('UR') print(j, k) if (table[j][k + 1] == 0): if (200 - j + k < 400): next_x = k - j else: next_x = 400 - k + j else: #U.L j = ball_location[0] k = ball_location[1] while (j > 0 and k > 0): if (table[j][k] == 1): hit = True break j = j - 1 k = k - 1 hit = False if (hit == False): j = ball_location[0] k = ball_location[1] + 3 while (j > 0 and k > 0): if (table[j][k] == 1): hit = True break j = j - 1 k = k - 1 hit = False if (hit): print('UL') print(j, k) if (table[j][k + 1] == 0): if (j + k < 400): next_x = 400 - (j + k) else: next_x = j + k - 400 else: next_x = 100 if (next_x > 200): next_x = next_x - 200 if (next_x < 0): next_x = next_x * -1 # 3.3. Put the code here to handle the scene information # 3.4. Send the instruction for this frame to the game process next_x = next_x - next_x % 5 if (int(plat_location[0]) + 20 > next_x): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif (int(plat_location[0]) + 20 < next_x): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE)
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 last_x = 75 last_y = 400 # 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 (ball_x, ball_y) = scene_info.ball platform_x = scene_info.platform[0] # 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 (last_y - ball_y) < 0 and ball_y > 350: if ball_y > 300: x = predict(ball_x, last_x, ball_y, last_y) if x < (platform_x + 20): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif x > (platform_x + 25): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE) """ else: if ball_x < (platform_x + 20): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif ball_x > (platform_x + 25): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE) """ last_x = ball_x last_y = ball_y
def ml_loop(): mode = "TestTrain" # TestTrain RuleBase predictFunction = "svm" #svm aid = 100 past_ball_position = [] ball_down = False comm.ml_ready() if mode == "RuleBase": 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 now_ball_position = scene_info.ball if len(past_ball_position) == 0: past_ball_position = now_ball_position bricks_history = scene_info.bricks else: if (now_ball_position[1] - past_ball_position[1]) > 0: ball_down = True else: ball_down = False m = 0 if ball_down == True and now_ball_position[1] > 250: if now_ball_position[0] - past_ball_position[0] != 0: m = (now_ball_position[1] - past_ball_position[1]) / (now_ball_position[0] - past_ball_position[0]) aid = round(now_ball_position[0] - ((now_ball_position[1] - 395) / m)) if aid < 0: aid = -aid elif aid > 195: aid = 200 - (aid - 200) else: if now_ball_position[0] < 10 or now_ball_position[0] > 175 or now_ball_position[1] < 250: aid = 100 move = True bricks = list(filter(lambda x: x[1] > now_ball_position[1], scene_info.bricks)) if check_rect_collide(now_ball_position, m, bricks): move = False now_platform_positionX = scene_info.platform[0] + 20 if m != 0: if (m > 0 and aid >= 100 and aid <= 120) or (m > 0 and aid < 80): now_platform_positionX = scene_info.platform[0] + 5 elif (m < 0 and aid <= 100 and aid >= 80) or (m < 0 and aid > 120): now_platform_positionX = scene_info.platform[0] + 35 if move and aid > now_platform_positionX: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) if move and aid < now_platform_positionX: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) if move and aid == now_platform_positionX: comm.send_instruction(scene_info.frame, PlatformAction.NONE) past_ball_position = now_ball_position if mode == "TestTrain": filename = predictFunction + ".sav" model = pickle.load(open(filename, 'rb')) while True: scene_info = comm.get_scene_info() now_ball_position = scene_info.ball if len(past_ball_position) != 0: m = 0 if now_ball_position[0] - past_ball_position[0] != 0: m = (now_ball_position[1] - past_ball_position[1]) / (now_ball_position[0] - past_ball_position[0]) bricks = list(filter(lambda x: x[1] > now_ball_position[1], scene_info.bricks)) overlap = get_rect_collide(now_ball_position, m, bricks) inp_temp = np.array([past_ball_position[0], past_ball_position[1], now_ball_position[0], now_ball_position[1], scene_info.platform[0], overlap[0], overlap[1], m]) 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) past_ball_position = now_ball_position
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 prev_ball_coor = (0, 0) prev_plat_coor = (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() # 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 esti_ball_x = calculate(prev_ball_coor[0], prev_ball_coor[1], 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 else: if (esti_ball_x - (scene_info.platform[0] + 40)) > 0: print("Estimate: ", esti_ball_x, ", Current: ", scene_info.platform[0], ", Moving right") comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif (esti_ball_x - scene_info.platform[0]) < 0: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) print("Estimate: ", esti_ball_x, ", Current: ", scene_info.platform[0], ", Moving left") else: comm.send_instruction(scene_info.frame, PlatformAction.NONE) print("Estimate: ", esti_ball_x, ", Current: ", scene_info.platform[0], ", Staying") prev_ball_coor = (scene_info.ball[0], scene_info.ball[1]) prev_plat_coor = (scene_info.platform[0], scene_info.platform[1])
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_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() 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, :] print(input) # 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) print(move) 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)
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 platform_arrived = False ball_dir = 0 ball_preposition = -1 destination = -1 position = 0 fall = 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 platform_arrived = False ball_dir = 0 ball_preposition = -1 destination = -1 position = 0 fall = False else: position = scene_info.platform[0] + 20 if scene_info.ball[1] >= 280 and scene_info.ball[ 1] <= 320 and fall == True: if ball_dir == 0 and platform_arrived == False: if scene_info.ball[0] >= 10 and scene_info.ball[0] <= 190: if ball_preposition == -1: ball_preposition = scene_info.ball[0] else: if scene_info.ball[0] - ball_preposition > 0: ball_dir = 1 else: ball_dir = -1 if scene_info.ball[1] >= 350: ball_dir = 0 ball_preposition = -1 if platform_arrived == False and destination == -1: if scene_info.ball[1] >= 280 and not ball_dir == 0: temp = 400 - scene_info.ball[1] destination = scene_info.ball[0] + ball_dir * temp fall = False if destination > 200: destination = 200 - (destination - 200) if destination < 0: destination = -destination if abs(position - destination) <= 5: platform_arrived = True destination = -1 if scene_info.ball[1] < 270: platform_arrived = False fall = True if scene_info.ball[1] > 395: platform_arrived = False if platform_arrived == True: comm.send_instruction(scene_info.frame, PlatformAction.NONE) else: if destination == -1: if position < 90: ##print(1) comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) if position >= 90 and position <= 110: ##print(2) comm.send_instruction(scene_info.frame, PlatformAction.NONE) if position > 110: ##print(3) comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) else: if position - destination > 5: ##print(4) comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) if position - destination < -5: ##print(5) comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
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() ball_x = 0 ball_y = 0 m = 0 delta_x = 0 delta_y = 0 predict_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() # 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: last_ball_x = ball_x last_ball_y = ball_y ball_x = scene_info.ball[0] ball_y = scene_info.ball[1] delta_x = ball_x - last_ball_x # "+"means move right delta_y = ball_y - last_ball_y # "+"means move downward m = delta_y / delta_x platform_x = scene_info.platform[0] if delta_y < 0: #ball is moving upward if platform_x < 80: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_x >= 80: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) #if platform_x<ball_x: # comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) #else: # comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) else: #ball is moving downward predict_x = (400 - ball_y + m * ball_x) / m while predict_x > 200 or predict_x < 0: if predict_x > 200: predict_x = 400 - predict_x if predict_x < 0: predict_x = -predict_x if predict_x - 20 > platform_x: if (predict_x - 20 - platform_x) > 5: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) else: if (platform_x - (predict_x - 20)) > 5: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
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() a = 0 b = 0 c = 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() # 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] ball_y = scene_info.ball[1] platform_x = scene_info.platform[0] platform_y = scene_info.platform[1] if ball_y < 150: if platform_x <75: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_x >75: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE) else: if ball_y - a >0:#down if ball_x - b >0:#right 395-ball_y == height if (395 - ball_y)>(200-ball_x): #bounce c = 200- ((395 - ball_y)-(200-ball_x)) else: c = ball_x + (395 - ball_y) #comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) else:#left if (395 - ball_y)>ball_x: #bounce c = ((395 - ball_y)-ball_x) else: c = ball_x - (395 - ball_y) b = ball_x if platform_x <(c-20): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_x >(c-20): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE)
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 hit = False expect_x = 75 pre_x = 75 pre_y = 0 else: ball_x = scene_info.ball[0] ball_y = 400 - scene_info.ball[1] plat_x = scene_info.platform[0] vec_x = ball_x - pre_x vec_y = ball_y - pre_y if(vec_y >0 and ball_y > 280): expect_x = 75 if(vec_x < 0 and vec_y < 0): if(ball_y >= 195): expect_x = 390 - (ball_y - ball_x) print("S") if(expect_x >= 195): expect_x = ball_y - ball_x elif(ball_y > ball_x): expect_x = ball_y - ball_x print("2") if(expect_x < 0): expect_x = ball_x - ball_y print("2-2") else: expect_x = ball_x - ball_y if(vec_x > 0 and vec_y < 0): if(ball_y >= 195): expect_x = ball_y + ball_x - 390 print("3-1") if(expect_x < 0): expect_x = 390 - ball_x - ball_y print("3") else: expect_x = ball_x + ball_y if(expect_x > 195): expect_x = 390 - ball_x - ball_y pre_x = ball_x pre_y = ball_y print(expect_x) if (plat_x< expect_x) and (plat_x + 20 > expect_x): comm.send_instruction(scene_info.frame, PlatformAction.NONE) elif plat_x > expect_x: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif plat_x < expect_x: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) """
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 prepare_cal = False fall = 75 former_ball_y = 395 former_ball_x = 75 #now_ball_y = 400 # 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 now_ball_x = scene_info.ball[0] now_ball_y = scene_info.ball[1] if now_ball_y != former_ball_y: if 247 < now_ball_y and now_ball_y < 255 and now_ball_y > former_ball_y: prepare_cal = True # 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: platform_x = scene_info.platform[0] if now_ball_y > 260: if prepare_cal: fall = fall_final(former_ball_x, now_ball_x) prepare_cal = False if now_ball_y >= 388 or abs(platform_x - fall) < 5: comm.send_instruction(scene_info.frame, PlatformAction.NONE) elif fall > platform_x: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif fall < platform_x: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE) else: if platform_x < 80: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_x > 80: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE) former_ball_x = now_ball_x former_ball_y = now_ball_y
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 lowest = -1 #lowest block position lock = False #locker old_x = 0 old_y = 0 est = -1 # the estimate position of x # 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 ball_x = scene_info.ball[0] ball_y = scene_info.ball[1] plat_x = scene_info.platform[0] plat_y = scene_info.platform[1] for x in scene_info.bricks: if x[1] > lowest: lowest = x[1] for x in scene_info.hard_bricks: if x[1] > lowest: lowest = x[1] print(scene_info.ball) #print("con1" + str(ball_y>(lowest+10))) #print("con2" + str(not lock)) #print("con3" + str(ball_y-old_y)) #print(est) # 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 elif lock == True: if ball_y == plat_y-5: print("unlock") lock = False if plat_x > est-20: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif plat_x < est-20: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE) elif ball_y > (lowest+110) and not lock and (ball_y - old_y) > 0: lock = True print("locked") vy = (ball_y-old_y) vx = abs(ball_x-old_x) #velocity of x if vx < 7: vx = 7 print("velocity of x =" + str(vx)) print("velocity of y =" + str(vy)) #plat_y = 400 if ball_x - old_x > 0: #move right if (200-ball_x)/vx > (plat_y-ball_y)/vy: est = (plat_y-ball_y)/vy*vx+ball_x else: #hit x = 200 hit_y = ball_y+(200-ball_x)/vx*vy est = 200 - (plat_y-hit_y)/vy*vx else: #move left if (ball_x-0)/vx > (plat_y-ball_y)/vy: est = ball_x-(plat_y-ball_y)/vy*vx else: #hit x = 0 hit_y = ball_y+ball_x/vx*vy est = (plat_y-hit_y)/(ball_y-old_y)*vx print("begin with " + str(ball_x) + "," + str(ball_y)) print("est=" + str(est)) comm.send_instruction(scene_info.frame, PlatformAction.NONE) else: if plat_x < 100: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif plat_x > 100: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE) old_x = ball_x old_y = ball_y
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 = [] vx = 0 vy = 0 ball_destination = 0 ball_going_down = 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() ball_position_history.append(scene_info.ball) platform_center_x = scene_info.platform[0] + 20 #platform length = 40 #print(scene_info.ball) #球目前的位置印出 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 print(vx, vy) # 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 #scene_info = comm.get_scene_info() ball_going_down = 0 # 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 platform_position_value = ball_position_history[-1][ 0] - platform_center_x #判斷球在底盤左邊還是右邊 if ball_going_down == 0: #當球往上,底盤回歸中點 if platform_center_x > 100: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif platform_center_x < 100: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) #else: # comm.send_instruction(scene_info.frame, PlatformAction.NONE) elif ball_going_down == 1: if scene_info.ball[1] > 200: if (platform_position_value + 20) > 0 and vx > 0: #底盤最右邊當出發點 comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) #往右移 elif (platform_position_value - 20) < 0 and vx < 0: #底盤最左邊當出發點 comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) #往左移
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. import pickle import numpy as np filename = "C:\\Users\\ASUS\\Desktop\\108-1\\Mechine_learning\\MLGame-master\\games\\arkanoid\\Train\\svr_example1.sav" model = pickle.load(open(filename, 'rb')) #print(model) ball_position_history = [] vx = 0 vy = 0 ball_destination = 0 ball_going_down = 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() ball_position_history.append(scene_info.ball) platform_center_x = scene_info.platform[0] #platform length = 40 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], platform_center_x, vx, vy ]) input = inp_temp[np.newaxis, :] #print(input) # 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 \ if scene_info.status == GameStatus.GAME_PASS: # Do some stuff if needed #scene_info = comm.get_scene_info() # 3.2.1. Inform the game process that ml process is ready comm.ml_ready() continue platform_center_y = scene_info.platform[1] ball_height = scene_info.ball[1] #球Y位點 # 3.3. Put the code here to handle the scene information if (len(ball_position_history) > 1): if platform_center_y - ball_height <= 150: move = model.predict(input) else: move = 0 else: move = 0 # 3.4. Send the instruction for this frame to the game process #print(ball_destination) #print(platform_center_x) 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: if platform_center_x < 80: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > 80: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass
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 dirX = 0 dirY = 0 xRec = 95 yRec = 200 left = 0 right = 195 location = 0 platSpeed = 5 ballSpeed = 7 count = 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 ballx, bally = scene_info.ball platx, platy = scene_info.platform if not ball_served: comm.send_instruction(scene_info.frame, PlatformAction.SERVE_TO_LEFT) ball_served = True max = 0 for x, y in scene_info.bricks: if y > max: max = y print(max) else: if ballx - xRec > 0: dirX = 1 else: dirX = -1 xRec = ballx if bally - yRec > 0: dirY = 1 else: dirY = -1 yRec = bally # print(dirY) if (bally < max + ballSpeed * 4 and bally > max + ballSpeed * 2) and dirY == 1: fps = (platy - bally) / ballSpeed tmpdirX = dirX tmpBall = ballx for i in range(int(fps)): # print(tmpBall) if tmpdirX == 1 and tmpBall + ballSpeed <= right: tmpBall = tmpBall + ballSpeed elif tmpdirX == 1 and tmpBall + ballSpeed > right: tmpBall = right tmpdirX = -1 continue if tmpdirX == -1 and tmpBall - ballSpeed >= left: tmpBall = tmpBall - ballSpeed elif tmpdirX == -1 and tmpBall - ballSpeed < left: tmpBall = left tmpdirX = 1 continue location = tmpBall if platx + 20 < ballx: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platx + 20 > ballx: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE) # print(fps) elif bally > max + ballSpeed * 2 and dirY == 1: # print(count) # print(scene_info.ball) # print(location) count = count + 1 if platx + 20 < location + platSpeed / 2 and platx + 20 > location - platSpeed / 2: comm.send_instruction(scene_info.frame, PlatformAction.NONE) elif platx + 20 < location: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platx + 20 > location: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) else: if platx + 20 < ballx: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platx + 20 > ballx: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE)
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 ball_x.append(scene_info.ball[0]) ball_y.append(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_RIGHT) ball_served = True else: global final if (scene_info.ball[1] > 200): if (scene_info.ball[1] - ball_y[-2] > 0): # 球往下 if (scene_info.ball[0] - ball_x[-2] > 0): # 球往右 final = (400 - scene_info.ball[1]) + scene_info.ball[0] if (final > 200): final = 400 - final else: # 往左 final = scene_info.ball[0] - (400 - scene_info.ball[1]) if (final < 0): final = -final if (scene_info.platform[0] + 10 > final): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif (scene_info.platform[0] + 10 < final): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) else: if (scene_info.platform[0] + 10 > scene_info.ball[0]): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) else: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
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 last_x = 75 last_y = 400 predict_x = 75 predict_y = 401 direction = 0 # 0:leftup, 1:rightup, 2:leftdown, 3:rightdown # 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 (ball_x, ball_y) = scene_info.ball platform_x = scene_info.platform[0] platform_y = scene_info.platform[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 else: if ((ball_x - last_x) < 0 and (ball_y - last_y) < 0): direction = 0 elif ((ball_x - last_x) > 0 and (ball_y - last_y) < 0): direction = 1 elif ((ball_x - last_x) < 0 and (ball_y - last_y) > 0): direction = 2 elif ((ball_x - last_x) > 0 and (ball_y - last_y) > 0): direction = 3 m = (ball_y - last_y) / (ball_x - last_x) for i in range(30): if (direction == 0): predict_x = 0 predict_y = m * (0 - ball_x) + ball_y direction = 1 if (predict_y < 0): predict_x = (0 - ball_y) / m + ball_x predict_y = 0 direction = 2 elif (direction == 1): predict_x = 195 predict_y = m * (195 - ball_x) + ball_y direction = 0 if (predict_y < 0): predict_x = (0 - ball_y) / m + ball_x predict_y = 0 direction = 3 elif (direction == 2): predict_x = 0 predict_y = m * (0 - ball_x) + ball_y direction = 3 if (predict_y > 395): predict_x = (400 - ball_y) / m + ball_x predict_y = 400 direction = 0 break elif (direction == 3): predict_x = 195 predict_y = m * (195 - ball_x) + ball_y direction = 2 if (predict_y > 395): predict_x = (400 - ball_y) / m + ball_x predict_y = 400 direction = 1 break m = -m """ if(predict_x < platform_x + 20): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif(predict_x > platform_x + 20): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE) """ if (last_y - ball_y) < 0: #x = predict(ball_x,last_x,ball_y,last_y) if predict_x < (platform_x + 20): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif predict_x > (platform_x + 25): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE) else: if ball_x < (platform_x + 20): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif ball_x > (platform_x + 25): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE) last_x = ball_x last_y = ball_y
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. """ ball_pos_history = [] # === 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() ball_pos_history.append(scene_info.ball) #判定球為上升還是落下 if (len(ball_pos_history)) == 1: ball_godown = 0 elif ball_pos_history[-1][1] - ball_pos_history[-2][ 1] > 0: #[-1]代表倒數最後一列 ball_godown = 1 vy = ball_pos_history[-1][1] - ball_pos_history[-2][1] vx = ball_pos_history[-1][0] - ball_pos_history[-2][0] m = vy / vx else: ball_godown = 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 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] #the x of tuple(x, y) ball_y = scene_info.ball[1] platform_x = scene_info.platform[0] if ball_godown == 1 and ball_y >= 45: final_x = (400 - ball_y) / m + ball_x if final_x > 200: final_x = 200 - (final_x - 200) elif final_x < 0: final_x = -final_x if final_x > platform_x + 40: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif final_x < platform_x: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE)
def ml_loop(): mode = "TestTrain" # TestTrain RuleBase predictFunction = "knn" #svm aid = 0 past_ball_position = [] ball_down = False comm.ml_ready() if mode == "RuleBase": 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 now_ball_position = scene_info.ball if len(past_ball_position) == 0: past_ball_position = now_ball_position else: if (now_ball_position[1] - past_ball_position[1]) > 0: ball_down = True else: ball_down = False now_platform_positionX = scene_info.platform[0] + 20 if ball_down == True and now_ball_position[1] > 200: m = (now_ball_position[1] - past_ball_position[1]) / ( now_ball_position[0] - past_ball_position[0]) aid = now_ball_position[0] - ((now_ball_position[1] - 395) / m) if aid < 0: aid = -aid elif aid > 200: aid = 200 - (aid - 200) else: aid = 100 if aid > now_platform_positionX: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) if aid < now_platform_positionX: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) if aid == now_platform_positionX: comm.send_instruction(scene_info.frame, PlatformAction.NONE) past_ball_position = now_ball_position if mode == "TestTrain": filename = predictFunction + ".sav" model = pickle.load(open(filename, 'rb')) while True: scene_info = comm.get_scene_info() now_ball_position = scene_info.ball if len(past_ball_position) != 0: inp_temp = np.array([ past_ball_position[0], past_ball_position[1], now_ball_position[0], now_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) past_ball_position = now_ball_position
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] #slope = vy/vx #print(slope) else: ball_going_down = 0 #print(ball_position_history) # 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 #scene_info = comm.get_scene_info() comm.ml_ready() continue # 3.3. Put the code here to handle the scene information if ball_going_down == 1: 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 print(ball_destination) if ball_destination < platform_center_x-10: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif ball_destination > platform_center_x+10: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE)
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 BallX = -1 BallY = -1 BallNewX = -1 BallNewY = -1 downX = -1 # 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 platform = scene_info.platform[0] if (BallX == -1): BallX, BallY = scene_info.ball else: BallNewX, BallNewY = scene_info.ball nowVec = BallNewX - BallX, BallNewY - BallY BallX = BallNewX BallY = BallNewY # 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 (nowVec[1] < 0): comm.send_instruction(scene_info.frame, PlatformAction.NONE) elif (nowVec[0] != 0): ins = nowVec[0] downStep = int((400 - BallNewY) / nowVec[1]) #print(downStep) downX = BallNewX + int(downStep * ins) #print(downX) while (downX < 0 or downX > 200): if (downX < 0): downX = -downX else: downX = 400 - downX #print(downX) if (scene_info.platform[0] + 20 > downX): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif (scene_info.platform[0] - 20 < downX): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE)
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 #有沒有發球 ball_x = 93 ball_y = 93 preball_y = 93 # 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: preball_x = ball_x if preball_y > ball_y: up = True else: up = False preball_y = ball_y ball_x = scene_info.ball[0] + 2.5 if preball_x > ball_x: left = True else: left = False ball_y = scene_info.ball[1] + 2.5 platform_x = scene_info.platform[0] + 20 if up: if platform_x == 95: comm.send_instruction(scene_info.frame, PlatformAction.NONE) elif platform_x > 95: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) else: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) else: if left: if ball_x - (397.5 - ball_y) < 0: drop_x = -(ball_x - (397.5 - ball_y)) else: drop_x = ball_x - (397.5 - ball_y) else: if ball_x + (397.5 - ball_y) > 200: drop_x = 400 - (ball_x + (397.5 - ball_y)) else: drop_x = ball_x + (397.5 - ball_y) if platform_x > drop_x + 3.5: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif platform_x < drop_x - 3.5: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE)
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 = [] test=-1 test2=-1 test_x=0 test_y=0 test2_x=0 test2_y=0 two_down =-1 two_down1 =-1 new_downHeight=0 new_down=0 new_downHeight1=0 new_down1=0 # 2. Inform the game process that ml process is ready before start the loop. comm.ml_ready() mx =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() ball_position_history.append(scene_info.ball) 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 my = ball_position_history[-1][1] - ball_position_history[-2][1] mx = ball_position_history[-1][0] - ball_position_history[-2][0] #print(ball_going_down) platform_center_x = scene_info.platform[0]+20 platform_center_y = scene_info.platform[1] #400 球初始 100 #球一步X,Y= 7 平台一步 = 5 ball_center = scene_info.ball[0]#球X位點左上角+2.5為正中央 ball_height = scene_info.ball[1]#球Y位點 #if scene_info.status == GameStatus.GAME_OVER or \ if scene_info.status == GameStatus.GAME_PASS: print("succcess") comm.ml_ready() continue #scene_info = comm.get_scene_info() # Do some stuff if needed # 3.2.1. Inform the game process that ml process is ready # Do some stuff if needed # 3.2.1. Inform the game process that ml process is ready if platform_center_y-ball_height>=125: vx = ball_center #紀錄 在100 x是多少 one_mx = mx record_vx = 1 test = -1 test2 = -1 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_y-ball_height>=112: vx = ball_center #紀錄 在100 x是多少 one_mx = mx if record_vx ==1: if ball_position_history[-1][1] - ball_position_history[-2][1] >0: up = platform_center_x print(one_mx) print(vx) if one_mx > 0 : if vx<=88: ##在左邊往右 if platform_center_x < vx+112: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > vx+112: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass else: if mx>0: test = 0 test_x = ball_center test_y = platform_center_y-ball_height if test_x>170: test = 1 if platform_center_x < 200-abs(88-vx): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > 200-abs(88-vx): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass else: if platform_center_x < (200-abs(88-vx)+150)/2: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > (200-abs(88-vx)+150)/2: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass elif test ==0 : if platform_center_x < test_x-test_y: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > test_x-test_y: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass elif one_mx == 5 or one_mx == 4: if platform_center_x < vx-112: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x >vx-112: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass elif test != 1: print("在112以上撞到反彈下") print(test) if platform_center_x < vx-112: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > vx-112: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass else: print("HALO1") print(test2_x-test2_y) if platform_center_x < test_x-test_y: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > test_x-test_y: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass elif one_mx < 0 : if vx>=112:##在又邊往左 if platform_center_x < vx-112: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > vx-112: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass else: if mx<0: print(test2) print(platform_center_x) test2 = 0 test2_x = ball_center test2_y = platform_center_y-ball_height if test2_x<30: test2 = 1 if platform_center_x < abs(vx-112): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > abs(vx-112): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass else: if platform_center_x < (abs(vx-112)+70)/2: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > (abs(vx-112)+70)/2: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass elif test2 == 0 : print(test2_x+test2_y) if platform_center_x < test2_x+test2_y: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > test2_x+test2_y: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass elif one_mx == -5 or one_mx == -4 or one_mx == -2: print(vx+112) if platform_center_x < vx+112: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x >vx+112: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass elif test2 != 1: print("在112以上撞到反彈下") print(test2) if platform_center_x < vx+112: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > vx+112: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass else: print("HALO") print(test2_x+test2_y) if platform_center_x < test2_x+test2_y: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > test2_x+test2_y: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass else: record_vx = 0 else: if ball_position_history[-1][1] - ball_position_history[-2][1] >0:##第二次下降 if down_mx > 0: print("down_mx又") print(mx) if mx>0 : two_down=1 new_downHeight = platform_center_y-ball_height newdown =ball_center if newdown<190: two_down=0 if platform_center_x <200-abs(down + down_height-200): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > 200-abs(down + down_height-200): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass else: if platform_center_x <(330-abs(down + down_height-200))/2: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > (330-abs(down + down_height-200))/2: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass elif two_down==1: print(newdown) print(new_downHeight) if platform_center_x <abs(newdown - new_downHeight): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > abs(newdown - new_downHeight): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass else: if mx>0: if platform_center_x <200-abs(200-(down+down_height)): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x >200-abs(200-(down+down_height)): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass else: if platform_center_x <abs(down - down_height): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > abs(down - down_height): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass else: if mx<0: two_down1=1 new_downHeight1 = platform_center_y-ball_height newdown1 =ball_center print(newdown1) if newdown1>10: two_down1=0 print(abs(down - down_height)) if platform_center_x <abs(down - down_height): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > abs(down - down_height): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass else: if platform_center_x <(abs(down - down_height)+100)/2: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > (abs(down - down_height)+100)/2: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass elif two_down1 ==1: if platform_center_x <abs(newdown1 + new_downHeight1): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > abs(newdown1 + new_downHeight1): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass else: if mx>0: if platform_center_x <200-abs(200-(down+down_height)): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x >200-abs(200-(down+down_height)): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass else: if platform_center_x <abs(down - down_height): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > abs(down - down_height): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass else: #回擊後上去 print("回擊後上去") print(up) #回擊後托盤X位置 print(platform_center_x) down = ball_center down_height = platform_center_y-ball_height down_mx = ball_position_history[-1][0] - ball_position_history[-2][0] newdown=ball_center new_downHeight=platform_center_y-ball_height newdown1=ball_center new_downHeight1=platform_center_y-ball_height if up > 160: #UP第一次回及時托盤X if mx>0: if platform_center_x <up -30: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > up-30: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass else: if platform_center_x < 70: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > 70: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass elif up < 40 : if mx<0: if platform_center_x < up +30: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > up +30: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass else: if platform_center_x < 130: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > 130: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass else: if mx<0: if platform_center_x < 70: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > 70: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass elif mx>0: if platform_center_x < 130: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > 130: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass
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_KNN_BallAndDirection.pickle') with open(filename, 'rb') as file: clf = pickle.load(file) 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() feature = [] feature.append(scene_info.ball[0]) feature.append(scene_info.ball[1]) feature.append(scene_info.platform[0]) feature.append(feature[0] - s[0]) feature.append(feature[1] - s[1]) s = [feature[0], feature[1]] feature = np.array(feature) feature = feature.reshape((-1, 5)) # 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)
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) 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 def get_point(ball_x, ball_y, dir, dir_x_abs): if (dir_x_abs == 0): return scene_info.platform[0] crush = -1 while (ball_y < 400): ball_x = ball_x + dir ball_y = ball_y + dir_x_abs if (ball_x < 0): ball_x = 0 dir = -dir crush = crush * -1 elif (ball_x > 200): ball_x = 200 dir = -dir crush = crush * -1 while (ball_y > 400): ball_x = ball_x - dir / dir_x_abs ball_y = ball_y - 1 return ball_x * crush # 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() feature = [] feature.append(scene_info.ball[0]) feature.append(scene_info.ball[1]) feature.append(scene_info.platform[0]) dir = feature[0] - s[0] dir_x_abs = abs(dir) direction = get_direction(feature[0], feature[1], s[0], s[1]) feature.append(direction) 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: instruction = 'NONE' elif y == 1: instruction = 'LEFT' elif y == 2: instruction = 'RIGHT'""" instruction = 'NONE' if (scene_info.ball[1] > 255): #and (direction == 0 or direction == 2)): point = get_point(scene_info.ball[0], scene_info.ball[1], dir, dir_x_abs) if (point < 0): point = -point elif (direction == 0): direction = 2 else: direction = 0 if (direction == 0): if (point - scene_info.platform[0] > 39): instruction = 'RIGHT' elif (point - scene_info.platform[0] < 28): instruction = 'LEFT' else: if (point - scene_info.platform[0] < 1): instruction = 'LEFT' elif (point - scene_info.platform[0] > 12): instruction = 'RIGHT' elif (scene_info.ball[1] > 200): #and (direction == 0 or direction == 2)): if (scene_info.platform[0] < 60): instruction = 'RIGHT' elif (scene_info.platform[0] > 90): instruction = 'LEFT' if instruction == 'LEFT': comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) #print('LEFT') elif instruction == 'RIGHT': comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) #print('RIGHT') else: comm.send_instruction(scene_info.frame, PlatformAction.NONE)
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_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_center_record.append(scene_info.ball) platform_center = scene_info.platform[0] + 20 ball_center = scene_info.ball[0] + 2.5 ball_height = scene_info.ball[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: # 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 # 3.4. Send the instruction for this frame to the game process if ball_height <= 300: if ball_center > 100: if platform_center < 40: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center > 40: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass elif ball_center < 100: if platform_center < 160: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center > 160: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass pass elif ball_height >= 350: 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) pass pass
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 ball_x_before = 100 ball_y_before = 395 destination_x = 100 save = 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 ball_x = scene_info.ball[0] ball_y = scene_info.ball[1] platform_x = scene_info.platform[0] f = scene_info.frame # 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 ball_y > 240 and ball_y < 395 and f > save: if ball_x > ball_x_before and ball_y > ball_y_before : save = ((400 - ball_y) / 7) + 1 + f t = ball_y + (200 - ball_x) if (400 - t) >= 200 : destination_x = 200 - t else : destination_x = t - 200 elif ball_x < ball_x_before and ball_y > ball_y_before : save = ((400 - ball_y) / 7) + 1 + f t = ball_y + ball_x if (400 - t) >= 200 : destination_x = t else : destination_x = 400 - t elif ball_y <= 240 : destination_x = 100 if platform_x + 20 >= destination_x - 5 and platform_x + 20 <= destination_x + 5 : comm.send_instruction(scene_info.frame, PlatformAction.NONE) elif platform_x + 20 < destination_x : comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_x + 20 > destination_x : comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) else : comm.send_instruction(scene_info.frame, PlatformAction.NONE) ball_x_before = ball_x ball_y_before = ball_y