分享相关知识

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

这段代码是一个简单的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 = 20

def 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 = 20

def 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_count

    if obstacle.shape() == "circle":
        score += 1
        print(f"碰撞到圆球! 得分: {score}")
        reset_obstacle(obstacle)
    elif obstacle.shape() == "triangle":
        death_count += 1
        print(f"已死亡!死亡次数: {death_count}")
        if death_count == 3:
            print("游戏结束,请重新开始")
            time.sleep(3)
            wn.bye()  # 关闭窗口
        reset_obstacle(obstacle)

    # 根据分数动态改变无人机宽度
    drone_width_percent = 1 + score * 0.01
    drone.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, obstacle6
    if 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 score
    score_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)
  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值