Class: GamesAndRpgParadise::GUI::Gtk::NibblesBoard
- Inherits:
-
Gtk::DrawingArea
- Object
- Gtk::DrawingArea
- GamesAndRpgParadise::GUI::Gtk::NibblesBoard
- Includes:
- Gtk::BaseModule
- Defined in:
- lib/games_and_rpg_paradise/gui/gtk3/nibbles/nibbles_board.rb
Constant Summary collapse
- NAMESPACE =
#
NAMESPACE
#
inspect
- DOT_SIZE =
#
DOT_SIZE
#
::GamesAndRpgParadise::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
#
46 47 48 49 50 51 52 53 54 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 46 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
#
221 222 223 224 225 226 227 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 221 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.
#
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 265 def check_collision z = @dots while z > 0 if z > 4 and ::GamesAndRpgParadise::GUI::Gtk::Nibbles.x_coordinate?[0] == ::GamesAndRpgParadise::GUI::Gtk::Nibbles.x_coordinate?[z] and ::GamesAndRpgParadise::GUI::Gtk::Nibbles.y_coordinate?[0] == ::GamesAndRpgParadise::GUI::Gtk::Nibbles.y_coordinate?[z] @within_the_game_board = false end z -= 1 end if ::GamesAndRpgParadise::GUI::Gtk::Nibbles.y_coordinate?[0] > (::GamesAndRpgParadise::Nibbles::HEIGHT - DOT_SIZE) @within_the_game_board = false end if ::GamesAndRpgParadise::GUI::Gtk::Nibbles.y_coordinate?[0] < 0 @within_the_game_board = false end if ::GamesAndRpgParadise::GUI::Gtk::Nibbles.x_coordinate?[0] > (::GamesAndRpgParadise::Nibbles::WIDTH_OF_THE_BOARD - DOT_SIZE) @within_the_game_board = false end if ::GamesAndRpgParadise::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.
#
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 215 216 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 184 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.
#
347 348 349 350 351 352 353 354 355 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 347 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
#
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 152 153 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 103 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
#
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 232 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 = GamesAndRpgParadise::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
#
170 171 172 173 174 175 176 177 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 170 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
#
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 318 319 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 293 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
#
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 84 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
#
331 332 333 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 331 def path_to_the_apple_image "#{IMG_DIR}misc/apple.png" end |
#path_to_the_dot_image ⇒ Object
#
path_to_the_dot_image
#
338 339 340 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 338 def path_to_the_dot_image "#{IMG_DIR}misc/dot.png" end |
#path_to_the_head_image ⇒ Object
#
path_to_the_head_image
#
324 325 326 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 324 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”.
#
160 161 162 163 164 165 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 160 def randomly_place_the_apple r = rand(Nibbles::RAND_POS) @apple_x_coordinate = r * ::GamesAndRpgParadise::Nibbles::DOT_SIZE r = rand(Nibbles::RAND_POS) @apple_y_coordinate = r * ::GamesAndRpgParadise::Nibbles::DOT_SIZE end |
#reset ⇒ Object
#
reset
#
59 60 61 62 63 64 65 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 59 def reset reset_the_internal_variables # ======================================================================= # # === @configuration # ======================================================================= # @configuration = [true, __dir__, NAMESPACE] end |
#run ⇒ Object
#
run
#
360 361 362 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 360 def run initialize_the_game_state end |
#toplevel_x_coordinate? ⇒ Boolean
#
toplevel_x_coordinate?
#
70 71 72 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 70 def toplevel_x_coordinate? Nibbles.x_coordinate? end |
#toplevel_y_coordinate? ⇒ Boolean
#
toplevel_y_coordinate?
#
77 78 79 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/nibbles/nibbles_board.rb', line 77 def toplevel_y_coordinate? Nibbles.y_coordinate? end |