-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpong.py
More file actions
154 lines (109 loc) · 3.42 KB
/
Copy pathpong.py
File metadata and controls
154 lines (109 loc) · 3.42 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
143
144
145
146
147
148
149
150
151
152
153
154
from turtle import Turtle, Screen
screen = Screen()
screen.bgcolor('black')
screen.setup(width = 800, height = 600)
screen.title('Pong')
score = Turtle()
left_score = 0
right_score = 0
score.color('white')
score.hideturtle()
score.teleport(-100,200)
score.write(left_score,align='center',font=('Courier',80,'normal'))
score.teleport(100,200)
score.write(right_score,align='center',font=('Courier',80,'normal'))
left_tim = Turtle()
left_tim.shape('square')
left_tim.color('white')
left_tim.speed('fastest')
left_tim.setheading(90)
left_tim.shapesize(stretch_wid=1, stretch_len=5)
left_tim.teleport(x=-350, y=0)
right_tim = Turtle()
right_tim.shape('square')
right_tim.color('white')
right_tim.speed('fastest')
right_tim.shapesize(stretch_wid=1, stretch_len=5)
right_tim.teleport(x=350, y=0)
right_tim.setheading(90)
def up():
left_tim.penup()
if left_tim.ycor() > 230:
return
left_tim.forward(20)
def down():
left_tim.penup()
if left_tim.ycor() < -230:
return
left_tim.backward(20)
def other_up():
right_tim.penup()
if right_tim.ycor() > 230:
return
right_tim.forward(20)
def other_down():
right_tim.penup()
if right_tim.ycor() < -230:
return
right_tim.backward(20)
ball = Turtle()
ball.shape('turtle')
ball.color('turquoise')
ball.speed('slowest')
ball.penup()
ball.setheading(30)
keep_moving = True
screen.listen()
#for left side
screen.onkeypress(fun = up, key= 'w')
screen.onkeypress(fun = down, key= 's')
#for right side
screen.onkeypress(fun = other_up, key= 'Up')
screen.onkeypress(fun = other_down, key= 'Down')
def move():
right_score = 0
left_score = 0
while keep_moving:
ball.forward(10)
if ball.xcor() > 330 and abs(ball.ycor() - right_tim.ycor()) < 50:
ball.speed('fastest')
ball.setheading(180 - ball.heading())
ball.speed('slowest')
if ball.xcor() < -330 and abs(ball.ycor() - left_tim.ycor()) < 50:
ball.speed('fastest')
ball.setheading(180 - ball.heading())
ball.speed('slowest')
if ball.ycor() > 280 or ball.ycor() < -280:
ball.speed('fastest')
ball.setheading(-ball.heading())
ball.speed('slowest')
if ball.xcor() > 390:
print('Round over for Player 2')
score.clear()
left_tim.teleport(x=-350, y=0)
right_tim.teleport(x=350, y=0)
left_score += 1
score.teleport(-100, 200)
score.write(left_score, align='center', font=('Courier', 80, 'normal'))
score.teleport(100, 200)
score.write(right_score, align='center', font=('Courier', 80, 'normal'))
ball.speed('fastest')
ball.teleport(0,0)
ball.setheading(210)
ball.speed('slowest')
if ball.xcor() < -380:
print('Round over for Player 1')
score.clear()
left_tim.teleport(x=-350, y=0)
right_tim.teleport(x=350, y=0)
right_score += 1
score.teleport(100, 200)
score.write(right_score, align='center', font=('Courier', 80, 'normal'))
score.teleport(-100, 200)
score.write(left_score, align='center', font=('Courier', 80, 'normal'))
ball.speed('fastest')
ball.teleport(0, 0)
ball.setheading(30)
ball.speed('slowest')
move()
screen.exitonclick()