-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_gamebot.py
54 lines (41 loc) · 1.38 KB
/
basic_gamebot.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
48
49
50
51
52
53
54
import gym
import universe # register the universe environments
import random
env = gym.make('flashgames.NeonRace-v0')
env.configure(remotes=1) # automatically creates a local docker container
observation_n = env.reset()
# Create variable for moving the car
goleft = [('KeyEvent', 'ArrowUp', True), ('KeyEvent', 'ArrowLeft', True),
('KeyEvent', 'ArrowRight', False)]
goright = [('KeyEvent', 'ArrowUp', True), ('KeyEvent', 'ArrowLeft', False),
('KeyEvent', 'ArrowRight', True)]
# Move the car forward with nitroboost
boostforward = [('KeyEvent', 'ArrowUp', True), ('KeyEvent', 'ArrowRight', False),
('KeyEvent', 'ArrowLeft', False), ('KeyEvent', 'n', True)]
sum_reward = 0
turn = 0
rewards = []
buffer_size = 100
action = boostforward
while True:
turn -= 1
if turn <= 0:
action = boostforward
turn = 0
# choose action based on speed
action_n = [action for ob in observation_n]
# perform action
observation_n, reward_n, done_n, info = env.step(action_n)
sum_reward += reward_n[0]
rewards += [reward_n[0]]
#if stuck, try going one side for some time
if len(rewards) >= buffer_size:
mean = sum(rewards)/len(rewards)
if mean == 0:
turn = 25
if random.random() < 0.5:
action = goleft
else:
action = goright
rewards = []
env.render()