|
| 1 | +from random import choice |
| 2 | + |
| 3 | +WHITE = color(255) |
| 4 | +BROWN = color(102,51,0) |
| 5 | +RED = color(255,0,0) |
| 6 | +GREEN = color(0,102,0) |
| 7 | +YELLOW = color(255,255,0) |
| 8 | +PURPLE = color(102,0,204) |
| 9 | + |
| 10 | +class Sheep: |
| 11 | + def __init__(self,x,y,col): |
| 12 | + self.x = x #x-position |
| 13 | + self.y = y #y-position |
| 14 | + self.sz = 10 #size |
| 15 | + self.energy = 30 #energy level |
| 16 | + self.eaten = False |
| 17 | + self.col = col |
| 18 | + self.age = 0 #age of sheep |
| 19 | + |
| 20 | + def update(self): |
| 21 | + #make sheep walk randomly |
| 22 | + move = 5 #the maximum it can move in any direction |
| 23 | + self.energy -= 1 #walking costs energy |
| 24 | + self.age += 0.1 #sheep loses a small part of its age as it walks |
| 25 | + if self.energy <= 0 or self.age >= 10: #remove a Sheep if energy is finished or its time is finished. |
| 26 | + sheepList.remove(self) |
| 27 | + if self.energy >= 50: |
| 28 | + self.energy -= 30 #giving birth takes energy |
| 29 | + #add another sheep to the list |
| 30 | + sheepList.append(Sheep(self.x,self.y,self.col)) |
| 31 | + self.x += random(-move, move) |
| 32 | + self.y += random(-move, move) |
| 33 | + #"wrap" the world Asteroids-style |
| 34 | + if self.x > width: |
| 35 | + self.x %= width |
| 36 | + if self.y > height: |
| 37 | + self.y %= height |
| 38 | + if self.x < 0: |
| 39 | + self.x += width |
| 40 | + if self.y < 0: |
| 41 | + self.y += height |
| 42 | + #find the patch of grass you're on in the grassList: |
| 43 | + xscl = int(self.x / patchSize) |
| 44 | + yscl = int(self.y / patchSize) |
| 45 | + grass = grassList[xscl * rows_of_grass + yscl] |
| 46 | + if not grass.eaten: |
| 47 | + self.energy += grass.energy |
| 48 | + grass.eaten = True |
| 49 | + fill(self.col) #its own color |
| 50 | + self.sz = 0.75*self.energy |
| 51 | + ellipse(self.x,self.y,self.sz,self.sz) |
| 52 | + |
| 53 | +class Grass: |
| 54 | + def __init__(self,x,y,sz): |
| 55 | + self.x = x #x-position |
| 56 | + self.y = y #y-position |
| 57 | + self.energy = 10 #energy from eating this patch |
| 58 | + self.eaten = False #hasn't been eaten yet |
| 59 | + self.sz = sz |
| 60 | + |
| 61 | + def update(self): |
| 62 | + if self.eaten: |
| 63 | + if random(100) < 5: |
| 64 | + self.eaten = False |
| 65 | + else: |
| 66 | + fill(BROWN) |
| 67 | + else: |
| 68 | + fill(GREEN) |
| 69 | + rect(self.x,self.y,self.sz,self.sz) |
| 70 | + |
| 71 | +sheepList = [] #list to store sheep |
| 72 | +grassList = [] #list to store grass“def setup(): |
| 73 | +patchSize = 10 #size of each patch of grass |
| 74 | +colorList = [WHITE,RED,YELLOW,PURPLE] |
| 75 | + |
| 76 | +def setup(): |
| 77 | + global patchSize, rows_of_grass |
| 78 | + size(600,600) |
| 79 | + rows_of_grass = height/patchSize |
| 80 | + noStroke() |
| 81 | + #create the sheep |
| 82 | + for i in range(20): |
| 83 | + sheepList.append(Sheep(random(width),random(height),choice(colorList))) |
| 84 | + #create the grass: |
| 85 | + for x in range(0,width,patchSize): |
| 86 | + for y in range(0,height,patchSize): |
| 87 | + grassList.append(Grass(x,y,patchSize)) |
| 88 | + |
| 89 | +def draw(): |
| 90 | + background(255) |
| 91 | + #update the grass first |
| 92 | + for grass in grassList: |
| 93 | + grass.update() |
| 94 | + #then the sheep |
| 95 | + for sheep in sheepList: |
| 96 | + sheep.update() |
0 commit comments