[gosuhunt] draw sprites from blob instead of text

This commit is contained in:
Gwenhael Le Moine 2023-03-15 14:16:49 +01:00
parent 3e2e0416ec
commit af513c4d9a
No known key found for this signature in database
GPG key ID: FDFE3669426707A7
2 changed files with 23 additions and 11 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 24 KiB

View file

@ -7,7 +7,11 @@ require 'minehunt'
class GosuHunt < Gosu::Window class GosuHunt < Gosu::Window
def initialize( number_of_mines = 20 ) def initialize( number_of_mines = 20 )
super 325, 180 @scale = 4
@sprite_size = 6
super ( @scale * ( @sprite_size * 16 ) ) + 16 + @scale,
( @scale * ( @sprite_size * 8 ) ) + 20 + 8 + @scale
self.caption = 'Gosu Hunt' self.caption = 'Gosu Hunt'
@ -17,12 +21,16 @@ class GosuHunt < Gosu::Window
@state = { dead: false, victory: false } @state = { dead: false, victory: false }
@font = Gosu::Font.new( 20 ) @font = Gosu::Font.new( 20 )
@images = { explorer: Gosu::Image.from_text( '()', 20 ), @images = { explorer: Gosu::Image.from_blob( @sprite_size, @sprite_size, bits_to_rgbas( '000000001100011110011110001100000000' ) ),
dead_explorer: Gosu::Image.from_text( '☠ ', 20 ), dead_explorer: Gosu::Image.from_blob( @sprite_size, @sprite_size, bits_to_rgbas( '100001010010001100001100010010100001' ) ),
door: Gosu::Image.from_text( '=>', 20 ), door: Gosu::Image.from_blob( @sprite_size, @sprite_size, bits_to_rgbas( '010101101010' ) * ( @sprite_size / 2 ) ),
mine: Gosu::Image.from_text( '<>', 20 ), mine: Gosu::Image.from_blob( @sprite_size, @sprite_size, bits_to_rgbas( '111111110011100001100001110011111111' ) ),
cell: Gosu::Image.from_text( '██', 20 ), cell: Gosu::Image.from_blob( @sprite_size, @sprite_size, bits_to_rgbas( '1' ) * @sprite_size * @sprite_size ),
visited_cell: Gosu::Image.from_text( ' ', 20 ) } visited_cell: Gosu::Image.from_blob( @sprite_size, @sprite_size, bits_to_rgbas( '0' ) * @sprite_size * @sprite_size ) }
end
def bits_to_rgbas( bits )
bits.gsub( '0', "\0\0\0\0" ).gsub( '1', "\255\255\255\255" )
end end
def new_level def new_level
@ -75,11 +83,11 @@ class GosuHunt < Gosu::Window
def draw def draw
self.caption = if @state[:dead] self.caption = if @state[:dead]
"You died! :(, score #{@field.score} " "You blew up!! :(, score: #{@field.score} "
elsif @state[:victory] elsif @state[:victory]
"You won!! :), score #{@field.score} " "You won!! :), score: #{@field.score} "
else else
"#{@field.number_of_mines} mines, #{@field.count_nearby_mines} nearby, score #{@field.score}" "Near #{@field.count_nearby_mines} mines, score: #{@field.score}"
end end
@font.draw_text( caption, 0, 0, 0 ) @font.draw_text( caption, 0, 0, 0 )
@ -96,7 +104,11 @@ class GosuHunt < Gosu::Window
@images[:mine] @images[:mine]
else else
@images[:cell] @images[:cell]
end ).draw( x * 10, (y * 20) + 20, 0 ) end ).draw ( @scale * ( x * @sprite_size ) ) + x + @scale,
( @scale * ( y * @sprite_size ) ) + 20 + y + @scale,
0,
@scale,
@scale
end end
end end
end end