python-3.x 如何使海龟去某处,如果撞到或接近另一只海龟

0aydgbwb  于 2023-01-27  发布在  Python
关注(0)|答案(1)|浏览(118)
import turtle

player = input('Pick square or arrow: ')
if player == "arrow":
    turtle.shape("arrow")
    turtle.pu()
    turtle.goto(-400, 20)
elif player == "square":
    turtle.shape("square")
    turtle.pu()
    turtle.goto(-400, 20)
else:
    print ('You did not type square or arrow, so you have been given default.')
    turtle.pu()
    turtle.goto(-400, 20)

    
def up():
    turtle.forward(45)
      
def right():
    turtle.right(90)
  
def left():
    turtle.left(90)

def down():
    turtle.backward(45)
   
sc=turtle.Screen()
sc.setup(600, 600)

turtle.onkey(up,'w')
turtle.onkey(right,'d')
turtle.onkey(left,'a')
turtle.onkey(down,'s')
  
turtle.listen()

turtle.penup()

instructions = turtle.Turtle()
instructions.ht()
instructions.pu()
instructions.goto(-600, 200)
instructions.write("Maze Game", font=('Arial', 32, 'normal'))
instructions.goto(-600, 125)
instructions.write("Touch the Green", font=('Arial', 26, 'normal'))
instructions.goto(-580, 100)
instructions.write("And DIE!!!", font=('Arial', 26, 'normal'))
instructions.goto(-620, 50)
instructions.write("Make it Out to Win", font=('Arial', 26, 'normal'))
instructions.goto(-620, -75)
instructions.write("Use w/a/s/d to move", font=('Arial', 26, 'normal'))

#drawing the maze
maze = turtle.Turtle()

maze.ht()
maze.speed(0)
maze.penup()
maze.goto(-310, 300)
maze.pendown()
maze.pensize(40)
maze.color('green')
maze.forward(700)

maze.ht()
maze.speed(0)
maze.penup()
maze.goto(390, 300)
maze.pendown()
maze.right(90)
maze.pensize(40)
maze.color('green')
maze.forward(300)

maze.ht()
maze.speed(0)
maze.penup()
maze.goto(390, 0)
maze.pendown()
maze.left(90)
maze.pensize(40)
maze.color('green')
maze.forward(125)

maze.ht()
maze.speed(0)
maze.penup()
maze.goto(515, 0)
maze.pendown()
maze.right(90)
maze.pensize(40)
maze.color('green')
maze.forward(100)

我正在做一个迷宫游戏,但我不会它,以便当玩家击中迷宫,它会被重置到起始位置,但我不知道如何。
我试过一种方法,用距离法,它没有工作,但可能是因为我做错了。我希望它,以便当球员击中迷宫它得到重置到开始的位置。

y53ybaqx

y53ybaqx1#

一般来说,使用turtle时这是一个难题,但由于您在迷宫中只使用水平线和垂直线(假设您继续这样做),我们可以实现一个简单的墙壁碰撞测试。
首先,你需要明确地将你的行存储在一个数据结构中;其次,为了让简单测试工作,你所有的行都必须从左到右或从上到下描述:

from turtle import Screen, Turtle

LINES = [
    ((-310, 300), (390, 300)),
    ((390, 300), (390, 0)),
    ((390, 0), (515, 0)),
    ((515, 100), (515, 0)),
]

WALL_WIDTH = 40
PLAYER_WIDTH = 20
JUST_TOUCHING = WALL_WIDTH/2 + PLAYER_WIDTH/2

LARGE_FONT = ('Arial', 32, 'normal')
MEDIUM_FONT = ('Arial', 26, 'normal')

def up():
    player.forward(PLAYER_WIDTH/2)
    check_collision()

def right():
    player.right(90)
    check_collision()

def left():
    player.left(90)
    check_collision()

def down():
    player.backward(PLAYER_WIDTH/2)
    check_collision()

def check_collision():
    x, y = player.position()

    for ((a, b), (c, d)) in LINES:
        if a == c:  # vertical wall
            if abs(x - a) < JUST_TOUCHING and d - JUST_TOUCHING < y < b + JUST_TOUCHING:
                player.undo()
        elif b == d:  # horizontal wall
            if abs(y - b) < JUST_TOUCHING and a - JUST_TOUCHING < x < c + JUST_TOUCHING:
                player.undo()

screen = Screen()
screen.setup(1300, 650)

instructions = Turtle()
instructions.hideturtle()
instructions.penup()
instructions.goto(-600, 200)
instructions.write("Maze Game", font=LARGE_FONT)
instructions.goto(-600, 125)
instructions.write("Touch the Green", font=MEDIUM_FONT)
instructions.goto(-580, 100)
instructions.write("And DIE!!!", font=MEDIUM_FONT)
instructions.goto(-620, 50)
instructions.write("Make it Out to Win", font=MEDIUM_FONT)
instructions.goto(-620, -75)
instructions.write("Use w/a/s/d to move", font=MEDIUM_FONT)

maze = Turtle()
maze.hideturtle()
maze.speed('fastest')
maze.pensize(WALL_WIDTH)
maze.color('green')

for ((a, b), (c, d)) in LINES:
    maze.penup()
    maze.goto(a, b)
    maze.pendown()
    maze.goto(c, d)

player = Turtle()
player.shape("turtle")
player.penup()
player.goto(-400, 20)

screen.onkey(up, 'w')
screen.onkey(right, 'd')
screen.onkey(left, 'a')
screen.onkey(down, 's')

screen.listen()

screen.mainloop()

以上只是防止玩家为了测试目的而穿越迷宫线。你可以改变代码来结束游戏。
我需要让玩家的移动更小,因为你原来的步距离大于墙的宽度,所以玩家可以“跨越”一堵墙与我的简单碰撞测试!
这种碰撞逻辑是 * 脆弱的 *(可能在某些地方是不正确的),所以保持你的迷宫简单和宽敞!

相关问题