打乒乓小游戏代码150行可以运行可复制python
时间: 2023-07-31 10:11:21 浏览: 242
以下是一个简单的乒乓球小游戏的代码,使用 Python 语言编写,共计 150 行左右,可以运行并复制:
```python
import turtle
import time
import random
# 设置窗口大小和背景颜色
win = turtle.Screen()
win.title("Pong Game")
win.bgcolor("black")
win.setup(width=600, height=400)
# 绘制乒乓球场地
border = turtle.Turtle()
border.color("white")
border.speed(0)
border.penup()
border.goto(-290, 200)
border.pendown()
for side in range(2):
border.fd(580)
border.rt(90)
border.fd(400)
border.rt(90)
# 绘制乒乓球网
net = turtle.Turtle()
net.color("white")
net.speed(0)
net.penup()
net.goto(0, 200)
net.pendown()
net.rt(90)
for i in range(10):
net.fd(20)
net.penup()
net.fd(20)
net.pendown()
# 创建乒乓球
ball = turtle.Turtle()
ball.shape("circle")
ball.color("white")
ball.speed(40)
ball.penup()
ball.goto(0, 0)
ball.dx = 3
ball.dy = -3
# 创建左边球拍
paddle_l = turtle.Turtle()
paddle_l.color("white")
paddle_l.speed(0)
paddle_l.shape("square")
paddle_l.shapesize(stretch_wid=6, stretch_len=2)
paddle_l.penup()
paddle_l.goto(-250, 0)
# 创建右边球拍
paddle_r = turtle.Turtle()
paddle_r.color("white")
paddle_r.speed(0)
paddle_r.shape("square")
paddle_r.shapesize(stretch_wid=6, stretch_len=2)
paddle_r.penup()
paddle_r.goto(250, 0)
# 创建左边球拍移动函数
def paddle_l_up():
y = paddle_l.ycor()
y += 20
paddle_l.sety(y)
def paddle_l_down():
y = paddle_l.ycor()
y -= 20
paddle_l.sety(y)
# 创建右边球拍移动函数
def paddle_r_up():
y = paddle_r.ycor()
y += 20
paddle_r.sety(y)
def paddle_r_down():
y = paddle_r.ycor()
y -= 20
paddle_r.sety(y)
# 键盘绑定
win.listen()
win.onkeypress(paddle_l_up, "w")
win.onkeypress(paddle_l_down, "s")
win.onkeypress(paddle_r_up, "Up")
win.onkeypress(paddle_r_down, "Down")
# 设置得分
score_l = 0
score_r = 0
# 显示得分
pen = turtle.Turtle()
pen.speed(0)
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 170)
pen.write("Player 1: {} Player 2: {}".format(score_l, score_r), align="center", font=("Courier", 16, "normal"))
# 游戏循环
while True:
win.update()
# 移动乒乓球
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)
# 碰到上下边界反弹
if ball.ycor() > 190:
ball.sety(190)
ball.dy *= -1
if ball.ycor() < -190:
ball.sety(-190)
ball.dy *= -1
# 碰到左右边界得分
if ball.xcor() > 290:
score_l += 1
pen.clear()
pen.write("Player 1: {} Player 2: {}".format(score_l, score_r), align="center", font=("Courier", 16, "normal"))
ball.goto(0, 0)
ball.dx *= -1
if ball.xcor() < -290:
score_r += 1
pen.clear()
pen.write("Player 1: {} Player 2: {}".format(score_l, score_r), align="center", font=("Courier", 16, "normal"))
ball.goto(0, 0)
ball.dx *= -1
# 碰到球拍反弹
if (ball.xcor() > 240 and ball.xcor() < 250) and (ball.ycor() < paddle_r.ycor() + 60 and ball.ycor() > paddle_r.ycor() - 60):
ball.setx(240)
ball.dx *= -1
ball.dy = random.randint(-3, 3)
if (ball.xcor() < -240 and ball.xcor() > -250) and (ball.ycor() < paddle_l.ycor() + 60 and ball.ycor() > paddle_l.ycor() - 60):
ball.setx(-240)
ball.dx *= -1
ball.dy = random.randint(-3, 3)
```
这个乒乓球小游戏使用了 Python 的 turtle 库,可以通过键盘控制左右两个球拍的移动,如果乒乓球碰到了球拍,则会反弹,如果乒乓球碰到了上下边界,则也会反弹,如果乒乓球碰到了左右边界,则对应的玩家得分。游戏循环会一直运行,直到窗口被关闭。
阅读全文