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

本文详细介绍了如何使用Python和Turtle库编写一个具有自定义AI对手的乒乓球游戏,包括球体、挡板的创建、移动逻辑和碰撞检测机制。通过代码示例,读者可以学习Python编程并实践基本的游戏开发技能。
摘要由CSDN通过智能技术生成

本文提供了一个 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 += 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()

- END -

关于Python学习指南

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

包括:Python激活码+安装包、Python web开发,Python爬虫,Python数据分析,人工智能、自动化办公等学习教程。带你从零基础系统性的学好Python!

👉Python所有方向的学习路线👈

Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。(全套教程文末领取)

在这里插入图片描述

👉Python学习视频600合集👈

观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。

在这里插入图片描述

温馨提示:篇幅有限,已打包文件夹,获取方式在:文末

👉Python70个实战练手案例&源码👈

光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。

在这里插入图片描述

👉Python大厂面试资料👈

我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

在这里插入图片描述

在这里插入图片描述

👉Python副业兼职路线&方法👈

学好 Python 不论是就业还是做副业赚钱都不错,但要学会兼职接单还是要有一个学习规划。

在这里插入图片描述

👉 这份完整版的Python全套学习资料已经上传,朋友们如果需要可以扫描下方CSDN官方认证二维码或者点击链接免费领取保证100%免费

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值