【问题标题】:Python Turtle - Unable to restrict turtle within a borderPython Turtle - 无法将海龟限制在边界内
【发布时间】:2018-04-05 15:59:10
【问题描述】:

我试图让乌龟在碰到边界时停止移动,直到它改变它的方向。

from turtle import *
import turtle
import time

user_speed = 46
border_pen = Turtle()
shouldMove = True
#alreadyStoppedDown = False
#alreadyStoppedUp = False
prev_heading = heading()

#Set up border
def setup(turt):
    turt.ht()
    turt.pu()
    turt.speed(0)
    turt.goto(-250,250)
    turt.pd()
    turt.begin_fill()
    for x in range(4):
        turt.fd(500)
        turt.rt(90)
    turt.color('black')
    turt.end_fill()

#Set up user
def user():
    pu()
    shape('square')
    shapesize(2)
    color('orange')
    speed(0)
    goto(-230,230)
    speed(1)

#Keybind functions
def down():
    global prev_heading
    prev_heading = heading()
    speed(0)
    setheading(-90)
    speed(1)
def up():
    global prev_heading
    prev_heading = heading()
    speed(0)
    setheading(90)
    speed(1)
def left():
    global prev_heading
    prev_heading = heading()
    speed(0)
    setheading(180)
    speed(1)
def right():
    global prev_heading
    prev_heading = heading()
    speed(0)
    setheading(0)
    speed(1)

#Border restriction
def restrict():
    global shouldMove
    if ycor() <= -230:
        shouldMove = False
        global prev_heading
        prev_heading = heading()

def main():
    global shouldMove
    while True:
        #Debugging
        print(shouldMove)
        print(heading(),prev_heading,ycor())

        if shouldMove:
            fd(user_speed)
            restrict()
        else:
            if heading() != prev_heading:
                shouldMove = True
            else:
                continue

setup(border_pen)
user()

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

main()

我只设置了边框底部的限制。

我可以让乌龟停止移动,但是当它停止移动时它不会改变它的航向。我尝试通过按wasd 来更改它的标题,但它只是停留在那里直到窗口崩溃。我试图想出多种解决方案,但我做不到。这里的主要问题是当海龟的 ycor() 小于或等于 -230 时,它会停止但不会改变它的航向。

当我更改 main() 以使其在 ycor() 小于 或等于 -230 时将其设置为 0,它将向右移动一次,因为 ycor () 仍然是 -230,它将停止移动。

def main():
    while True:
        #Debugging
        print(shouldMove)
        print(heading(),prev_heading,ycor())

        if shouldMove:
            fd(user_speed)
            restrict()
        else:
            if heading() != prev_heading:
                shouldMove = True
            else:
                setheading(0)

我这样做是为了好玩,我观看了 The Coding Train 使用处理编写了一个蛇游戏,我正在尝试使用海龟模块在 Python 中重新创建它。是的,我是初学者。我想尝试使用 turtle 模块重新制作蛇游戏,即使我可以使用 PyGame 等其他模块,因为我不熟悉 PyGame。这个问题有什么解决办法吗?还是我不能使用 turtle 模块做到这一点?

【问题讨论】:

    标签: python python-3.x turtle-graphics


    【解决方案1】:

    我在您的代码中看到的主要问题是while True:,它在事件驱动的世界中没有位置。 (因为它可以阻止事件!)我们将改用ontimer() 事件。下一个问题是控制结构太复杂了。我们将扔掉大部分,只保留布尔值shouldMove 进行控制。最后,您将 functional 接口与 turtle 的 面向对象 接口混合在一起。我们将更改import 以仅允许面向对象的接口:

    from turtle import Turtle, Screen
    
    USER_SPEED = 20
    
    WIDTH = 500
    CURSOR_SIZE = 20
    CURSOR_MULTIPLIER = 2
    MINIMUM = ((CURSOR_SIZE * CURSOR_MULTIPLIER) / 2) - WIDTH / 2
    MAXIMUM = WIDTH / 2 - ((CURSOR_SIZE * CURSOR_MULTIPLIER) / 2)
    
    # Set up border
    def setup(turtle):
        turtle.penup()
        turtle.speed('fastest')
        turtle.goto(-WIDTH / 2, WIDTH / 2)
        turtle.pendown()
    
        turtle.begin_fill()
        for _ in range(4):
            turtle.forward(WIDTH)
            turtle.right(90)
        turtle.end_fill()
    
    # Set up user
    def user(turtle):
        turtle.penup()
        turtle.shapesize(CURSOR_MULTIPLIER)
        turtle.color('orange')
        turtle.speed('fastest')
    
    # Keybind functions
    def down():
        global shouldMove
    
        if user_pen.heading() != 270:
            user_pen.setheading(270)
            shouldMove = True
    
    def up():
        global shouldMove
    
        if user_pen.heading() != 90:
            user_pen.setheading(90)
            shouldMove = True
    
    def left():
        global shouldMove
    
        if user_pen.heading() != 180:
            user_pen.setheading(180)
            shouldMove = True
    
    def right():
        global shouldMove
    
        if user_pen.heading() != 0:
            user_pen.setheading(0)
            shouldMove = True
    
    # Border restriction
    
    def move():
        global shouldMove
    
        if shouldMove:
            user_pen.forward(USER_SPEED)
    
            if not(MINIMUM <= user_pen.ycor() <= MAXIMUM and MINIMUM <= user_pen.xcor() <= MAXIMUM):
                user_pen.undo()
                shouldMove = False
    
        screen.ontimer(move, 25)
    
    screen = Screen()
    
    border_pen = Turtle(visible=False)
    user_pen = Turtle('square')
    
    setup(border_pen)
    user(user_pen)
    
    screen.onkey(up, 'w')
    screen.onkey(down, 's')
    screen.onkey(left, 'a')
    screen.onkey(right, 'd')
    screen.listen()
    
    shouldMove = True
    
    move()
    
    screen.mainloop()
    

    这应该像你描述的那样。海龟会一直沿直线移动,直到您按下方向键或它停在边界处。

    【讨论】:

    • 谢谢!现在工作得很好。还没有看到你使用的一些海龟模块功能,所以我查了一下。我怎么不知道这些功能?哈哈。再次感谢您的帮助。 (我设法让蛇游戏正常运行——除非你不能输。)
    猜你喜欢
    • 2021-09-18
    • 2016-08-16
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    相关资源
    最近更新 更多