-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong.py
221 lines (153 loc) · 5.64 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
210
211
212
213
214
215
216
217
218
import pygame
import random
#define variables for game
FPS = 60
#size of our window
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 400
#size of our paddle
PADDLE_WIDTH = 10
PADDLE_HEIGHT = 60
PADDLE_BUFFER = 10
#size of our ball
BALL_WIDTH = 10
BALL_HEIGHT = 10
#speed of our paddle & ball
PADDLE_SPEED = 2
BALL_X_SPEED = 3
BALL_Y_SPEED = 2
#RGB Colors paddle and ball
WHITE = (255, 255, 255)
BLACK = (0, 0, 0) #background
#initialize our screen
screen = pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HEIGHT))
def drawBall(ballXpos, ballYPos):
ball = pygame.Rect(ballXpos, ballYPos, BALL_WIDTH, BALL_HEIGHT)
pygame.draw.rect(screen,WHITE,ball)
def drawPaddle1(paddle1YPos):
paddle1=pygame.Rect(PADDLE_BUFFER,paddle1YPos, PADDLE_WIDTH,PADDLE_HEIGHT)
pygame.draw.rect(screen, WHITE, paddle1)
def drawPaddle2(paddle2YPos):
paddle2 = pygame.Rect(WINDOW_WIDTH - PADDLE_BUFFER - PADDLE_WIDTH, paddle2YPos,PADDLE_WIDTH,PADDLE_HEIGHT)
pygame.draw.rect(screen, WHITE, paddle2)
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]
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
#dont lete 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):
#if move down
if(paddle2YPos + PADDLE_HEIGHT/2 < ballYPos + BALL_HEIGHT/2):
paddle2YPos = paddle2YPos + PADDLE_SPEED
#if move up
if(paddle2YPos + PADDLE_HEIGHT/2 > ballYPos + BALL_HEIGHT/2):
paddle2YPos = paddle2YPos - PADDLE_SPEED
#dont lete it move off the screen!
if(paddle2YPos < 0):
paddle2YPos = 0
if(paddle2YPos > WINDOW_HEIGHT - PADDLE_HEIGHT):
paddle2YPos = WINDOW_HEIGHT - PADDLE_HEIGHT
return paddle2YPos
class PongGame:
def __init__(self):
#random no for initial direction of ball
num = random.randint(0,9)
#keep score
self.tally = 0
#initialize positions of our paddle
self.paddle1YPos = WINDOW_HEIGHT/2 - PADDLE_HEIGHT/2
self.paddle2YPos = WINDOW_HEIGHT/2 - PADDLE_HEIGHT/2
#ball direction definition
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
num = random.randint(0,9)
self.ballYPos = num*(WINDOW_HEIGHT - BALL_HEIGHT)/9
def getPresentFrame(self):
#for each frame call the event queue
pygame.event.pump()
#make background black
screen.fill(BLACK)
#draw our padddles
drawPaddle1(self.paddle1YPos)
drawPaddle2(self.paddle2YPos)
#draw ball
drawBall(self.ballXPos, self.ballYPos)
#get pixels
image_data = pygame.surfarray.array3d(pygame.display.get_surface())
#update the window
pygame.display.flip()
#return the screen data
return image_data
def getNextFrame(self, action):
pygame.event.pump()
score=0
screen.fill(BLACK)
self.paddle1YPos = updatePaddle1(action, self.paddle1YPos)
drawPaddle1(self.paddle1YPos)
self.paddle2YPos = updatePaddle2(self.paddle2YPos,self.ballYPos)
drawPaddle2(self.paddle2YPos)
#[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)
[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)
image_data = pygame.surfarray.array3d(pygame.display.get_surface())
pygame.display.flip()
self.tally=self.tally + score
print("Tally is " + str(self.tally))
return [score , image_data]