Advertisement
LiTTleDRAgo

[RGSS/2/3] Minigame SusunGambar

Jan 29th, 2012
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 9.23 KB | None | 0 0
  1. #===============================================================================
  2. # * Minigame Susun Gambar
  3. #  by hart, ported to cross engine by LiTTleDRAgo
  4. #-------------------------------------------------------------------------------
  5. # Cara Pakai:
  6. # - Copy script ini ke script editor, di atas Main
  7. # - Panggil melalui event script:
  8. #
  9. #  for RMXP and RMVX
  10. #  $scene = Scene_SusunGambar.new(num_of_columns, num_of_rows, filename[, time])
  11. #
  12. #  for RMVXA
  13. #  $scene_susun = [num_of_columns, num_of_rows, filename  [, time]]
  14. #
  15. #  num_of_columns = jumlah kolom yang diinginkan
  16. #  num_of_rows    = jumlah baris yang diinginkan
  17. #  filename      = nama file yang akan dijadikan gambar dari puzzle
  18. #                    (file boleh berukuran bebas, tapi sebaiknya jangan melebihi
  19. #                    screen, karena kacau ntar . Dan file gambar harus
  20. #                    disimpan di folder Graphics/Pictures)
  21. #  time          = batas waktu dalam detik. Kosongkan bila tidak ingin
  22. #                    memiliki waktu
  23. #
  24. #
  25. # - Setelah permainan selesai, switch yang telah diset di bawah akan menjadi
  26. #  ON bila menang, dan OFF bila kalah, kemudian variabel yg telah diset di bawah
  27. #  akan berisi sisa waktu ketika menyelesaikan permainan.
  28. #-------------------------------------------------------------------------------
  29. # Term Of Use:
  30. # - Terserah anda script ini mau diapain, dan tidak credit juga tidak apa-apa,
  31. #  asalkan anda sopan dan menghargai sesama manusia
  32. #-------------------------------------------------------------------------------
  33. module SusunGambar
  34.   #-----------------------------------------------------------------------------
  35.   # RESULT_SWITCH_ID = ID switch untuk menyimpan hasil permainan
  36.   # REMAINING_TIME_ID = ID variabel untuk menyimpan sisa waktu ketika selesai
  37.   # BACKGROUND = Gambar Background dibelakang permainan
  38.   # COMPLETE_SE = Suara yang keluar kalo menang (di Audio/SE)
  39.   #-----------------------------------------------------------------------------
  40.   RESULT_SWITCH_ID  = 21
  41.   REMAINING_TIME_ID = 16
  42.   BACKGROUND        = ''
  43.   BACKGROUND_MOVING = true
  44.   COMPLETE_SE       = '060-Cheer01'
  45.   VOCAB_WIN         = 'Congratulations!'
  46.   VX = defined?(Window_ActorCommand)
  47.   VXA = defined?(Window_BattleActor)
  48.   #-----------------------------------------------------------------------------
  49. end
  50. #===============================================================================
  51.  
  52. class Scene_SusunGambar
  53.  
  54.   def main
  55.     start                        # Start processing
  56.     perform_transition            # Perform transition
  57.     background
  58.     Input.update                  # Update input information
  59.     while scene == self           # When screen is switched, interrupt loop
  60.       Graphics.update             # Update game screen
  61.       Input.update                # Update input information
  62.       update                      # Update frame
  63.     end
  64.     Graphics.update
  65.     Graphics.freeze              # Prepare transition
  66.     terminate                    # Termination processing
  67.   end
  68.  
  69.   def update
  70.     background_gerak
  71.     update_sprite
  72.     input || game_clear
  73.   end
  74.  
  75.   def change_scene(somescene)
  76.     SusunGambar::VXA ? SceneManager.call(somescene) : $scene = somescene.new
  77.   end
  78.  
  79.   def scene() SusunGambar::VXA ? SceneManager.scene : $scene end
  80.   def perform_transition()   Graphics.transition(10) end
  81.   def cache()  SusunGambar::VX ? Cache : RPG::Cache end
  82.    
  83.   if SusunGambar::VXA
  84.     def initialize
  85.       return not_completed if !$scene_susun.is_a?(Array)
  86.       num_of_columns = $scene_susun[0]
  87.       num_of_rows    = $scene_susun[1]
  88.       filename       = $scene_susun[2]
  89.       time           = $scene_susun[3] || -1
  90.       $scene_susun   = []
  91.       init_susungambar(num_of_columns, num_of_rows, filename, time)
  92.     end
  93.   else
  94.     def initialize(a,b,c,d=-1) init_susungambar(a,b,c,d) end
  95.   end
  96.    
  97.   def init_susungambar(num_of_columns, num_of_rows, filename, time = -1)
  98.     @num_of_columns = num_of_columns
  99.     @num_of_rows = num_of_rows
  100.     @image = filename.is_a?(Bitmap) ? filename : cache.picture(filename)
  101.     @image_pieces = []
  102.     @num_of_cells = @num_of_columns * @num_of_rows
  103.     @removed_cell = rand(@num_of_cells)
  104.     @time = time
  105.     @framecount = 0
  106.     @timesprite = Sprite.new
  107.     @timesprite.x = 0
  108.     @timesprite.y = 0
  109.     @timesprite.z = 50
  110.     @timesprite.bitmap = Bitmap.new(544, 12)
  111.     @timesprite.bitmap.font = Font.new('Arial', 12)
  112.   end
  113.  
  114.   def start
  115.     (0 ... @num_of_cells).each {|i|
  116.       @image_pieces[i] = Sprite_ImagePieces.new(i)
  117.       @image_pieces[i].bitmap = Bitmap.new(@image.width / @num_of_columns,
  118.                                               @image.height / @num_of_rows)
  119.       @image_pieces[i].bitmap.blt(0, 0, @image,
  120.           Rect.new((i % @num_of_columns) * @image.width / @num_of_columns,
  121.           (i / @num_of_columns) * @image.height / @num_of_rows,
  122.           @image.width / @num_of_columns, @image.height / @num_of_rows))}
  123.     @image_pieces[@removed_cell].opacity = 0
  124.     @image_pieces[@removed_cell].removed = true
  125.     randomize_position()
  126.   end
  127.    
  128.   def background
  129.     @mnback = Plane.new
  130.     @mnback.bitmap = cache.picture(SusunGambar::BACKGROUND)
  131.     @mnback.blend_type = 0
  132.     @mnback.z = -6
  133.     @mnback2 = Plane.new
  134.     @mnback2.bitmap = cache.picture(SusunGambar::BACKGROUND)
  135.     @mnback2.blend_type = 0
  136.     @mnback2.z = -6
  137.     @mnback2.opacity = 60
  138.   end
  139.  
  140.   def background_gerak
  141.     return if !SusunGambar::BACKGROUND_MOVING
  142.     @mnback.oy += 1
  143.     @mnback.ox += 1
  144.     @mnback2.oy += 1
  145.     @mnback2.ox -= 1
  146.   end
  147.  
  148.   def randomize_position
  149.     tmp = []
  150.     (0...@num_of_cells).each {|i| tmp.push(i)}
  151.     @image_pieces.each {|i|
  152.       @tmp2 = rand(tmp.size)
  153.       i.pos = tmp[@tmp2]
  154.       tmp.delete_at(@tmp2)
  155.       tmp.sort!}
  156.   end
  157.  
  158.   def terminate
  159.     @image_pieces.each {|i| i.dispose}
  160.     [@mnback, @mnback2].each {|i| i.dispose}
  161.     @timesprite.dispose
  162.   end
  163.  
  164.   def update_sprite
  165.     @framecount += 1 if @time >= 0
  166.     if @framecount >= 60
  167.       @time -= 1 if @time > 0
  168.       @framecount = 0
  169.     end
  170.     @timesprite.bitmap.clear
  171.     @timesprite.bitmap.draw_text(0,0,544,12,"Time: " + @time.to_s) if @time >= 0
  172.     @image_pieces.each {|i|
  173.       i.x = (i.pos % @num_of_columns) * @image.width / @num_of_columns
  174.       i.y = (i.pos / @num_of_columns) * @image.height / @num_of_rows}
  175.   end
  176.  
  177.   def input
  178.     if Input.trigger?(Input::DOWN)
  179.       if @image_pieces[@removed_cell].pos + @num_of_columns < @num_of_cells
  180.         swap(@image_pieces[@removed_cell],
  181.         get_piece_from_pos(@image_pieces[@removed_cell].pos + @num_of_columns))
  182.       end
  183.     elsif Input.trigger?(Input::UP)
  184.       if @image_pieces[@removed_cell].pos - @num_of_columns >= 0
  185.         swap(@image_pieces[@removed_cell],
  186.         get_piece_from_pos(@image_pieces[@removed_cell].pos - @num_of_columns))
  187.       end
  188.     elsif Input.trigger?(Input::LEFT)
  189.       if @image_pieces[@removed_cell].pos % @num_of_columns != 0
  190.         swap(@image_pieces[@removed_cell],
  191.         get_piece_from_pos(@image_pieces[@removed_cell].pos - 1))
  192.       end
  193.     elsif Input.trigger?(Input::RIGHT)
  194.       if (@image_pieces[@removed_cell].pos + 1) % @num_of_columns != 0
  195.         swap(@image_pieces[@removed_cell],
  196.         get_piece_from_pos(@image_pieces[@removed_cell].pos + 1))
  197.       end
  198.     elsif Input.trigger?(Input::B)
  199.       [not_completed]
  200.     end
  201.   end
  202.  
  203.   def game_clear
  204.     return completed if completed?
  205.     return not_completed if @time == 0
  206.   end
  207.  
  208.   def completed
  209.     $game_switches[SusunGambar::RESULT_SWITCH_ID] = true
  210.     $game_variables[SusunGambar::REMAINING_TIME_ID] = @time
  211.     Audio.se_play("Audio/SE/" + SusunGambar::COMPLETE_SE , 70, 100) rescue nil
  212.     congrats
  213.     change_scene(Scene_Map)
  214.   end
  215.    
  216.   def not_completed
  217.     $game_switches[SusunGambar::RESULT_SWITCH_ID] = false
  218.     $game_variables[SusunGambar::REMAINING_TIME_ID] = 0
  219.     change_scene(Scene_Map)
  220.   end
  221.  
  222.   def congrats
  223.     text = SusunGambar::VOCAB_WIN
  224.     sprite = s     = Sprite.new
  225.     sprite.bitmap  = Bitmap.new(640,480)
  226.     sprite.opacity = (sprite.z += 9999) && 0
  227.     sprite.bitmap.font.name = ['Georgia',Font.default_name].flatten
  228.     sprite.bitmap.font.size   = 32
  229.     y = (1 * sprite.bitmap.text_size(text).height) / 2
  230.     sprite.bitmap.draw_text(0,0-y,s.bitmap.width,s.bitmap.height,text,1)
  231.     (sprite.opacity += 10) && Graphics.update  until sprite.opacity >= 255
  232.     [Graphics,Input].each {|i| i.update }      until Input.trigger?(Input::C)
  233.     (sprite.opacity -= 10) && Graphics.update  until sprite.opacity <= 0
  234.     sprite.dispose
  235.   end
  236.    
  237.   def swap(a, b)
  238.     tmp = a.pos
  239.     a.pos = b.pos
  240.     b.pos = tmp
  241.   end
  242.  
  243.   def completed?
  244.     @image_pieces.all? {|i| i.pos == i.right_pos}
  245.   end
  246.  
  247.   def get_piece_from_pos(pos)
  248.     @image_pieces.each {|i| return i if i.pos == pos}
  249.   end
  250. end
  251.  
  252. if SusunGambar::VXA
  253.   class Scene_Map
  254.     alias drg146_upd update unless method_defined?(:drg146_upd)
  255.     def update
  256.       drg146_upd
  257.       return unless $scene_susun && !$scene_susun.empty?
  258.       SceneManager.call(Scene_SusunGambar)
  259.     end
  260.   end
  261. end
  262.    
  263. class Sprite_ImagePieces < Sprite
  264.   attr_accessor :removed, :pos, :right_pos
  265.  
  266.   def initialize(right_pos)
  267.     @right_pos,@pos,@removed = right_pos,right_pos,false
  268.     super()
  269.   end
  270. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement