从概念上讲,从我在网上和课堂上收集的信息来看,这应该可以制作一个迷宫,然后允许用户通过它来引导乌龟在最后收集金圈但不能穿过墙壁。但是在我添加了碰撞代码之后,它创建了迷宫并放置了目标,但是乌龟然后变得没有响应并且崩溃了。任何修复?
import turtle
import math
turtle.setup(1000,1000)
window=turtle.Screen()
window.bgcolor('white')
def forward():
print("Key Up")
turtle1.forward(10)
def right():
print("Right")
turtle1.right(90)
def left():
print("Left")
turtle1.left(90)
def backward():
print("Backward")
turtle1.backward(10)
def iscollision(t1, t2):
d = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2)+ math.pow(t1.ycor()-t2.ycor(),2))
if d < 20:
return True
elif():
return False
window.listen()
#boarders
boarder=turtle.Turtle()
boarder.penup()
boarder.setposition(-450,-450)
boarder.pendown()
boarder.pensize(10)
boarder.pencolor('black')
for side in range(4):
boarder.speed(100)
boarder.forward(900)
boarder.left(90)
boarder.hideturtle()
#Maze
Maze=turtle.Turtle()
Maze.pensize(10)
#Speed
Maze.speed(100)
#Line 1
Maze.penup()
Maze.setposition(0,-450)
Maze.pendown()
Maze.setposition(300,-450)
Maze.setposition(0,-450)
Maze.setposition(0,-250)
Maze.setposition(150,-250)
Maze.setposition(0,-250)
Maze.setposition(0,-50)
Maze.setposition(100,-50)
Maze.setposition(0,-50)
Maze.setposition(0,-450)
#Line 2
Maze.setposition(300,-450)
Maze.setposition(300,100)
Maze.setposition(300,150)
Maze.setposition(100,150)
Maze.setposition(300,150)
Maze.setposition(300,50)
Maze.setposition(100,50)
Maze.setposition(300,50)
Maze.setposition(300,-150)
Maze.setposition(100,-150)
Maze.setposition(300,-150)
Maze.setposition(300,-450)
#Line 3
Maze.setposition(450,-450)
Maze.setposition(450,450)
Maze.setposition(200,450)
Maze.setposition(200,400)
Maze.setposition(50,400)
Maze.setposition(50,350)
Maze.setposition(0,350)
Maze.setposition(0,400)
Maze.setposition(-200,400)
Maze.setposition(-200,350)
Maze.setposition(-275,350)
Maze.setposition(-275,400)
Maze.setposition(-275,250)
#Maze2
Maze2=turtle.Turtle()
Maze2.pensize(10)
#Speed
Maze2.speed(100)
#line 4
Maze2.penup()
Maze2.setposition(300,150)
Maze2.pendown()
Maze2.setposition(100,150)
Maze2.setposition(100,200)
Maze2.setposition(-200,200)
Maze2.setposition(-200,150)
Maze2.setposition(-350,150)
Maze2.setposition(-350,400)
#Maze3
Maze3=turtle.Turtle()
Maze3.pensize(10)
#Speed
Maze3.speed(100)
#line 5
Maze3.penup()
Maze3.setposition(-275,150)
Maze3.pendown()
Maze3.setposition(-275,0)
Maze3.setposition(-200,0)
Maze3.setposition(-200,50)
Maze3.setposition(-150,50)
Maze3.setposition(-150,100)
Maze3.setposition(-100,100)
Maze3.setposition(-100,150)
Maze3.setposition(-50,150)
Maze3.setposition(-100,150)
Maze3.setposition(-100,100)
Maze3.setposition(-150,100)
Maze3.setposition(-150,50)
Maze3.setposition(-200,50)
Maze3.setposition(-200,0)
Maze3.setposition(-275,0)
Maze3.setposition(-275,-150)
Maze3.setposition(-350,-150)
Maze3.setposition(-275,-150)
Maze3.setposition(-275,-250)
Maze3.setposition(-350,-250)
Maze3.setposition(-200,-250)
Maze3.setposition(-200,-350)
Maze3.penup()
Maze3.setposition(-300,-450)
Maze3.pendown()
Maze3.setposition(-300,-350)
Maze3.setposition(-300,-450)
Maze3.setposition(-100,-450)
Maze3.setposition(-100,-350)
Maze3.setposition(-100,-450)
Maze3.setposition(450,-450)
Maze3.setposition(450,250)
Maze3.setposition(200,250)
Maze3.setposition(450,250)
Maze3.setposition(450,450)
Maze3.setposition(300,450)
Maze3.setposition(300,300)
Maze3.setposition(300,350)
Maze3.setposition(350,350)
Maze3.penup()
Maze3.setposition(-450,0)
Maze3.pendown()
Maze3.setposition(-350,0)
Maze3.penup()
Maze3.setposition(0,-200)
Maze3.pendown()
Maze3.setposition(-100,-200)
Maze3.setposition(-100,-100)
Maze3.setposition(-150,-100)
#end goal
goal=turtle.Turtle()
goal.color('gold')
goal.shape('circle')
goal.penup()
goal.setposition(375,-350)
turtle1=turtle.Turtle()
turtle1.color('black')
turtle1.shape("turtle")
turtle1.penup()
turtle1.setposition(100,-300)
window.onkey(forward,"Up")
window.onkey(right,"Right")
window.onkey(left,"Left")
window.onkey(backward,"Down")
window.listen()
while True:
#boundary check
if turtle1.xcor()> 450 or turtle1.xcor()< -450:
turtle1.right(180)
if turtle1.ycor()> 450 or turtle1.ycor()< -450:
turtle1.right(180)
## if iscollision(turtle1, goal):
## goal.hideturtle()
## if iscollision(turtle1, Maze):
## turtle1.right(180)
## if iscollision(turtle1, Maze2):
## turtle1.right(180)
## if iscollision(turtle1, Maze3):
## turtle1.right(180)
答案 0 :(得分:0)
我添加了碰撞代码后,它会创建迷宫并放置 目标,但乌龟然后变得反应迟钝
问题是你在乌龟代码中添加了一个无限循环while True:
,这意味着它永远不会运行mainloop()
,并且不会触发乌龟事件。在这种情况下,解决方案很简单,使边界检查成为正常函数,没有无限循环,只需从forward()
和backward()
调用它,即只要乌龟移动。
我已经进行了上述更改以及下面的一些风格调整。你应该能够再次移动你的乌龟。但是,虽然您的边界碰撞检测很好,但是您的目标检测也是如此,我不相信您的迷宫墙检测可以与您当前所持有的策略一起使用:
from turtle import Turtle, Screen
import math
def forward():
turtle_1.forward(10)
boundary_check()
def right():
turtle_1.right(90)
def left():
turtle_1.left(90)
def backward():
turtle_1.backward(10)
boundary_check()
def iscollision(t1, t2):
d = math.sqrt((t2.xcor() - t1.xcor()) ** 2 + (t2.ycor() - t1.ycor()) ** 2)
return d < 20.0
def draw_wall(turtle, positions):
turtle.penup()
turtle.setposition(positions[0])
turtle.pendown()
for position in positions[1:]:
turtle.setposition(position)
screen = Screen()
screen.setup(1000, 1000)
screen.bgcolor('white')
screen.onkey(forward, "Up")
screen.onkey(right, "Right")
screen.onkey(left, "Left")
screen.onkey(backward, "Down")
screen.listen()
# border
border = Turtle(visible=False)
border.penup()
border.pensize(10)
border.pencolor('black')
border.setposition(-450, -450)
border.pendown()
border.speed("fastest")
for _ in range(4):
border.forward(900)
border.left(90)
# Maze
maze_1 = Turtle(visible=False)
maze_1.pensize(10)
maze_1.speed("fastest")
wall_1 = [(0, -450), (300, -450), (0, -450), (0, -250), (150, -250), \
(0, -250), (0, -50), (100, -50), (0, -50), (0, -450)]
draw_wall(maze_1, wall_1)
wall_2 = [(300, -450), (300, 100), (300, 150), (100, 150), (300, 150), \
(300, 50), (100, 50), (300, 50), (300, -150), (100, -150), \
(300, -150), (300, -450)]
draw_wall(maze_1, wall_2)
wall_3 = [(450, -450), (450, 450), (200, 450), (200, 400), (50, 400), \
(50, 350), (0, 350), (0, 400), (-200, 400), (-200, 350), \
(-275, 350), (-275, 400), (-275, 250)]
draw_wall(maze_1, wall_3)
maze_2 = Turtle(visible=False)
maze_2.pensize(10)
maze_2.speed("fastest")
wall_4 = [(300, 150), (100, 150), (100, 200), (-200, 200), \
(-200, 150), (-350, 150), (-350, 400)]
draw_wall(maze_2, wall_4)
maze_3 = Turtle(visible=False)
maze_3.pensize(10)
maze_3.speed("fastest")
wall_5 = [(-275, 150), (-275, 0), (-200, 0), (-200, 50), (-150, 50), \
(-150, 100), (-100, 100), (-100, 150), (-50, 150), (-100, 150), \
(-100, 100), (-150, 100), (-150, 50), (-200, 50), (-200, 0), \
(-275, 0), (-275, -150), (-350, -150), (-275, -150), (-275, -250), \
(-350, -250), (-200, -250), (-200, -350)]
draw_wall(maze_3, wall_5)
wall_6 = [(-300, -450), (-300, -350), (-300, -450), (-100, -450), \
(-100, -350), (-100, -450), (450, -450), (450, 250), (200, 250), \
(450, 250), (450, 450), (300, 450), (300, 300), (300, 350), (350, 350)]
draw_wall(maze_3, wall_6)
wall_7 = [(-450, 0), (-350, 0)]
draw_wall(maze_3, wall_7)
wall_8 = [(0, -200), (-100, -200), (-100, -100), (-150, -100)]
draw_wall(maze_3, wall_8)
# end goal
goal = Turtle(shape='circle')
goal.color('gold')
goal.penup()
goal.setposition(375, -350)
turtle_1 = Turtle(shape='turtle')
turtle_1.color('black')
turtle_1.penup()
turtle_1.setposition(100, -300)
def boundary_check():
# boundary check
if not -450 < turtle_1.xcor() < 450 or not -450 < turtle_1.ycor() < 450:
turtle_1.right(180)
if iscollision(turtle_1, goal):
goal.hideturtle()
# if iscollision(turtle, maze_1):
# turtle_1.right(180)
# if iscollision(turtle, maze_2):
# turtle_1.right(180)
# if iscollision(turtle, maze_3):
# turtle_1.right(180)
screen.mainloop()