te42

# -*-coding:utf-8-*-  
class TheThing(object):

	def __init__(self):
		self.number = 0#初始化  .number是self的一个属性   self就是自己
		
	def some_function(self):
		print"i got called."#类下面的函数 打印xxxxx
	
	def add_me_up(self,more):
		self.number += more#对self.number加more
		return self.number
		
#two different things
a = TheThing()#初始化
b= TheThing()

a.some_function()
b.some_function()

print a.add_me_up(20)
print b.add_me_up(30)

print a.number
print b.number

#study this. this is how you pass a variable
#feom one class to another. you will need this
class TheMultiplier(object):
	
	def __init__(self,base):
		self.base = base  #base是再后面的参数 self是前面的参数
		
	def do_it(self,m):
		return m * self.base
		
x = TheMultiplier(a.number)   #等价与x 现在变成一个object 并且初始化 让x.base=a.number
print x.base
print x.do_it(b.number) #等价于 do_it(x,b.number)返回值是 x.base*b.number


from sys import exit
from random import randint  #aimed to create random number


class Game(object):


	def __init__(self,start):
		
		self.die  = ["you died.you kinda suck at this.","nice job, you died ... jackass.","suck a luser.","i have a puppy that's better at this."]
		self.start = start
	def play(self):
		next = self.start
		
		while True:
			#print "\n-------"
			room = getattr(self,next)
			next= room()
	
	def death(self):
		print self.die[randint(0,len(self.die)-1)]
		exit(1)
	
	
	def central_corridor(self):
		print"the Gothons of planet percal #25 have invaded your ship an destroyed"
		print"your entire crew. you are the last surviving menber an your last"
		print"mission is to get the neutron destruct bomb from the weapons armory"
		print"put it in the brige, and blow the ship up after geting into an"
		print"escape pod"
		print"\n"
		print"you're running down the central corridor to the weapons armory when"
		print"a Gothon jumps out, red scaly skin, ark grimy teeth, and evil clown costume"
		print"flowing around his hate filled bidy.he's blocking the door to the"
		print"armory and about to pull a weapon to blast you. "
	
		action = raw_input(">")
	
		if action == "shoot":
			print"quick on the draw you yank out your blaster and fire it at the Gothon."
			print"his clown costume is flowing and moving around his body, which throws"
			print"off your aim. your laser hits his costume but misses him entirely. this"
			print"completely ruins his brand new costume his mother bought him, which"
			print"makes him fly into an insane rage and blast you repeatedly in the face until"
			print"you are dead. then he eats you."
			return "death"
		elif action == "dodge":
			print"like a world class boxer you dodge, weave, slip and slide right"
			print"as the Gothon's blaster cranks a laser past your head."
			print"in the middle of your artful dodge your foot slips and you"
			print"bang your head on the metal wall and pass out."
			print"you wake up shortly after only to die as the Gothon stomps on"
			print"your head and eats you."
			print"death"
		
		elif action == "tell a joke":
			print"lucky for you they made you learn Gothon insults in the academy."
			print"you tell the one Gothon joke you know:"
			print"lbhe sa aoih aoid aiu aoih aojf aoijoafijo."
			print"the Gothon stops, tries not to laugh, then busts out laughing and can't move."
			print"while he's laughing you run up and shoot him square in the head"
			print"putting him down, then jump through the weapon armory door."
			return"laser_weapon_armory"
		
	
		else:
			print"does not compute."
			return"centeal_corridor"
		
	def laser_weapon_armory(self):
		print"you do a dove roll into the weapon armory, crouch and scan the room"
		print"for more Gothons that might be hiding. it's dead quiet, too quiet."
		print"you stand up and run to the far side of the room and find the"
		print"neutron bomb in the container. there's a keypad lock on the box"
		print"and you need the code to get the bomb out.if you get code"
		print"wrong 10 times then the lock closes forever an you can't"
		print"get the bomb.the code is 3 digits."
		code = "%d%d%d"%(randint(1,9),randint(1,9),randint(1,9))
		guess = raw_input("[keypad]>")
		guesses = 0
	
		while guess != code and guesses <10:
			print"bzzzzd"
			guesses += 1
			guess = raw_input("[keypad]>")
		
		if guess == code:
			print "the comntainer clicks open and the seal breaks, letting gas out."
			print"you grab the neutron bomb and run as fast as you can to the"
			print"brige where you must place it in the right spot"
			return"the_brige"
		else:
			print"the lock buzzes one last time and then you hear a sickening"
			print"melting sound as the mechanism is fused together."
			print"you decide to sit there, and finally the Gothons blow the"
			print"ship from their ship and you die."
			return"death"
		
		
	def the_bridge(self):
		print"you burst onto the brige with the neutron destruct bomb"
		print"under your arm and surprises 5 Gothons who are trying to"
		print"take control of the ship. each of them have an even uglier."
		print"clown costume than the last. they haven't pulled their"
		print"weapons out yet, as they see the active bomb under your"
		print"arm and don't want to set it off."
	
		action == input(">")
	
		if action == "throw the bomb":
			print"in a panic you throw the bomb at the group of Gothons"
			print"and make a leap for the door. right as you drop it a "
			print"Gothon shoots your right in the bavk killing you."
			print"as you die you see another Gothon frantically try to disarm"
			print"the bomb. you die knowing they will probably blow up when"
			print"it goes off."
			return "death"
		
		elif action == "slowly pace the bomb":
			print"you point your blaster at the bomb under your arm"
			print"and the Gothons put their hands up and start to sweat."
			print"you inch backward to the door, open it and then carefully"
			print"you jumped back the lock so the Gothons can't get out."
			print"now that the bomb is placed your run to escape pod to"
			print"get off this tin can"
			return"escape_pod"
		else:
			print"does not compute"
			return "the_bridge"
		
		
		
	def escape_pod(self):
		print"you rush through the ship desperately trying to make it to"
		print"the escape pod before the whole ship explodes. it seems like"
		print"hardly any Gothons are on the ship,so your run is clear of"
		print"interference. you get to the chamber with the escape pods, and"
		print"now need to pick one to take. some of them could be damaged"
		print"but you don't have time to look. there's 5 pods, whic one"
		print"do you take?"
	
		good_pod= randint(1,5)
		guess = raw_input("[pod]>")
	
	
		if int(guess) != good_pod:
			print"you jumped into %s and hit the eject buttom."%guess
			print"the pod escapes out into the void of space, then"
			print"implodes as the hull ruptures, crushing your body"
			print"into jam jelly"
			return"death"
		else:
			print"you jumped into %s and hit the eject buttom."%guess
			print"the pod easily slides out into the space heading to"
			print"the planet below. as it flies to the planet, as you look"
			print"back and see your ship implode then explose like a"
			print"bright star, taking put the Gothon ship at the same"
			print"time, you won!"
			exit(0)
		

a_game = Game("central_corridor")
a_game.play()
  1. 对class的理解更深一层了  class像一个函数,变量(称为属性) 集合  self就是那object
  2. __init__(self)是是初始化的一种方法
  3. 每个函数起码有一个参数(self)(不知道可不可以没有)
  4. getattr(看上去就像 get attribute) 访问某个参数 直接用”xx “  就可以访问xx了不再需要字典给函数取名字了
  5. a = Game() b = Game() 与Game()不是一个东西 对Game()修改以后就都变了但现在对a修改却只有a的属性的值会变
  6. __dict__得到class中的属性与相应的值  5中 a b 与Game()的属性值是不一定不一样的 而且Game会多出几个属性(我不知道是干什么的 如__main__)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值