Created
January 22, 2012 17:16
-
-
Save orlp/1657726 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Eye(object): | |
| MAX_EYESIGHT = 500 | |
| FOV = 160 | |
| IMAGE_RESOLUTION = 360 | |
| def __init__(self, owner, game): | |
| self.owner = owner | |
| self.x = 0 | |
| self.y = 0 | |
| self.direction = 0 | |
| def update(self, x, y, direction): | |
| self.x = x | |
| self.y = y | |
| self.direction = direction | |
| def render(self, game): | |
| distances = [self.MAX_EYESIGHT+1] * 360 | |
| raw_image = [self.owner.COLOR_NOTHING] * 360 | |
| for creature in game.creaturelist: | |
| angle = math.atan2(creature.y-self.y, creature.x-self.x) | |
| angle = int(math.degrees(angle)) | |
| distance = math.hypot(creature.x-self.x, creature.y-self.y) | |
| if distance < distances[angle]: # If the creature is nearer than the last object stored in that pixel | |
| distances[angle] = distance | |
| raw_image[angle] = self.owner.COLOR_CREATURE | |
| for food in game.foodlist: | |
| angle = math.atan2(food.y-self.y, food.x-self.x) | |
| angle = int(math.degrees(angle)) | |
| distance = math.hypot(food.x-self.x, food.y-self.y) | |
| if distance > distances[angle]: # If the food is nearer than the last object stored in that pixel | |
| distances[angle] = distance | |
| raw_image[angle] = self.owner.COLOR_FOOD | |
| # Now remove anything not in the FOV, and store what is in a list | |
| start_angle = int(self.direction - self.FOV/2) | |
| stop_angle = int(start_angle + self.FOV) | |
| image = [] | |
| for angle in range(start_angle, stop_angle): | |
| image.append(raw_image[angle % 360]) | |
| return image |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment