-
Notifications
You must be signed in to change notification settings - Fork 0
/
env_flappybird_kogun.py
59 lines (49 loc) · 1.62 KB
/
env_flappybird_kogun.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
55
56
57
58
59
import numpy as np
from ple.games.flappybird import FlappyBird
from ple import PLE
from pygame.constants import K_w
class FlappyBirdEnv:
def __init__(self, seed=0, display_screen=False, max_episode_length=2000, pipe_gap=150, delay_step=1):
game = FlappyBird(pipe_gap=pipe_gap)
self.p = PLE(game, fps=30, display_screen=display_screen, rng=seed)
self.max_l = max_episode_length
self.step_count = 0
self.delay_step = delay_step
self.pass_pipe = 0
def to_state(self, game_state_dict):
state_list = [i for i in game_state_dict.values()]
state_list = state_list[:5]
state_list[3] = state_list[0] - state_list[3]
state_list[4] = state_list[0] - state_list[4]
return np.array(state_list) / 100
def reset(self):
self.p.init()
self.p.previous_score = 0
self.step_count = 0
self.pass_pipe = 0
return self.to_state(self.p.getGameState())
def step(self, act):
if act == 0:
act = None
else:
act = 119
r = self.p.act(act)
done = self.p.game_over()
snext = self.to_state(self.p.getGameState())
if r == 1:
self.pass_pipe += 1
if self.pass_pipe == self.delay_step:
reward = self.delay_step
self.pass_pipe = 0
else:
reward = 0
if done:
reward += -5
info = {}
self.step_count += 1
if self.step_count >= self.max_l:
done = True
return snext, reward, done, info
@staticmethod
def get_space_dim():
return 5, 2