-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathping_pong.py
More file actions
142 lines (110 loc) · 2.64 KB
/
ping_pong.py
File metadata and controls
142 lines (110 loc) · 2.64 KB
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
#Ping Pong game
import turtle
#Screen t
t = turtle.Screen()
t.title("Ping Pong")
t.setup( width = 1000, height = 600)
#Light or Dark Mode
info = turtle.textinput("Dark Mode","Y/N")
if info == "Y":
pad_col = "white"
scr_col = "black"
ball_col = "cyan"
else:
pad_col = "black"
scr_col = "white"
ball_col = "red"
t.bgcolor(scr_col)
#Right Paddle rp
rp = turtle.Turtle()
rp.speed(0)
rp.shape("square")
rp.shapesize(stretch_wid=5, stretch_len=1)
rp.color(pad_col)
rp.penup()
rp.goto(400, 0)
#Left Paddle lp
lp = turtle.Turtle()
lp.speed(0)
lp.shape("square")
lp.shapesize(stretch_wid=5, stretch_len=1)
lp.color(pad_col)
lp.penup()
lp.goto(-400, 0)
#Ball bl
bl = turtle.Turtle()
bl.speed(40)
bl.shape("circle")
bl.color(ball_col)
bl.penup()
bl.goto(0,0)
bl.dx = 5
bl.dy = -5
#Score
left_ply = 0
right_ply = 0
#Score Board sb
sb= turtle.Turtle()
sb.speed(0)
sb.color("blue")
sb.penup()
sb.hideturtle()
sb.goto(0, 260)
sb.write("Left Player : 0 RightPlayer : 0", align = "center", font=("Courier",15,"normal"))
#Paddle Move pd
def lpup():
a = lp.ycor()
a += 20
lp.sety(a)
def lpdown():
a = lp.ycor()
a -= 20
lp.sety(a)
def rpup():
a = rp.ycor()
a += 20
rp.sety(a)
def rpdown():
a = rp.ycor()
a -= 20
rp.sety(a)
#Keys
t.listen()
t.onkeypress(lpup, "w")
t.onkeypress(lpdown, "s")
t.onkeypress(rpup, "Up")
t.onkeypress(rpdown, "Down")
while True:
t.update()
bl.setx(bl.xcor()+bl.dx)
bl.sety(bl.ycor()+bl.dy)
#Boundary
#Vertical
if bl.ycor() > 300:
bl.sety(300)
bl.dy *= -1
if bl.ycor() < -300:
bl.sety(-300)
bl.dy *= -1
#Horizontal
if bl.xcor() > 500:
bl.goto(0,0)
bl.dy *= -1
left_ply += 1
sb.clear()
sb.write("Left Player : {} RightPlayer : {}".format(left_ply, right_ply),
align = "center", font=("Courier",15,"normal"))
if bl.xcor() < -500:
bl.goto(0,0)
bl.dy *= -1
right_ply += 1
sb.clear()
sb.write("Left Player : {} RightPlayer : {}".format(left_ply, right_ply),
align = "center", font=("Courier",15,"normal"))
#Paddle collision with ball
if(bl.xcor() > 360 and bl.xcor() < 370 )and (bl.ycor() < rp.ycor()+40 and bl.ycor()> rp.ycor()-40):
bl.setx(360)
bl.dx *= -1
if(bl.xcor() < -360 and bl.xcor() > -370 )and (bl.ycor() < lp.ycor()+40 and bl.ycor()> lp.ycor()-40):
bl.setx(-360)
bl.dx *= -1