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

Python实现的AI乒乓球游戏教程
(永久免费,扫码加入)

来源丨网络

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

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

95213ed52611ff796282b26b2019334d.png


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 += 20
    you.sety(y)


def paddle2_down():
    y = you.ycor()
    y -= 20
    you.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 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 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 += 20
    you.sety(y)


def paddle2_down():
    y = you.ycor()
    y -= 20
    you.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 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()

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

最后推荐一下我们的会员群,目前有风投天使投资人,猎头HR,抖音大V,情感博主,律师,心理咨询师,医疗销售,地产,保险,钢琴老师,运营商,企业咨询,跨境电商,建筑,互联网行业的数据分析师,后端开发,python测试等行业的同学加入。

微信咨询:coder_v5 (务必备注你的来意)

性价比超高的星球

目前星球470+人,专栏的内容秘籍已经更新了41篇,每天都有星球发布自己心得。只花一份钱可以学:

Python : python 入门课程44节+Django专栏9节+趣味实战案例

chatgpt :入门,进阶,趣味办公,高级课程

AI绘画:Mj的基础,入门,进阶,小红书玩法

如果你想学Python,又想学ChatGPT,又想学AI绘画,只想花一份钱欢迎加入我们星球会员群,还能认识很多牛人!

加入就送ChatGPT独立账号

b61ee382e7ba94b9eaaa06cf9d5083b2.jpeg

另外还送ChatGPT高级视频课程

原价99,现在免费送星球会员

微信长按试看内容

三天内不满意可直接退款!!!

1ea72f23dcc680af77815022d5fbe715.png

推荐阅读:
入门: 最全的零基础学Python的问题  | 零基础学了8个月的Python  | 实战项目 |学Python就是这条捷径
干货:爬取豆瓣短评,电影《后来的我们》 | 38年NBA最佳球员分析 |   从万众期待到口碑扑街!唐探3令人失望  | 笑看新倚天屠龙记 | 灯谜答题王 |用Python做个海量小姐姐素描图 |碟中谍这么火,我用机器学习做个迷你推荐系统电影
趣味:弹球游戏  | 九宫格  | 漂亮的花 | 两百行Python《天天酷跑》游戏!
AI: 会做诗的机器人 | 给图片上色 | 预测收入 | 碟中谍这么火,我用机器学习做个迷你推荐系统电影
小工具: Pdf转Word,轻松搞定表格和水印! | 一键把html网页保存为pdf!|  再见PDF提取收费! | 用90行代码打造最强PDF转换器,word、PPT、excel、markdown、html一键转换 | 制作一款钉钉低价机票提示器! |60行代码做了一个语音壁纸切换器天天看小姐姐!|
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值