import random, copy import items ## monster,weapon, building and world classes class monster: def __init__(self,name,speed,damage,health=100): self.distance=100 self.health=health self.name=name self.speed=speed self.damage=damage self.value=10 def info(self): if self.health>0: print self.name + " is " + str(self.distance) + " feet away." return 0 else: print self.name + " is dead." return self.value def update(self): #move based on speed self.distance -= self.speed #Get too close and stil alive means boom if self.distance<1 and self.health>0: self.health=0 return self.damage return 0 #weapon class class weapon: def __init__(self,name="Basic Weapon",reach=60,damage=34,aoe=10): self.reach=reach self.damage=damage self.aoe=aoe self.name=name def fire(self,target,mlist): for m in mlist: if abs(m.distance - target)0: print "You were hit for " + str(dam) + " damage" def mrun(self): #fire ze missles templist= "Your men shot at " shotswitch=False for i in range(self.length): if self.towers[1][i]==1 and random.randrange(0,4): shotswitch=True temp=random.randrange(1,self.towers[0][i].reach) templist+= str(temp) + " " self.towers[0][i].fire(temp,self.mlist) elif self.towers[1][i]==3 and random.randrange(0,7): viable=filter(lambda x:x.distance<=self.towers[0][i].reach,self.mlist) if viable: shotswitch=True temp=random.choice(viable).distance templist+= str(temp) + " " self.towers[0][i].fire(temp,self.mlist) if shotswitch: print templist + "." #did anyone explode? for m in self.mlist: self.damage(m.update()) #more monsters if self.time%5==0: self.mlist+=[monster(*random.choice(items.m[max(min((self.time//20)-1,2),0)]))] if self.time%7==3 and self.time>10: self.mlist+=[monster(*random.choice(items.m[max(min((self.time//20)-1,2),0)]))] if self.time%11==0 and self.time>30: self.mlist+=[monster(*random.choice(items.m[max(min((self.time//20)-1,2),0)]))] if self.time%17==1 and self.time>50: self.mlist+=[monster(*random.choice(items.m[max(min((self.time//20)-1,2),0)]))] if self.time%19==6 and self.time>70: self.mlist+=[monster(*random.choice(items.m[max(min((self.time//20)-1,2),0)]))] #list them by distance self.mlist.sort(cmp=lambda x,y: cmp(y.distance, x.distance)) #Where are they? for m in self.mlist: self.score+=m.info() #dead monsters leave self.mlist=[m for m in self.mlist if m.health>0] #cheap weapons! self.offer=weapon(*random.choice(items.w[max(min((self.time//15)-1,1),0)])) if self.money>=50+self.time//2 and self.time%3==2: self.offer.info(" is for sale for " + str(50+self.time//2) + " dollars. It") #tick tock self.time+=1 def status(self): print "Health: " + str(self.health) + " Money: " + str(self.money) + " Score: " + str(self.score) def produce(self): tot=0 for b in self.towers[2]: self.money+=b.produce() ##init values score = 0 build={"Mini-money Tree":["Money eye",10],"Money eye":["Money mountain",15],"Money mountain":["Money cloud",20],"Money cloud":["Money dishwasher",23],"Money dishwasher":["Money dishwasher",25]} world=worldclass() welcome = "Welcome to Awesome Block Game 3.5 . You must protect the castle. Type help for help." helptext = "Commands are: quit, status, expand, produce, train, wait, inventory, upgrade, and fire x . Where x is some distance." print welcome #main loop while score==0: #get input and split it into a list of words i=raw_input("> ").lower() i=i.split(" ") if i[0]=="quit" or i[0]=="q": score=world.score elif i[0]=="help" or i[0]=="h": print helptext elif i[0]=="status" or i[0]=="s": world.status() elif i[0]=="produce" or i[0]=="p": world.produce() world.mrun() elif i[0]=="wait" or i[0]=="w": world.mrun() if not random.randrange(0,world.time + 5): print "You find 200 gold just lying around." world.money+=200 elif i[0]=="upgrade" or i[0]=="u" : if world.money>=10+2*world.time: print "Which slot?" slot=raw_input(": ") try: int(slot) except: print "We only accept numbers." else: if 0<=int(slot)=50+10*sum(world.towers[1]): print "Which slot?" slot=raw_input(": ") try: int(slot) except: print "We only accept numbers." else: if 1<=int(slot)=50+world.time//2: print "Which slot?" slot=raw_input(": ") try: int(slot) except: print "We only accept numbers." else: if 0<=int(slot)=50+20*world.length+20*(world.length//4): world.money-=50+20*world.length+20*(world.length//4) world.length+=1 world.towers[0]+=[weapon()] world.towers[1]+=[1] world.towers[2]+=[building()] print "You now have a castle " + str(world.length) + " towers long." world.mrun() else: print "No, you are short by " + str(50+20*world.length-world.money) + " dollars." # fire command. see if they gave a range and if it's legal. elif i[0]=="fire" or i[0]=="f": try : int(i[1]) except: print "Please enter a valid number after the word fire" else: firecmd=int(i[1]) if firecmd>world.towers[0][0].reach: print "It's too far away. You have a range of " + str(world.towers[0][0].reach) + " currently." else: world.towers[0][0].fire(firecmd,world.mlist) world.mrun() #bad command else: print '"' + i[0] + '" is not a recognized verb' #If you are dead if world.health<=0: score=world.score+100 #it's over! print "Game Over. " + str(score) + " was your score. Press enter to quit" raw_input()