为什么我的Dragon Game无法正常工作? 龙跳时,障碍物暂停。 我希望那时候龙跳 障碍不断。 该游戏与在互联网上发现的龙游戏一样,障碍物一一进入。但是它不能正常工作。 什么是逻辑错误?
import turtle
import random
import time
win = turtle.Screen()
win.title("Dragon game")
win.bgcolor("white")
win.setup(width=800, height=300)
base_line = turtle.Turtle()
base_line.speed(0)
base_line.back(300)
base_line.fd(600)
dragon = turtle.Turtle()
dragon.shape("square")
dragon.speed(0)
dragon.color("black")
dragon.penup()
dragon.goto(-250, 10)
dragon.pendown()
dragon.setheading(90)
dragon_state = "steady"
number_of_obstacles = 10
obstacles_list = []
for i in range(number_of_obstacles):
obstacles_list.append(turtle.Turtle())
for i in obstacles_list:
i.hideturtle()
i.color("black")
i.shape("square")
i.shapesize(1, 2)
i.penup()
i.speed(0)
i.setheading(90)
i.setposition(400, 5)
i.showturtle()
def jump_dragon():
global dragon_state
if dragon_state == "steady":
dragon_state = "jump"
dragon.penup()
dragon.speed(1)
x = dragon.xcor()
y = dragon.ycor() + 90
dragon.setposition(x, y)
y = dragon.ycor() - 90
dragon.setposition(x, y)
dragon.showturtle()
win.listen()
win.onkey(jump_dragon, "space")
index = 0
while True:
x = random.randint(300, 505)
while index < 10:
s = random.randint(1, 4)
x = x - 10
obstacles_list[index].penup()
obstacles_list[index].speed(1)
obstacles_list[index].goto(x, 5)
if dragon.distance(obstacles_list[index]) < 20:
dragon_state = "steady"
obstacles_list[index].hideturtle()
print("Collision", index)
x = random.randint(300, 405)
index += 1
elif obstacles_list[index].xcor() < -290:
obstacles_list[index].hideturtle()
index += 1
win.mainloop()
答案 0 :(得分:0)
我想跳龙,但障碍不断前进
为此,我们首先将您的while True:
丢掉,在像乌龟这样的事件驱动世界中没有地位。在其位置,我们放置了一个定期触发的计时器事件。接下来,我们将障碍物的水平运动和龙的垂直运动分解为较小的交错运动。以下是根据上述内容对您的代码进行的重做-为了简化此示例,我简化了您的障碍逻辑:
from turtle import Screen, Turtle
def jump_dragon():
global dragon_state, dragon_direction
if dragon_state == "steady":
dragon_state = "jump"
dragon_direction = 1
dragon.forward(10)
def play():
global dragon_state, dragon_direction
if dragon_state == "jump":
dragon.forward(dragon_direction * 10)
y = dragon.ycor()
if y >= 100:
dragon_direction *= -1
elif y <= 10:
dragon_state = "steady"
obstacle.forward(10)
if obstacle.xcor() < -290:
obstacle.hideturtle()
obstacle.goto(290, 20)
obstacle.showturtle()
elif dragon.distance(obstacle) < 20:
print("Collision")
obstacle.hideturtle()
obstacle.goto(290, 20)
obstacle.showturtle()
win.ontimer(play, 75)
win = Screen()
win.title("Dragon game")
win.setup(width=800, height=300)
base_line = Turtle(visible=False)
base_line.speed('fastest')
base_line.back(300)
base_line.forward(600)
dragon = Turtle("square", visible=False)
dragon.speed('slow')
dragon.setheading(90)
dragon.penup()
dragon.goto(-250, 10)
dragon.showturtle()
dragon_state = "steady"
dragon_direction = 1
obstacle = Turtle("square", visible=False)
obstacle.shapesize(2, 1)
obstacle.speed('fastest')
obstacle.penup()
obstacle.setheading(180)
obstacle.setposition(290, 20)
obstacle.showturtle()
win.onkey(jump_dragon, "space")
win.listen()
play()
win.mainloop()
为了样式和清晰度,我还重新编写了代码的其他部分。