Created
July 10, 2010 11:26
-
-
Save johobemax/470648 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Character | |
attr_accessor :hp, :mp, :name | |
def initialize(name) | |
@hp = 100 | |
@mp = 100 | |
@name = name | |
end | |
def attack(enemy) # enemy<敵> | |
dam = rand(6) + 5 # 5~10のダメージを与える | |
enemy.hp -= dam | |
puts "#{@name}の物理攻撃!!" | |
puts "#{enemy.name}に#{dam}pointのダメージを与えた" | |
puts "残り#{enemy.hp}HPになった" | |
end | |
def magic(enemy) | |
if @mp >= 20 then | |
dam = rand(16) + 5 # 5~20のダメージを与える | |
@mp -= 20 | |
enemy.hp -= dam | |
puts "#{@name}の魔法攻撃!!" | |
puts "#{enemy.name}に#{dam}pointのダメージを与えた" | |
puts "残り#{enemy.hp}HPになった" | |
else | |
puts "MPが不足して魔法が使えなかった" | |
end | |
end | |
end | |
class Hero < Character | |
def portion() | |
@hp += 50 | |
if @hp > 100 then | |
@hp = 100 | |
end | |
puts "#{@name}は薬を使った!!" | |
puts "HPが#{@hp}に回復した" | |
end | |
end | |
class Monster < Character | |
attr_accessor :trophy #trophy<戦利品> | |
def initialize(name,trophy) | |
@trophy = trophy | |
super(name) | |
end | |
end | |
h = Hero.new("ビーマックス") | |
m = Monster.new("スライム","聖剣エクスカリバー") | |
until h.hp <= 0 or m.hp <= 0 | |
puts "#{h.name}のターン" | |
puts "1.攻撃 2.魔法 3.薬" | |
case gets.to_i | |
when 1 then | |
h.attack(m) | |
when 2 then | |
h.magic(m) | |
when 3 then | |
h.portion() | |
else | |
puts "うっかりして攻撃のチャンスを逃した" | |
end | |
puts | |
puts "press EnterKey" | |
gets | |
case rand(2) + 1 | |
when 1 then | |
m.attack(h) | |
when 2 then | |
m.magic(h) | |
end | |
puts | |
puts "#{h.name}:HP #{h.hp}:MP #{h.mp}" | |
puts "#{m.name}:HP #{m.hp}:MP #{m.mp}" | |
puts | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
作りかけのRubyでGameプログラミング