-
Notifications
You must be signed in to change notification settings - Fork 0
/
pong.py
209 lines (181 loc) · 6.94 KB
/
pong.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
import pygame #helps us make GUI games in python
import random #help us define which direction the ball will start moving in
#frame rate per second
FPS = 100
#size of our window
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 400
#size of our paddle
PADDLE_WIDTH = 10
PADDLE_HEIGHT = 60
#distance from the edge of the window
PADDLE_BUFFER = 10
#size of our ball
BALL_WIDTH = 10
BALL_HEIGHT = 10
#speeds of our paddle and ball
PADDLE_SPEED = 2
BALL_X_SPEED = 3
BALL_Y_SPEED = 2
#RGB colors for our paddle and ball
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
#initialize our screen using width and height vars
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
#Paddle 1 is our learning agent/us
#paddle 2 is the evil AI
#draw our ball
def drawBall(ballXPos, ballYPos):
#small rectangle, create it
ball = pygame.Rect(ballXPos, ballYPos, BALL_WIDTH, BALL_HEIGHT)
#draw it
pygame.draw.rect(screen, WHITE, ball)
def drawPaddle1(paddle1YPos):
#crreate it
paddle1 = pygame.Rect(PADDLE_BUFFER, paddle1YPos, PADDLE_WIDTH, PADDLE_HEIGHT)
#draw it
pygame.draw.rect(screen, WHITE, paddle1)
def drawPaddle2(paddle2YPos):
#create it, opposite side
paddle2 = pygame.Rect(WINDOW_WIDTH - PADDLE_BUFFER - PADDLE_WIDTH, paddle2YPos, PADDLE_WIDTH, PADDLE_HEIGHT)
#draw it
pygame.draw.rect(screen, WHITE, paddle2)
#update the ball, using the paddle posistions the balls positions and the balls directions
def updateBall(paddle1YPos, paddle2YPos, ballXPos, ballYPos, ballXDirection, ballYDirection):
#update the x and y position
ballXPos = ballXPos + ballXDirection * BALL_X_SPEED
ballYPos = ballYPos + ballYDirection * BALL_Y_SPEED
score = 0
#checks for a collision, if the ball hits the left side, our learning agent
if (
ballXPos <= PADDLE_BUFFER + PADDLE_WIDTH and ballYPos + BALL_HEIGHT >= paddle1YPos and ballYPos - BALL_HEIGHT <= paddle1YPos + PADDLE_HEIGHT):
#switches directions
ballXDirection = 1
#past it
elif (ballXPos <= 0):
#negative score
ballXDirection = 1
score = -1
return [score, paddle1YPos, paddle2YPos, ballXPos, ballYPos, ballXDirection, ballYDirection]
#check if hits the other side
if (
ballXPos >= WINDOW_WIDTH - PADDLE_WIDTH - PADDLE_BUFFER and ballYPos + BALL_HEIGHT >= paddle2YPos and ballYPos - BALL_HEIGHT <= paddle2YPos + PADDLE_HEIGHT):
#switch directions
ballXDirection = -1
#past it
elif (ballXPos >= WINDOW_WIDTH - BALL_WIDTH):
#positive score
ballXDirection = -1
score = 1
return [score, paddle1YPos, paddle2YPos, ballXPos, ballYPos, ballXDirection, ballYDirection]
#if it hits the top
#move down
if (ballYPos <= 0):
ballYPos = 0;
ballYDirection = 1;
#if it hits the bottom, move up
elif (ballYPos >= WINDOW_HEIGHT - BALL_HEIGHT):
ballYPos = WINDOW_HEIGHT - BALL_HEIGHT
ballYDirection = -1
return [score, paddle1YPos, paddle2YPos, ballXPos, ballYPos, ballXDirection, ballYDirection]
#update the paddle position
def updatePaddle1(action, paddle1YPos):
#if move up
if (action[1] == 1):
paddle1YPos = paddle1YPos - PADDLE_SPEED
#if move down
if (action[2] == 1):
paddle1YPos = paddle1YPos + PADDLE_SPEED
#don't let it move off the screen
if (paddle1YPos < 0):
paddle1YPos = 0
if (paddle1YPos > WINDOW_HEIGHT - PADDLE_HEIGHT):
paddle1YPos = WINDOW_HEIGHT - PADDLE_HEIGHT
return paddle1YPos
def updatePaddle2(paddle2YPos, ballYPos):
#move down if ball is in upper half
if (paddle2YPos + PADDLE_HEIGHT/2 < ballYPos + BALL_HEIGHT/2):
paddle2YPos = paddle2YPos + PADDLE_SPEED
#move up if ball is in lower half
if (paddle2YPos + PADDLE_HEIGHT/2 > ballYPos + BALL_HEIGHT/2):
paddle2YPos = paddle2YPos - PADDLE_SPEED
#don't let it hit top
if (paddle2YPos < 0):
paddle2YPos = 0
#dont let it hit bottom
if (paddle2YPos > WINDOW_HEIGHT - PADDLE_HEIGHT):
paddle2YPos = WINDOW_HEIGHT - PADDLE_HEIGHT
return paddle2YPos
#game class
class PongGame:
def __init__(self):
#random number for initial direction of ball
num = random.randint(0,9)
#keep score
self.tally = 0
#initialie positions of paddle
self.paddle1YPos = WINDOW_HEIGHT / 2 - PADDLE_HEIGHT / 2
self.paddle2YPos = WINDOW_HEIGHT / 2 - PADDLE_HEIGHT / 2
#and ball direction
self.ballXDirection = 1
self.ballYDirection = 1
#starting point
self.ballXPos = WINDOW_WIDTH/2 - BALL_WIDTH/2
#randomly decide where the ball will move
if(0 < num < 3):
self.ballXDirection = 1
self.ballYDirection = 1
if (3 <= num < 5):
self.ballXDirection = -1
self.ballYDirection = 1
if (5 <= num < 8):
self.ballXDirection = 1
self.ballYDirection = -1
if (8 <= num < 10):
self.ballXDirection = -1
self.ballYDirection = -1
#new random number
num = random.randint(0,9)
#where it will start, y part
self.ballYPos = num*(WINDOW_HEIGHT - BALL_HEIGHT)/9
#
def getPresentFrame(self):
#for each frame, calls the event queue, like if the main window needs to be repainted
pygame.event.pump()
#make the background black
screen.fill(BLACK)
#draw our paddles
drawPaddle1(self.paddle1YPos)
drawPaddle2(self.paddle2YPos)
#draw our ball
drawBall(self.ballXPos, self.ballYPos)
#copies the pixels from our surface to a 3D array. we'll use this for RL
image_data = pygame.surfarray.array3d(pygame.display.get_surface())
#updates the window
pygame.display.flip()
#return our surface data
return image_data
#update our screen
def getNextFrame(self, action):
pygame.event.pump()
score = 0
screen.fill(BLACK)
#update our paddle
self.paddle1YPos = updatePaddle1(action, self.paddle1YPos)
drawPaddle1(self.paddle1YPos)
#update evil AI paddle
self.paddle2YPos = updatePaddle2(self.paddle2YPos, self.ballYPos)
drawPaddle2(self.paddle2YPos)
#update our vars by updating ball position
[score, self.paddle1YPos, self.paddle2YPos, self.ballXPos, self.ballYPos, self.ballXDirection, self.ballYDirection] = updateBall(self.paddle1YPos, self.paddle2YPos, self.ballXPos, self.ballYPos, self.ballXDirection, self.ballYDirection)
#draw the ball
drawBall(self.ballXPos, self.ballYPos)
#get the surface data
image_data = pygame.surfarray.array3d(pygame.display.get_surface())
#update the window
pygame.display.flip()
#record the total score
self.tally = self.tally + score
print ("Tally is " + str(self.tally))
#return the score and the surface data
return [score, image_data]