繁体   English   中英

如何暂停和取消暂停海龟程序 - python

[英]How to pause and unpause a turtle program - python

所以我有一个算法可以找到退出迷宫的最佳路径。 然而,问题是我无法实现一个按键按钮来暂停和取消使用空格键作为按键的算法。 例如,我希望算法在我按下空格键时暂停(不退出窗口),当我再次按下空格键时,它应该继续运行。 它基本上应该像拨动开关一样工作。

代码如下:

# Import libraries
import turtle
import sys
import numpy as np
import os
from collections import deque

# Configure some basic settings for the map
bs = turtle.Screen()                                       # Define the turtle screen
bs.bgcolor("black")                                        # Set the background colour
bs.setup(width=0.9, height=0.9)                            # Setup the dimensions of the working window 
.
.
.
.
.
.
.
in_motion = False

def stopMovement():
    global in_motion
    if in_motion == True:
        in_motion == False
    else:
        in_motion == True

while True:
    if not in_motion:
        # Setting up classes
        turtle.title("Breadth first search algorithm ; steps taken: 0")
        title = title()
        building = Building()
        road = Road()
        start = Start()
        end = End()
        searcher = Searcher()
        route = Route()
        sprite = sprite()

        # Setting up lists
        walls = []
        path = []   
        routeCoords = []
        visited = set()
        frontier = deque()
        solution = {} 

        # Activation
        setupMaze(mazeSet)
        search(start_x, start_y)
        correctRoute(end_x, end_y)
        sprite.moveSprite(end_x, end_y)
    else:
        bs.update()

bs.listen()
bs.onkeypress(stopMovement, 'space')

if not in_motion 部分基本上运行算法的功能。 我已经尝试实现暂停和取消暂停的开关,但它似乎仍然不起作用。 帮助将不胜感激!

错别字:

def stopMovement():
    global in_motion
    if in_motion == True:
        in_motion == False
    else:
        in_motion == True

应该

def stopMovement():
    global in_motion
    if in_motion == True:
        in_motion = False # here
    else:
        in_motion = True # and here

两个等号太多了,效果这么大:)

没有可运行的代码来调试,我建议除了 @Justlearnedit 指出的= vs. ==错别字之外,我相信你的while True:正在阻止事件处理程序将键盘输入视为无限循环在事件驱动的世界中没有立足之地。 我们应该改用定时事件,我会重新编写您的代码,例如:

# Import libraries
from turtle import Screen, Turtle
# ...

# Configure some basic settings for the map
screen = Screen()  # Define the turtle screen
screen.bgcolor('black')  # Set the background colour
screen.setup(width=0.9, height=0.9)  # Setup the dimensions of the working window

# ...

in_motion = False

def flipMovement():
    global in_motion

    in_motion = not in_motion

def running():
    if not in_motion:
        screen.update()
    else:
        screen.title("Breadth first search algorithm ; steps taken: 0")

        # Set up classes
        title = title()
        building = Building()
        road = Road()
        start = Start()
        end = End()
        searcher = Searcher()
        route = Route()
        sprite = sprite()

        # Set up lists
        walls = []
        path = []
        routeCoords = []
        visited = set()
        frontier = deque()
        solution = {}

        # Activation
        setupMaze(mazeSet)
        search(start_x, start_y)
        correctRoute(end_x, end_y)
        sprite.moveSprite(end_x, end_y)

    screen.ontimer(running)

screen.onkeypress(flipMovement, 'space')
screen.listen()

running()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM