本文會介紹打磚塊基本(Non-Machine-Learning),本題材取自於 MLGame.py,https://github.com/LanKuDot/MLGame?fbclid=IwAR3Eni0s0wp-Wpv-2iLB8Vtt_Bn8l3TBw6lDhXFh3WYhl5EQ342Yk_yTYpc 此參考中,先以利用不是產生 Machine-Learning,來完成打磚塊的程式,相信大家清楚打磚塊規則,以磚塊全數消失就是過關為主,如果球低於板子則是失敗,所以我們第一次先不使用,Machine-Learning的做法來完成自動使用板子來打球體的動作。
import games.arkanoid.communication as comm
from games.arkanoid.communication import ( \
SceneInfo, GameInstruction, GameStatus, PlatformAction
)
def ml_loop():
ball_position_history = []
comm.ml_ready()
while True:
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]:
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
if scene_info.status == GameStatus.GAME_OVER or \
scene_info.status == GameStatus.GAME_PASS:
comm.ml_ready()
continue
if ball_going_down ==1 and ball_position_history[-1][1] >= 150 :
ball_destination = ball_position_history[-1][0]+(390-ball_position_history[-1][1]/vy)*vx
#print(platform_center_x)
print(ball_destination)
if ball_destination >= 195 :
ball_destination = 195-(ball_destination - 195)
#print(ball_destination)
comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
elif ball_destination <= 0 :
ball_destination = -ball_destination
#print(ball_destination)
comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
else:
ball_destination = platform_center_x
上方程式碼,是利用 cmd 指令 python MLGame.py -r arkanoid 2 -i ml_play_template.py 進行驅動上方指令動作,vy = ball_position_history[-1][1]-ball_position_history[-2][1]
vx = ball_position_history[-1][0]-ball_position_history[-2][0]
以上兩條式子,是將球的參數算出速度以及距離,根據這個距離資料來做判斷來藉由讓板子移動。
以上為不專業的AI介紹,謝謝各位的觀看,那我們下篇見~~~~