-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeings.py
136 lines (107 loc) · 3.16 KB
/
beings.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import pygame
import random as rnd
import pandas as pd
import numpy as np
from misc import Position
from misc import OccupiedArea
import world_parameters
wp = world_parameters.Params()
occupied = OccupiedArea(wp.width, wp.height)
class Being(object):
def __init__(self, *args, **kwargs):
pass
def determineSpawnLoc(self):
pass
def checkPositionOK(self):
pass
def setBeingParameters(self, alive, position):
pass
def checkIfOccupied(self, positions):
return occupied.isOccupied(positions)
def drawSelf(self, surface):
pass
def died(self):
return None
class PointBeing(Being):
def drawSelf(self, surface):
x = self.position.x
y = self.position.y
pygame.draw.rect(surface, self.colour,
pygame.Rect(x, y, self.size, self.size))
def reportAreaOccupied(self):
return self.position
class Fungus(PointBeing):
def __init__(self, *args, **kwargs):
position = self.determineSpawnLoc(kwargs)
alive = self.checkPositionOK(position)
self.setBeingParameters(alive, position)
def determineSpawnLoc(self, kwargs):
if kwargs.get('position', None) == None:
#Fungus created by pressing space bar, position determined randomly
x = rnd.randint(0,wp.width)
y = rnd.randint(0,wp.height)
position = Position(x,y)
elif kwargs.get('position', None) != None:
#Fungus created by multiplying - position based on existing fungus
position = kwargs.get('position', None)
else:
print "something has gone wrong"
position = Position(-1,-1)
return position
def checkPositionOK(self, position):
if position == None:
#this shouldn't happen but is included to makes sure beings with no
#defined position end up dead.
print "returned none"
return False
elif position.x < 0 or position.x > wp.width - 1:
#position outside of the allowable x position range
print "outside x range"
return False
elif position.y < 0 or position.y > wp.height - 1:
#position outside the allowable y position range
print "outside y range"
return False
else:
#position is a valid position
pass
check = self.checkIfOccupied(position)
if check == True:
#if position is already occupied, the being dies
return False
elif check == False:
#if position is unoccupied, the being lives.
return True
def setBeingParameters(self, alive, position):
if alive:
self.position = position
self.alive = True
occupied.occupy(self, position)
self.colour = (255, 255, 255)
elif not alive:
self.position = Position(0,0)
self.alive = False
self.colour = (255, 0, 0)
else:
print "hmm"
self.spawnPosition = position
self.size = 1
self.multiplyProb = 0.004
self.isColony = False
def attemptToMultiply(self):
prob = self.multiplyProb
rand = rnd.uniform(0.0, 1.0)
if rand < prob:
offspring = self.multiply()
else:
#print "died"
offspring = self.died()
return offspring
def died(self):
return None
def multiply(self):
rng = rnd.SystemRandom()
newPos = Position(self.position.x + rng.randint(-5,5),
self.position.y + rng.randint(-5,5))
newFungus = Fungus(position=newPos)
return newFungus