-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathsheepWolves.pyde
More file actions
86 lines (73 loc) · 2.14 KB
/
Copy pathsheepWolves.pyde
File metadata and controls
86 lines (73 loc) · 2.14 KB
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
'''Sheep and Wolves
April 10, 2018'''
import random
WHITE = color(255,255,255)
BLACK = color(0,0,0)
BLUE = color(0,0,255)
RED = color(255,0,0)
GREEN = color(0,102,0)
BROWN = color(102,51,0)
YELLOW = color(255,255,0)
PURPLE = color(102,0,204)
colors = [WHITE,BLUE,RED,YELLOW,PURPLE]
patchSize = 10
class Sheep:
def __init__(self,col):
self.x = random.randint(0,600)
self.y = random.randint(0,600)
self.col = col
self.energy = 20
self.move = 3 #how far they can move
def update(self,sheepList):
self.x += random.randint(-self.move,self.move)
self.y += random.randint(-self.move,self.move)
self.energy -= 1
patchx, patchy = patchNum(self.x,self.y)
if not grass[patchx*(600/patchSize)+patchy].eaten:
grass[patchx*(600/patchSize)+patchy].eaten = True
self.energy += patch.energy
#reproduce
if self.energy >= 50:
self.energy = 20
sheepList.append(Sheep(self.col))
fill(self.col)
en = map(self.energy,0,100,0,20)
ellipse(self.x,self.y,en,en)
class Patch:
def __init__(self,x,y):
self.x = x
self.y = y
self.eaten = False
self.col = GREEN
self.energy = 5
def update(self):
if self.eaten:
self.col = BROWN
if random.random()<0.5:
self.eaten = False
else:
self.col=GREEN
fill(self.col)
rect(self.x,self.y,patchSize,patchSize)
def setup():
global sheepList,grass
size(600,600)
noStroke()
grass = [Patch(x,y) for x in range(0,600,patchSize) \
for y in range(0,600,patchSize)]
sheepList = []
for x in range(20):
col = random.choice(colors)
sheepList.append(Sheep(col))
def draw():
background(GREEN)
for patch in grass:
patch.update()
for sheep in sheepList:
sheep.update(sheepList)
if sheep.energy <= 0:
sheepList.remove(sheep)
def patchNum(x,y):
xmult = x // patchSize
ymult = y // patchSize
return xmult,ymult