当前位置: 首页 > news >正文

分享相关知识

直接使用海龟图进行创作移动动态的游戏

这段代码是一个简单的turtle模块实现的小游戏,主要功能包括:

  1. 窗口和无人机初始化:

    • 创建了一个turtle窗口,设置了窗口的背景颜色和标题。
    • 创建了一个表示无人机的turtle,形状为正方形,蓝色,大小为(2 * 5)。
    • 创建了四个表示旋翼的turtle,形状为圆形,红色,大小根据半径计算。
  2. 障碍物的创建和初始化:

    • 创建了三个初始位置随机的障碍物,其中两个形状为圆形,一个形状为三角形。
  3. 碰撞检测和处理:

    • 使用 is_collision 函数检测无人机和障碍物之间的碰撞。
    • handle_collision 函数用于处理碰撞,根据障碍物的形状进行分数和死亡次数的更新,并根据分数动态改变无人机的宽度。
    • 障碍物的位置在碰撞后会重新设置。
  4. 按键事件和无人机移动:

    • 使用 wn.listen() 启用键盘事件监听。
    • 定义了四个移动函数 move_upmove_downmove_leftmove_right,分别控制无人机的上下左右移动。
    • move_rotors 函数用于移动旋翼。
  5. 动态增加新的障碍物:

    • add_obstacle 函数根据分数的不同,动态地增加新的三角形障碍物,并且仅在之前没有创建过相应障碍物的情况下执行。
  6. 计时和异常处理:

    • 计时程序记录了游戏的运行时间。
    • 在主循环中加入异常处理,避免窗口关闭时出现错误。
  7. 更新分数和死亡次数的显示:

    • update_score_display 函数用于更新分数和死亡次数的显示,并在需要时增加新的障碍物。
  8. 游戏结束提示:

    • 在死亡次数达到三次时,输出游戏结束提示。

这个小游戏通过键盘操作无人机,避开圆形障碍物,触碰三角形障碍物会导致死亡。分数在碰撞到圆形障碍物时增加,死亡次数在碰撞到三角形障碍物时增加。同时,游戏会动态地增加新的三角形障碍物。

import turtle
import random
import math
import time# 设置窗口
wn = turtle.Screen()
wn.bgcolor("white")
wn.title("无人机")# 创建飞行器
drone = turtle.Turtle()
drone.shape("square")
drone.color("blue")
drone.shapesize(stretch_wid=2, stretch_len=5)# 创建圆形的旋翼
rotor_radius = 20def draw_rotor(rotor, x, y):rotor.speed(0)rotor.shape("circle")rotor.color("red")rotor.shapesize(rotor_radius / 10)rotor.penup()rotor.goto(x, y)# 创建旋翼
rotor1 = turtle.Turtle()
draw_rotor(rotor1, -50, 50)rotor2 = turtle.Turtle()
draw_rotor(rotor2, 50, 50)rotor3 = turtle.Turtle()
draw_rotor(rotor3, -50, -50)rotor4 = turtle.Turtle()
draw_rotor(rotor4, 50, -50)# 创建障碍物(圆形和三角形)
obstacle_size = 20def draw_obstacle(obstacle, shape, x, y):obstacle.speed(0)obstacle.shape(shape)obstacle.color("black")obstacle.penup()obstacle.goto(x, y)# 随机初始位置
obstacle1_initial_pos = (random.randint(-200, 200), random.randint(-200, 200))
obstacle2_initial_pos = (random.randint(-200, 200), random.randint(-200, 200))
obstacle3_initial_pos = (random.randint(-200, 200), random.randint(-200, 200))# 创建障碍物
obstacle1 = turtle.Turtle()
obstacle_shape1 = "circle"
draw_obstacle(obstacle1, obstacle_shape1, *obstacle1_initial_pos)obstacle2 = turtle.Turtle()
obstacle_shape2 = "circle"
draw_obstacle(obstacle2, obstacle_shape2, *obstacle2_initial_pos)obstacle3 = turtle.Turtle()
obstacle_shape3 = "triangle"
draw_obstacle(obstacle3, obstacle_shape3, *obstacle3_initial_pos)obstacle4 = None
obstacle5 = None
obstacle6 = None# 记录分值和死亡次数
score = 0
death_count = 0# 分数和死亡次数显示
score_display = turtle.Turtle()
score_display.hideturtle()
score_display.penup()
score_display.goto(-200, 200)
score_display.write(f"分数: {score}  死亡次数: {death_count}", align="left", font=("Arial", 16, "normal"))# 移动无人机及旋翼
def move_up():y = drone.ycor()drone.sety(y + 10)check_collision()def move_down():y = drone.ycor()drone.sety(y - 10)check_collision()def move_left():x = drone.xcor()drone.setx(x - 10)check_collision()def move_right():x = drone.xcor()drone.setx(x + 10)check_collision()# 移动旋翼函数
def move_rotors():rotor1.setpos(drone.xcor() - 50, drone.ycor() + 50)rotor2.setpos(drone.xcor() + 50, drone.ycor() + 50)rotor3.setpos(drone.xcor() - 50, drone.ycor() - 50)rotor4.setpos(drone.xcor() + 50, drone.ycor() - 50)# 碰撞检测函数
def check_collision():global score, death_count# 检测与障碍物1的碰撞if is_collision(drone, obstacle1):handle_collision(obstacle1)# 检测与障碍物2的碰撞if is_collision(drone, obstacle2):handle_collision(obstacle2)# 检测与障碍物3的碰撞if is_collision(drone, obstacle3):handle_collision(obstacle3)#这个是中级难度的,会增加一个障碍物 if score>=10:if is_collision(drone, obstacle4):handle_collision(obstacle4)if score>=20:if is_collision(drone, obstacle5):handle_collision(obstacle5)if score>=30:if is_collision(drone, obstacle6):handle_collision(obstacle6)# 判断是否发生碰撞
def is_collision(t1, t2):x1, y1 = t1.xcor(), t1.ycor()x2, y2 = t2.xcor(), t2.ycor()distance = math.sqrt((x1 - x2)**2 + (y1 - y2)**2)return distance < (obstacle_size + 20)# 处理碰撞函数
def handle_collision(obstacle):global score, death_countif obstacle.shape() == "circle":score += 1print(f"碰撞到圆球! 得分: {score}")reset_obstacle(obstacle)elif obstacle.shape() == "triangle":death_count += 1print(f"已死亡!死亡次数: {death_count}")if death_count == 3:print("游戏结束,请重新开始")time.sleep(3)wn.bye()  # 关闭窗口reset_obstacle(obstacle)# 根据分数动态改变无人机宽度drone_width_percent = 1 + score * 0.01drone.shapesize(stretch_wid=2 * drone_width_percent, stretch_len=5)update_score_display()# 重置障碍物位置
def reset_obstacle(obstacle):obstacle.setpos(random.randint(-200, 200), random.randint(-200, 200))
def game_over():# 游戏结束的操作print("Game Over")turtle.bye()  # 关闭窗口
# 动态增加新的障碍物
def add_obstacle():global obstacle4, obstacle5, obstacle6if score >= 10 and obstacle4 is None:obstacle4 = turtle.Turtle()obstacle_shape4 = "triangle"draw_obstacle(obstacle4, obstacle_shape4, random.randint(-200, 200), random.randint(-200, 200))elif score >= 20 and obstacle5 is None:obstacle5 = turtle.Turtle()obstacle_shape5 = "triangle"draw_obstacle(obstacle5, obstacle_shape5, random.randint(-200, 200), random.randint(-200, 200))elif score >= 30 and obstacle6 is None:obstacle6 = turtle.Turtle()obstacle_shape6 = "triangle"draw_obstacle(obstacle6, obstacle_shape6, random.randint(-200, 200), random.randint(-200, 200))
def update_score_display():global scorescore_display.clear()score_display.write(f"分数: {score}  死亡次数: {death_count}", align="left", font=("Arial", 16, "normal"))add_obstacle()  # 检查是否需要增加新的障碍物
# 绑定按键事件,就是你键盘w s a d按下就能移动
wn.listen()
wn.onkeypress(move_up, "w")
wn.onkeypress(move_down, "s")
wn.onkeypress(move_left, "a")
wn.onkeypress(move_right, "d")# 主循环
# 计时程序
starTime = time.time()
while True:try:move_rotors()wn.update()except:print("请重新启动游戏")break
endTime = time.time()
elapsedTime = endTime - starTime
print("你最终花费的时间是:", elapsedTime)
http://www.lryc.cn/news/271958.html

相关文章:

  • RabbitMQ(七)ACK 消息确认机制
  • ubuntu 编译内核报错
  • Python之自然语言处理库snowNLP
  • C# 语法进阶 委托
  • 开源可观测性平台Signoz(四)【链路监控及数据库中间件监控篇】
  • 【嵌入式开发 Linux 常用命令系列 4.2 -- git .gitignore 使用详细介绍】
  • 【熔断限流组件resilience4j和hystrix】
  • 微服务雪崩问题及解决方案
  • 008、所有权
  • 千里马2023年终总结-android framework实战
  • vue3中pinia的使用及持久化(详细解释)
  • 安装 yarn、pnpm、功能比较
  • 计算机专业个人简历范文(8篇)
  • 几个实用网站
  • Pycharm 切换interpreter---python的环境和第三方库问题
  • TP-LINK 路由器忘记密码 - 恢复出厂设置
  • 关闭 Elasticsearch 集群的安全性设置
  • [技术分享]一招解决 MySQL 中 DDL 被阻塞的问题
  • Windows搭建Emby媒体库服务器,无公网IP远程访问本地影音文件
  • 自动化测试系列 之 Python单元测试框架unittest
  • C语言朴素算法
  • 【力扣题解】P501-二叉搜索树中的众数-Java题解
  • Wnmp本地部署结合内网穿透实现任意浏览器远程访问本地服务
  • 深信服AF防火墙配置SSL VPN
  • 在Spring Cloud中使用Gateway 网关
  • 【Python】配置环境变量
  • 使用.Net nanoFramework 驱动ESP32的OLED显示屏
  • 0基础学习VR全景平台篇第134篇:720VR全景,云台调整节点
  • 扫地机器人地图与用户终端的同步
  • 使用机器学习进行语法错误检测/纠正