forked from yen900611/maze_car
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ml_play_manual.py
47 lines (40 loc) · 1.52 KB
/
ml_play_manual.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import json
from pprint import pprint
import pygame
class MLPlay:
def __init__(self, ai_name,*args,**kwargs):
self.player_no = ai_name
self.r_sensor_value = 0
self.l_sensor_value = 0
self.f_sensor_value = 0
self.control_list = {"left_PWM": 0, "right_PWM": 0}
# print("Initial ml script")
def update(self, scene_info: dict, keyboard: list = [], *args, **kwargs):
"""
Generate the command according to the received scene information
"""
print(json.dumps(scene_info,indent=2))
if scene_info["status"] != "GAME_ALIVE":
return "RESET"
if pygame.K_w in keyboard or pygame.K_UP in keyboard:
self.control_list["left_PWM"] = 200
self.control_list["right_PWM"] = 200
elif pygame.K_a in keyboard or pygame.K_LEFT in keyboard:
self.control_list["left_PWM"] = -150
self.control_list["right_PWM"] = 150
elif pygame.K_d in keyboard or pygame.K_RIGHT in keyboard:
self.control_list["left_PWM"] = 150
self.control_list["right_PWM"] = -150
elif pygame.K_s in keyboard or pygame.K_DOWN in keyboard:
self.control_list["left_PWM"] = -150
self.control_list["right_PWM"] = -150
else:
self.control_list["left_PWM"] = 100
self.control_list["right_PWM"] = 100
return self.control_list
def reset(self):
"""
Reset the status
"""
# print("reset ml script")
pass