#==============================================================================
# ■ Game_Neigong
#------------------------------------------------------------------------------
# 真气条小游戏(贪吃蛇),继承Window_Base类来实现
# 调用方法 mywnd = Game_Neigong.new
# mywnd.close
# mywnd.dispose
# 显示文字 :循環周天結束!已沖過的穴道數是\V[859]
#==============================================================================
class Game_Neigong < Window_Base
#--------------------------------------------------------------------------
# ● 初始化对像
#--------------------------------------------------------------------------
def initialize
@mySize = 30 # 界面大小(单位:图块)
@myWidth = 30 * 12 # 界面宽度(单位:像素)
@myHeight = 30 * 12 # 界面高度(单位:像素)
# 界面背景图的位图
@bgBitmap = Bitmap.new("Graphics/Pictures/ngxl_bg.png")
# 分五行的真气的图形文件名,每次小游戏一开始会随机选定一种
@zhenqi = "Graphics/Pictures/zhenqi" + rand(5).to_s + ".png"
# 蛇身上的一节的位图,节数会越来越多,蛇也越来越长
@NeiGongBitmap = Bitmap.new("Graphics/Pictures/neigong.png")
# 小游戏的整个外框,大小与背景图片相同
super(0, 0, @bgBitmap.width, @bgBitmap.height)
self.opacity = 32
@score = 0 # 初始分数
@level = 1 # 初始化难度等级
@max_length = 35 # 初始化真气条最大长度,即蛇身的最大长度
@array_Neigong = [[14,8],[13,8]] # 界面划分成格子
@speed = 20 # 初始化速度,数值越大,移动速度越慢
@direction = "left" # 初始化方向
@run = false # 初始化真气条是否为运动状态
@time_count = 0 # 计时器
@zhenqi_exist = false # 标记真气是否存在
@bGameOver = false # 标记游戏是否终结
game_Neigong_Main
end
#--------------------------------------------------------------------------
# ● 主处理
#--------------------------------------------------------------------------
def game_Neigong_Main
# 真气条背景
@sprite = Sprite.new
@sprite.bitmap = @bgBitmap
# 初始化屏幕
@sprite_clear = Sprite.new
@sprite_clear.bitmap = Bitmap.new(@myWidth,@myHeight)
@sprite_clear.x = 254
@sprite_clear.y = 70
# 显示得分
@sprite_score = Sprite.new
@sprite_score.bitmap = Bitmap.new(@myWidth,@myHeight)
@sprite_score.x = 254
@sprite_score.y = 440
refresh_score
# 初始化蛇的图形
@sprite_Neigong = []
for i in [email]0...@array_Neigong.length[/email]
@sprite_Neigong[i] = Sprite.new
@sprite_Neigong[i].bitmap = @NeiGongBitmap
@sprite_Neigong[i].x = @array_Neigong[i][0] * @mySize
@sprite_Neigong[i].y = @array_Neigong[i][1] * @mySize
end
# 描绘真气(蛇要去吃的饵食)
@sprite_zhenqi = Sprite.new
@sprite_zhenqi.bitmap = Bitmap.new(@zhenqi)
# 执行画面过渡
Graphics.transition
# 主循环
loop do
# 刷新游戏画面
Graphics.update
# 刷新输入信息
Input.update
# 刷新画面情报
update
# 如果标记游戏终结,就中断循环
if @bGameOver
break
end
end
# 执行画面过渡
Graphics.transition
end
#--------------------------------------------------------------------------
# ● 刷新画面
#--------------------------------------------------------------------------
def update
# 如果真气不存在则描绘真气
if @zhenqi_exist == false
# 描绘真气
draw_zhenqi
@zhenqi_exist = true
end
# 开始运动
start_move
# 如果真气条正在运动
if @run
if @time_count % @speed == 0
move1
if check_eat
@score += 1
# 刷新等级
if @score >= @level * 20
@level += 1
@speed -= 1
if @level > 6
#p "本次周天循環沖穴數量已達上限!"
exit
end
end
refresh_score
# 增加真气条长度
if @array_Neigong.length < @max_length
@array_Neigong.push([@array_Neigong[@array_Neigong.length-1][0],
@array_Neigong[@array_Neigong.length-1][1]])
@sprite_Neigong[@array_Neigong.length-1] = Sprite.new
@sprite_Neigong[@array_Neigong.length-1].bitmap = @NeiGongBitmap
end
@zhenqi_exist = false
end
# 如果撞到墙壁或自身则游戏结束
if check_collision
game_over
end
end
end
@time_count += 1
end
#--------------------------------------------------------------------------
# 开始运动
def start_move
# 前后按下两个不同的方向键后开始运动
if Input.trigger?(Input::UP)
unless @direction == "down"
@run = true
@direction = "up"
end
end
if Input.trigger?(Input::DOWN)
unless @direction == "up"
@run = true
@direction = "down"
end
end
if Input.trigger?(Input::LEFT)
unless @direction == "right"
@run = true
@direction = "left"
end
end
if Input.trigger?(Input::RIGHT)
unless @direction == "left"
@run = true
@direction = "right"
end
end
end
#--------------------------------------------------------------------------
# 真气条移动
def move1
case @direction
when "up"
for i in [email]1...@array_Neigong.length[/email]
@array_Neigong[@array_Neigong.length-i][0] = @array_Neigong[@array_Neigong.length-i-1][0]
@array_Neigong[@array_Neigong.length-i][1] = @array_Neigong[@array_Neigong.length-i-1][1]
end
@array_Neigong[0][1] -= 1
when "down"
for i in [email]1...@array_Neigong.length[/email]
@array_Neigong[@array_Neigong.length-i][0] = @array_Neigong[@array_Neigong.length-i-1][0]
@array_Neigong[@array_Neigong.length-i][1] = @array_Neigong[@array_Neigong.length-i-1][1]
end
@array_Neigong[0][1] += 1
when "left"
for i in [email]1...@array_Neigong.length[/email]
@array_Neigong[@array_Neigong.length-i][0] = @array_Neigong[@array_Neigong.length-i-1][0]
@array_Neigong[@array_Neigong.length-i][1] = @array_Neigong[@array_Neigong.length-i-1][1]
end
@array_Neigong[0][0] -= 1
when "right"
for i in [email]1...@array_Neigong.length[/email]
@array_Neigong[@array_Neigong.length-i][0] = @array_Neigong[@array_Neigong.length-i-1][0]
@array_Neigong[@array_Neigong.length-i][1] = @array_Neigong[@array_Neigong.length-i-1][1]
end
@array_Neigong[0][0] += 1
end
draw_Neigong # 描绘真气条
end
#--------------------------------------------------------------------------
# 刷新得分
def refresh_score
@sprite_score.bitmap.clear
@sprite_score.bitmap.font.size = 18
@sprite_score.bitmap.font.color.set(255,255,255)
@sprite_score.bitmap.draw_text(10, 0, 160, 32, "本次循經穴道:" + @score.to_s)
@sprite_score.bitmap.draw_text(10, 32, 160, 32, "當前循環周天:" + @array_Neigong.length.to_s)
@sprite_score.bitmap.draw_text(10, 64, 160, 32, "當前循環度為:" + @level.to_s)
end
#--------------------------------------------------------------------------
# 生成真气坐标
def creat_zhenqi
loop do
zhenqi_x = rand(11) + 8
zhenqi_y = rand(11) + 3
creat_succes = true # 标记是否成功生成的开关
for i in [email]0...@array_Neigong.length[/email]
if zhenqi_x == @array_Neigong[i][0] and zhenqi_y == @array_Neigong[i][1]
creat_succes = false
end
end
# 如果成功生成真气则中断循环
if creat_succes
return zhenqi_x * @mySize,zhenqi_y * @mySize
break
end
end
end
#--------------------------------------------------------------------------
# 描绘真气
def draw_zhenqi
@zhenqi_x,@zhenqi_y = creat_zhenqi
@sprite_zhenqi.x = @zhenqi_x
@sprite_zhenqi.y = @zhenqi_y
end
#--------------------------------------------------------------------------
# 描绘蛇的图形
def draw_Neigong
for i in [email]0...@array_Neigong.length[/email]
@sprite_Neigong[i].x = @array_Neigong[i][0] * @mySize
@sprite_Neigong[i].y = @array_Neigong[i][1] * @mySize
end
end
#--------------------------------------------------------------------------
# 检测真气条是否撞到墙壁或自身
def check_collision
if @array_Neigong[0][0] < 8 or @array_Neigong[0][0] > 18 or
@array_Neigong[0][1] < 3 or @array_Neigong[0][1] > 13
return true
end
for i in [email]1...@array_Neigong.length[/email]
if @array_Neigong[0][0] == @array_Neigong[i][0] and
@array_Neigong[0][1] == @array_Neigong[i][1]
return true
end
end
return false
end
#--------------------------------------------------------------------------
# 检测是否吃到真气
def check_eat
if @array_Neigong[0][0] == @zhenqi_x / @mySize and
@array_Neigong[0][1] == @zhenqi_y / @mySize
return true
end
end
#--------------------------------------------------------------------------
# 游戏结束的处理
def game_over
#print "循環周天結束!本次沖過的穴道數:#{@score}"
# 第859号系统变量中存放:累计冲穴的穴道总数
$game_variables[859] += @score
@bGameOver=true
# 释放初始化、主程序和增加真气条长度时动态分配的内存
@NeiGongBitmap.dispose
for i in [email]0...@array_Neigong.length[/email]
@sprite_Neigong[i].dispose
end
@sprite_zhenqi.bitmap.dispose
@sprite_zhenqi.dispose
@sprite_score.bitmap.dispose
@sprite_score.dispose
@sprite_clear.bitmap.dispose
@sprite_clear.dispose
@bgBitmap.dispose
@sprite.dispose
end
end