#==============================================================================
# ※ WaterWay
#------------------------------------------------------------------------------
# 小游戏:水道灌溉(仿《太阁立志传5》的农业小游戏。在金圭子复刻版本上修改。
#
# 脚本调用方法:SceneManager.call(Scene_WaterWay)
# SceneManager.scene.Start_WaterWay_Game(6)
# SceneManager.return
#==============================================================================
class Scene_WaterWay < Scene_Base
# 总的灌溉完成进度放在以如下常量为序号的系统变量中
CONST_WATERWAY_SCORE_V_NO = 737
#--------------------------------------------------------------------------
# ● 开始游戏,r指定了棋盘的大小,4~8,6即6X6,7即7X7,8即8X8
#--------------------------------------------------------------------------
def Start_WaterWay_Game(r = 6)
#
# 初始化棋盘和子块,即计算用的数据结构
# 棋盘左上角:164,91;左框左上角:25,20;水库左上角:272,2
[url=home.php?mod=space&uid=186386]@r[/url] = r
@chess = []
@new_block = [0,0,0,0]
@steps = 0
@r.times{@chess.push([])}
@r.times{@r.times{|i|@chess[i].push(0)}}
@game_over = false
# 生成游戏图形
# 生成显示端口
@viewport = Viewport.new(0, 0, 640, 480)
# 生成背景平面
@back_sprite = Sprite.new(@viewport)
@back_sprite.bitmap = Cache.picture("pano.png")
# 生成水源出口图象
@water_start = Sprite.new(@viewport)
@water_start.bitmap = Cache.character("water_start.png")
@water_start.x = 272
@water_start.y = 2
@water_start2 = Sprite.new(@viewport)
@water_start2.bitmap = Cache.character("spr16.png")
@water_start2.x = 308
@water_start2.y = 45
# 生成左框
@move_box = Sprite.new(@viewport)
@move_box.bitmap = Cache.character("move_box.png")
@move_box.x = 25
@move_box.y = 20
# 生成左框4个子块
@leftnew_sprites = []
for i in 0...4
@leftnew_sprites[i] = Sprite.new(@viewport)
@leftnew_sprites[i].bitmap = Cache.character("mud.png")
@leftnew_sprites[i].x = 42
@leftnew_sprites[i].y = 204 - i * 56
end
@leftnew_sprites[0].y = 212
# 生成全部地面
@character_sprites = []
@r.times{@character_sprites.push([])}
for i in 0...@r
for j in 0...@r
ww_sprite = Sprite.new(@viewport)
ww_sprite.bitmap = Cache.character("mud.png")
ww_sprite.x = i * 48 + 164
ww_sprite.y = j * 48 + 91
@character_sprites[i][j] = ww_sprite
end
end
# 生成光标图象
@move_point = Sprite.new(@viewport)
@move_point.bitmap = Cache.character("choose_box.png")
@move_point.x = 3 * 48 + 164
@move_point.y = 91
@move_point.z = 100
# 落下开始的4个子块
4.times{new_down}
# 刷新画面
@move_point.update
# 执行过渡
Graphics.transition
# 主循环
loop do
# 刷新游戏画面
Graphics.update
# 刷新输入信息
Input.update
# 刷新移动过子块后的画面
refresh_move
# 如果按下ESC或通水有了结果,就结束循环
if @game_over then
# 显示作业结果,等待240帧后场景消失
resultMessage = Window_Base.new(0, Graphics.height * 7 /8, Graphics.width, Graphics.height / 8)
resultMessage.draw_text_ex(0, 0, @str_result)
Graphics.wait(240)
resultMessage.dispose
break
end
end
# 释放游戏图形
dispose_WaterWay_Game
# 执行画面过渡
Graphics.transition
end
#--------------------------------------------------------------------------
# ● 释放动态分配的资源
#--------------------------------------------------------------------------
def dispose_WaterWay_Game
@back_sprite.dispose
@water_start.dispose
@water_start2.dispose
@move_box.dispose
@move_point.dispose
# 释放左框的子块
for sprite in @leftnew_sprites
sprite.dispose
end
# 释放通水的子块
for j in [email]0...@character_sprites.size[/email]
for sprite in @character_sprites[j]
sprite.dispose
end
end
# 释放显示端口
@viewport.dispose
end
#--------------------------------------------------------------------------
# ※ 按上下左右键移动画面
#--------------------------------------------------------------------------
def refresh_move
@move_point.update
if Input.trigger?(Input::DOWN)
if @move_point.y < @r * 48 + 91 - 48
@move_point.y += 48
end
elsif Input.trigger?(Input::LEFT)
if @move_point.x > 164
@move_point.x -= 48
end
elsif Input.trigger?(Input::RIGHT)
if @move_point.x < @r * 48 + 164 - 48
@move_point.x += 48
end
elsif Input.trigger?(Input::UP)
if @move_point.y > 91
@move_point.y -= 48
end
elsif Input.trigger?(Input::C)
put_judge((@move_point.x - 164)/48,(@move_point.y - 91)/48)
elsif Input.trigger?(Input::B)
# 按下了ESC,本次作业结束,做结局判断
end_judge
end
end
#--------------------------------------------------------------------------
# ※ 按确定键落子
#--------------------------------------------------------------------------
def put_judge(px, py)
if @chess[px][py] == 0
@character_sprites[px][py].bitmap = @leftnew_sprites[0].bitmap
@chess[px][py] = @new_block[0]
#落子后,左侧有新的子块生成
new_down
# 如果所有子块都已放上,则作业结束,做结局判断
end_judge if @new_block[0] == 0
end
end
#--------------------------------------------------------------------------
# ※ 新子块处理
#--------------------------------------------------------------------------
def new_down
# 左侧后三个子块往下落
@new_block[0] = @new_block[1]
@leftnew_sprites[0].bitmap = @leftnew_sprites[1].bitmap
@new_block[1] = @new_block[2]
@leftnew_sprites[1].bitmap = @leftnew_sprites[2].bitmap
@new_block[2] = @new_block[3]
@leftnew_sprites[2].bitmap = @leftnew_sprites[3].bitmap
# 新生成第四个子块
if @steps < @r * @r
tmparr = [3,5,6,7,9,10,11,12,13,14,15]
tmpint = tmparr[rand(11)]
@new_block[3] = tmpint
@leftnew_sprites[3].bitmap = Cache.character("spr#{tmpint}.png")
else
@new_block[3] = 0
@leftnew_sprites[3].bitmap = Cache.character("mud.png")
end
@steps = @steps + 1
for i in 0...4
@leftnew_sprites[i].update
end
end
#--------------------------------------------------------------------------
# ※ 结局判断
#--------------------------------------------------------------------------
def end_judge
allscore = 0
arr1 = [] # 土块数组
arr2 = [] # 中间块数组
arr3 = [] # 水块数组
allsum = @r * @r
allsum.times{|i|arr1.push(i)}
@water_start2.bitmap = Cache.character("wspr16.png")
if cross_judge(3,0,1) then
arr2.push(arr1.delete(t2o(3,0)))
end
while !arr2.empty?
tmp = arr2.pop
tx,ty = o2t(tmp)
if cross_judge(tx,ty,1) and ty > 0 then
arr2.push(arr1.delete(t2o(tx,ty-1))) if cross_judge(tx,ty-1,3) and arr1.include?(t2o(tx,ty-1))
end
if cross_judge(tx,ty,2) and tx < @r-1 then
arr2.push(arr1.delete(t2o(tx+1,ty))) if cross_judge(tx+1,ty,4) and arr1.include?(t2o(tx+1,ty))
end
if cross_judge(tx,ty,3) and ty < @r-1 then
arr2.push(arr1.delete(t2o(tx,ty+1))) if cross_judge(tx,ty+1,1) and arr1.include?(t2o(tx,ty+1))
end
if cross_judge(tx,ty,4) and tx > 0 then
arr2.push(arr1.delete(t2o(tx-1,ty))) if cross_judge(tx-1,ty,2) and arr1.include?(t2o(tx-1,ty))
end
tmpint = @chess[tx][ty]
@character_sprites[tx][ty].bitmap = Cache.character("wspr#{tmpint}.png")
allscore = allscore + 1
# 刷新游戏画面,让通了水的子块显示出来
Graphics.update
end
pscore = allscore * 100 / allsum
pjscore = pscore < 30 ? 1 : (pscore < 50 ? 2 : (pscore < 70 ? 3 : (pscore < 95 ? 4 : 5)))
case pjscore
when 1
pj = "完成度:#{pscore}% 级别:差。 如此……不如不做……"
$game_variables[CONST_WATERWAY_SCORE_V_NO]+=0
when 2
pj = "完成度:#{pscore}% 级别:一般。 馬馬虎虎……"
$game_variables[CONST_WATERWAY_SCORE_V_NO]+=5
when 3
pj = "完成度:#{pscore}% 级别:良好。 呵呵,還有進步空間……"
$game_variables[CONST_WATERWAY_SCORE_V_NO]+=10
when 4
pj = "完成度:#{pscore}% 级别:優秀。 這真是令人激賞的成績!"
$game_variables[CONST_WATERWAY_SCORE_V_NO]+=15
when 5
pj = "完成度:#{pscore}% 级别:完美。 奇功一件啊,你簡直大禹再世!"
$game_variables[CONST_WATERWAY_SCORE_V_NO]+=20
end
@str_result = pj
# 本此作业结束
@game_over = true
end
#--------------------------------------------------------------------------
# ※ 连通度判断
#--------------------------------------------------------------------------
def cross_judge(posx,posy,dire)
flag = false
case dire
when 1 # 上判断
flag = true if [3,5,7,9,11,13,15].include?(@chess[posx][posy])
when 2 # 右判断
flag = true if [3,6,7,10,11,14,15].include?(@chess[posx][posy])
when 3 # 下判断
flag = true if [5,6,7,12,13,14,15].include?(@chess[posx][posy])
when 4 # 左判断
flag = true if [9,10,11,12,13,14,15].include?(@chess[posx][posy])
end
return flag
end
#--------------------------------------------------------------------------
# ※ 2维变1维
#--------------------------------------------------------------------------
def t2o(posx,posy)
return posy * @r + posx
end
#--------------------------------------------------------------------------
# ※ 1维变2维
#--------------------------------------------------------------------------
def o2t(pos)
iy = pos / @r
ix = pos - iy * @r
return ix,iy
end
end