打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
用Python实现智能乒乓球游戏!
userphoto

2023.06.13 广西

关注

来源丨网络

本文提供了一个 Python 实现的乒乓球游戏代码,你只需要将代码复制并粘贴到编辑器中即可。
你好,亲爱的 Python 爱好者,今天我想分享一下如何在 Python 中编写一个带有自定义机器人 AI 的乒乓球游戏。我们的挑战是制作两个用户,第一个用户是你,第二个用户是精准度达到 100% 的AI机器人。


Step 1 导入 turtle 和 Screen

# Step 1 import set up turtle and Screenimport turtleimport randoms = turtle.Screen()s.title('Pong')s.bgcolor('black')s.setup(width=600, height=400)

Step 2 创建一个球

# Step 2 Create ballball = turtle.Turtle()ball.speed(0)ball.shape('circle')ball.color('white')ball.penup()ball.goto(0, 0)ball.dx = 4ball.dy = 4

Step 3 创建一个 AI 挡板

# Step 3 Create AI paddleai = turtle.Turtle()ai.speed(0)ai.shape('square')ai.color('white')ai.penup()ai.goto(-250, 0)ai.shapesize(stretch_wid=5, stretch_len=1)

Step 4 创建自己的挡板

# Step 4 Create a paddle For Youyou = turtle.Turtle()you.speed(0)you.shape('square')you.color('white')you.penup()you.goto(250, 0)you.shapesize(stretch_wid=5, stretch_len=1)

Step 5 创建移动AI挡板的函数

# Step 5 Create Function to move AI paddledef move_ai_paddle(): y = ball.ycor() if y > 0: ai.sety(ai.ycor() + 2) else: ai.sety(ai.ycor() - 2)

Step 6 创建一个函数以移动你的挡板并用键盘控制它

# Step 6 Create a Function to move the your paddle with up and down keydef paddle2_up():    y = you.ycor()    y += 20    you.sety(y)
def paddle2_down(): y = you.ycor() y -= 20 you.sety(y)# Your Paddle control it with keys.listen()s.onkeypress(paddle2_up, 'Up')s.onkeypress(paddle2_down, 'Down')Step 7 使用 while 循环开始游戏# Step 7 Start the game with a while loopwhile True: s.update() # Move the ball ball.setx(ball.xcor() + ball.dx) ball.sety(ball.ycor() + ball.dy) # Check for collisions with the walls if ball.ycor() > 190 or ball.ycor() < -190: ball.dy *= -1 # Move the robot paddle towards the ball if ball.ycor() > ai.ycor(): ai.sety(ai.ycor() + 4) elif ball.ycor() < ai.ycor(): ai.sety(ai.ycor() - 4) # Check for end game conditions if ball.xcor() > 300: turtle.textinput('Game End', 'You Loss Pong Game With AI!') break if ball.xcor() < -300: turtle.textinput('Game End', 'You Win Pong Game With AI!') break # Check for collisions with the robot paddle if (ball.xcor() < -240 and ball.xcor() > -250) and (ball.ycor() < ai.ycor() + 40 and ball.ycor() > ai.ycor() - 40): if random.random() < 0.9: # 90% chance of collision ball.dx *= -1 # Check for collisions with the user paddle if (ball.xcor() > 240 and ball.xcor() < 250) and (ball.ycor() < you.ycor() + 40 and ball.ycor() > you.ycor() - 40): ball.dx *= -1

全部代码

# Step 1 import set up turtle and Screenimport turtleimport randoms = turtle.Screen()s.title('Pong')s.bgcolor('black')s.setup(width=600, height=400)
# Step 2 Create ballball = turtle.Turtle()ball.speed(0)ball.shape('circle')ball.color('white')ball.penup()ball.goto(0, 0)ball.dx = 4ball.dy = 4
# Step 3 Create AI paddleai = turtle.Turtle()ai.speed(0)ai.shape('square')ai.color('white')ai.penup()ai.goto(-250, 0)ai.shapesize(stretch_wid=5, stretch_len=1)
# Step 4 Create a paddle For Youyou = turtle.Turtle()you.speed(0)you.shape('square')you.color('white')you.penup()you.goto(250, 0)you.shapesize(stretch_wid=5, stretch_len=1)
# Step 5 Create Function to move AI paddledef move_ai_paddle(): y = ball.ycor() if y > 0: ai.sety(ai.ycor() + 2) else: ai.sety(ai.ycor() - 2)
# Step 6 Create a Function to move the your paddledef paddle2_up(): y = you.ycor() y += 20 you.sety(y)
def paddle2_down(): y = you.ycor() y -= 20 you.sety(y)# Your Paddle control it with keys.listen()s.onkeypress(paddle2_up, 'Up')s.onkeypress(paddle2_down, 'Down')
# Step 7 Start the game with a while loopwhile True: s.update() # Move the ball ball.setx(ball.xcor() + ball.dx) ball.sety(ball.ycor() + ball.dy) # Check for collisions with the walls if ball.ycor() > 190 or ball.ycor() < -190: ball.dy *= -1 # Move the robot paddle towards the ball if ball.ycor() > ai.ycor(): ai.sety(ai.ycor() + 4) elif ball.ycor() < ai.ycor(): ai.sety(ai.ycor() - 4) # Check for end game conditions if ball.xcor() > 300: turtle.textinput('Game End', 'You Loss Pong Game With AI!') break if ball.xcor() < -300: turtle.textinput('Game End', 'You Win Pong Game With AI!') break # Check for collisions with the robot paddle if (ball.xcor() < -240 and ball.xcor() > -250) and (ball.ycor() < ai.ycor() + 40 and ball.ycor() > ai.ycor() - 40): if random.random() < 0.9: # 90% chance of collision ball.dx *= -1 # Check for collisions with the user paddle if (ball.xcor() > 240 and ball.xcor() < 250) and (ball.ycor() < you.ycor() + 40 and ball.ycor() > you.ycor() - 40): ball.dx *= -1 turtle.exitonclick()

万水千山总是情,点个 👍 行不行

EOF

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Python|一男子竟然用python干这事儿
什么是深度学习?
Turtle海龟库:python3内置简单却很强大的绘图库
You can play football well 教学设计
圣德医疗完成近亿元A轮融资,加速创新“心血管器械+AI+服务”产业闭环落地
小学英语人教新起点课标版 一年级起点一年级下册Unit3 Toys Lesson 1教学设计
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服