import turtle as t
**
game = t.Screen()
game.title("打乒乓")
game.bgcolor("black")
game.setup(800,600)
game.tracer(0)
xm = t.Turtle()
xm.ht()
xm.up()
xm.speed(0)
xm.color('yellow')
xm.shape('square')
xm.shapesize(5, 1)
xm.goto(-350, 0)
xm.st()
ruhua = t.Turtle()
ruhua.ht()
ruhua.up()
ruhua.speed(0)
ruhua.color('white')
ruhua.shape('square')
ruhua.shapesize(5, 1)
ruhua.goto(350, 0)
ruhua.st()
pp = t.Turtle()
pp.up()
pp.speed(0)
pp.color('white')
pp.shape('circle')
pp.st()
pp.dx = 2
pp.dy = 2
player_speed = 10
xm_score = 0
ruhua_score = 0
def write_score():
pen.clear()
score_text = "小明:{} 如花:{}".format(xm_score, ruhua_score)
pen.write(score_text, align="center", font=("Arial", 20, 'bold'))
pen = t.Turtle()
pen.ht()
pen.up()
pen.color('white')
pen.goto(-30, 250)
write_score()
def xm_up():
y = xm.ycor()
y = y + player_speed
xm.sety(y)
def xm_down():
y = xm.ycor()
y = y - player_speed
xm.sety(y)
def ruhua_up():
y = ruhua.ycor()
y = y + player_speed
ruhua.sety(y)
def ruhua_down():
y = ruhua.ycor()
y = y - player_speed
ruhua.sety(y)
game.listen()
game.onkey(xm_up, 's')
game.onkey(xm_down, 'x')
game.onkey(ruhua_up, 'Up')
game.onkey(ruhua_down, 'Down')
while True:
game.update()
pp.setx(pp.xcor() + pp.dx)
pp.sety(pp.ycor() + pp.dy)
if(pp.ycor() > 290) or (pp.ycor() < -290):
pp.dy *= -1
y_up = ruhua.ycor()+50
y_down = ruhua.ycor()-50
if(pp.ycor() < y_up and pp.ycor() > y_down and pp.xcor() > 340):
pp.dx *= -1
pp.setx(339)
if(pp.ycor() < xm.ycor() + 50 and pp.ycor() > xm.ycor() - 50 and pp.xcor() < -340):
pp.dx *= -1
pp.setx(-339)
if (pp.xcor() > 380):
pp.goto(0,0)
xm_score += 1
print("小明得分!")
write_score()
if (pp.xcor() < -380):
pp.goto(0,0)
ruhua_score += 1
print("如花得分!")
write_score()
game.mainloop()