Add libgosu based GUI
This commit is contained in:
parent
4498aadbfb
commit
b1a04ec10a
1 changed files with 102 additions and 0 deletions
102
bin/gosustar
Executable file
102
bin/gosustar
Executable file
|
@ -0,0 +1,102 @@
|
|||
#!/usr/bin/env ruby
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'gosu'
|
||||
require 'optparse'
|
||||
|
||||
require 'rbstar'
|
||||
|
||||
class GosuStar < Gosu::Window
|
||||
def initialize( level, scale )
|
||||
@scale = scale
|
||||
@sprite_width = 9
|
||||
@sprite_height = 8
|
||||
|
||||
@level = level
|
||||
@field = RbStar::Board.new( @level )
|
||||
@finished = false
|
||||
@state = { dead: false, victory: false }
|
||||
|
||||
@font = Gosu::Font.new( 20 )
|
||||
@images = { '@' => Gosu::Image.from_blob( @sprite_width * @scale, @sprite_height * @scale, bits_to_rgbas( '000000000000000000000110000001101000001111000000110000000000000000000000' ) ),
|
||||
'H' => Gosu::Image.from_blob( @sprite_width * @scale, @sprite_height * @scale, bits_to_rgbas( '000000000000000000001111000001111000001111000001111000000000000000000000' ) ),
|
||||
'#' => Gosu::Image.from_blob( @sprite_width * @scale, @sprite_height * @scale, bits_to_rgbas( '011111110100101001110000111101100001110001011100100101101001001011111110' ) ),
|
||||
'x' => Gosu::Image.from_blob( @sprite_width * @scale, @sprite_height * @scale, bits_to_rgbas( '000000000000000000000110000001001000001001000000110000000000000000000000' ) ),
|
||||
' ' => Gosu::Image.from_blob( @sprite_width * @scale, @sprite_height * @scale, bits_to_rgbas( '0' * @sprite_width * @sprite_height ) ) }
|
||||
|
||||
@finished = false
|
||||
|
||||
# Creation of the window
|
||||
super ( ( ( ( @sprite_width - 1 ) * @field.width ) + 2 ) * @scale ) + @scale,
|
||||
( ( ( ( @sprite_height - 1 ) * @field.height ) + 2 ) * @scale ) + @scale + 20
|
||||
end
|
||||
|
||||
def bits_to_rgbas( bits )
|
||||
bits.scan(/.{#{@sprite_width}}/).map do |line|
|
||||
line.gsub( '0', "\0\0\0\0" * @scale ).gsub( '1', "\255\255\255\255" * @scale ) * @scale
|
||||
end.join
|
||||
end
|
||||
|
||||
def button_up(id)
|
||||
case id
|
||||
when Gosu::KB_NUMPAD_2, Gosu::KB_DOWN
|
||||
@state = @field.move :down
|
||||
when Gosu::KB_NUMPAD_4, Gosu::KB_LEFT
|
||||
@state = @field.move :left
|
||||
when Gosu::KB_NUMPAD_6, Gosu::KB_RIGHT
|
||||
@state = @field.move :right
|
||||
when Gosu::KB_NUMPAD_8, Gosu::KB_UP
|
||||
@state = @field.move :up
|
||||
when Gosu::KB_ESCAPE, Gosu::KB_Q
|
||||
close
|
||||
when Gosu::KB_N
|
||||
@field.next_level
|
||||
when Gosu::KB_P
|
||||
@field.prev_level
|
||||
when Gosu::KB_R
|
||||
@field.reload_level
|
||||
when Gosu::KB_SPACE
|
||||
@field.switch_moving
|
||||
else
|
||||
super
|
||||
end
|
||||
|
||||
@finished = @field.success?
|
||||
|
||||
@field.next_level if @finished
|
||||
end
|
||||
|
||||
def update; end
|
||||
|
||||
def draw
|
||||
caption = "Level #{@field.level + 1}/#{@field.levels.length}, #{@field.whats_moving} moving, #{@field.count_gifts} gems left, score: #{@field.distance} "
|
||||
|
||||
@font.draw_text( caption, 0, 0, 0 )
|
||||
|
||||
@field.height.times do |y|
|
||||
@field.width.times do |x|
|
||||
@images[ @field.get_cell( x, y ) ].draw ( ( @sprite_width - 1 ) * x * @scale ) + @scale,
|
||||
( ( @sprite_height - 1 ) * y * @scale ) + @scale + 20,
|
||||
0
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
options = { level: 0,
|
||||
scale: 3 }
|
||||
OptionParser.new do |opts|
|
||||
opts.banner = 'Usage: gosustar <options>'
|
||||
|
||||
opts.on( '-l', '--level INT' ) do |level|
|
||||
options[:level] = level.to_i
|
||||
end
|
||||
|
||||
opts.on( '-s', '--scale INT' ) do |scale|
|
||||
options[:scale] = scale.to_i
|
||||
end
|
||||
end
|
||||
.parse!
|
||||
|
||||
GosuStar.new( options[:level], options[:scale] )
|
||||
.show
|
Loading…
Reference in a new issue