#===============================================================================
# * Minigame Susun Gambar
#-------------------------------------------------------------------------------
# Cara Pakai:
# - Copy script ini ke script editor, di atas Main
# - Panggil melalui event script:
# $scene = Scene_SusunGambar.new(num_of_columns, num_of_rows, filename[, time])
# num_of_columns = jumlah kolom yang diinginkan
# num_of_rows = jumlah baris yang diinginkan
# filename = nama file yang akan dijadikan gambar dari puzzle
# (file boleh berukuran bebas, tapi sebaiknya jangan melebihi
# 544 x 416, karena kacau ntar
. Dan file gambar harus
# disimpan di folder Graphics/Pictures)
# time = batas waktu dalam detik. Kosongkan bila tidak ingin
# memiliki waktu
# - Setelah permainan selesai, switch yang telah diset di bawah akan menjadi
# ON bila menang, dan OFF bila kalah, kemudian variabel yg telah diset di bawah
# akan berisi sisa waktu ketika menyelesaikan permainan.
#-------------------------------------------------------------------------------
# Term Of Use:
# - Terserah anda script ini mau diapain, dan tidak credit juga tidak apa-apa,
# asalkan anda sopan dan menghargai sesama manusia
#-------------------------------------------------------------------------------
module SusunGambar
#-------------------------------------------------------------------------------
# RESULT_SWITCH_ID = ID switch untuk menyimpan hasil permainan
# REMAINING_TIME_ID = ID variabel untuk menyimpan sisa waktu ketika selesai
#-------------------------------------------------------------------------------
RESULT_SWITCH_ID = 1
REMAINING_TIME_ID = 1
#-------------------------------------------------------------------------------
# BLANK_COLOR = Warna kotak kosong
#-------------------------------------------------------------------------------
BLANK_COLOR1 = Color.new(255, 255, 255)
BLANK_COLOR2 = Color.new(0, 0, 0)
VERTICAL_GRADIENT = false
#-------------------------------------------------------------------------------
end
#===============================================================================
class Scene_SusunGambar < Scene_Base
def initialize(num_of_columns, num_of_rows, filename, time = -1)
@num_of_columns = num_of_columns
@num_of_rows = num_of_rows
@image = Cache.picture(filename)
@image_pieces = []
@num_of_cells = @num_of_columns * @num_of_rows
@removed_cell = rand(@num_of_cells)
@time = time
@framecount = 0
@timesprite = Sprite.new
@timesprite.x = 0
@timesprite.y = 0
@timesprite.z = 50
@timesprite.bitmap = Bitmap.new(544, 12)
@timesprite.bitmap.font = Font.new("Arial", 12)
end
def start
for i in 0 ... @num_of_cells
@image_pieces[i] = Sprite_ImagePieces.new(i)
@image_pieces[i].bitmap = Bitmap.new(@image.width / @num_of_columns,
@image.height / @num_of_rows)
@image_pieces[i].bitmap.blt(0, 0, @image,
Rect.new((i % @num_of_columns) * @image.width / @num_of_columns,
(i / @num_of_columns) * @image.height / @num_of_rows,
@image.width / @num_of_columns,
@image.height / @num_of_rows))
end
@image_pieces[@removed_cell].bitmap.gradient_fill_rect(0, 0,
@image_pieces[@removed_cell].bitmap.width, @image_pieces[@removed_cell].bitmap.height,
SusunGambar::BLANK_COLOR1, SusunGambar::BLANK_COLOR2, SusunGambar::VERTICAL_GRADIENT)
@image_pieces[@removed_cell].removed = true
randomize_position()
end
def randomize_position
tmp = []
for i in 0 ... @num_of_cells
tmp.push(i)
end
for i in @image_pieces
tmp2 = rand(tmp.size)
i.pos = tmp[tmp2]
tmp.delete_at(tmp2)
tmp.sort!
end
end
def terminate
for i in @image_pieces
i.dispose
end
@timesprite.dispose
end
def update
@framecount += 1 if @time >= 0
if @framecount >= 60
@time -= 1 if @time > 0
@framecount = 0
end
@timesprite.bitmap.clear
@timesprite.bitmap.draw_text(0, 0, 544, 12, "Time: " + @time.to_s) if @time >= 0
for i in @image_pieces
i.x = (i.pos % @num_of_columns) * @image.width / @num_of_columns
i.y = (i.pos / @num_of_columns) * @image.height / @num_of_rows
end
if Input.trigger?(Input::UP)
if @image_pieces[@removed_cell].pos + @num_of_columns < @num_of_cells
swap(@image_pieces[@removed_cell], get_piece_from_pos(@image_pieces[@removed_cell].pos + @num_of_columns))
end
elsif Input.trigger?(Input::DOWN)
if @image_pieces[@removed_cell].pos - @num_of_columns >= 0
swap(@image_pieces[@removed_cell], get_piece_from_pos(@image_pieces[@removed_cell].pos - @num_of_columns))
end
elsif Input.trigger?(Input::RIGHT)
if @image_pieces[@removed_cell].pos % @num_of_columns != 0
swap(@image_pieces[@removed_cell], get_piece_from_pos(@image_pieces[@removed_cell].pos - 1))
end
elsif Input.trigger?(Input::LEFT)
if (@image_pieces[@removed_cell].pos + 1) % @num_of_columns != 0
swap(@image_pieces[@removed_cell], get_piece_from_pos(@image_pieces[@removed_cell].pos + 1))
end
end
if completed?
$game_switches[SusunGambar::RESULT_SWITCH_ID] = true
$game_variables[SusunGambar::REMAINING_TIME_ID] = @time
$game_system.battle_end_me.play
$scene = Scene_Map.new
elsif @time == 0
$game_switches[SusunGambar::RESULT_SWITCH_ID] = false
$game_variables[SusunGambar::REMAINING_TIME_ID] = 0
$scene = Scene_Map.new
end
end
def swap(a, b)
tmp = a.pos
a.pos = b.pos
b.pos = tmp
end
def completed?
for i in @image_pieces
return false if i.pos != i.right_pos
end
return true
end
def get_piece_from_pos(pos)
for i in @image_pieces
if i.pos == pos
return i
end
end
end
end
class Sprite_ImagePieces < Sprite
attr_reader :right_pos
attr_accessor :removed
attr_accessor :pos
def initialize(right_pos)
super()
@right_pos = right_pos
@removed = false
@pos = right_pos
end
end