用Python实现智能乒乓球游戏!

2024-02-17 03:20

本文主要是介绍用Python实现智能乒乓球游戏!,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本文提供了一个 Python 实现的乒乓球游戏代码,你只需要将代码复制并粘贴到编辑器中即可。

你好,亲爱的 Python 爱好者,今天我想分享一下如何在 Python 中编写一个带有自定义机器人 AI 的乒乓球游戏。我们的挑战是制作两个用户,第一个用户是你,第二个用户是精准度达到 100% 的AI机器人。


Step 1

导入 turtle 和 Screen

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

Step 2

创建一个球

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

Step 3

创建一个 AI 挡板

# Step 3 Create AI paddle
ai = 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 You
you = 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 paddle
def 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 key
def paddle2_up():y = you.ycor()y += 20you.sety(y)def paddle2_down():y = you.ycor()y -= 20you.sety(y)
# Your Paddle control it with key
s.listen()
s.onkeypress(paddle2_up, "Up")
s.onkeypress(paddle2_down, "Down")
Step 7 使用 while 循环开始游戏
# Step 7 Start the game with a while loop
while True:s.update()# Move the ballball.setx(ball.xcor() + ball.dx)ball.sety(ball.ycor() + ball.dy)# Check for collisions with the wallsif ball.ycor() > 190 or ball.ycor() < -190:ball.dy *= -1# Move the robot paddle towards the ballif ball.ycor() > ai.ycor():ai.sety(ai.ycor() + 4)elif ball.ycor() < ai.ycor():ai.sety(ai.ycor() - 4)# Check for end game conditionsif ball.xcor() > 300:turtle.textinput("Game End", "You Loss Pong Game With AI!")breakif ball.xcor() < -300:turtle.textinput("Game End", "You Win Pong Game With AI!")break# Check for collisions with the robot paddleif (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 collisionball.dx *= -1# Check for collisions with the user paddleif (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 Screen
import turtle
import random
s = turtle.Screen()
s.title("Pong")
s.bgcolor("black")
s.setup(width=600, height=400)# Step 2 Create ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("white")
ball.penup()
ball.goto(0, 0)
ball.dx = 4
ball.dy = 4# Step 3 Create AI paddle
ai = 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 You
you = 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 paddle
def 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 paddle
def paddle2_up():y = you.ycor()y += 20you.sety(y)def paddle2_down():y = you.ycor()y -= 20you.sety(y)
# Your Paddle control it with key
s.listen()
s.onkeypress(paddle2_up, "Up")
s.onkeypress(paddle2_down, "Down")# Step 7 Start the game with a while loop
while True:s.update()# Move the ballball.setx(ball.xcor() + ball.dx)ball.sety(ball.ycor() + ball.dy)# Check for collisions with the wallsif ball.ycor() > 190 or ball.ycor() < -190:ball.dy *= -1# Move the robot paddle towards the ballif ball.ycor() > ai.ycor():ai.sety(ai.ycor() + 4)elif ball.ycor() < ai.ycor():ai.sety(ai.ycor() - 4)# Check for end game conditionsif ball.xcor() > 300:turtle.textinput("Game End", "You Loss Pong Game With AI!")breakif ball.xcor() < -300:turtle.textinput("Game End", "You Win Pong Game With AI!")break# Check for collisions with the robot paddleif (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 collisionball.dx *= -1# Check for collisions with the user paddleif (ball.xcor() > 240 and ball.xcor() < 250) and (ball.ycor() < you.ycor() + 40 and ball.ycor() > you.ycor() - 40):ball.dx *= -1turtle.exitonclick()

- END -

如果你对Python感兴趣,想要学习pyhton,这里给大家分享一份Python全套学习资料,里面的内容都是适合零基础小白的笔记和资料,超多实战案例,不懂编程也能听懂、看懂。

需要的话可以微信扫描下方二维码免费获得。

学习资源推荐

除了上述分享,如果你也喜欢编程,想通过学习Python获取更高薪资,这里给大家分享一份Python学习资料。

这里给大家展示一下我进的兼职群和最近接单的截图

兼职群

私单

😝朋友们如果有需要的话,可以V扫描下方二维码联系领取,也可以内推兼职群哦~

学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!

### 1.Python学习路线

image-20230619144606466

python学习路线图1

2.Python基础学习
01.开发工具

02.学习笔记

在这里插入图片描述

03.学习视频

在这里插入图片描述

3.Python小白必备手册

图片

4.数据分析全套资源

在这里插入图片描述

5.Python面试集锦
01.面试资料

在这里插入图片描述

在这里插入图片描述

02.简历模板

在这里插入图片描述

因篇幅有限,仅展示部分资料,添加上方即可获取👆

------ 🙇‍♂️ 本文转自网络,如有侵权,请联系删除 🙇‍♂️ ------

这篇关于用Python实现智能乒乓球游戏!的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/716521

相关文章

Vue ElementUI中Upload组件批量上传的实现代码

《VueElementUI中Upload组件批量上传的实现代码》ElementUI中Upload组件批量上传通过获取upload组件的DOM、文件、上传地址和数据,封装uploadFiles方法,使... ElementUI中Upload组件如何批量上传首先就是upload组件 <el-upl

Docker部署Jenkins持续集成(CI)工具的实现

《Docker部署Jenkins持续集成(CI)工具的实现》Jenkins是一个流行的开源自动化工具,广泛应用于持续集成(CI)和持续交付(CD)的环境中,本文介绍了使用Docker部署Jenkins... 目录前言一、准备工作二、设置变量和目录结构三、配置 docker 权限和网络四、启动 Jenkins

Python3脚本实现Excel与TXT的智能转换

《Python3脚本实现Excel与TXT的智能转换》在数据处理的日常工作中,我们经常需要将Excel中的结构化数据转换为其他格式,本文将使用Python3实现Excel与TXT的智能转换,需要的可以... 目录场景应用:为什么需要这种转换技术解析:代码实现详解核心代码展示改进点说明实战演练:从Excel到

Python中常用的四种取整方式分享

《Python中常用的四种取整方式分享》在数据处理和数值计算中,取整操作是非常常见的需求,Python提供了多种取整方式,本文为大家整理了四种常用的方法,希望对大家有所帮助... 目录引言向零取整(Truncate)向下取整(Floor)向上取整(Ceil)四舍五入(Round)四种取整方式的对比综合示例应

python 3.8 的anaconda下载方法

《python3.8的anaconda下载方法》本文详细介绍了如何下载和安装带有Python3.8的Anaconda发行版,包括Anaconda简介、下载步骤、安装指南以及验证安装结果,此外,还介... 目录python3.8 版本的 Anaconda 下载与安装指南一、Anaconda 简介二、下载 An

如何使用CSS3实现波浪式图片墙

《如何使用CSS3实现波浪式图片墙》:本文主要介绍了如何使用CSS3的transform属性和动画技巧实现波浪式图片墙,通过设置图片的垂直偏移量,并使用动画使其周期性地改变位置,可以创建出动态且具有波浪效果的图片墙,同时,还强调了响应式设计的重要性,以确保图片墙在不同设备上都能良好显示,详细内容请阅读本文,希望能对你有所帮助...

Python自动化处理手机验证码

《Python自动化处理手机验证码》手机验证码是一种常见的身份验证手段,广泛应用于用户注册、登录、交易确认等场景,下面我们来看看如何使用Python自动化处理手机验证码吧... 目录一、获取手机验证码1.1 通过短信接收验证码1.2 使用第三方短信接收服务1.3 使用ADB读取手机短信1.4 通过API获取

C# string转unicode字符的实现

《C#string转unicode字符的实现》本文主要介绍了C#string转unicode字符的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录1. 获取字符串中每个字符的 Unicode 值示例代码:输出:2. 将 Unicode 值格式化

python安装whl包并解决依赖关系的实现

《python安装whl包并解决依赖关系的实现》本文主要介绍了python安装whl包并解决依赖关系的实现,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录一、什么是whl文件?二、我们为什么需要使用whl文件来安装python库?三、我们应该去哪儿下

Python脚本实现图片文件批量命名

《Python脚本实现图片文件批量命名》这篇文章主要为大家详细介绍了一个用python第三方库pillow写的批量处理图片命名的脚本,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言源码批量处理图片尺寸脚本源码GUI界面源码打包成.exe可执行文件前言本文介绍一个用python第三方库pi