-
Notifications
You must be signed in to change notification settings - Fork 0
/
pongGym.py
232 lines (217 loc) · 12.6 KB
/
pongGym.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
226
227
228
229
230
231
232
import numpy as np
from gym import spaces
import random
import pygame, sys
from pygame.locals import *
import pong_config
class DoublePong:
WHITE = (255,255,255)
RED = (255,0,0)
GREEN = (0,255,0)
BLACK = (0,0,0)
PAD_WIDTH = pong_config.PAD_WIDTH
PAD_HEIGHT = pong_config.PAD_HEIGHT
BALL_RADIUS = pong_config.BALL_RADIUS
PAD_SPACE = pong_config.PAD_SPACE
HALF_PAD_WIDTH = PAD_WIDTH // 2
HALF_PAD_HEIGHT = PAD_HEIGHT // 2
ball_num = pong_config.ball_num
colorlist = [RED] * 50
action_space = spaces.Discrete(4)
observation_space = spaces.Box(np.array([np.float32(-320)] * (ball_num * 4 + 4)), np.array([np.float32(pong_config.WIDTH)] * (ball_num * 4 + 4)))
def __init__(self, WIDTH = pong_config.WIDTH, HEIGHT = pong_config.HEIGHT, ball_num = pong_config.ball_num):
self.WIDTH = WIDTH
self.HEIGHT = HEIGHT
self.ball_num = ball_num
self.reward = 0
self.action_space = spaces.Discrete(4)
observation_high = np.array([np.float32(max(WIDTH, HEIGHT))] * (ball_num * 4 + 4))
observation_low = np.array([np.float32(-max(WIDTH, HEIGHT))] * (ball_num * 4 + 4))
self.observation_space = spaces.Box(observation_low, observation_high)
self.ball_pos = [[0,0]] * ball_num
self.ball_vel = [[0,0]] * ball_num
self.paddle1_vel = [0, 0]
self.paddle2_vel = [0, 0]
self.paddle1_pos = [[0, 0], [0, 0]]
self.paddle2_pos = [[0, 0], [0, 0]]
self.colorlist = [(255,0,0)] * 50
self.l_score = 0
self.r_score = 0
self.first_show = True
self.counter = 0
self.horz = 2
self.vert = -3
self.defense = 0
self.reset()
def reset(self):
# global paddle1_pos, paddle2_pos, paddle1_vel, paddle2_vel,l_score,r_score # these are floats
self.paddle1_pos[0] = [DoublePong.HALF_PAD_WIDTH - 1, self.HEIGHT//2]
self.paddle2_pos[0] = [self.WIDTH +1 - DoublePong.HALF_PAD_WIDTH, self.HEIGHT//2]
self.paddle1_pos[1] = [DoublePong.HALF_PAD_WIDTH - 1 + DoublePong.PAD_SPACE, self.HEIGHT//2]
self.paddle2_pos[1] = [self.WIDTH +1 - DoublePong.HALF_PAD_WIDTH - DoublePong.PAD_SPACE, self.HEIGHT//2]
self.l_score = 0
self.r_score = 0
self.counter = 0
self.reward = 0
for i in range(self.ball_num):
self.ball_init(i)
observation = []
for i in range(self.ball_num):
observation.append(self.ball_pos[i][0])
observation.append(self.ball_pos[i][1])
observation.append(self.ball_vel[i][0])
observation.append(self.ball_vel[i][1])
observation.append(self.paddle1_pos[0][0])
observation.append(self.paddle1_pos[0][1])
observation.append(self.paddle1_pos[1][0])
observation.append(self.paddle1_pos[1][1])
return observation
def ball_init(self, id):
self.ball_pos[id] = [self.WIDTH//2,self.HEIGHT//2]
horz = random.randrange(-4,4)
vert = random.randrange(-4,4)
# self.horz = self.horz + 1 if self.horz != 4 else 2
# self.vert = self.vert + 1 if self.vert != 3 else 0
if horz == 0:
horz = horz + 1
self.colorlist[id] = (random.randrange(0,255),random.randrange(0,255),random.randrange(0,255))
# if self.defense == 0:
# self.horz = - self.horz
# self.defense = 1 if self.defense == 0 else 0
self.ball_vel[id] = [horz, vert]
def step(self, action):
observation = []
reward = 0
done = False if self.counter < 10800 else True
info = []
self.counter += 1
action += 1
if action == 1:
self.paddle1_vel[0] = 12
elif action == 2:
self.paddle1_vel[0] = -12
elif action == 3:
self.paddle1_vel[1] = 12
elif action == 4:
self.paddle1_vel[1] = -12
if self.paddle1_pos[0][1] > DoublePong.HALF_PAD_HEIGHT and self.paddle1_pos[0][1] < self.HEIGHT - DoublePong.HALF_PAD_HEIGHT:
self.paddle1_pos[0][1] += self.paddle1_vel[0]
elif self.paddle1_pos[0][1] <= DoublePong.HALF_PAD_HEIGHT and self.paddle1_vel[0] > 0:
self.paddle1_pos[0][1] += self.paddle1_vel[0]
elif self.paddle1_pos[0][1] >= self.HEIGHT - DoublePong.HALF_PAD_HEIGHT and self.paddle1_vel[0] < 0:
self.paddle1_pos[0][1] += self.paddle1_vel[0]
if self.paddle1_pos[1][1] > DoublePong.HALF_PAD_HEIGHT and self.paddle1_pos[1][1] < self.HEIGHT - DoublePong.HALF_PAD_HEIGHT:
self.paddle1_pos[1][1] += self.paddle1_vel[1]
elif self.paddle1_pos[1][1] <= DoublePong.HALF_PAD_HEIGHT and self.paddle1_vel[1] > 0:
self.paddle1_pos[1][1] += self.paddle1_vel[1]
elif self.paddle1_pos[1][1] >= self.HEIGHT - DoublePong.HALF_PAD_HEIGHT and self.paddle1_vel[1] < 0:
self.paddle1_pos[1][1] += self.paddle1_vel[1]
if self.paddle2_pos[0][1] > DoublePong.HALF_PAD_HEIGHT and self.paddle2_pos[0][1] < self.HEIGHT - DoublePong.HALF_PAD_HEIGHT:
self.paddle2_pos[0][1] += self.paddle2_vel[0]
elif self.paddle2_pos[0][1] <= DoublePong.HALF_PAD_HEIGHT and self.paddle2_vel[0] > 0:
self.paddle2_pos[0][1] += self.paddle2_vel[0]
elif self.paddle2_pos[0][1] >= self.HEIGHT - DoublePong.HALF_PAD_HEIGHT and self.paddle2_vel[0] < 0:
self.paddle2_pos[0][1] += self.paddle2_vel[0]
if self.paddle2_pos[1][1] > DoublePong.HALF_PAD_HEIGHT and self.paddle2_pos[1][1] < self.HEIGHT - DoublePong.HALF_PAD_HEIGHT:
self.paddle2_pos[1][1] += self.paddle2_vel[1]
elif self.paddle2_pos[1][1] <= DoublePong.HALF_PAD_HEIGHT and self.paddle2_vel[1] > 0:
self.paddle2_pos[1][1] += self.paddle2_vel[1]
elif self.paddle2_pos[1][1] >= self.HEIGHT - DoublePong.HALF_PAD_HEIGHT and self.paddle2_vel[1] < 0:
self.paddle2_pos[1][1] += self.paddle2_vel[1]
self.paddle1_vel = [0, 0]
#update ball
for i in range(self.ball_num):
self.ball_pos[i][0] += int(self.ball_vel[i][0])
self.ball_pos[i][1] += int(self.ball_vel[i][1])
#ball collision check on top and bottom walls
for i in range(self.ball_num):
if int(self.ball_pos[i][1]) <= DoublePong.BALL_RADIUS:
self.ball_vel[i][1] = - self.ball_vel[i][1]
if int(self.ball_pos[i][1]) >= self.HEIGHT + 1 - DoublePong.BALL_RADIUS:
self.ball_vel[i][1] = -self.ball_vel[i][1]
#ball collison check on gutters or paddles
for i in range(self.ball_num):
for q in range(2):
if int(self.ball_pos[i][0]) + DoublePong.BALL_RADIUS + self.ball_vel[i][0] >= self.paddle1_pos[q][0] - DoublePong.PAD_WIDTH and int(self.ball_pos[i][0]) <= self.paddle1_pos[q][0] - DoublePong.PAD_WIDTH and\
int(self.ball_pos[i][1]) >= self.paddle1_pos[q][1] - DoublePong.HALF_PAD_HEIGHT - DoublePong.BALL_RADIUS and int(self.ball_pos[i][1]) <= self.paddle1_pos[q][1] + DoublePong.HALF_PAD_HEIGHT + DoublePong.BALL_RADIUS and self.ball_vel[i][0] > 0:
self.ball_vel[i][0] = self.ball_vel[i][0] * -1 - 1
self.ball_vel[i][1] += 1 if self.ball_vel[i][1] > 0 else -1
reward -= 5 + abs(self.ball_vel[i][0])
elif int(self.ball_pos[i][0]) - DoublePong.BALL_RADIUS + self.ball_vel[i][0] <= self.paddle1_pos[q][0] + DoublePong.PAD_WIDTH and int(self.ball_pos[i][0]) >= self.paddle1_pos[q][0] + DoublePong.PAD_WIDTH and\
int(self.ball_pos[i][1]) >= self.paddle1_pos[q][1] - DoublePong.HALF_PAD_HEIGHT - DoublePong.BALL_RADIUS and int(self.ball_pos[i][1]) <= self.paddle1_pos[q][1] + DoublePong.HALF_PAD_HEIGHT + DoublePong.BALL_RADIUS and self.ball_vel[i][0] < 0:
self.ball_vel[i][0] = self.ball_vel[i][0] * -1 + 1
self.ball_vel[i][1] += 1 if self.ball_vel[i][1] > 0 else -1
reward += 5 + abs(self.ball_vel[i][0])
elif int(self.ball_pos[i][0]) <= DoublePong.BALL_RADIUS + DoublePong.PAD_WIDTH:
self.r_score += 1
reward -= 10
self.ball_init(i)
for i in range(self.ball_num):
for q in range(2):
if int(self.ball_pos[i][0]) + DoublePong.BALL_RADIUS + self.ball_vel[i][0] >= self.paddle2_pos[q][0] - DoublePong.PAD_WIDTH and self.ball_pos[i][0] <= self.paddle2_pos[q][0] - DoublePong.PAD_WIDTH and\
random.randrange(1,4) <= 2:
# int(self.ball_pos[i][1]) >= self.paddle2_pos[q][1] - DoublePong.HALF_PAD_HEIGHT - DoublePong.BALL_RADIUS and int(self.ball_pos[i][1]) <= self.paddle2_pos[q][1] + DoublePong.HALF_PAD_HEIGHT and self.ball_vel[i][0] > 0:
self.ball_vel[i][0] = self.ball_vel[i][0] * -1 - 1
self.ball_vel[i][1] += 1 if self.ball_vel[i][1] > 0 else -1
elif int(self.ball_pos[i][0]) - DoublePong.BALL_RADIUS + self.ball_vel[i][0] <= self.paddle2_pos[q][0] + DoublePong.PAD_WIDTH and self.ball_pos[i][0] >= self.paddle2_pos[q][0] + DoublePong.PAD_WIDTH and\
int(self.ball_pos[i][1]) >= self.paddle2_pos[q][1] - DoublePong.HALF_PAD_HEIGHT - DoublePong.BALL_RADIUS and int(self.ball_pos[i][1]) <= self.paddle2_pos[q][1] + DoublePong.HALF_PAD_HEIGHT and self.ball_vel[i][0] < 0:
self.ball_vel[i][0] = self.ball_vel[i][0] * -1 + 1
self.ball_vel[i][1] += 1 if self.ball_vel[i][1] > 0 else -1
elif int(self.ball_pos[i][0]) >= self.WIDTH + 1 - DoublePong.BALL_RADIUS - DoublePong.PAD_WIDTH:
self.l_score += 1
self.ball_init(i)
observation = []
for i in range(self.ball_num):
observation.append(int(self.ball_pos[i][0]))
observation.append(int(self.ball_pos[i][1]))
observation.append(int(self.ball_vel[i][0]))
observation.append(int(self.ball_vel[i][1]))
observation.append(int(self.paddle1_pos[0][0]))
observation.append(int(self.paddle1_pos[0][1]))
observation.append(int(self.paddle1_pos[1][0]))
observation.append(int(self.paddle1_pos[1][1]))
info = {"l_score":self.l_score, "r_score":self.r_score}
self.reward += reward
return [observation, reward, done, info]
def render(self):
if self.first_show:
pygame.init()
self.window = pygame.display.set_mode((self.WIDTH, self.HEIGHT), 0, 32)
pygame.display.set_caption('Hello World')
self.first_show = False
pygame.event.get()
self.window.fill(DoublePong.BLACK)
pygame.draw.line(self.window, self.WHITE, [self.WIDTH // 2, 0],[self.WIDTH // 2, self.HEIGHT], 1)
pygame.draw.line(self.window, self.WHITE, [DoublePong.PAD_WIDTH, 0],[DoublePong.PAD_WIDTH, self.HEIGHT], 1)
pygame.draw.line(self.window, self.WHITE, [self.WIDTH - DoublePong.PAD_WIDTH, 0],[self.WIDTH - DoublePong.PAD_WIDTH, self.HEIGHT], 1)
pygame.draw.circle(self.window, self.WHITE, [self.WIDTH//2, self.HEIGHT//2], 70, 1)
for i in range(self.ball_num):
pygame.draw.rect(self.window, self.colorlist[i], [self.ball_pos[i][0]-DoublePong.BALL_RADIUS, self.ball_pos[i][1]-DoublePong.BALL_RADIUS, DoublePong.BALL_RADIUS * 2, DoublePong.BALL_RADIUS * 2], 2)
pygame.draw.polygon(self.window, DoublePong.GREEN, [[self.paddle1_pos[0][0] - DoublePong.HALF_PAD_WIDTH, self.paddle1_pos[0][1] - DoublePong.HALF_PAD_HEIGHT], [self.paddle1_pos[0][0] - DoublePong.HALF_PAD_WIDTH, self.paddle1_pos[0][1] + DoublePong.HALF_PAD_HEIGHT], [self.paddle1_pos[0][0] + DoublePong.HALF_PAD_WIDTH, self.paddle1_pos[0][1] + DoublePong.HALF_PAD_HEIGHT], [self.paddle1_pos[0][0] + DoublePong.HALF_PAD_WIDTH, self.paddle1_pos[0][1] - DoublePong.HALF_PAD_HEIGHT]], 0)
pygame.draw.polygon(self.window, DoublePong.GREEN, [[self.paddle2_pos[0][0] - DoublePong.HALF_PAD_WIDTH, self.paddle2_pos[0][1] - DoublePong.HALF_PAD_HEIGHT], [self.paddle2_pos[0][0] - DoublePong.HALF_PAD_WIDTH, self.paddle2_pos[0][1] + DoublePong.HALF_PAD_HEIGHT], [self.paddle2_pos[0][0] + DoublePong.HALF_PAD_WIDTH, self.paddle2_pos[0][1] + DoublePong.HALF_PAD_HEIGHT], [self.paddle2_pos[0][0] + DoublePong.HALF_PAD_WIDTH, self.paddle2_pos[0][1] - DoublePong.HALF_PAD_HEIGHT]], 0)
pygame.draw.polygon(self.window, DoublePong.GREEN, [[self.paddle1_pos[1][0] - DoublePong.HALF_PAD_WIDTH, self.paddle1_pos[1][1] - DoublePong.HALF_PAD_HEIGHT], [self.paddle1_pos[1][0] - DoublePong.HALF_PAD_WIDTH, self.paddle1_pos[1][1] + DoublePong.HALF_PAD_HEIGHT], [self.paddle1_pos[1][0] + DoublePong.HALF_PAD_WIDTH, self.paddle1_pos[1][1] + DoublePong.HALF_PAD_HEIGHT], [self.paddle1_pos[1][0] + DoublePong.HALF_PAD_WIDTH, self.paddle1_pos[1][1] - DoublePong.HALF_PAD_HEIGHT]], 0)
pygame.draw.polygon(self.window, DoublePong.GREEN, [[self.paddle2_pos[1][0] - DoublePong.HALF_PAD_WIDTH, self.paddle2_pos[1][1] - DoublePong.HALF_PAD_HEIGHT], [self.paddle2_pos[1][0] - DoublePong.HALF_PAD_WIDTH, self.paddle2_pos[1][1] + DoublePong.HALF_PAD_HEIGHT], [self.paddle2_pos[1][0] + DoublePong.HALF_PAD_WIDTH, self.paddle2_pos[1][1] + DoublePong.HALF_PAD_HEIGHT], [self.paddle2_pos[1][0] + DoublePong.HALF_PAD_WIDTH, self.paddle2_pos[1][1] - DoublePong.HALF_PAD_HEIGHT]], 0)
#update scores
myfont1 = pygame.font.SysFont("Comic Sans MS", 20)
label1 = myfont1.render("Score "+str(self.l_score), 1, (255,255,0))
self.window.blit(label1, (10,20))
myfont2 = pygame.font.SysFont("Comic Sans MS", 20)
label2 = myfont2.render("Score "+str(self.r_score), 1, (255,255,0))
self.window.blit(label2, (100, 20))
myfont3 = pygame.font.SysFont("Comic Sans MS", 20)
label2 = myfont3.render("reward "+str(int(self.reward)), 1, (255,255,0))
self.window.blit(label2, (190, 20))
pygame.display.update()
def close(self):
if not self.first_show:
pygame.quit()
# env = DoublePong()
# fps = pygame.time.Clock()
# while True:
# st = env.reset()
# done = False
# while not done:
# action = random.randrange(0,6)
# observation, reward, done, info = env.step(action)
# env.render()
# fps.tick(60)