李志自己做网站,html网页框架代码,合肥专门做网站,永嘉规划建设局网站直接使用海龟图进行创作移动动态的游戏
这段代码是一个简单的turtle模块实现的小游戏#xff0c;主要功能包括#xff1a; 窗口和无人机初始化#xff1a; 创建了一个turtle窗口#xff0c;设置了窗口的背景颜色和标题。创建了一个表示无人机的turtle#xff0c;形状为正…直接使用海龟图进行创作移动动态的游戏
这段代码是一个简单的turtle模块实现的小游戏主要功能包括 窗口和无人机初始化 创建了一个turtle窗口设置了窗口的背景颜色和标题。创建了一个表示无人机的turtle形状为正方形蓝色大小为2 * 5。创建了四个表示旋翼的turtle形状为圆形红色大小根据半径计算。 障碍物的创建和初始化 创建了三个初始位置随机的障碍物其中两个形状为圆形一个形状为三角形。 碰撞检测和处理 使用 is_collision 函数检测无人机和障碍物之间的碰撞。handle_collision 函数用于处理碰撞根据障碍物的形状进行分数和死亡次数的更新并根据分数动态改变无人机的宽度。障碍物的位置在碰撞后会重新设置。 按键事件和无人机移动 使用 wn.listen() 启用键盘事件监听。定义了四个移动函数 move_up、move_down、move_left、move_right分别控制无人机的上下左右移动。move_rotors 函数用于移动旋翼。 动态增加新的障碍物 add_obstacle 函数根据分数的不同动态地增加新的三角形障碍物并且仅在之前没有创建过相应障碍物的情况下执行。 计时和异常处理 计时程序记录了游戏的运行时间。在主循环中加入异常处理避免窗口关闭时出现错误。 更新分数和死亡次数的显示 update_score_display 函数用于更新分数和死亡次数的显示并在需要时增加新的障碍物。 游戏结束提示 在死亡次数达到三次时输出游戏结束提示。
这个小游戏通过键盘操作无人机避开圆形障碍物触碰三角形障碍物会导致死亡。分数在碰撞到圆形障碍物时增加死亡次数在碰撞到三角形障碍物时增加。同时游戏会动态地增加新的三角形障碍物。
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_wid2, stretch_len5)# 创建圆形的旋翼
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}, alignleft, 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 score10:if is_collision(drone, obstacle4):handle_collision(obstacle4)if score20:if is_collision(drone, obstacle5):handle_collision(obstacle5)if score30: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_wid2 * drone_width_percent, stretch_len5)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 triangledraw_obstacle(obstacle4, obstacle_shape4, random.randint(-200, 200), random.randint(-200, 200))elif score 20 and obstacle5 is None:obstacle5 turtle.Turtle()obstacle_shape5 triangledraw_obstacle(obstacle5, obstacle_shape5, random.randint(-200, 200), random.randint(-200, 200))elif score 30 and obstacle6 is None:obstacle6 turtle.Turtle()obstacle_shape6 triangledraw_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}, alignleft, 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)