Piece Substitution Puzzle Game
#1
Piece Substitution Puzzle Game
By Ánemus
Aug 27, 2008

This is a locked, single-post thread from Creation Asylum. Archived here to prevent its loss.
No support is given. If you are the owner of the thread, please contact administration.


The by Ánemus, and the Press Esc for more information are the map that is behind.


Code:
#==============================================================================
# Piece Substitution Puzzle Game (PSPG)
#  By Ánemus
#   [url=http://www.arcadiumrpg.net]http://www.arcadiumrpg.net[/url]
#==============================================================================
#
# This time I present a simple script. This is the second version of an older
# script I made a long time ago. It's just a substitution puzzle game, but,
# with the right drawings, you can make a real challenge.
#
# For customizing this we have the next options:
#
# YELLOW: Indicates the color the Title of the puzzle will be. Red Green Blue
YELLOW = Color.new(255, 206, 0)
# LOSE_SOUND: Indicates the sound played when you lose.
LOSE_SOUND = "Audio/SE/058-Wrong02.ogg"
# WIN_SOUND: Indicates the sound played when you win.
WIN_SOUND = "Audio/SE/056-Right02.ogg"
# PIECE_SOUND: indicates the sound played when you select a piece.
PIECE_SOUND = "Audio/SE/105-Heal01.ogg"
# BIP_SOUND: Indicates the sound that is played when you are running out of
# time.
BIP_SOUND = "Audio/SE/002-System02.ogg"
# READY: Indicates the text shown when you start the game. Keep it short.
READY = "Ready?"
# DONE: Indicates teh text shown when you win the game. Keep it short.
DONE = "Done!"
# TIME: Indicates teh text shown when you lose the game. Keep it short.
TIME = "Time is up!"
#
# And that's it! The picture shal be 300 x 300 pixels.
#
# For calling the system use the next structure:
#
# $scene = Scene_Puzzle.new("image", pieces, seconds, variable)
#  -"image" is the picture (stored in pictures) 300x300 in size. The name of
#   the file goes between " ".
#  -pieces: is a number form 3 (minimum) to 6 (maximum) that indicates the
#   number of pieces. 3 will make 3x3 6, 6x6.
#  -seconds: is the number of seconds. 60 seconds is a minute 120 is two...
#  -variable: is the variable where the result of the game is stored. Look at
#   the example to see how it is done (you need Local Switches and another page
#   in the event). 1 means lose, 2 means win.
#
#==============================================================================
#
#==============================================================================
class Scene_Puzzle
#------------------------------------------------------------------------------  
  def initialize(image, pieces, sec, goto, text)
    @img = image
    @px = pieces
    @tm = sec
    @gt = goto
    @tx = text
    @cl = YELLOW
    @waitcount = 0
  end
#------------------------------------------------------------------------------
  def main
    @sprite = Spriteset_Map.new
    @t = WT.new
    @t.refresh @tm
    @m = Mdl.new (RPG::Cache.picture(@img), @tx, @cl)
    @p = WP.new (RPG::Cache.picture(@img), @px)
    @p.active = false
    @r = WR.new
    @r.z = 10000
    Audio.me_stop
    Audio.bgs_stop
    @fstc = false
    @fsti
    @ac = 0
    @waitcount = Graphics.frame_rate
    Graphics.transition
    loop do
      Graphics.update
      Input.update
      update
      if $scene != self
        break
      end
    end
    Graphics.freeze
    @c.dispose
    @t.dispose
    @m.dispose
    @p.dispose
    @sprite.bitmap.dispose
    @sprite.dispose
  end
#------------------------------------------------------------------------------
  def update
    @waitcount -= 1 if @c == nil and @r == nil
    @p.update
    @p.active = true
    @t.update
    if @waitcount == 0
      @tm -= 1
      @t.refresh (@tm)
      @waitcount = Graphics.frame_rate
    end
    if @tm == 0 and @c == nil
      if @st
        Audio.bgm_stop
      end
      Audio.se_play(LOSE_SOUND, 70, 150)
      $game_variables[@gt] = 1
      @c = WL.new false
      @c.z = 10000
      @p.active = false
    end
    if Input.trigger?(Input::C)
      if @r != nil
        $game_system.se_play($data_system.decision_se)
        @r.dispose
        @r = nil
        @p.active = true
        return
      end
      if @c != nil
        $game_system.se_play($data_system.decision_se)
        $scene = Scene_Map.new
        return
      end
      if @fstc == false
        $game_system.se_play($data_system.decision_se)
        @fsti = @p.index
        @p.set_oldindex @fsti
        @fstc = true
      else
        if @fsti != @p.index
          Audio.se_play(WIN_SOUND, 70, 150)
          @p.change (@fsti, @p.index)
          if @p.win?
            if @st
              Audio.bgm_stop
            end
            Audio.se_play(PIECE_SOUND, 70, 150)
            $game_variables[@gt] = 2
            @c = WL.new true
            @c.z = 10000
            @p.active = false
          end
          @fstc = false
          @p.set_oldindex -1
        else
          $game_system.se_play($data_system.buzzer_se)
        end
      end
    end
    if Input.trigger?(Input::B)
      if @fstc == true
        $game_system.se_play($data_system.cancel_se)
        @fstc = false
        @p.set_oldindex -1
      end
    end
  end
end
#==============================================================================
#
#==============================================================================
class WT < Window_Base
  def initialize
    super (418,224,149,64)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.contents.font.name = $fontface
    self.contents.font.size = 25
    @time = 0
    refresh
  end
#------------------------------------------------------------------------------
  def refresh (t = 0)
    if @time < t
      @time = t
      @addv = 50/(15-7)
      @addf = 50/7
    end
    self.contents.clear
    if t <= 15 and t > 7
      color = Color.new(255,210,0)
      Audio.se_play(BIP_SOUND, 50+ (@addv * (15-t)).round, 100)
    elsif t <15 and t > 0
      color = Color.new(255,0,0)
      Audio.se_play(BIP_SOUND, 50 + (@addv * (15-t)).round, 100 + (@addf * (7-t)).round)
    else
      color = normal_color
    end
    min = t / 60
    sec = t % 60
    text = sprintf("%02d:%02d", min, sec)
    draw_text_outline(0, 6, 104, 32, text, 2, Color.new(0,0,0), color)
  end
end
#==============================================================================
#
#==============================================================================
class Mdl < Window_Base
  def initialize (img, nam = "Puzzle", colr = YELLOW)
    super (145,40,458,172)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.contents.font.name = $fontface
    self.contents.font.size = 25
    self.contents.font.color = normal_color
    bit = img
    x = -2
    y = -3
    for i in 0..100
      for a in 0..100
        self.contents.blt(i+318+x, a+32+y, bit, Rect.new(i*3, a*3, 1, 1), 200)
      end
    end
    for i in 0..100
      for a in 0..100
        self.contents.blt(i+318+x, a+32+y, bit, Rect.new(i*3+1, a*3+1, 1, 1), 200)
      end
    end
    for i in 0..100
      for a in 0..100
        self.contents.blt(i+318+x, a+32+y, bit, Rect.new(i*3+2, a*3+2, 1, 1), 200)
      end
    end
    @nm = nam
    refresh
  end
#------------------------------------------------------------------------------
  def refresh
    draw_text_outline (14,0, 300, 32, @nm, 0, Color.new(0,0,0), YELLOW)
  end
end
#==============================================================================
#
#==============================================================================
class WP < Window_Base
  def initialize(image, num)
    if num <3
      num = 3
    end
    if num > 6
      num = 6
    end
    x = 640 - 331 -206 - 1
    super(x, 90, 360, 360)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.contents.font.name = $fontface
    self.contents.font.size = $fontsize
    @im = image
    @num = num
    @fstch = false
    pieces = num*num
    pieces -= 1
    @op = []
    @us = []
    for i in 0..pieces
      @us.push i
    end
    i = 0
    for i in 0..pieces
      don = rand(@us.size - 1)
      @op.push (@us[don])
      @us[don] = nil
      replacearray
    end
    @column_max = @num
    @index = 0
    refresh
  end
#------------------------------------------------------------------------------
  def index
    return @index
  end
#------------------------------------------------------------------------------
  def set_index=(index)
    @index = index
    update_cursor_rect
  end
#------------------------------------------------------------------------------
  def set_oldindex (index)
    @oindex = index
  end
#------------------------------------------------------------------------------
  def refresh
    self.contents.clear
    bit = @im
    cw = bit.rect.width / @num
    ch = bit.rect.height / @num
    @d = cw
    @o =  255
    @re = true
    cox = coy = (6-@num)/2
    for i in 0...@op.size
      ln = ask_lin i
      picy = ask_picy i
      x = i - ln*@num
      picx = @op[i] - picy*@num
      src_rect = Rect.new(cw*picx, ch*picy, cw, ch)
      @o =  255
      @re = true
      if i == @index and i != @oindex
        @o = 150
      end
      if i == @oindex
        @o = 100
      end
      self.contents.blt((cw+1)*x+5+cox, (ch+1)*ln+4+coy, RPG::Cache.picture("back"), Rect.new (0, 0, cw+2, cw+2))
      self.contents.blt((cw+1)*x+6+cox, (ch+1)*ln+5+coy, bit, src_rect, @o)
    end
  end
#------------------------------------------------------------------------------
  def replacearray
    faf = []
    for i in 0...@us.size
      if @us[i] != nil
        faf.push (@us[i])
      end
    end
    @us = faf
  end
#------------------------------------------------------------------------------
  def update
    super
    if self.active == true
      ml = ask_lin (@op. size - 1)
      if Input.repeat?(Input::DOWN)
        $game_system.se_play($data_system.cursor_se)
        if @index > @num*ml-1
          @index -= @num *ml
        else
          @index += @num
        end
      end
      if Input.repeat?(Input::UP) or Input.trigger? (Input::UP)
        $game_system.se_play($data_system.cursor_se)
          @index -= @num
        if @index < 0
          @index +=@num * @num
        end
      end
      if Input.repeat?(Input::LEFT)
        $game_system.se_play($data_system.cursor_se)
        if @index == 0 or @index == @num or @index == @num*2 or @index == @num*3 or @index == @num*4 or @index == @num* 5
          @index += @num-1
        else
          @index -= 1
        end
      end
      if Input.repeat?(Input::RIGHT)
        $game_system.se_play($data_system.cursor_se)
        if @index == (@num -1) or @index == ((@num*2) -1) or @index == ((@num * 3) -1) or @index == ((@num * 4)-1) or @index == ((@num * 5) -1) or @index == ((@num * 6) -1)
          @index -= @num - 1
        else
          @index += 1
        end
      end
      update_cursor_rect
      refresh
    end
  end
#------------------------------------------------------------------------------
  def change (i, a)
    res = @op[i]
    @op[i] = @op[a]
    @op[a] = res
    refresh
  end
#------------------------------------------------------------------------------
  def update_cursor_rect
    self.cursor_rect.empty
  end
#------------------------------------------------------------------------------
  def win?
    ol = 0
    for i in 0...@op.size
      if @op[i] == i
        ol += 1
      end
    end
    if ol == @num * @num
      return true
    else
      return false
    end
  end
#------------------------------------------------------------------------------
  def ask_lin (i)
    case @num
    when 4
      if i <4
        ln = 0
      end
      if i >3 and i<8
        ln = 1
      end
      if i >7 and i <12
        ln = 2
      end
      if i >11
        ln = 3
      end
    when 3
      if i <3
        ln = 0
      end
      if i >2 and i<6
        ln = 1
      end
      if i >5
        ln = 2
      end
    when 5
      if i <5
        ln = 0
      end
      if i >4 and i<10
        ln = 1
      end
      if i >9 and i <15
        ln = 2
      end
      if i >14 and i <20
        ln = 3
      end
      if i > 19
        ln = 4
      end
    when 6
      if i <6
        ln = 0
      end
      if i >5 and i<12
        ln = 1
      end
      if i >11 and i <18
        ln = 2
      end
      if i >17 and i <24
        ln = 3
      end
      if i > 23 and i < 30
        ln = 4
      end
      if i > 29
        ln = 5
      end
    end
    return ln
  end
#------------------------------------------------------------------------------
  def ask_picy (i)
      if @op[i] < @num
        picy = 0
      end
      if @op[i] >@num-1 and @op[i]<@num *2
        picy = 1
      end
      if @op[i] >@num*2-1 and @op[i] < @num*3
        picy = 2
      end
      if @op[i] >@num*3-1 and @op[i] <@num * 4
        picy = 3
      end
      if @op[i] > @num * 4 -1 and @op[i] < @num * 5
        picy = 4
      end
      if @op[i] > @num*5 - 1
        picy = 5
      end
    return picy
  end
end
#==============================================================================
#
#==============================================================================
class WL < Window_Base
  def initialize (caso)
    super (251,208,144,72)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.contents.font.name = $fontface
    self.contents.font.size = $fontsize
    @cs = caso
    refresh
  end
#------------------------------------------------------------------------------
  def refresh
    self.contents.clear
    if @cs == true
      text = DONE
    else
      text = TIME
    end
    self.contents.font.color = normal_color
    draw_text_outline(1, 0, 104, 32, text, 1)
  end
end
#==============================================================================
#
#==============================================================================
class WR< Window_Base
  def initialize
    super (251,208,144,72)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.contents.font.name = $fontface
    self.contents.font.size = $fontsize
    refresh
  end
#------------------------------------------------------------------------------
  def refresh
    self.contents.clear
    text = READY
    draw_text_outline(1, 0, 104, 32, text, 1)
  end
end
#==============================================================================
#
#==============================================================================
class Window_Base < Window
  def draw_text_outline(x, y, wid, hei, text, align = 0, line = Color.new(0,0,0),fore = normal_color)
    self.contents.font.color = line
    self.contents.draw_text(x + 1,y + 1,wid,hei,text, align)
    self.contents.draw_text(x + 1,y - 1,wid,hei,text, align)
    self.contents.draw_text(x - 1,y - 1,wid,hei,text, align)
    self.contents.draw_text(x - 1,y + 1,wid,hei,text, align)
    self.contents.draw_text(x - 1,y,wid,hei,text, align)
    self.contents.draw_text(x + 1,y,wid,hei,text, align)
    self.contents.draw_text(x ,y-1,wid,hei,text, align)
    self.contents.draw_text(x,y+1,wid,hei,text, align)
    self.contents.font.color = fore
    self.contents.draw_text(x,y,wid,hei,text, align)
  end
end


Instructions:
Put this in a page above the MAIN page.

Copy the back.png file from the Pictures folder of the Demo.

Configure the system as the green text says.

The calling istructions are in the green text, and also on the Demo.



Warning:
The disclaimer said you would need to look at the demo to see how it works. Unfortunatly, the demo and its host site no longer exists.
}


Possibly Related Threads…
Thread Author Replies Views Last Post
  Game Completion Window Leon Blade 0 2,561 03-11-2007, 01:00 PM
Last Post: Leon Blade



Users browsing this thread: 2 Guest(s)