-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong.py
273 lines (218 loc) · 6.88 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#we are going to make a pong game
import turtle
import os
import tkinter as tk
import random
import random
wn = turtle.Screen()
wn.title("Pong by @jarlen")
wn.bgcolor("black")
wn.setup(width=800, height=600)
wn.tracer(0)
initial_dx = 3
initial_dy = -3
#score
score_a = 0
score_b = 0
#paddle a
paddle_a = turtle.Turtle()
paddle_a.speed(0) #speed of animation
paddle_a.shape("square")
paddle_a.color("white")
paddle_a.shapesize(stretch_wid=5,stretch_len=0.3) #default size is 20px by 20px
paddle_a.penup()
paddle_a.goto(-350,0)
#paddle b
paddle_b = turtle.Turtle()
paddle_b.speed(0) #speed of animation
paddle_b.shape("square")
paddle_b.color("white")
paddle_b.shapesize(stretch_wid=5,stretch_len=0.3) #default size is 20px by 20px
paddle_b.penup()
paddle_b.goto(-350,0)
#Initial ball speed
#ball
ball = turtle.Turtle()
ball.speed(9)
ball.shape("square")
ball.shapesize(stretch_wid=0.5,stretch_len=0.5) #default size is 20px by 20px
ball.color("white")
ball.penup()
ball.goto(0,0)
# Randomize initial direction
ball.dx = initial_dx * random.choice([1, -1])
ball.dy = initial_dy * random.choice([1, -1])
#pen
pen = turtle.Turtle()
pen.speed(0)
pen.color("white")
pen.penup()
pen.hideturtle() #hides the turtle
pen.goto(0,260)
pen.write("Player A: 0 Player B: 0", align="center", font=("Courier",24,"normal"))
#functions
def paddle_a_up():
y = paddle_a.ycor()
if y < 250:
y += 5
paddle_a.sety(y)
def paddle_a_down():
y = paddle_a.ycor()
if y > -250:
y -= 5
paddle_a.sety(y)
def paddle_b_up():
y = paddle_b.ycor()
if y < 250:
y += 5
paddle_b.sety(y)
def paddle_b_down():
y = paddle_b.ycor()
if y > -250:
y -= 5
paddle_b.sety(y)
paddle_b.goto(350, 0)
#keyboard binding
wn.listen() #listen for keyboard input
wn.onkeypress(paddle_a_up,"w") #when user presses w, call function paddle_a_up
wn.onkeypress(paddle_a_down,"s")
wn.onkeypress(paddle_b_up,"Up") #when user presses w, call function paddle_a_up
wn.onkeypress(paddle_b_down,"Down")
paddle_a_up_key = False
paddle_a_down_key = False
paddle_b_up_key = False
paddle_b_down_key = False
# Functions to set the state of the keys
def paddle_b_up_press():
global paddle_b_up_key
paddle_b_up_key = True
def paddle_b_up_release():
global paddle_b_up_key
paddle_b_up_key = False
def paddle_b_down_press():
global paddle_b_down_key
paddle_b_down_key = True
def paddle_b_down_release():
global paddle_b_down_key
paddle_b_down_key = False
# Functions to set the state of the keys
def paddle_a_up_press():
global paddle_a_up_key
paddle_a_up_key = True
def paddle_a_up_release():
global paddle_a_up_key
paddle_a_up_key = False
def paddle_a_down_press():
global paddle_a_down_key
paddle_a_down_key = True
def paddle_a_down_release():
global paddle_a_down_key
paddle_a_down_key = False
# Bind the new functions to the key events
wn.listen()
wn.onkeypress(paddle_a_up_press, "w")
wn.onkeyrelease(paddle_a_up_release, "w")
wn.onkeypress(paddle_a_down_press, "s")
wn.onkeyrelease(paddle_a_down_release, "s")
wn.onkeypress(paddle_b_up_press, "Up")
wn.onkeyrelease(paddle_b_up_release, "Up")
wn.onkeypress(paddle_b_down_press, "Down")
wn.onkeyrelease(paddle_b_down_release, "Down")
def reset_game():
global score_a, score_b
score_a = 0
score_b = 0
ball.goto(0, 0)
ball.dx = 2
ball.dy = -2
pen.clear()
wn.listen()
wn.onkeypress(reset_game, "space")
#GAME LOOP
while True:
wn.update()
#move the ball
ball.setx(ball.xcor() + ball.dx) #setx is a turtle function
ball.sety(ball.ycor() + ball.dy)
if paddle_a_up_key:
paddle_a_up()
if paddle_a_down_key:
paddle_a_down()
if paddle_b_up_key:
paddle_b_up()
if paddle_b_down_key:
paddle_b_down()
#border checking
if ball.ycor() > 290: #top border
ball.sety(290)
ball.dy *= -1
#& allows the sound to play while the game is running
if ball.ycor() < -290: #bottom border
ball.sety(-290)
ball.dy *= -1
#& allows the sound to play while the game is running
if ball.xcor() > 390: #right border
ball.goto(0,0)
ball.dx = initial_dx * random.choice([1, -1]) # Reset ball speed
ball.dy = initial_dy * random.choice([1, -1]) # Reset ball speed
score_a += 1
pen.clear() #clears the previous score
pen.write("Player A: {} Player B: {}".format(score_a,score_b), align="center", font=("Courier",24,"normal"))
if ball.xcor() < -390: #left border
ball.goto(0,0)
ball.dx = -initial_dx * random.choice([1, -1]) # Reset ball speed
ball.dy = initial_dy * random.choice([1, -1]) # Reset ball speed
score_b += 1
pen.clear() #clears the previous score
pen.write("Player A: {} Player B: {}".format(score_a,score_b), align="center", font=("Courier",24,"normal"))
#paddle and ball collisions
if (ball.dx > 0) and (350 > ball.xcor() > 340) and (paddle_b.ycor() + 60 > ball.ycor() > paddle_b.ycor() - 60):
ball.color("blue")
ball.dx *= -1
ball.dx *= 1.05
ball.dy *= 1.05
elif (ball.dx < 0) and (-350 < ball.xcor() < -340) and (paddle_a.ycor() + 60 > ball.ycor() > paddle_a.ycor() - 60):
ball.color("red")
ball.dx *= -1
ball.dx *= 1.05
ball.dy *= 1.05
def predict_ball_position(ball):
# Calculate how much the ball will move horizontally until it reaches the paddle
distance_to_paddle = 350 - abs(ball.xcor())
# Calculate how much the ball will move vertically in this time
future_ball_movement = distance_to_paddle * ball.dy / abs(ball.dx)
# Predict the future y-position of the ball
future_ball_y = ball.ycor() + future_ball_movement
if abs(future_ball_y) > 290:
# Calculate how much the ball will move after bouncing
bounce = abs(future_ball_y) - 290
# If the ball is moving up, subtract the bounce from the top of the screen
if future_ball_y > 0:
future_ball_y = 290 - bounce
# If the ball is moving down, add the bounce to the bottom of the screen
else:
future_ball_y = -290 + bounce
return future_ball_y
# Missed ball paddle_b
if ball.xcor() > 390:
score_a += 1
ball.goto(0, 0)
ball.dx *= 1
# Missed ball paddle_a
if ball.xcor() < -390:
score_b += 1
ball.goto(0, 0)
ball.dx *= 1
#winning condition
if score_a == 5:
pen.clear()
pen.write("Player A wins!", align="center", font=("Courier",24,"normal"))
ball.goto(0, 0)
ball.dx *= 0
ball.dy *= 0
elif score_b == 5:
pen.clear()
pen.write("Player B wins!", align="center", font=("Courier",24,"normal"))
ball.goto(0, 0)
ball.dx *= 0
ball.dy *= 0