-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHuman.py
76 lines (57 loc) · 2.47 KB
/
Human.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""Consider using cartesian vectors only, and ditching the polar. (self.vector = x, y for direction)"""
from math import pi, sqrt, cos, sin
from random import random
from pygame import sprite, Color, Surface
from pygame.locals import *
import physics
import pygame
class Human(sprite.Sprite):
"""This is the Fish Sprite that will move around the aquarium. y-axis points DOWN"""
count = 0
def __init__(self, rect=None, color=None, deathSound=None):
sprite.Sprite.__init__(self)
Human.count += 1
self.fishID = Human.count
if color is not None:
self.color = color
else:
self.color = Color(255, 0, 0)
if rect is not None:
image = pygame.image.load(self.color)
self.image, self.rect = image, image.get_rect()
else:
self.image = Surface([100, 100])
self.image.fill(self.color)
self.rect = self.image.get_rect()
self.deathSound=deathSound
self.blindFOV = 0.5
self.blindLeft = pi - self.blindFOV/2.
self.blindRight = pi + self.blindFOV/2.
initialDirection = random()*2.0*pi
self.MAX_SPEED_X = 6.0
self.MAX_SPEED_Y = 6.0
self.xVel = self.MAX_SPEED_X*cos(initialDirection)
self.yVel = self.MAX_SPEED_Y*sin(initialDirection)
def __del__(self):
if self.deathSound is not None:
self.deathSound.play()
def calc_orientation(self):
"""Based on xVel, yVel, which way am I facing?
Change to call this once per timestep!"""
return physics.orientation_from_components(self.xVel, self.yVel)
def behind_me(self, otherFish):
"""Return boolean wether the other fish is behind this fish.
Uses xVel, yVel and position."""
theta1 = self.calc_orientation()
theta2 = self.direction_to(otherFish)
return abs(theta1-theta2) > self.blindLeft and abs(theta1-theta2) < self.blindRight
def direction_to(self, otherFish):
"""Use the two coordinates to determine direction to other fish."""
dx = otherFish.rect[0] - self.rect[0]
dy = otherFish.rect[1] - self.rect[1]
return physics.orientation_from_components(dx, dy)
def distance_to(self, otherFish):
"""Calculate the distance to another fish."""
myX, myY = self.rect[0], self.rect[1]
otherX, otherY = otherFish.rect[0], otherFish.rect[1]
return sqrt((myX-otherX)**2 + (myY-otherY)**2)