rogue_part1


※上記の広告は60日以上更新のないWIKIに表示されています。更新することで広告が下部へ移動します。

微妙にリファクタ。

テストコード書いていない。

まずい、

rogue_classes

#coding:utf-8
#ブロック入力のローグ。
def talklog(string):
  print string

def debuglog(string):
  print 'debug:%s'%string

class Charcter:
  DIRECT_TABLE={'north':(0,-1),'south':(0,1),'east':(1,0),'west':(-1,0)}
  def __init__(self,x,y,hp,pw,df,direct='north'):
    self.pos=[x,y]
    self.hp=hp
    self.pw=pw
    self.df=df
    self.direct = direct
    self.name='no_name'
  def damage(self,target):
    damage= target.pw - self.df
    self.hp -= damage
    talklog('%s:%dのダメージをうけた、のこり%d'%(self.name,damage,self.hp))
  def think(self):
    pass
  def get_direct(self):
    return self.direct
  def get_pos(self):
    return tuple(self.pos)
  def step(self,direction):
    x,y = Charcter.DIRECT_TABLE[direction]
    self.pos[0]+=x
    self.pos[1]+=y
  def calc_steped_pos(self,direction):
    x,y =Charcter.DIRECT_TABLE[direction]
    return (self.pos[0]+x,self.pos[1]+y)
  def get_name(self):
    return self.name
  def get_id(self):
    return 0

class Player(Charcter):
  def __init__(self,x,y):
    Charcter.__init__(self,x,y,100,10,10)
  def think(self,objs,stage):
    k = raw_input()
    if len(k)==0:
      k='n'
    act = 'rest'
    if k == 'j':
      self.direct='west'
      act = 'move'
    elif k == 'l':
      self.direct='east'
      act = 'move'
    elif k == 'i':
      self.direct='north'
      act = 'move'
    elif k == 'k':
      self.direct='south'
      act = 'move'
    elif k == ' ':
      act='attack'

    return (act,self.direct)

class Enemy(Charcter):
  def __init__(self,x,y,hp=100,pw=10,df=10):
    Charcter.__init__(self,x,y,hp,pw,df)
    self.name='storker'
  def think(self,objs,stage):
    p = filter(lambda o:isinstance(o,Player),objs)[0]
    x,y = p.get_pos()
    #探索もしないとは情けない
    dx = (x - self.pos[0])
    dy = (y - self.pos[1])
    if abs(dx) > abs(dy):
      if dx > 0:
        self.direct='east'
      else:
        self.direct='west'
    else:
      if dy > 0:
        self.direct='south'
      else:
        self.direct='north'

    return ('move',self.direct)

class Stage:
  #あとで実装。Stageデータは一度作成下ならば普遍なので、
  #今回はタプルで。
  def __init__(self,width,height):
    self.stage = self.__make_stage(width,height)
  def __make_stage(self,width,height):
    #data = [[1]*width]*height
    return ((1,)*10,)+((1,)+(0,)*8+(1,),)*8 +((1,)*10,)
  def is_wallpos(self,(x,y)):
    return self.stage[y][x]==1
  def __iter__(self):
    return (line for line in self.stage)

class ObjPool:
  def __init__(self,seq):
    self.pool=[]
    self.pool+=seq
  def get_existin(self,pos):
    for o in self.pool:
      if o.get_pos()==pos:
        return o
    return None
  def __iter__(self):
    return (item for item in self.pool)

class ConsoleView:
  FLOR_TILE=['.','H']
  DIR_CHAR={'north':'A','west':'<','east':'>','south':'V'}
  def __ratic_put(self,pos,flor,objs):
    inputed=False
    for obj in objs:
      if obj.get_pos() == pos:
        print ConsoleView.DIR_CHAR[obj.get_direct()],
        inputed=True
    if not inputed:
      print ConsoleView.FLOR_TILE[flor],
 
  def view(self,obj_pool,stage):
    for y,line in enumerate(stage):
      for x,flor in enumerate(line):
        self.__ratic_put((x,y),flor,obj_pool)
      print ''      

def addvect((x1,y1),(x2,y2)):
  return (x1+x2,y1+y2)

def main():
  console = ConsoleView()
  stage = Stage(10,10)
  obj_pool = ObjPool([Player(1,1),Enemy(8,8)])
  while True:
    console.view(obj_pool,stage)
    for obj in obj_pool:
      (action,direct) = obj.think(obj_pool,stage)
      if action == 'move':
        new_pos = obj.calc_steped_pos(direct)
        if not stage.is_wallpos(new_pos) and not obj_pool.get_existin(new_pos):
          obj.step(direct)
      elif action == 'attack':
        target = obj_pool.get_existin(addvect(obj.get_pos(),Charcter.DIRECT_TABLE[direct]))
        if target:
          #ターゲットが取得できたならダメージを与える
          target.damage(obj)
        else:
          talklog('%s:は風を切った!'%obj.get_name())
      debuglog('object%s-%d action:%s direction:%s'%(obj.get_name(),obj.get_id(),action,direct))

if __name__=='__main__':
  main()

rogue_part2

cuiのインプットについて