Python和穿墙

问题描述 投票:0回答:0

我怎样才能让乌龟不能穿过边界?

def can_move(player, dx, dy, boundaries_lvl1):
    # Calculate the player's next position
    next_x = player.xcor() + dx
    next_y = player.ycor() + dy
    
    # Check if the player's next position collides with any of the barriers
    for barrier in boundaries_lvl1():
        if (boundaries_lvl1.xcor() - 20 < next_x < boundaries_lvl1.xcor() + 20 and
boundaries_lvl1.ycor() - 20 < next_y < boundaries_lvl1.ycor() + 20):
            return False
    # If the player's next position doesn't collide with any barriers, they can move
    return True

def boundaries_lvl1():
    trtl.penup()
    trtl.setpos(-130, 125)
    trtl.right(90)
    trtl.pensize(2)
    trtl.pendown()
    trtl.forward(90)
    trtl.penup()
    trtl.setpos(50, 125)
    trtl.pensize(2)
    trtl.pendown()
    trtl.forward(140)
    trtl.left(90)
    trtl.forward(90)
    trtl.penup()
    trtl.setpos(0, -175)
    trtl.left(90)
    trtl.pensize(2)
    trtl.pendown()
    trtl.forward(100)
    trtl.left(90)
    trtl.forward(150)
python collision-detection turtle-graphics python-turtle boundary
© www.soinside.com 2019 - 2024. All rights reserved.