Class: GamesParadise::GUI::Gtk::NibblesBoard
- Inherits:
-
Gtk::DrawingArea
- Object
- Gtk::DrawingArea
- GamesParadise::GUI::Gtk::NibblesBoard
- Includes:
- Gtk::BaseModule
- Defined in:
- lib/games_paradise/gui/gtk3/nibbles/nibbles_board.rb
Constant Summary collapse
- NAMESPACE =
#
NAMESPACE
#
inspect
- DOT_SIZE =
#
DOT_SIZE
#
::GamesParadise::Nibbles::DOT_SIZE
Instance Method Summary collapse
-
#check_apple ⇒ Object
# === check_apple ========================================================================= #.
-
#check_collision ⇒ Object
# === check_collision.
-
#draw_objects(cr) ⇒ Object
# === draw_objects.
-
#game_over(cr) ⇒ Object
# === game_over.
-
#initialize(run_already = true) ⇒ NibblesBoard
constructor
# === initialize ========================================================================= #.
-
#initialize_the_game_state ⇒ Object
# === initialize_the_game_state ========================================================================= #.
-
#move ⇒ Object
# === move ========================================================================= #.
-
#on_draw ⇒ Object
# === on_draw ========================================================================= #.
-
#on_key_down(event) ⇒ Object
# === on_key_down ========================================================================= #.
-
#on_timer ⇒ Object
# === on_timer ========================================================================= #.
-
#path_to_the_apple_image ⇒ Object
# === path_to_the_apple_image ========================================================================= #.
-
#path_to_the_dot_image ⇒ Object
# === path_to_the_dot_image ========================================================================= #.
-
#path_to_the_head_image ⇒ Object
# === path_to_the_head_image ========================================================================= #.
-
#randomly_place_the_apple ⇒ Object
# === randomly_place_the_apple.
-
#reset ⇒ Object
# === reset ========================================================================= #.
-
#run ⇒ Object
# === run ========================================================================= #.
-
#toplevel_x_coordinate? ⇒ Boolean
# === toplevel_x_coordinate? ========================================================================= #.
-
#toplevel_y_coordinate? ⇒ Boolean
# === toplevel_y_coordinate? ========================================================================= #.
Constructor Details
#initialize(run_already = true) ⇒ NibblesBoard
#
initialize
#
44 45 46 47 48 49 50 51 52 |
# File 'lib/games_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 44 def initialize( run_already = true ) super() reset override_background_color(:normal, Gdk::RGBA.new(0, 0, 0, 1)) signal_connect(:draw) { on_draw } run if run_already end |
Instance Method Details
#check_apple ⇒ Object
#
check_apple
#
219 220 221 222 223 224 225 |
# File 'lib/games_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 219 def check_apple if Gtk::Nibbles.x_coordinate?[0] == @apple_x_coordinate and Gtk::Nibbles.y_coordinate?[0] == @apple_y_coordinate @dots += 1 randomly_place_the_apple # Set a new apple here. end end |
#check_collision ⇒ Object
#
check_collision
This method is the one that will check for collisions, e. g. the snake crashing into the wall.
#
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/games_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 263 def check_collision z = @dots while z > 0 if z > 4 and ::GamesParadise::GUI::Gtk::Nibbles.x_coordinate?[0] == ::GamesParadise::GUI::Gtk::Nibbles.x_coordinate?[z] and ::GamesParadise::GUI::Gtk::Nibbles.y_coordinate?[0] == ::GamesParadise::GUI::Gtk::Nibbles.y_coordinate?[z] @within_the_game_board = false end z -= 1 end if ::GamesParadise::GUI::Gtk::Nibbles.y_coordinate?[0] > (::GamesParadise::Nibbles::HEIGHT - DOT_SIZE) @within_the_game_board = false end if ::GamesParadise::GUI::Gtk::Nibbles.y_coordinate?[0] < 0 @within_the_game_board = false end if ::GamesParadise::GUI::Gtk::Nibbles.x_coordinate?[0] > (::GamesParadise::Nibbles::WIDTH_OF_THE_BOARD - DOT_SIZE) @within_the_game_board = false end if ::GamesParadise::GUI::Gtk::Nibbles.x_coordinate?[0] < 0 @within_the_game_board = false end end |
#draw_objects(cr) ⇒ Object
#
draw_objects
This method will do the actual drawing.
#
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/games_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 182 def draw_objects(cr) cr.set_source_rgb(0, 0, 0) cr.paint cr.set_source(@apple, @apple_x_coordinate, @apple_y_coordinate) cr.paint for i in 0..@dots case i when 0 # =================================================================== # # Draw the head part here: # =================================================================== # cr.set_source( @head, Gtk::Nibbles.x_coordinate?[i], Gtk::Nibbles.y_coordinate?[i] ) cr.paint else # =================================================================== # # Draw the dot part here: # =================================================================== # cr.set_source( @dot, Gtk::Nibbles.x_coordinate?[i], Gtk::Nibbles.y_coordinate?[i] ) cr.paint end end end |
#game_over(cr) ⇒ Object
#
game_over
This finishes the game.
#
345 346 347 348 349 350 351 352 353 |
# File 'lib/games_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 345 def game_over(cr) w = allocation.width / 2 h = allocation.height / 2 cr.set_font_size(15) text = cr.text_extents('Game Over') cr.set_source_rgb(65535, 65535, 65535) cr.move_to(w - text.width/2, h) cr.show_text('Game Over') end |
#initialize_the_game_state ⇒ Object
#
initialize_the_game_state
#
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/games_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 101 def initialize_the_game_state # ======================================================================= # # === @left # ======================================================================= # @left = false # ======================================================================= # # === @right # ======================================================================= # @right = true # ======================================================================= # # === @up # ======================================================================= # @up = false # ======================================================================= # # === @down # ======================================================================= # @down = false # ======================================================================= # # === @within_the_game_board # ======================================================================= # @within_the_game_board = true # ======================================================================= # # Determine the length of the snake. # ======================================================================= # @dots = Nibbles::LENGTH_OF_THE_SNAKE for i in 0 .. @dots toplevel_x_coordinate?[i] = 50 - i * 10 toplevel_y_coordinate?[i] = 50 end begin # ===================================================================== # # Next, load up our three images, via Cairo::ImageSurface. # ===================================================================== # @dot = Cairo::ImageSurface.from_png(path_to_the_dot_image) @head = Cairo::ImageSurface.from_png(path_to_the_head_image) @apple = Cairo::ImageSurface.from_png(path_to_the_apple_image) rescue Exception => error puts 'cannot load the three images (dot.png, head.png, apple.png)' pp error exit end # ======================================================================= # # Next, initialize the apple on the game-map. # ======================================================================= # randomly_place_the_apple # ======================================================================= # # Add a specific delay to GLib::Timeout.add() next. # ======================================================================= # GLib::Timeout.add(Nibbles::DELAY) { on_timer } end |
#move ⇒ Object
#
move
#
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/games_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 230 def move z = @dots loop { Gtk::Nibbles.x_coordinate?[z] = Gtk::Nibbles.x_coordinate?[(z - 1)] Gtk::Nibbles.y_coordinate?[z] = Gtk::Nibbles.y_coordinate?[(z - 1)] z -= 1 break if z < 1 } dot_size = GamesParadise::Nibbles::DOT_SIZE if @left # Move leftwards. Gtk::Nibbles.x_coordinate?[0] -= dot_size end if @right # Move rightwards. Gtk::Nibbles.x_coordinate?[0] += dot_size end if @up # Move upwards. Gtk::Nibbles.y_coordinate?[0] -= dot_size end if @down # Move downwards. Gtk::Nibbles.y_coordinate?[0] += dot_size end end |
#on_draw ⇒ Object
#
on_draw
#
168 169 170 171 172 173 174 175 |
# File 'lib/games_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 168 def on_draw cairo_context = window.create_cairo_context if @within_the_game_board draw_objects(cairo_context) else game_over(cairo_context) end end |
#on_key_down(event) ⇒ Object
#
on_key_down
#
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/games_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 291 def on_key_down(event) # Get the key that was passed to this method next: key = event.keyval if key == Gdk::Keyval::KEY_Left and not @right @left = true @up = false @down = false end if key == Gdk::Keyval::KEY_Right and not @left @right = true @up = false @down = false end if key == Gdk::Keyval::KEY_Up and not @down @up = true @right = false @left = false end if key == Gdk::Keyval::KEY_Down and not @up @down = true @right = false @left = false end end |
#on_timer ⇒ Object
#
on_timer
#
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/games_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 82 def on_timer if @within_the_game_board # ===================================================================== # # These are the steps that we need to do. Before the move action # we must check whether there is a collision-situation. # ===================================================================== # check_apple check_collision move queue_draw return true else return false end end |
#path_to_the_apple_image ⇒ Object
#
path_to_the_apple_image
#
329 330 331 |
# File 'lib/games_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 329 def path_to_the_apple_image "#{IMG_DIR}misc/apple.png" end |
#path_to_the_dot_image ⇒ Object
#
path_to_the_dot_image
#
336 337 338 |
# File 'lib/games_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 336 def path_to_the_dot_image "#{IMG_DIR}misc/dot.png" end |
#path_to_the_head_image ⇒ Object
#
path_to_the_head_image
#
322 323 324 |
# File 'lib/games_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 322 def path_to_the_head_image "#{IMG_DIR}misc/head.png" end |
#randomly_place_the_apple ⇒ Object
#
randomly_place_the_apple
This will randomly set the starting coordination for the “apple”.
#
158 159 160 161 162 163 |
# File 'lib/games_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 158 def randomly_place_the_apple r = rand(Nibbles::RAND_POS) @apple_x_coordinate = r * ::GamesParadise::Nibbles::DOT_SIZE r = rand(Nibbles::RAND_POS) @apple_y_coordinate = r * ::GamesParadise::Nibbles::DOT_SIZE end |
#reset ⇒ Object
#
reset
#
57 58 59 60 61 62 63 |
# File 'lib/games_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 57 def reset reset_the_internal_variables # ======================================================================= # # === @configuration # ======================================================================= # @configuration = [true, __dir__, NAMESPACE] end |
#run ⇒ Object
#
run
#
358 359 360 |
# File 'lib/games_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 358 def run initialize_the_game_state end |
#toplevel_x_coordinate? ⇒ Boolean
#
toplevel_x_coordinate?
#
68 69 70 |
# File 'lib/games_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 68 def toplevel_x_coordinate? Nibbles.x_coordinate? end |
#toplevel_y_coordinate? ⇒ Boolean
#
toplevel_y_coordinate?
#
75 76 77 |
# File 'lib/games_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 75 def toplevel_y_coordinate? Nibbles.y_coordinate? end |