-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpingpong.py
117 lines (85 loc) · 2.67 KB
/
pingpong.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
import turtle
sc = turtle.Screen()
sc.title("Pong game")
sc.bgcolor("grey")
sc.setup(width=1000, height=600)
left_pad = turtle.Turtle()
left_pad.speed(0)
left_pad.shape("square")
left_pad.color("black")
left_pad.shapesize(stretch_wid=6, stretch_len=2)
left_pad.penup()
left_pad.goto(-400, 0)
right_pad = turtle.Turtle()
right_pad.speed(0)
right_pad.shape("square")
right_pad.color("black")
right_pad.shapesize(stretch_wid=6, stretch_len=2)
right_pad.penup()
right_pad.goto(400, 0)
hit_ball = turtle.Turtle()
hit_ball.speed(40)
hit_ball.shape("circle")
hit_ball.color("blue")
hit_ball.penup()
hit_ball.goto(0, 0)
hit_ball.dx = 5
hit_ball.dy = -5
left_player = 0
right_player = 0
sketch = turtle.Turtle()
sketch.speed(0)
sketch.color("blue")
sketch.penup()
sketch.hideturtle()
sketch.goto(0, 260)
sketch.write("Left_player : 0 Right_player: 0", align="center", font=("Courier", 24, "normal"))
def paddleaup():
y = left_pad.ycor()
y += 20
left_pad.sety(y)
def paddleadown():
y = left_pad.ycor()
y -= 20
left_pad.sety(y)
def paddlebup():
y = right_pad.ycor()
y += 20
right_pad.sety(y)
def paddlebdown():
y = right_pad.ycor()
y -= 20
right_pad.sety(y)
sc.listen()
sc.onkeypress(paddleaup, "e")
sc.onkeypress(paddleadown, "x")
sc.onkeypress(paddlebup, "Up")
sc.onkeypress(paddlebdown, "Down")
while True:
sc.update()
hit_ball.setx(hit_ball.xcor() + hit_ball.dx)
hit_ball.sety(hit_ball.ycor() + hit_ball.dy)
if hit_ball.ycor() > 280:
hit_ball.sety(280)
hit_ball.dy *= -1
if hit_ball.ycor() < -280:
hit_ball.sety(-280)
hit_ball.dy *= -1
if hit_ball.xcor() > 500:
hit_ball.goto(0, 0)
hit_ball.dy *= -1
left_player += 1
sketch.clear()
sketch.write("Left_player : {} Right_player: {}".format(left_player, right_player), align="center", font=("Courier", 24, "normal"))
if hit_ball.xcor() < -500:
hit_ball.goto(0, 0)
hit_ball.dy *= -1
right_player += 1
sketch.clear()
sketch.write("Left_player : {} Right_player: {}".format(left_player, right_player), align="center", font=("Courier", 24, "normal"))
if (hit_ball.xcor() > 360 and hit_ball.xcor() < 370) and (hit_ball.ycor() < right_pad.ycor() + 40 and hit_ball.ycor() > right_pad.ycor() - 40):
hit_ball.setx(360)
hit_ball.dx *= -1
if (hit_ball.xcor() < -360 and hit_ball.xcor() > -370) and (hit_ball.ycor() < left_pad.ycor() + 40 and hit_ball.ycor() > left_pad.ycor() - 40):
hit_ball.setx(-360)
hit_ball.dx *= -1