roguev6 > guifield.py


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

だいぶそれっぽい画面は出力できるようになってきた・・・のか?

roguev6/image

…これはほとんどオジャンにするコードだけど。

#coding:shift-jis
import pygame
from field import TileType
import random
from guirogue import entry,SequenceBase,VPad

#クラスSurface確保のコードのために
#メタクラスを利用したほうがいいか、面倒か。
class Window:
  #coding:shift-jis
  TEXT_TABLE=(u'あいうえおかきくけこさしすせそ'
      u'たちつてとなにぬねのはひふへほ'
      u'まみむめもやゐゆゑよらりるれろ'
      u'わをんゃゅょぁぃぅぇぉゎっ!?'
      u'がぎぐげござじずぜぞだぢづでど'
      u'ばびぶべぼぱぴぴぺぽ¥ー~=:'
      u'アイウエオカキクケコサシスセソ'
      u'タチツテトナニヌネノハヒフヘホ'
      u'マミムメモヤヰユヱヨラリルレロ'
      u'ワヲンャュョァィゥェォヮッ、。'
      u'ガギグゲゴザジズゼゾダヂヅデド'
      u'バビブベボパピプペポヴ()… '
      u'0123456789+-   ')
  boot_flag=False
  @classmethod
  def boot(cls):
    if cls.boot_flag:return 
    cls.boot_flag=True
    cls.text_surface= pygame.image.load('./img/88font.png')
    cls.text_surface.set_colorkey(0x0)
    cls.window_surface = pygame.image.load('./img/windowframe.png')
    cls.window_surface.set_colorkey(0x0)

    cls.parts = {}
    setting =zip(
        ['top_left',   'top',   'top_right',
         'left',       'enter', 'right',
         'bottom_left','bottom','bottom_right'],
        [(x,y) for y in range(3) for x in range(3)])
    for (key,(x,y)) in setting:
       cls.parts[key] = pygame.Rect((x*8,y*8,8,8)),

  def __init__(self,(width,height),frame=True):
    self.textnums=[len(Window.TEXT_TABLE)-1,] 
    self.window_width = width+2 if frame else width
    self.text_width = width
    self.window_height = height+2 if frame else height
    self.text_height = height
    self.frame=frame

    Window.boot()
  def draw(self,pos,back):
    if self.frame:
      self._draw_window(pos,back)
    self._draw_text(pos,back)
  def _draw_text(self,pos,back):
    chipsize=8
    width=15
    for i,num in enumerate(self.textnums):
      #すまん、次のバージョンでちゃんと整理するからゆるして
      x = (i)%self.text_width+(1 if self.frame else 0)
      y = (i)/self.text_width+(1 if self.frame else 0)
      if y > self.text_height:
        break
      font_rect = pygame.Rect(
          ((num%width)*chipsize,
          (num/width)*chipsize, chipsize, chipsize)
          )
 
      back.blit(
          Window.text_surface,
          (x*chipsize+pos[0],y*chipsize+pos[1]),
          font_rect)
  def _draw_window(self,(adx,ady),back):
    def _parts_draw(x,y,parts_name):
      back.blit(
              Window.window_surface,
              (x*8+adx,y*8+ady),
              Window.parts[parts_name])
    xedge=self.window_width-1
    yedge=self.window_height-1
    for y in xrange(self.window_height):
      for x in xrange(self.window_width):
        if x==0:
          if y==0:
            _parts_draw(x,y,'top_left')
          elif y==yedge:
            _parts_draw(x,y,'bottom_left')
          else:
            _parts_draw(x,y,'left')
        elif x==xedge:
          if y==0:
            _parts_draw(x,y,'top_right')
          elif y==yedge:
            _parts_draw(x,y,'bottom_right')
          else:
            _parts_draw(x,y,'right')
        elif y==0:
          _parts_draw(x,y,'top')
        elif y==(self.window_height-1):
          _parts_draw(x,y,'bottom')
        else:
          pygame.draw.rect(
            back,
            0xFFFFFF,
            pygame.Rect(x*8+adx,y*8+ady,8,8))

  def set_text(self,text):
    self.textnums= [Window.string_to_num(c) for c in text]

  @classmethod
  def string_to_num(cls,uni_char):
    if not uni_char in cls.TEXT_TABLE:
      order = len(cls.TEXT_TABLE)-1
    else:
      order = cls.TEXT_TABLE.find(uni_char)
    return order

class Papet:
  boot_flag=False
  @classmethod
  def boot(cls):
    if cls.boot_flag:return
    cls.boot_flag=True
    cls.surf = pygame.image.load('img/charbase.png')
    cls.surf.set_colorkey(0x2)
    cls.normal = [pygame.Rect(( 0,48,16,16)),
                  pygame.Rect((16,48,16,16)),
                  pygame.Rect(( 0,48,16,16)),
                  pygame.Rect((32,48,16,16))]
  def __init__(self):
    Papet.boot()
    self.time =0
  def update(self):
    self.time += 1
    if self.time/60 > len(Papet.normal)-1:
      self.time = 0

  def draw(self,pos,back):
    back.blit(
        Papet.surf,
        pos,
        Papet.normal[self.time/60]
        )

class TestSeqTile(SequenceBase):
  boot=False
  tile_surface=None
  tilerec={}
  def __init__(self):
    SequenceBase.__init__(self)
    if not TestSeqTile.boot:
      TestSeqTile.init()
    self.field = []
    for x in range(10):
      line = [random.randint(0,1) for x in range(10)]
      self.field.append(line)
    self.human = Papet()
    self.pos=[0,0]
    self.rel=True

    self.logwindow = Window((10,3))
    self.logwindow.set_text(
        u'テストです てすと。\nカラスは+10デシベルのこうげきをうけた')
    self.statewindow = Window((10,1),frame=False)
    self.statewindow.set_text(u'100:100')

  @classmethod
  def init(cls):
    cls.boot=True
    cls.tilesurface = pygame.image.load('img/sampleblock.png')
    cls.tilesurface.set_colorkey(0x0)
    cls.tilerec[TileType.WALL] = pygame.Rect((0,0,16,32))
    cls.tilerec[TileType.FLOOR] = pygame.Rect((16,0,16,32))
  def update(self):
    self.human.update()
    pat = VPad.get_instance()
    if pat.on('up'):
      self.pos[1]-=1
    elif pat.on('down'):
      self.pos[1]+=1
    elif pat.on('left'):
      self.pos[0]-=1
    elif pat.on('right'):
      self.pos[0]+=1

  def draw(self,back):
    for y,line in enumerate(self.field):
      for x,c in enumerate(line):
        #field
        back.blit(
            TestSeqTile.tilesurface,
            (x*16,(y-1)*12),
            TestSeqTile.tilerec[c])
        #charcter
        if x==self.pos[0] and y==self.pos[1]:
          self.human.draw((x*16,(y)*12),back)
    #window
    self.logwindow.draw((0,60),back)
    self.statewindow.draw((0,0),back)
if __name__=='__main__':
  entry(TestSeqTile())

roguev6/guiroguev2.py