再次调用该函数时如何停止帧移动?

时间:2019-04-26 03:39:26

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

对于我的作业,我必须画一个矩形框,乌龟应该是一个点并移动到随机的目标位置。

当我按下空格键(开始和停止模拟)时,框架开始更改位置并以点反弹。点也不会移动,只会在中心反弹。

'''

import turtle
import random

#used to infect 
class Virus:
    def __init__(self, colour, duration):
        self.colour = colour
        self.duration = duration

## This class represents a person
class Person:
    def __init__(self, world_size):
        self.world_size = world_size
        self.radius = 7
        self.location = turtle.position()
        self.destination = self._get_random_location()

    #random locations are used to assign a destination for the person
    #the possible locations should not be closer than 1 radius to the edge of the world 
    def _get_random_location(self):
        x = random.randint(-349, 349)
        y = random.randint(-249, 249)
        return (x, y)


    #draw a person using a dot.  Use colour if implementing Viruses 
    def draw(self):
        turtle.penup()
        turtle.home()
        turtle.pendown()
        turtle.dot(self.radius*2)


    #returns true if within 1 radius
    def reached_destination(self):
        self.location = turtle.position()
        distX = abs(abs(self.destination[0])-abs(self.location[0]))
        distY = abs(abs(self.destination[1])- abs(self.location[1]))
        if distX and distY < self.radius:
            return True
        else:
            pass



    #Updates the person each hour.
    #- moves each person by calling the move method
    #- if the destination is reached then set a new destination
    #- progress any illness
    def update(self):
        self.move()
        if self.reached_destination():
            self._get_random_location()

        else:
            self.move()

    #moves person towards the destination
    def move(self):
        turtle.setheading(turtle.towards(self.destination))
        turtle.forward(self.radius/2)


class World:
    def __init__(self, width, height, n):
        self.size = (width, height)
        self.hours = 0
        self.people = []
        self.add_person()


    #add a person to the list
    def add_person(self):
        person = Person(1)
        self.people.append(person)

    #simulate one hour in the world.
    #- increase hours passed.
    #- update all people
    #- update all infection transmissions
    def simulate(self):
        self.hours += 1
        for item in self.people:
            item.update()

    #Draw the world.  Perform the following tasks:
    #   - clear the current screen
    #   - draw all the people
    #   - draw the box that frames the world
    #   - write the number of hours and number of people infected at the top of the frame
    def draw(self):
        turtle.clear()
        turtle.hideturtle()
        turtle.penup()
        turtle.right(180)
        turtle.forward(250)
        turtle.right(90)
        turtle.forward(350)
        turtle.left(180)
        turtle.pendown()
        turtle.forward(700)
        turtle.left(90)
        turtle.forward(500)
        turtle.left(90)
        turtle.forward(700)
        turtle.left(90)
        turtle.forward(500)
        turtle.right(180)
        turtle.forward(500)
        turtle.write(f'Hours: {self.hours}', False, 'left')
        turtle.update()
        for item in self.people:
            item.draw()



#---------------------------------------------------------
#Should not need to alter any of the code below this line
#---------------------------------------------------------
class GraphicalWorld:
    """ Handles the user interface for the simulation

    space - starts and stops the simulation
    'z' - resets the application to the initial state
    'x' - infects a random person
    'c' - cures all the people
    """
    def __init__(self):
        self.WIDTH = 800
        self.HEIGHT = 600
        self.TITLE = 'COMPSCI 130 Project One'
        self.MARGIN = 50 #gap around each side
        self.PEOPLE = 200 #number of people in the simulation
        self.framework = AnimationFramework(self.WIDTH, self.HEIGHT, self.TITLE)

        self.framework.add_key_action(self.setup, 'z') 
        self.framework.add_key_action(self.infect, 'x')
        self.framework.add_key_action(self.cure, 'c')
        self.framework.add_key_action(self.toggle_simulation, ' ') 
        self.framework.add_tick_action(self.next_turn)

        self.world = None

    def setup(self):
        """ Reset the simulation to the initial state """
        print('resetting the world')        
        self.framework.stop_simulation()
        self.world = World(self.WIDTH - self.MARGIN * 2, self.HEIGHT - self.MARGIN * 2, self.PEOPLE)
        self.world.draw()

    def infect(self):
        """ Infect a person, and update the drawing """
        print('infecting a person')
        self.world.infect_person()
        self.world.draw()

    def cure(self):
        """ Remove infections from all the people """
        print('cured all people')
        self.world.cure_all()
        self.world.draw()

    def toggle_simulation(self):
        """ Starts and stops the simulation """
        if self.framework.simulation_is_running():
            self.framework.stop_simulation()
        else:
            self.framework.start_simulation()           

    def next_turn(self):
        """ Perform the tasks needed for the next animation cycle """
        self.world.simulate()
        self.world.draw()

## This is the animation framework
## Do not edit this framework
class AnimationFramework:
    """This framework is used to provide support for animation of
       interactive applications using the turtle library.  There is
       no need to edit any of the code in this framework.
    """
    def __init__(self, width, height, title):
        self.width = width
        self.height = height
        self.title = title
        self.simulation_running = False
        self.tick = None #function to call for each animation cycle
        self.delay = 1 #smallest delay is 1 millisecond      
        turtle.title(title) #title for the window
        turtle.setup(width, height) #set window display
        turtle.hideturtle() #prevent turtle appearance
        turtle.tracer(0, 0) #prevent turtle animation
        turtle.listen() #set window focus to the turtle window
        turtle.mode('logo') #set 0 direction as straight up
        turtle.penup() #don't draw anything
        turtle.setundobuffer(None)
        self.__animation_loop()

    def start_simulation(self):
        self.simulation_running = True

    def stop_simulation(self):
        self.simulation_running = False

    def simulation_is_running(self):
        return self.simulation_running

    def add_key_action(self, func, key):
        turtle.onkeypress(func, key)

    def add_tick_action(self, func):
        self.tick = func

    def __animation_loop(self):
        try:
            if self.simulation_running:
                self.tick()
            turtle.ontimer(self.__animation_loop, self.delay)
        except turtle.Terminator:
            pass


gw = GraphicalWorld()
gw.setup()
turtle.mainloop()

'''

当我按空格键时,乌龟点应缓慢弹跳到随机位置,并且框架应保持静止。而且我知道代码对此深表歉意。

1 个答案:

答案 0 :(得分:0)

  

框架开始改变以点反弹的位置。点是   也不动,只在中心反弹。

我对下面的PersonWorld类进行了重新处理,以解决这两个问题。您获得的模拟模型只使用一只乌龟,这意味着我们必须遵循某些规则:

  • 每个Person必须跟踪自己的位置和前进方向

  • 如果使用乌龟进行计算,则仅draw()方法应将笔放下,所有其他Person方法应将笔放下

  • 每当Person使用乌龟时,您就无法假设它是什么,因为另一个Person只是在使用乌龟,因此您必须设置标题,位置和笔状态

已发布代码的更改部分:

FONT = ('Arial', 16, 'normal')

# This class represents a person
class Person():
    def __init__(self, world_size):
        self.world_size = world_size
        self.radius = 7
        self.location = turtle.position()
        self.destination = self._get_random_location()

        turtle.penup()
        turtle.setposition(self.location)
        turtle.setheading(turtle.towards(self.destination))
        self.heading = turtle.heading()

    # random locations are used to assign a destination for the person
    # the possible locations should not be closer than 1 radius to the edge of the world
    def _get_random_location(self):
        x = random.randint(self.radius - 349, 349 - self.radius)
        y = random.randint(self.radius - 249, 249 - self.radius)
        return (x, y)

    # draw a person using a dot.  Use colour if implementing Viruses
    def draw(self):
        x, y = self.location

        # use .circle() not .dot() as the latter causes an extra update (flicker)
        turtle.penup()
        turtle.setposition(x, y - self.radius)
        turtle.pendown()
        turtle.begin_fill()
        turtle.circle(self.radius)
        turtle.end_fill()

    # returns true if within 1 radius
    def reached_destination(self):
        distX = abs(self.destination[0] - self.location[0])
        distY = abs(self.destination[1] - self.location[1])
        return distX < self.radius and distY < self.radius

    # Updates the person each hour.
    # - moves each person by calling the move method
    # - if the destination is reached then set a new destination
    # - progress any illness
    def update(self):
        self.move()

        if self.reached_destination():
            self.destination = self._get_random_location()
            turtle.penup()
            turtle.setposition(self.location)
            turtle.setheading(turtle.towards(self.destination))
            self.heading = turtle.heading()

    # moves person towards the destination
    def move(self):
        turtle.penup()
        turtle.setheading(self.heading)
        turtle.setposition(self.location)
        turtle.forward(self.radius / 2)
        self.location = turtle.position()

class World:
    def __init__(self, width, height, n):
        self.size = (width, height)
        self.hours = 0
        self.people = []
        for _ in range(n):
            self.add_person()

    # add a person to the list
    def add_person(self):
        person = Person(1)
        self.people.append(person)

    # simulate one hour in the world.
    # - increase hours passed.
    # - update all people
    # - update all infection transmissions
    def simulate(self):
        self.hours += 1

        for item in self.people:
            item.update()

    # Draw the world.  Perform the following tasks:
    # - clear the current screen
    # - draw all the people
    # - draw the box that frames the world
    # - write the number of hours and number of people infected at the top of the frame
    def draw(self):
        turtle.clear()  # also undoes hideturtle(), etc.
        turtle.hideturtle()
        turtle.setheading(0)

        turtle.penup()
        turtle.setposition(-350, -250)

        turtle.pendown()

        for _ in range(2):
            turtle.forward(500)
            turtle.right(90)
            turtle.forward(700)
            turtle.right(90)

        for item in self.people:
            item.draw()

        turtle.penup()
        turtle.setposition(-350, -250)

        # leave this to the end as .write() forces an extra update (flicker)
        turtle.write(f'Hours: {self.hours}', move=False, align='left', font=FONT)

        turtle.update()

我还进行了一些调整以减少闪烁。我确信还有很多工作要做。

如果我们想更快地运行此模拟,我们将每个Person做成一个单独的乌龟(例如,通过继承),并使用重塑后的乌龟本身作为它在屏幕上的形状,而不是 draw Person。而且,我们会在大纲框和文本处放置单独的海龟以简化更新。

enter image description here