2016-11-14 12 views
0

概念的には、私がオンラインとクラスで集めたものから、これは迷路を作って、それを通って亀を操り、最後に金の円を集めることができるようにする壁を通してしかし、私は衝突コードを追加した後、それは迷路を作成し、目標を配置しますが、その後、カメは応答しなくなり、クラッシュします。すべての修正?私は衝突のコードを追加した後Python Turtle Maze Crashing

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

を使用すると、2つの 'ウィンドウを持っていないのはなぜ:あなたの目標検出があるとして、あなたの国境の衝突検出は、細かいですがしかし、私はあなたの迷路の壁の検出は、あなたがcurrenly persuingている戦略に動作するとは思いません。 listen() '呼び出し? –

答えて

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()