Class: GamesAndRpgParadise::GUI::Memory::PlayGame

Inherits:
Base
  • Object
show all
Includes:
Memory
Defined in:
lib/games_and_rpg_paradise/gui/gosu/memory/play_game.rb

Constant Summary collapse

GOSU_USE_THIS_FONT =
#

GOSU_USE_THIS_FONT

#
Gosu::Font.new(20, {name: USE_THIS_FONT_FAMILY})
USE_N_CARDS =
#

USE_N_CARDS

You can specify how many cards are to be used here, by default.

#
24
POSITION =
#

POSITION

This hash will store the positions of the cards in use.

#
{
   0 => [  2,   2, 1],
   1 => [ 69,   2, 1],
   2 => [136,   2, 1],
   3 => [203,   2, 1],
   4 => [270,   2, 1],
   5 => [  2,  69, 1],
   6 => [ 69,  69, 1],
   7 => [136,  69, 1],
   8 => [203,  69, 1],
   9 => [270,  69, 1],
  10 => [  2, 136, 1],
  11 => [ 69, 136, 1],
  12 => [136, 136, 1],
  13 => [203, 136, 1],
  14 => [270, 136, 1],
  15 => [  2, 203, 1],
  16 => [ 69, 203, 1],
  17 => [136, 203, 1],
  18 => [203, 203, 1],
  19 => [270, 203, 1]
}

Constants included from Memory

Memory::ARRAY_IMAGES_FOR_THE_MEMORY_GAME, Memory::FILE_BLACK_BACKGROUND, Memory::FILE_CAMEL_HEAD, Memory::FILE_CARD_RANDOM, Memory::FILE_DONKEY, Memory::FILE_FROG, Memory::FILE_LION, Memory::FILE_NEW_GAME, Memory::FILE_OCTOPUS, Memory::FILE_SAD_CRAB, Memory::FILE_SHEEP, Memory::FILE_SNAIL, Memory::FILE_SPERM_WHALE, Memory::FILE_UNICORN, Memory::GAME_MESSAGE_YOU_WON, Memory::IMAGES_DIRECTORY, Memory::SCREENHEIGHT, Memory::SCREENWIDTH, Memory::TITLE, Memory::USE_THIS_FONT_FAMILY

Constants included from Base::Extensions::Colours

Base::Extensions::Colours::ARRAY_AVAILABLE_KONSOLE_COLOURS

Constants included from CommonExtensions

CommonExtensions::CONTROL_C_CODE, CommonExtensions::N

Instance Method Summary collapse

Methods included from Base::Extensions::Colours

ecomment, #efancy, #eparse, #forestgreen, #gold, #grey, #lightblue, #mediumseagreen, #mediumslateblue, #peru, #rev, sdir, sfancy, #sfile, simp, #teal, #yellow

Methods included from Base::Extensions::CommandlineArguments

#commandline_arguments?, #filter_away_commandline_arguments, #first_argument?, #first_non_hyphened_argument?, #set_commandline_arguments

Methods included from CommonExtensions

#cat, #cd, #cliner, #copy_file, #delete, #dirname_but_retains_the_trailing_slash, #disable_colours, #ensure_that_the_log_directory_exists, #esystem, #get_user_input, #infer_the_namespace, #is_on_roebe?, #log_dir?, #mkdir, #mkdir_then_cd_into_it, #mv, #namespace?, #opne, #opnn, #project_base_directory?, #project_image_directory?, #project_yaml_directory?, #rds, #register_sigint, #remove_this_directory, #rename_file, #reset_the_internal_hash, #return_pwd, #return_today, #touch_file, #wrap, #write_what_into

Constructor Details

#initialize(commandline_arguments = ARGV, run_already = true) ⇒ PlayGame

#

initialize

#


98
99
100
101
102
103
104
105
106
107
# File 'lib/games_and_rpg_paradise/gui/gosu/memory/play_game.rb', line 98

def initialize(
    commandline_arguments = ARGV,
    run_already           = true
  )
  reset
  set_commandline_arguments(
    commandline_arguments
  ) 
  run if run_already
end

Instance Method Details

#button_down(id) ⇒ Object

#

button_down

#


197
198
# File 'lib/games_and_rpg_paradise/gui/gosu/memory/play_game.rb', line 197

def button_down(id)
end

#button_up(id) ⇒ Object

#

button_up

#


203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/games_and_rpg_paradise/gui/gosu/memory/play_game.rb', line 203

def button_up(id)
  case id
  when Gosu::MsLeft # On left-mouse-button click:
    game_instance = game_instance?
    POSITION.each { |k, arr|
      if game_instance.mouse_x > arr[0] &&
         game_instance.mouse_x < arr[0]+64 &&
         game_instance.mouse_y > arr[1] &&
         game_instance.mouse_y < arr[1]+64 &&
         !@cards[k].nil? &&
         (@last_clicked_card != k)
        @last_clicked_card = k if @last_clicked_card
        @click += 1
        if @click.odd?
          @first_card_opened = k
        else
          @second_card_opened = k
        end
      end
    }
    if @first_card_opened &&
       @second_card_opened &&
       @click.even? &&
       (@cards[@first_card_opened] == @cards[@second_card_opened]) &&
       (@first_card_opened != @second_card_opened)
      @cards[@first_card_opened] = nil
      @cards[@second_card_opened] = nil
      reset_the_cards_that_were_opened
    end
    if game_ended
      ::GamesAndRpgParadise::GUI::Memory.current_game = GameEnded.new(game_instance)
    end
  end
end

#do_draw_the_current_time(i = @current_time) ⇒ Object

#

do_draw_the_current_time

#


158
159
160
161
162
163
164
165
166
167
168
# File 'lib/games_and_rpg_paradise/gui/gosu/memory/play_game.rb', line 158

def do_draw_the_current_time(
    i = @current_time
  )
  # ======================================================================= #
  # draw_text(text, x, y, z, scale_x = 1, scale_y = 1, color = 0xff_ffffff, mode = :default) ⇒ void
  # ======================================================================= #
  @font.draw_text(
    i, 395, 30, 1, 1, 1, 
    ::Gosu::COLOUR_GOLD
  ) # Position it "center-aligned".
end

#drawObject

#

draw

#


173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/games_and_rpg_paradise/gui/gosu/memory/play_game.rb', line 173

def draw
  # ======================================================================= #
  # Do draw the current time next.
  # ======================================================================= #
  do_draw_the_current_time
  # ======================================================================= #
  # Display all the cards.
  # ======================================================================= #
  @use_n_cards.times { |entry|
    if !@cards[entry]
      next
    elsif (@second_card_opened == entry) && @click.even?
      @cards[@second_card_opened].draw(*POSITION[@second_card_opened])
    elsif @first_card_opened == entry
      @cards[@first_card_opened].draw(*POSITION[@first_card_opened])
    else
      @card_back.draw(*POSITION[entry])
    end
  }
end

#game_endedObject

#

game_ended

#


258
259
260
261
262
263
264
265
# File 'lib/games_and_rpg_paradise/gui/gosu/memory/play_game.rb', line 258

def game_ended
  if @cards[0 .. 19] == Array.new(20)
    if game_instance?.best_time > @current_time
      game_instance?.best_time = @current_time
    end
    return true
  end
end

#game_instance?Boolean

#

game_instance?

#

Returns:

  • (Boolean)


270
271
272
# File 'lib/games_and_rpg_paradise/gui/gosu/memory/play_game.rb', line 270

def game_instance?
  $w
end

#load_cardsObject

#

load_cards

This will fill up the pool of available cards.

#


305
306
307
308
309
310
311
312
313
# File 'lib/games_and_rpg_paradise/gui/gosu/memory/play_game.rb', line 305

def load_cards
  _ = ::GamesAndRpgParadise::GUI::Memory.available_images?
  2.times {
    1.upto(10) { |i|
      @cards << _[i]
    }
  }
  @cards.shuffle! # Shuffle it a bit afterwards, for some randomness.
end
#

menu

#


277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/games_and_rpg_paradise/gui/gosu/memory/play_game.rb', line 277

def menu(
    i = @commandline_arguments
  )
  if i.is_a? Array
    i.each {|entry| menu(entry) }
  else
    case i
    # ===================================================================== #
    # === memory --ncards=18
    # ===================================================================== #
    when /^-?-?ncards=(\d+)/i
      set_n_cards($1)
    end
  end
end

#resetObject

#

reset

#


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/gosu/memory/play_game.rb', line 112

def reset
  # ======================================================================= #
  # === @font
  # ======================================================================= #
  @font = GOSU_USE_THIS_FONT
  # ======================================================================= #
  # === @use_n_cards
  # ======================================================================= #
  @use_n_cards = USE_N_CARDS
  # ======================================================================= #
  # === @cards
  # ======================================================================= #
  @cards = []
  # ======================================================================= #
  # === @current_time
  # ======================================================================= #
  @current_time = 0
  # ======================================================================= #
  # === @click
  # ======================================================================= #
  @click = 0
  # ======================================================================= #
  # === @start_time
  # ======================================================================= #
  @start_time = Gosu.milliseconds / 1000
  # ======================================================================= #
  # === @last_clicked_card
  # ======================================================================= #
  @last_clicked_card = nil
  # ======================================================================= #
  # === @first_card_opened
  # ======================================================================= #
  @first_card_opened = nil
  # ======================================================================= #
  # === @second_card_opened
  # ======================================================================= #
  @second_card_opened = nil
  # ======================================================================= #
  # === @card_back
  # ======================================================================= #
  @card_back = ::GamesAndRpgParadise::GUI::Memory.available_images?['card_back']
end

#reset_the_cards_that_were_openedObject

#

reset_the_cards_that_were_opened

#


241
242
243
244
# File 'lib/games_and_rpg_paradise/gui/gosu/memory/play_game.rb', line 241

def reset_the_cards_that_were_opened
  @first_card_opened = nil
  @second_card_opened = nil
end

#runObject

#

run

#


318
319
320
321
# File 'lib/games_and_rpg_paradise/gui/gosu/memory/play_game.rb', line 318

def run
  menu
  load_cards
end

#set_n_cards(i) ⇒ Object

#

set_n_cards

#


296
297
298
# File 'lib/games_and_rpg_paradise/gui/gosu/memory/play_game.rb', line 296

def set_n_cards(i)
  @use_n_cards = i.to_i
end

#updateObject

#

update (update tag)

The current time will be re-calculated whenever update() is called.

#


251
252
253
# File 'lib/games_and_rpg_paradise/gui/gosu/memory/play_game.rb', line 251

def update
  @current_time = (Gosu.milliseconds / 1000) - @start_time
end