-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain
More file actions
140 lines (115 loc) · 2.94 KB
/
Copy pathmain
File metadata and controls
140 lines (115 loc) · 2.94 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
import turtle
from random import choice, randint
window = turtle.Screen()
window.title("Ping-Pong")
window.setup(width=1.0,height=1.0)
window.bgcolor("black")
window.tracer(2)
border = turtle.Turtle()
border.speed(0)
border.color('green')
border.begin_fill()
border.goto(-500,300)
border.goto(500,300)
border.goto(500,-300)
border.goto(-500,-300)
border.goto(-500,300)
border.end_fill()
border.goto(0,300)
border.color('white')
border.setheading(270)
for i in range(25):
if i%2==0:
border.forward(24)
else:
border.up()
border.forward(24)
border.down()
border.hideturtle()
rocket_a = turtle.Turtle()
rocket_a.color('white')
rocket_a.shape('square')
rocket_a.shapesize(stretch_len=1,stretch_wid=5)
rocket_a.penup()
rocket_a.goto(-450,0)
rocket_b = turtle.Turtle()
rocket_b.speed(3)
rocket_b.shape("square")
rocket_b.color("white")
rocket_b.shapesize(stretch_wid=5, stretch_len=1)
rocket_b.penup()
rocket_b.goto(450, 0)
FONT = ("Arial", 44)
score_a = 0
s1 = turtle.Turtle(visible=False)
s1.color('white')
s1.penup()
s1.setposition(-200,300)
s1.write(score_a, font=FONT)
score_b = 0
s2 = turtle.Turtle(visible=False)
s2.color('white')
s2.penup()
s2.setposition(200,300)
s2.write(score_a, font=FONT)
def move_up():
y = rocket_a.ycor() + 10
if y > 250:
y = 250
rocket_a.sety(y)
def move_down():
y = rocket_a.ycor() - 10
if y < -250:
y = -250
rocket_a.sety(y)
def move_up_b():
y = rocket_b.ycor() + 10
if y > 250:
y = 250
rocket_b.sety(y)
def move_down_b():
y = rocket_b.ycor() - 10
if y < -250:
y = -250
rocket_b.sety(y)
ball = turtle.Turtle()
ball.shape('circle')
ball.speed(0)
ball.color('red')
ball.dx = 3
ball.dy = -3
ball.penup()
window.listen()
window.onkeypress(move_up, "w")
window.onkeypress(move_down, "s")
window.onkeypress(move_up_b, "Up")
window.onkeypress(move_down_b, "Down")
while True:
window.update()
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)
if ball.ycor() >= 290:
ball.dy = -ball.dy
if ball.ycor() <= -290:
ball.dy = -ball.dy
if ball.xcor() >= 490:
score_b +=1
s2.clear()
s2.write(score_b, font=FONT)
ball.goto(0,randint(-150,150))
ball.dx = choice([-4,-3,-2, 2,3,4])
ball.dy = choice([-4,-3,-2, 2,3,4])
if ball.xcor() <= -490:
score_a += 1
s1.clear()
s1.write(score_a, font=FONT)
ball.goto(0,randint(-150,150))
ball.dx = choice([-4,-3,-2, 2,3,4])
ball.dy = choice([-4,-3,-2, 2,3,4])
if ball.ycor() >= rocket_b.ycor()-50 and ball.ycor() <= rocket_b.ycor()+50 \
and ball.xcor() >= rocket_b.xcor()-5 and ball.xcor() <= rocket_b.xcor()+5:
ball.dx = -ball.dx
if ball.ycor() >= rocket_a.ycor()-50 and ball.ycor() <= rocket_a.ycor()+50 \
and ball.xcor() >= rocket_a.xcor()-5 and ball.xcor() <= rocket_a.xcor()+5:
ball.dx = -ball.dx
window.mainloop()