8000 version3 by f74076205 · Pull Request #3 · f74076205/ML_Course · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

version3 #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 54 additions & 7 deletions ml_play.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,25 @@
from games.arkanoid.communication import ( \
SceneInfo, GameStatus, PlatformAction
)

def cal_m(x2,y2,m):
#m=(y2-y1)/(x2-x1)
board_x=-((y2-400)/m-x2)

rwall_y=y2-m*(x2-200)
lwall_y=y2-m*(x2-0)
if(rwall_y>y2 and rwall_y<400):
return cal_m(200,rwall_y,-m)
elif(lwall_y>y2 and lwall_y<400):
return cal_m(0,lwall_y,-m)
else:
return board_x
# print(x1)
# if(x1<=0):
# return -(x1)
# elif(x1>=400):
# return 400-(x1-400)
# else:
# return x1
def ml_loop():
"""
The main loop of the machine learning process
Expand All @@ -26,7 +44,10 @@ def ml_loop():
# 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()#取得遊戲場景資訊
Expand All @@ -43,18 +64,44 @@ def ml_loop():
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
goal=0
# pregoal=-1
m=-1
ball_x_pre=95
ball_y_pre=400
else:

ball_x=scene_info.ball[0]
platform_x=scene_info.platform[0]
ball_y=scene_info.ball[1]
platform_x_left=scene_info.platform[0]
platform_x_right=scene_info.platform[0]+40
#print(scene_info.platform[0])
#if(ball_x==0 or ball_x==200):
m=(ball_y-ball_y_pre)/(ball_x-ball_x_pre)
#print(m)


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)
goal=cal_m(ball_x,ball_y,m)
# print(goal)
# if(goal!=pregoal):
# print(goal)


if (platform_x_left-2.5)>goal:
comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
elif (platform_x_right+2.5)<goal:
comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
else :
comm.send_instruction(scene_info.frame, PlatformAction.NONE)


ball_x_pre=ball_x
ball_y_pre=ball_y
# pregoal=goal


0