[gosuhunt] little bit less crude
This commit is contained in:
parent
27b65a409b
commit
e6bb987641
1 changed files with 60 additions and 42 deletions
|
@ -7,40 +7,64 @@ require 'minehunt'
|
|||
|
||||
class GosuHunt < Gosu::Window
|
||||
def initialize( number_of_mines = 20 )
|
||||
super 640, 480
|
||||
super 325, 180
|
||||
|
||||
self.caption = "Gosu Hunt"
|
||||
|
||||
@number_of_mines = number_of_mines
|
||||
@field = MineHunt::Board.new( @number_of_mines )
|
||||
@finished = false
|
||||
@state = { dead: false, victory: false }
|
||||
|
||||
@images = { explorer: Gosu::Image.from_text( '()', 20 ),
|
||||
dead_explorer: Gosu::Image.from_text( '☠ ', 20 ),
|
||||
door: Gosu::Image.from_text( '=>', 20 ),
|
||||
mine: Gosu::Image.from_text( '<>', 20 ),
|
||||
cell: Gosu::Image.from_text( '██', 20 ),
|
||||
visited_cell: Gosu::Image.from_text( ' ', 20 ) }
|
||||
end
|
||||
|
||||
def new_level
|
||||
@field = MineHunt::Board.new( @number_of_mines )
|
||||
@finished = false
|
||||
@state = { dead: false, victory: false }
|
||||
end
|
||||
|
||||
def button_up(id)
|
||||
case id
|
||||
when Gosu::KB_ESCAPE, Gosu::KB_Q
|
||||
close
|
||||
when Gosu::KB_R
|
||||
@field = MineHunt::Board.new( @number_of_mines )
|
||||
@state = { dead: false, victory: false }
|
||||
when Gosu::KB_NUMPAD_1
|
||||
@state = @field.move 'down-left'
|
||||
when Gosu::KB_NUMPAD_2, Gosu::KB_DOWN
|
||||
@state = @field.move 'down'
|
||||
when Gosu::KB_NUMPAD_3
|
||||
@state = @field.move 'down-right'
|
||||
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_7
|
||||
@state = @field.move 'up-left'
|
||||
when Gosu::KB_NUMPAD_8, Gosu::KB_UP
|
||||
@state = @field.move 'up'
|
||||
when Gosu::KB_NUMPAD_9
|
||||
@state = @field.move 'up-right'
|
||||
if @finished
|
||||
case id
|
||||
when Gosu::KB_ESCAPE, Gosu::KB_Q
|
||||
close
|
||||
when Gosu::KB_R
|
||||
new_level
|
||||
else
|
||||
super
|
||||
end
|
||||
else
|
||||
super
|
||||
case id
|
||||
when Gosu::KB_ESCAPE, Gosu::KB_Q
|
||||
close
|
||||
when Gosu::KB_R
|
||||
new_level
|
||||
when Gosu::KB_NUMPAD_1
|
||||
@state = @field.move 'down-left'
|
||||
when Gosu::KB_NUMPAD_2, Gosu::KB_DOWN
|
||||
@state = @field.move 'down'
|
||||
when Gosu::KB_NUMPAD_3
|
||||
@state = @field.move 'down-right'
|
||||
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_7
|
||||
@state = @field.move 'up-left'
|
||||
when Gosu::KB_NUMPAD_8, Gosu::KB_UP
|
||||
@state = @field.move 'up'
|
||||
when Gosu::KB_NUMPAD_9
|
||||
@state = @field.move 'up-right'
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
@finished = @state[:dead] || @state[:victory]
|
||||
|
@ -57,27 +81,21 @@ class GosuHunt < Gosu::Window
|
|||
self.caption = "#{@field.number_of_mines} mines, #{@field.count_nearby_mines} nearby, score #{@field.score}"
|
||||
end
|
||||
|
||||
theme = { explorer: '()',
|
||||
dead_explorer: '☠ ',
|
||||
door: '=>',
|
||||
mine: '<>',
|
||||
cell: '██',
|
||||
visited_cell: ' ' }
|
||||
Gosu::Font.new( 20 ).draw_text( self.caption, 0, 0, 0 )
|
||||
|
||||
@field.height.times do |y|
|
||||
@field.width.times do |x|
|
||||
Gosu::Image.from_text( if [@field.explorer_x, @field.explorer_y] == [x, y]
|
||||
@state[:dead] ? theme[:dead_explorer] : theme[:explorer]
|
||||
elsif [@field.width, @field.height] == [x + 1, y + 1]
|
||||
theme[:door]
|
||||
elsif @field.field[[x, y]].open
|
||||
theme[:visited_cell]
|
||||
elsif @field.field[[x, y]].mine && ( @state[:dead] || @state[:victory] )
|
||||
theme[:mine]
|
||||
else
|
||||
theme[:cell]
|
||||
end, 20 )
|
||||
.draw( x * 2 * 10, y * 20, 0 )
|
||||
( if [@field.explorer_x, @field.explorer_y] == [x, y]
|
||||
@state[:dead] ? @images[:dead_explorer] : @images[:explorer]
|
||||
elsif [@field.width, @field.height] == [x + 1, y + 1]
|
||||
@images[:door]
|
||||
elsif @field.field[[x, y]].open
|
||||
@images[:visited_cell]
|
||||
elsif @field.field[[x, y]].mine && ( @state[:dead] || @state[:victory] )
|
||||
@images[:mine]
|
||||
else
|
||||
@images[:cell]
|
||||
end ).draw( x * 2 * 10, (y * 20) + 20, 0 )
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue