-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ping Pong.py
234 lines (189 loc) · 4.6 KB
/
Ping 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
from tkinter.constants import CENTER
from turtle import *
from time import *
time_start = perf_counter()
print(time_start)
wn = Screen()
wn.title("Pong")
wn.bgcolor("black")
wn.setup(800, 600)
wn.tracer(0)
Players = ['Player A', 'Player B']
game_over = Turtle()
game_over.speed(5)
game_over.color("white")
game_over.hideturtle()
game_over.penup()
game_over.goto(0, 0)
pen = Turtle()
pen.speed(0)
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
border = Turtle()
border.penup()
border.color("white")
border.setx(380)
border.right(90)
border.pendown()
border.fd(280)
border.right(90)
border.fd(770)
border.right(90)
border.fd(520)
border.rt(90)
border.fd(390)
border.rt(90)
border.fd(520)
border.bk(520)
border.lt(90)
border.fd(380)
border.rt(90)
border.fd(280)
border.hideturtle()
paddle_a = Turtle()
paddle_a.speed(0)
paddle_a.shape("square")
paddle_a.color("white")
paddle_a.shapesize(stretch_wid=5, stretch_len=1)
paddle_a.penup()
paddle_a.goto(-350, 0)
paddle_b = Turtle()
paddle_b.speed(0)
paddle_b.shape("square")
paddle_b.color("white")
paddle_b.shapesize(stretch_wid=5, stretch_len=1)
paddle_b.penup()
paddle_b.goto(350, 0)
ball = Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("white")
# ball.shapesize(stretch_wid=5, stretch_len=1)
ball.penup()
ball.goto(0, 0)
ball.dx = 0.5
ball.dy = 0.5
def paddle_a_up():
y = paddle_a.ycor()
if y <= 170:
y += 20
paddle_a.sety(y)
def paddle_a_down():
y = paddle_a.ycor()
if y >= -210:
y -= 20
paddle_a.sety(y)
def paddle_b_up():
y = paddle_b.ycor()
if y <= 170:
y += 20
paddle_b.sety(y)
def paddle_b_down():
y = paddle_b.ycor()
if y >= -210:
y -= 20
paddle_b.sety(y)
Score_a = 0
Score_b = 0
wn.listen()
wn.onkeypress(paddle_a_up, "w")
wn.onkeypress(paddle_a_down, "s")
wn.onkeypress(paddle_b_up, "Up")
wn.onkeypress(paddle_b_down, "Down")
pen.write(f"{Players[0]}: {Score_a} {Players[1]}: {Score_b}",
align=CENTER, font=("Courier", 24, "normal"))
num_a = 4
num_b = 4
Lives_a = [Turtle(), Turtle(), Turtle(), Turtle()]
Lives_b = [Turtle(), Turtle(), Turtle(), Turtle()]
def Life_a(n):
for i in Lives_a:
i.hideturtle()
for j, i in enumerate(Lives_a[:n]):
i.showturtle()
i.color("white")
i.shape("square")
i.shapesize(2, 1)
i.penup()
i.goto(270 + (30*j), 270)
def Life_b(n):
for i in Lives_b:
i.hideturtle()
for j, i in enumerate(Lives_b[:n]):
i.showturtle()
i.color("white")
i.shape("square")
i.shapesize(2, 1)
i.penup()
i.goto(-(270 + (30*j)), 270)
def GameOver(n):
ball.hideturtle()
paddle_a.hideturtle()
paddle_b.hideturtle()
game_over.write(f' GAME OVER\n{Players[n]} WINS', align=CENTER, font=(
"Courier", 36, "bold"))
border.clear()
# Main Game Loop
while True:
wn.update()
time_end = perf_counter()
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)
Life_a(num_a)
Life_b(num_b)
if ball.ycor() > 230:
ball.sety(230)
ball.dy *= -1
if ball.ycor() < -270:
ball.sety(-270)
ball.dy *= -1
if (ball.xcor() > 370):
ball.goto(0, 0)
time_start = time_end
if ball.dx < 0:
ball.dx = -0.5
else:
ball.dx = 0.5
if ball.dy < 0:
ball.dy = -0.5
else:
ball.dy = 0.5
ball.dx *= -1
num_a -= 1
if ball.xcor() < -380:
ball.goto(0, 0)
time_start = time_end
if ball.dx < 0:
ball.dx = -0.5
else:
ball.dx = 0.5
if ball.dy < 0:
ball.dy = -0.5
else:
ball.dy = 0.5
ball.dx *= -1
num_b -= 1
if ball.xcor() > 340 and ball.xcor() < 350 and (ball.ycor() < paddle_b.ycor() + 40 and ball.ycor() > paddle_b.ycor() - 40):
ball.dx *= -1
ball.setx(340)
Score_b += 1
pen.clear()
pen.write(f"Player A: {Score_a} Player B: {Score_b}",
align=CENTER, font=("Courier", 24, "normal"))
if ball.xcor() < -340 and ball.xcor() > -350 and (ball.ycor() < paddle_a.ycor() + 40 and ball.ycor() > paddle_a.ycor() - 40):
ball.dx *= -1
ball.setx(-340)
Score_a += 1
pen.clear()
pen.write(f"Player A: {Score_a} Player B: {Score_b}",
align=CENTER, font=("Courier", 24, "normal"))
if (num_a == 0):
GameOver(0)
if(num_b == 0):
GameOver(1)
if time_end - time_start >= 15:
ball.dx *= 1.25
ball.dy *= 1.25
time_start = time_end