Example #1
0
    def move(self, game):
        """
        The logic of moving the snake that includes collision detection and eating too.
        :return: whether the snake is still alive
        """
        # next location
        x, y = self.head.get_location()
        old_direction = self.direction
        # Calculate the next coordinate based on moving direction
        new = Location(*[sum(i) for i in zip((x, y), self.direction.value)])

        # verify valid move
        collision = self.check_collision(*new.get_location())

        if not collision:
            self.head.set_location(*new.get_location())
            self._add_body_part(Location(x, y), old_direction)

            # check whether he found food
            if self.check_for_food(game.board, new.get_location()):
                game.board.generate_food()
                game.score_inc(500)
            else:
                self.body.pop(-1)

        return not collision
Example #2
0
 def init(self):
     self.head: Location = Location(10, 10)
     self.body: List[Body] = [
         Body(Location(10, 11), Direction.UP),
         Body(Location(10, 12), Direction.UP),
         Body(Location(10, 13), Direction.UP)
     ]
     self.direction = Direction.UP
Example #3
0
 def simulateOnce(self, showCanvas=True):
     self.virus.spread()
     
     numFlyingIndividuals = self.settings.numFlyingIndividuals
     flyingIndividuals = random.sample(self.individuals, numFlyingIndividuals)
     for flyInd in flyingIndividuals:
         (x, y, z) = self.factory.generateLocation()
         flyInd.location = Location(x, y, z)
     
     
     if showCanvas: 
         self.mainView.updateCanvas(self.individuals)
     
     self.simResults.registerTimeEpoch()
Example #4
0
    def __init__(self, size):
        self.table_size = size
        self.food_location = Location(0, 0)

        self.generate_food()
Example #5
0
 def generate_food(self):
     self.food_location = Location(*random_position())
Example #6
0
 def generateIndividual(self, sirStatus):
     sphereLoc = self.generateLocation()
     location = Location(x=sphereLoc[0], y=sphereLoc[1], z=sphereLoc[2])
     individual = Individual(self.curIndex, location, sirStatus)
     self.curIndex += 1
     return individual
Example #7
0
 def createLocations(self):
     locations = CustomHashTable()
     addresses = self.getAddresses()
     for i in range(len(addresses)):
         locations[i] = Location(i, addresses[i])
     return locations