如何使用面向对象编程在Python中编程乌龟和兔子比赛,更具体地说,我们如何让它们实际移动?

2024-05-16 23:48:59 发布

您现在位置:Python中文网/ 问答频道 /正文

Hi there! Right now we are trying to use object-oriented programming to make the tortoise and the hare race. We are coding in jupyter notebook with python3. This is what we have so far.

This is the first part of the code and it seems like the way we get the animals to move is with the advance position method but we don't know what to put in there to get them to move. This will advance the position of the animal for a single second, based on the animal definition for speed and naptime.

class animal:
    time = 0

    def __init__(self, name, species, speed, napStart, napDuration, currentPosition):
        self.name = str(name)
        self.species = str(species)
        self.speed = int(speed)
        self.napStart = int(napStart)
        self.napDuration = int(napDuration)
        self.currentPosition = int(currentPosition)

    def __str__(self):
        return str(self.name) + " " + str(self.species) + " " + str(self.speed) + " " + str(self.napStart) + " " + str(self.napDuration) + " " + str(self.currentPosition)

    def getName(self):
        return self.name

    def getSpeed(self):
        return self.speed

    def getNapStart(self):
        return self.napStart

    def getNapDuration(self):
        return self.napDuration

    def getCurrentPosition(self):
        return self.currentPosition

    def getSpecies(self):
        return self.species

    def advancePosition():
        pass

    def setSpeed(self, newSpeed):
        self.speed = newSpeed

    def setNapStart(self, newNapStart):
        self.napStart = newNapStart

    def setNapDuration(self, newNapDuration):
        self.napDuration = newNapDuration

    def setCurrentPosition(self, newCurrentPosition):
        self.currentPosition = newCurrentPosition

Then we have the race code which doesn't work yet because I have no idea how to make them move in the first place

class Race:

    def __init__(self, eventName, distance, contestant1, contestant2):
        self.eventName = eventName
        self.distance = distance
        self.contestant1 = contestant1
        self.contestant2 = contestant2

    def __str__(self):
        return str(self.eventName) + " " + str(self.distance) + " " + str(self.contestant1) + " " + str(self.contestant2)

    def getEventName(self):
        return self.eventName

    def getDistance(self):
        return self.distance

    def runRace(self):
        pass

The last thing I have are just tester animals so you get an idea of what they do kind of?

tortoise = animal("tort", "tortoise", 2, 0, 0, 0)
hare = animal("harry", "hare", 10, 5, 20, 0)

And this is for the race code

race1 = Race("the race", 30, tortoise, hare)

Thank you so much for your help and if you need more info I can totally give it to you :)


Tags: thetonameselfreturndefwespecies
2条回答

方法advancePosition(self)可能只是将动物的速度加到位置上(即乌龟每秒钟移动3个单位,因此在1秒内它的位置现在是3)。每次更新都叫这个。你知道吗

我不知道“小睡”是为了什么,也不知道这场比赛的逻辑是什么,但这应该是一个开始。你知道吗

def runRace(self):
    time = 0
    while self.contestant1.getCurrentPosition() < self.getDistance() and self.contestant2.getCurrentPosition() < self.getDistance():
        time += 1
        if self.contestant2.getNapStart() > time or self.contestant2.getNapStart() + self.contestant2.getNapDuration < time:
            self.contestant1.advancePosition()
        self.contestant1.advancePosition()
    if self.contestant1.getCurrentPosition > self.contestant2.getCurrentPosition:
        return self.contestant1
    else: 
        return self.contestant2



def advancePosition():
    self.currentPosition += self.getSpeed()


winner = race1.runRace()
print(winner.getName())

小心,在这个示例代码中只有参赛者2可以小睡(应该总是兔子)

相关问题 更多 >