-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai_game.py
225 lines (180 loc) · 7.49 KB
/
ai_game.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import pygame
from pygame.locals import *
import random
import math
pygame.init()
pygame.font.init()
font = pygame.font.SysFont("Arial", 30)
FPS = 30000 # how fast each game progresses
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 600
PIPE_WIDTH = 20
PIPE_HEIGHT = 500
PLAYER_RADIUS = 10
BLACK_COLOR = [0, 0, 0]
RED_COLOR = [255, 0, 0]
GREEN_COLOR = [0, 255, 0]
WHITE_COLOR = [255, 255, 255]
class FlappyBirdAI:
def __init__(self):
self.display_width = SCREEN_WIDTH
self.display_height = SCREEN_HEIGHT
self.player_radius = PLAYER_RADIUS
self.pipe_height = PIPE_HEIGHT
self.pipe_width = PIPE_WIDTH
# self.display = pygame.display.set_mode((self.display_width, self.display_height))
# pygame.display.set_caption("Flappy Bird Clone")
self.clock = pygame.time.Clock()
self.reset()
def reset(self):
self.upcoming_pipes = []
self.player_x = int(self.display_width / 5)
self.player_y = int(self.display_height - self.player_radius) / 2
self.frame_iteration = 0
self.score = 0
self.player_x = int(self.display_width / 5)
self.player_y = int(self.display_height / 2)
self.new_pipe_1 = self.get_pipe()
self.new_pipe_2 = self.get_pipe()
self.new_pipe_3 = self.get_pipe()
self.pipe_velocity_x = -5
self.player_velocity_y = -9
self.player_max_velocity_y = 10
self.player_accelaration_y = 1
self.player_flap_velocity = -8
self.player_flapped = False
self.game_over = False
self.upper_pipes = [{"x": self.display_width + 200, "y": self.new_pipe_1[0]["y"]}]
self.lower_pipes = [{"x": self.display_width + 200, "y": self.new_pipe_1[1]["y"]}]
self.upper_pipes.append(
{"x": self.upper_pipes[-1]["x"] + 200, "y": self.new_pipe_2[0]["y"]}
)
self.lower_pipes.append(
{"x": self.lower_pipes[-1]["x"] + 200, "y": self.new_pipe_2[1]["y"]}
)
self.upper_pipes.append(
{"x": self.upper_pipes[-1]["x"] + 200, "y": self.new_pipe_3[0]["y"]}
)
self.lower_pipes.append(
{"x": self.lower_pipes[-1]["x"] + 200, "y": self.new_pipe_3[1]["y"]}
)
for upper_pipe, lower_pipe in zip(self.upper_pipes, self.lower_pipes):
if upper_pipe['x'] > self.player_x + self.player_radius:
self.upcoming_pipes.append(upper_pipe)
self.upcoming_pipes.append(lower_pipe)
# self.update_ui(action=0)
def play_step(self, action):
self.upcoming_pipes = []
for upper_pipe, lower_pipe in zip(self.upper_pipes, self.lower_pipes):
if upper_pipe['x'] >= self.player_x:
self.upcoming_pipes.append(upper_pipe)
self.upcoming_pipes.append(lower_pipe)
self.frame_iteration += 1
reward = 0
if action==True:
if self.player_y > 0:
self.player_velocity_y = self.player_flap_velocity
self.player_flapped = True
self.game_over = self.collision()
if self.game_over:
reward = -1000 # reward for dying
# reward += -.8*self.frame_iteration
return reward, self.game_over, self.score
else:
pass
# reward += 1 # reward for staying alive in a frame
player_mid_pos = self.player_x + self.player_radius // 2
for pipe in self.upper_pipes:
pipe_mid_pos = pipe["x"] + self.pipe_width / 2
if pipe_mid_pos <= player_mid_pos < pipe_mid_pos + 4:
self.score += 1
reward += 10
if (
self.player_velocity_y < self.player_max_velocity_y
and not self.player_flapped
):
self.player_velocity_y += self.player_accelaration_y
if self.player_flapped:
self.player_flapped = False
self.player_height = 5
self.player_y += min(
self.player_velocity_y,
self.display_height - self.player_y - self.player_height,
)
for upper_pipe, lower_pipe in zip(self.upper_pipes, self.lower_pipes):
upper_pipe["x"] += self.pipe_velocity_x
lower_pipe["x"] += self.pipe_velocity_x
if self.upper_pipes[0]["x"] < self.player_radius and len(self.upcoming_pipes) <= 10:
new_pipe = self.get_pipe(repeat=True)
self.upper_pipes.append(new_pipe[0])
self.lower_pipes.append(new_pipe[1])
if self.upper_pipes[0]["x"] < -self.pipe_width:
self.upper_pipes.pop(0)
self.lower_pipes.pop(0)
# self.update_ui(action)
# reward += self.frame_iteration
return reward, self.game_over, self.score
def update_ui(self, action):
self.display.fill(BLACK_COLOR)
pygame.draw.circle(
self.display,
WHITE_COLOR,
(self.player_x, self.player_y),
self.player_radius,
)
for upper_pipe, lower_pipe in zip(self.upper_pipes, self.lower_pipes):
pygame.draw.rect(
self.display,
WHITE_COLOR,
pygame.Rect(
upper_pipe["x"],
upper_pipe["y"],
self.pipe_width,
self.pipe_height,
),
)
pygame.draw.rect(
self.display,
WHITE_COLOR,
pygame.Rect(
lower_pipe["x"],
lower_pipe["y"],
self.pipe_width,
self.pipe_height,
),
)
score_text = f"Score: {self.score}"
text_width, text_height = font.size(str(score_text))
text_x = (self.display_width - text_width) // 2
text_surface = font.render(score_text, True, GREEN_COLOR)
self.display.blit(text_surface, (text_x, 20))
pygame.display.update()
self.clock.tick(FPS)
def collision(self):
if self.player_y >= self.display_height - self.player_radius or self.player_y - self.player_radius <= 0:
return True
for pipe in self.upper_pipes:
closest_x = max(pipe["x"], min(self.player_x, pipe["x"] + self.pipe_width))
closest_y = max(pipe["y"], min(self.player_y, pipe["y"] + self.pipe_height))
distance = math.sqrt(
(self.player_x - closest_x) ** 2 + (self.player_y - closest_y) ** 2
)
if distance <= self.player_radius:
return True
for pipe in self.lower_pipes:
closest_x = max(pipe["x"], min(self.player_x, pipe["x"] + self.pipe_width))
closest_y = max(pipe["y"], min(self.player_y, pipe["y"] + self.pipe_height))
distance = math.sqrt(
(self.player_x - closest_x) ** 2 + (self.player_y - closest_y) ** 2
)
if distance <= self.player_radius:
return True
def get_pipe(self, repeat = False):
gap = 150
if not repeat:
pipe_x = self.display_width + 10
else:
pipe_x = self.upper_pipes[-1]["x"] + 200
lower_pipe_y = random.randrange(gap + 50, self.display_height - 50)
upper_pipe_y = lower_pipe_y - gap - self.pipe_height
return [{"x": pipe_x, "y": upper_pipe_y}, {"x": pipe_x, "y": lower_pipe_y}]