439093 ランダム
 HOME | DIARY | PROFILE 【フォローする】 【ログイン】

濡れ手にアワー

濡れ手にアワー

【毎日開催】
15記事にいいね!で1ポイント
10秒滞在
いいね! --/--
おめでとうございます!
ミッションを達成しました。
※「ポイントを獲得する」ボタンを押すと広告が表示されます。
x
2010/08/02
XML
カテゴリ:趣味
職場で「pythonという言語が最高にシビれる!」という事で

適当にサンプルを作ってみました。

感覚的には、ぬるいC++インタプリタという印象

これは流行りそうな気がします!!

将来的にクラウド化していく上で、こういう言語を覚えておくと飯の種になるかもしれません!?



環境はココで入手(Python 2.6.2のwindows版を使用)

http://www.python.jp/Zope/download/pythoncore

サンプル画面

python01.jpg

python02.jpg


python歴1日なのでコレくらいで勘弁してください(笑)

下記のソースを保存→全角を4タブに置換→scc.batを実行→ウマー?

ファイル名:scc.py

# coding: UTF-8

#===============================================
#
# python test program
#
#===============================================


#===============================================
# import
#===============================================
import sys
import commands
import random


#===============================================
# クラス作成
#===============================================
class MyClass:
 #***********************************************
 # メンバ
 #***********************************************
 #-----------------------------------------------
 # パラメータ等
 #-----------------------------------------------
 xxx  = 0          # 座標X
 yyy  = 0          # 座標Y
 mhp  = 0          # 最大HP
 hp  = 0          # HP
 meat = 0          # 最大腹
 eat  = 0          # 腹
 lv  = 0          # 攻撃/防御
 floor = 0          # 階数
 sq  = 0          # シーケンス
 subsq = 0          # サブシーケンス
 fmap = []         # マップ

 #-----------------------------------------------
 # フロアサイズ 16 * 8
 #-----------------------------------------------
 mx  = 16         # フロアX方向
 my  = 8          # フロアY方向


 #***********************************************
 # メンバ関数
 #***********************************************

 #-----------------------------------------------
 # パラメータ読み込み
 #-----------------------------------------------
 def para_read( sc ):
  # パラメータ読み込み
  file = open( "data.csv","r" )

  # パラメータ
  sc.xxx  = int( file.readline() )  # 座標X
  sc.yyy  = int( file.readline() )  # 座標Y

  sc.mhp  = int( file.readline() )  # 最大HP
  sc.hp  = int( file.readline() )  # HP

  sc.meat  = int( file.readline() )  # 最大腹
  sc.eat  = int( file.readline() )  # 腹

  sc.lv  = int( file.readline() )  # 攻撃/防御

  sc.floor = int( file.readline() )  # 階数

  sc.sq  = int( file.readline() )  # シーケンス

  sc.subsq = int( file.readline() )  # サブシーケンス

  file.close


 #-----------------------------------------------
 # パラメータ書き込み
 #-----------------------------------------------
 def para_write( sc ):
  file = open( "data.csv","w" )

  work = str( sc.xxx ) + "\n"
  file.write( work )

  work = str( sc.yyy ) + "\n"
  file.write( work )

  work = str( sc.mhp ) + "\n"
  file.write( work )

  work = str( sc.hp ) + "\n"
  file.write( work )

  work = str( sc.meat ) + "\n"
  file.write( work )

  work = str( sc.eat ) + "\n"
  file.write( work )

  work = str( sc.lv ) + "\n"
  file.write( work )

  work = str( sc.floor ) + "\n"
  file.write( work )

  work = str( sc.sq ) + "\n"
  file.write( work )

  work = str( sc.subsq ) + "\n"
  file.write( work )

  file.close


 #-----------------------------------------------
 # INIT
 #-----------------------------------------------
 def init( sc ):
  # パラメータ
  sc.xxx  = 1        # 座標X
  sc.yyy  = 1        # 座標Y

  sc.mhp  = 16       # 最大HP
  sc.hp  = 16       # HP

  sc.meat  = 128       # 最大腹
  sc.eat  = 64       # 腹

  sc.lv  = 1        # 攻撃/防御

  sc.floor = 1        # 階数

  sc.sq  = 0        # シーケンス

  sc.subsq = 0        # サブシーケンス

  # パラメータ書き込み
  sc.para_write()


 #-----------------------------------------------
 # init_floor作成
 #-----------------------------------------------
 def init_floor( sc ):
  # 配列作成
  sc.fmap = []
  x = 0
  y = 0

  while y < sc.my:

   while x < sc.mx:
    if y == 0 or y == (sc.my-1) or x == 0 or x == (sc.mx-1):
     sc.fmap = sc.fmap + [ "#" ]
    else:
     sc.fmap = sc.fmap + [ " " ]
    x = x + 1

   x = 0
   y = y + 1

  # 食料
  y = 0

  while y < sc.my*2:
   xx = int( random.uniform( 1,(sc.mx-1) ) )
   yy = int( random.uniform( 1,(sc.my-1) ) )
   if sc.fmap[ yy*sc.mx+xx ] == " ":
    sc.fmap[ yy*sc.mx+xx ] = "*"
    y = y + 1

  # 敵
  x = 0

  while x < sc.mx*2:
   xx = int( random.uniform( 1,(sc.mx-1) ) )
   yy = int( random.uniform( 1,(sc.my-1) ) )
   if sc.fmap[ yy*sc.mx+xx ] == " ":
    sc.fmap[ yy*sc.mx+xx ] = "!"
    x = x + 1

  # 開始位置
  sc.fmap[ sc.mx+1 ] = " "

  # 目的地
  sc.fmap[ sc.mx*sc.my-sc.mx-2 ] = "/"


 #-----------------------------------------------
 # floor読み込み
 #-----------------------------------------------
 def read_floor( sc ):
  # フロアをファイルから読み出し
  file = open( "floor.csv","r" )

  # 配列作成
  sc.fmap = []
  x = 0
  y = 0

  while y < sc.my:
   work = file.readline()

   while x < sc.mx:
    sc.fmap = sc.fmap + [ work[ x ] ]
    x = x + 1

   x = 0
   y = y + 1

  file.close


 #-----------------------------------------------
 # floor書き込み
 #-----------------------------------------------
 def write_floor( sc ):
  file = open( "floor.csv","w" )

  x = 0
  y = 0

  while y < sc.my:

   while x < sc.mx:
    if sc.fmap[ y*sc.mx+x ] == "@":
     file.write( " " )
    else:
     file.write( sc.fmap[ y*sc.mx+x ] )

    if x == (sc.mx-1):
     file.write( "\n" )
    x = x + 1

   x = 0
   y = y + 1

  file.close


 #-----------------------------------------------
 # floor表示
 #-----------------------------------------------
 def view_floor( sc ):
  # 表示
  sc.fmap[ sc.yyy*sc.mx+sc.xxx ] = "@"

  x = 0
  y = 0

  while y < sc.my:

   work = ""
   while x < sc.mx:
    work = work + sc.fmap[ y*sc.mx+x ];
    x = x + 1

   print work
   x = 0
   y = y + 1

  # 状態表示
  print "HP/MHP:",sc.hp,"/",sc.mhp
  print "食料:",sc.eat,"/",sc.meat
  print "階数:",sc.floor


 #-----------------------------------------------
 # 死亡
 #-----------------------------------------------
 def deth( sc ):
  print "力尽きた…\n"

  sc.init()
  sc.init_floor()
  sc.write_floor()

  print "パラメータを初期化しました\n"


 #-----------------------------------------------
 # 休憩
 #-----------------------------------------------
 def rest( sc ):
  # 消耗
  if sc.eat > 0:
   # 腹-1 HP+1
   sc.eat = sc.eat - 1
   sc.hp = sc.hp + 1
   if sc.hp > sc.mhp:
    sc.hp = sc.mhp
  else:
   # 腹=0 HP-1
   sc.hp = sc.hp - 1
   # HP=0 で死亡
   if sc.hp <= 0:
    sc.deth()


 #-----------------------------------------------
 # 戦闘
 #-----------------------------------------------
 def battle( sc ):
  item1 = [ "大声","暴力","圧力","賄賂","法的処置","情報操作","談合","政治圧力","闇の組織","武力行使","ダミー" ]
  item2 = [ "一般人","親","大家","上司","社長","市議会議員","国会議員","首相","敵対国","大統領","ダミー" ]

  # 攻撃
  print "あなたの",item1[ sc.lv-1 ],"による攻撃"
  dmg1 = sc.lv * 5
  dmg2 = int( random.uniform( dmg1/2,dmg1 ) )
  print item2[ sc.floor-1 ],"に",dmg2,"のダメージを与えた"
  sc.subsq = sc.subsq - dmg2

  # ダメージ
  if sc.subsq > 0:
   print item2[ sc.floor-1 ],"の攻撃"
   dmg1 = sc.floor * 3
   dmg2 = int( random.uniform( dmg1/2,dmg1 ) )
   print dmg2,"のダメージを受けた"
   sc.hp = sc.hp - dmg2
  else:
   sc.sq = 0
   sc.subsq = 0

  # 共通処理
  if sc.sq == 0:
   print item2[ sc.floor-1 ],"をこらしめた"
   # 1/5の確率でアイテムゲット
   if sc.lv == sc.floor:
    if int( random.uniform( 1,5 ) ) == 1:
     sc.lv = sc.floor + 1
     sc.mhp = 16 * sc.lv
     print item1[ sc.lv-1 ],"を手に入れた"

  # 状態表示
  print "HP/MHP:",sc.hp,"/",sc.mhp

  # HP=0 で死亡
  if sc.hp <= 0:
   sc.deth()


 #-----------------------------------------------
 # 階段
 #-----------------------------------------------
 def floor_up( sc ):
  sc.xxx = 1         # 座標X
  sc.yyy = 1         # 座標Y
  sc.floor = sc.floor + 1     # 階数

  sc.init_floor()

  print "ようこそ",sc.floor,"階へ"

  if sc.floor > 10:
   sc.clear()


 #-----------------------------------------------
 # クリア
 #-----------------------------------------------
 def clear( sc ):
  print "おめでとう!あなたは野望を達成した!!\n"

  sc.init()
  sc.init_floor()
  sc.write_floor()

  print "パラメータを初期化しました\n"



続く…





お気に入りの記事を「いいね!」で応援しよう

Last updated  2010/08/02 09:56:03 PM
[趣味] カテゴリの最新記事


PR

Profile

ツナ2.0

ツナ2.0

Calendar

Recent Posts

Favorite Blog

ハリーの気ままなブ… Harry326さん
「ボロ物件でも高利… CASHFLOW101さん
ふんどし王子のダイ… 大日本☆越中ふんどし王子202さん
キャンベル’S ザク… CAMPBELL☆さん
竹内かなと旧ブログ 竹内かなとさん

© Rakuten Group, Inc.