最近在B站学python, 跟麦叔编程学着写了个双人乒乓球游戏, 自己做了一些简单改进, 代码简单容易上手, 不需要任何素材和外部函数库, 在此分享一下:
import turtle as t
# set the game window
game = t.Screen()
game.title("pingpang")
game.bgcolor("gray")
game.setup(800,800)
game.tracer(0)
step = 100
# define the Pingpang player externed from turtle.Turtle
class PingPang(t.Turtle):
def __init__(self, init_pos, name):
t.Turtle.__init__(self)
self.hideturtle()
self.up()
self.speed(0)
self.color("orange")
self.shape('square')
self.shapesize(5,1)
self.goto(init_pos)
self.showturtle()
self.name = name
self.init_print()
self.score = 0
self.y_vel = 0
self.accelerate = 0
def move_up(self):
#print("move_up_ing")
if self.ycor() < 400 - step:
#print("move_up_ed")
self.sety(self.ycor() + step)
def move_down(self):
if self.ycor() > -400 + step:
self.sety(self.ycor() - step)
def init_print(self):
print(f"{self.name} been constructed")
player1 = PingPang((-300, 0), "player1")
player2 = PingPang((300, 0), "player2")
#player1 move when press ↑↓
game.listen()
game.onkey(player1.move_up, "Up")
game.onkey(player1.move_down, "Down")
#player2 move when press w/s
game.onkey(player2.move_up, "w")
game.onkey(player2.move_down, "s")
#construct pingpang ball
ball = t.Turtle()
ball.up()
ball.speed(0)
ball.color("yellow")
ball.shape('circle')
ball.showturtle()
ball.dx = 0.5
ball.dy = 0.5
ball.ready_score = True
#draw the score of player1
pen = t.Turtle()
pen.color("orange")
pen.up()
pen.hideturtle()
pen.setpos(200,350)
pen.write("score: %s" % player2.score, False, align="left", font=("Arial", 20, 'normal'))
#draw the score of player2
pen2 = t.Turtle()
pen2.color("orange")
pen2.up()
pen2.hideturtle()
pen2.setpos(-200, 350)
pen2.write("score: %s" % player1.score, False, align="left", font=("Arial", 20, 'normal'))
while True:
game.update()
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)
#the rebound of ball
if(abs(ball.xcor()) > 290):
ball.dx *= -1
if(abs(ball.ycor()) > 400):
ball.dy *= -1
if( abs(ball.ycor() - player1.ycor()) <= 100 and abs(ball.xcor() - player1.xcor()) <= 20):
ball.dx *= -1
if (abs(ball.ycor() - player2.ycor()) <= 100 and abs(ball.xcor() - player2.xcor()) <= 20):
ball.dx *= -1
#score increase condition
if( abs(ball.ycor() - player1.ycor()) > 100 and abs(ball.xcor() - player1.xcor()) <= 20 and ball.ready_score):
player2.score += 1
pen.clear()
pen.write("score: %s" % player2.score, False, align="left", font=("Arial", 20, 'normal'))
ball.ready_score = False
if( abs(ball.ycor() - player2.ycor()) > 100 and abs(ball.xcor() - player2.xcor()) <= 20 and ball.ready_score):
player1.score += 1
pen2.clear()
pen2.write("score: %s" % player1.score, False, align="left", font=("Arial", 20, 'normal'))
ball.ready_score = False
if (not ( abs(ball.ycor() - player1.ycor()) > 100 and abs(ball.xcor() - player1.xcor()) <= 20)) and \
(not ( abs(ball.ycor() - player2.ycor()) > 100 and abs(ball.xcor() - player2.xcor()) <= 20)):
ball.ready_score = True
#always run the game
game.mainloop()
运行效果如下, 本来想改进乒乓球拍移动的方式, 让移动更顺畅一点, 可惜没搞明白加速度什么的, 只能简陋地运行了.