-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.py
244 lines (212 loc) · 6.76 KB
/
world.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import pygame, sys
from pygame.locals import *
from math import *
from random import shuffle,randint,random
pygame.init()
fpsClock = pygame.time.Clock()
window = pygame.display.set_mode((640,480))
pygame.display.set_caption("Pygame sample.")
black=pygame.Color( 0, 0, 0)
white=pygame.Color(255,255,255)
UP,DOWN,LEFT,RIGHT = range(4)
SPACE,SOLID,HUMAN,ZOMBIE = range(4)
colors = [(255,255,255),(0,0,0),(0,255,0),(0,0,128)]
class Cell:
color = (0,0,0)
def step(self,world):
return True
def canEnter(self):
return True
def canAttack(self):
return False
class AttackableCell(Cell):
underAttack = False #Gets set to true if you were attacked during the last update.
attack = 0
health = 100
dmg = 0
def alive(self):
return self.health > self.dmg
def canEnter(self):
return False
def canAttack(self):
return True
def dammage(self,dmg):
self.underAttack = True
self.dmg += dmg
return self.health <= self.dmg
def getAttack(self): #Whoevers attack is higher wins the cell.
return self.attack
class SPACE(Cell):
color = (255,255,255)
class SOLID(Cell):
canEnter = lambda x:False
class HUMAN(AttackableCell):
attack = 10
breedChance = .2
health = 100
color = (0,255,0)
age = 0
def step(self,world):
self.age = self.age+1
self.dmg += .1
self.attack += .01
return self.alive()
def breed(self,other):
print "Breed"
if self.age<10 or other.age<10: return
if random()>(self.breedChance+other.breedChance)/2: return
merge = lambda a,b:(a+b)*(.9+.2*random())/2
child = HUMAN()
child.attack = merge(other.attack,self.attack)
child.health = merge(other.health,self.health)
child.breedChance = merge(other.breedChance,self.breedChance)
self.dmg += 10
other.dmg += 10
print child.attack, child.health
return child
def attackHuman(self):
return random()<.3
class ZOMBIE(AttackableCell):
attack = 5
health = 75
color = (255,0,0)
def step(self,world):
self.dmg += .1
return self.alive()
def convert(self,src):
z = ZOMBIE()
z.attack = self.attack*(random()*.1+.9)
z.health = src.health*.75*(random()*.1+.9)
return z
def drawCells(world):
global window,colors
size = 20
for r,row in enumerate(world):
for c,v in enumerate(row):
pygame.draw.rect(window,v.color,(c*size,r*size,size,size))
def humanStep(r,c,world):
global LEFT,RIGHT,UP,DOWN
l = [ LEFT,RIGHT,UP,DOWN]
if isinstance(world[r+1][c],ZOMBIE):
return DOWN
if isinstance(world[r][c-1],ZOMBIE):
return LEFT
if isinstance(world[r][c+1],ZOMBIE):
return RIGHT
if isinstance(world[r-1][c],ZOMBIE):
return UP
shuffle(l)
world[r][c].underAttack = False#reset that flag
return l[0]
def zombieStep(r,c,world):
global LEFT,RIGHT,UP,DOWN
l = [ LEFT,RIGHT,UP,DOWN, None,None,None,None]
if world[r][c].underAttack:
if isinstance(world[r+1][c],AttackableCell):
return DOWN
if isinstance(world[r][c-1],AttackableCell):
return LEFT
if isinstance(world[r][c+1],AttackableCell):
return RIGHT
if isinstance(world[r-1][c],AttackableCell):
return UP
world[r][c].underAttack = False
shuffle(l)
return l[0]
def updateCells(world):
stats = []
moves= []
for r,row in enumerate(world):
for c,v in enumerate(row):
m = None
if not v.step(world):
world[r][c] = SPACE()
continue
if isinstance(v,HUMAN):
stats.append((HUMAN, v.health, v.attack))
m = humanStep(r,c,world)
elif isinstance(v,ZOMBIE):
stats.append((ZOMBIE, v.health, v.attack))
m = zombieStep(r,c,world)
if m!=None:
moves.append((m,r,c))
shuffle(moves)
dead = []
for m,r,c in moves:
if (r,c) in dead: continue
d=None
if m == LEFT:
d = (0,-1)
elif m==RIGHT:
d = (0,1)
elif m==UP:
d = (-1,0)
elif m==DOWN:
d = (1,0)
targPos = (d[0]+r,d[1]+c)
targ = world[targPos[0]][targPos[1]]
doMove = False
died = False
if isinstance(targ,HUMAN) and isinstance(world[r][c],HUMAN):
print "Human collision",world[r-d[0]][c-d[1]],r-d[0],c-d[1]
if world[r][c].attackHuman():
died = targ.dammage(world[r][c].getAttack())
if died:
doMove = True
dead.append(targPos)
elif isinstance(world[r-d[0]][c-d[1]],SPACE):
child = world[r][c].breed(targ)
if child:
world[r-d[0]][c-d[1]] = child
continue
elif isinstance(targ,ZOMBIE) and isinstance(world[r][c],ZOMBIE):
continue
else:
if targ.canEnter():
doMove = True
elif targ.canAttack():
print "attack!"
died = targ.dammage(world[r][c].getAttack())
if isinstance(targ,HUMAN):
died = False
world[targPos[0]][targPos[1]] = world[r][c].convert(targ)
if died:
doMove = True
dead.append(targPos)
if doMove:
#del world[d[0]][d[1]]
world[targPos[0]][targPos[1]] = world[r][c]
world[r][c] = SPACE()
return stats
world=[[]]
world[0] = [SOLID()]*20
world += [[SOLID()]+[SPACE()]*18+[SOLID()] for x in range(18)]
world += [[SOLID()]*20]
for n in xrange(1,5):
world[n][n] = HUMAN()
world[-2][-2]=ZOMBIE()
humanDrop = True
from time import time
lastTime = time()
while True:
if time()-lastTime > 5:
r,c = randint(0,19),randint(0,19)
if isinstance(world[r][c],SPACE):
cls = HUMAN if humanDrop else ZOMBIE
world[r][c] = cls()
humanDrop = not humanDrop
lastTime = time()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
window.fill(white)
stats = updateCells(world)
drawCells(world)
for n,(spec, health, attack) in enumerate(stats):
color = (255,0,0)
if spec == HUMAN:
color = (0,255,0)
pygame.draw.line(window,color,(400,n*1),(400+int(health),n*1),1)
pygame.display.update()
fpsClock.tick(30)