Class: GamesAndRpgParadise::Shoot

Inherits:
Gosu::Window show all
Includes:
Gosu
Defined in:
lib/games_and_rpg_paradise/gui/gosu/aero_exploder/aero_exploder.rb

Constant Summary collapse

TITLE =
#

TITLE

#
'Aero-Exploder'
N_ENEMIES =
#

N_ENEMIES

#
100
N_BULLETS =
#

N_BULLETS

#
40
WIDTH =
#

WIDTH

#
780
HEIGHT =
#

HEIGHT

#
520

Constants included from Gosu

Gosu::COLOUR_AQUA, Gosu::COLOUR_BLACK, Gosu::COLOUR_BLUE, Gosu::COLOUR_CYAN, Gosu::COLOUR_FUCHSIA, Gosu::COLOUR_GOLD, Gosu::COLOUR_GRAY, Gosu::COLOUR_GREEN, Gosu::COLOUR_NONE, Gosu::COLOUR_RED, Gosu::COLOUR_STEELBLUE, Gosu::COLOUR_WHITE, Gosu::COLOUR_YELLOW

Instance Method Summary collapse

Methods included from Gosu

#gosu_tilable_image

Methods inherited from Gosu::Window

#gosu_button_down?, #image, #image10?, #image1?, #image2?, #image3?, #image4?, #image5?, #image6?, #image7?, #image8?, #image9?, #on_left_arrow_pressed?, #on_right_arrow_pressed?, #q_means_quit, #set_font, #set_title, #sqrt, #tab_key?, #write_this_text

Constructor Details

#initialize(run_already = true) ⇒ Shoot

#

initialize

#


44
45
46
47
48
49
50
51
52
# File 'lib/games_and_rpg_paradise/gui/gosu/aero_exploder/aero_exploder.rb', line 44

def initialize(
    run_already = true
  )
  super(WIDTH, HEIGHT, false)
  set_title(TITLE)
  @background = Image.new('data/background.jpg')
  reset
  run if run_already
end

Instance Method Details

#add_enemiesObject

#

add_enemies

#


88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/games_and_rpg_paradise/gui/gosu/aero_exploder/aero_exploder.rb', line 88

def add_enemies
  N_ENEMIES.times {
    # ===================================================================== #
    # Add 100 enemies - or however many were assigned via the constant.
    # ===================================================================== #
    @enemies << Enemy.new(
                 -80,
                 rand(0..self.height),
                 rand(10..25),
                 @time_then + rand(1..60),
                 self
               )
  }
end

#any_bullets_left?Boolean

#

any_bullets_left?

#

Returns:

  • (Boolean)


135
136
137
# File 'lib/games_and_rpg_paradise/gui/gosu/aero_exploder/aero_exploder.rb', line 135

def any_bullets_left?
  @n_bullets > 0
end

#button_down(id) ⇒ Object

#

button_down

#


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/games_and_rpg_paradise/gui/gosu/aero_exploder/aero_exploder.rb', line 156

def button_down(id)
  @cursor.button_down(id)
  @enemies.each {|enemy| enemy.button_down(id, mouse_x, mouse_y)}
  case id
  when KbEscape
    exit
  when KbSpace
    if @game_over
      # Reset to the initial state.
      @time_then = Time.now
      @game_over = false
      @n_bullets = 40
      reset_the_score
      @enemies.each { |enemy|
        enemy.x = -85
        enemy.y = rand(0..self.height)
        enemy.speed = rand(10..25)
        enemy.time = @time_then + rand(1..60)
      }
    end
  end
end

#deduct_one_bulletObject

#

deduct_one_bullet

Note that this method will not deduct below 0.

#


115
116
117
# File 'lib/games_and_rpg_paradise/gui/gosu/aero_exploder/aero_exploder.rb', line 115

def deduct_one_bullet
  @n_bullets -= 1 unless @n_bullets <= 0
end

#display_the_timerObject

#

display_the_timer

#


234
235
236
237
238
# File 'lib/games_and_rpg_paradise/gui/gosu/aero_exploder/aero_exploder.rb', line 234

def display_the_timer
  @count_text.draw_text(
    "#{((@time_then + 30) - @count_time).round(1)}", (self.width / 2) - 72, 20, 10, 1, 1, Color::GRAY
  )
end

#drawObject

#

draw

draw() will check on the value of the @game_over variable.

#


191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/games_and_rpg_paradise/gui/gosu/aero_exploder/aero_exploder.rb', line 191

def draw
  case @game_over
  when false # The game is not yet over.
    @cursor.draw
    @enemies.each {|enemy| enemy.draw}
    @background.draw(0, 0, 0)
    @text.draw_text(
      "Score: #{@score}; Bullets remaining: #{@n_bullets}", 20, self.height - 40, 10, 1, 1, Color::GRAY
    )
    # Also display a timer.
    display_the_timer
  when true # The game is over.
    @game_over_text.draw_text(
      "GAME OVER!", 60, 20, 100000000000000000000000000000000000000000
    )
    notify_the_user_how_many_planes_were_shot_down
    notify_the_user_how_to_play_again_or_how_to_exit
  end
end

#increment_the_score_by_plus_oneObject

#

increment_the_score_by_plus_one

#


182
183
184
# File 'lib/games_and_rpg_paradise/gui/gosu/aero_exploder/aero_exploder.rb', line 182

def increment_the_score_by_plus_one
  @score += 1
end

#n_bullets?Boolean

#

n_bullets?

#

Returns:

  • (Boolean)


142
143
144
# File 'lib/games_and_rpg_paradise/gui/gosu/aero_exploder/aero_exploder.rb', line 142

def n_bullets?
  @n_bullets
end

#needs_cursor?Boolean

#

needs_cursor?

#

Returns:

  • (Boolean)


149
150
151
# File 'lib/games_and_rpg_paradise/gui/gosu/aero_exploder/aero_exploder.rb', line 149

def needs_cursor?
  not true
end

#notify_the_user_how_many_planes_were_shot_downObject

#

notify_the_user_how_many_planes_were_shot_down

#


224
225
226
227
228
229
# File 'lib/games_and_rpg_paradise/gui/gosu/aero_exploder/aero_exploder.rb', line 224

def notify_the_user_how_many_planes_were_shot_down
  @score_text.draw_text(
    "A total of #{@score} planes were downed!",
    self.width / 4 - 25, 200, 1
  )
end

#notify_the_user_how_to_play_again_or_how_to_exitObject

#

notify_the_user_how_to_play_again_or_how_to_exit

#


214
215
216
217
218
219
# File 'lib/games_and_rpg_paradise/gui/gosu/aero_exploder/aero_exploder.rb', line 214

def notify_the_user_how_to_play_again_or_how_to_exit
  @text.draw_text(
    "Press Space to play again or Escape to Exit.",
    self.width / 3.5, 400, 1
  )
end

#resetObject

#

reset

#


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/games_and_rpg_paradise/gui/gosu/aero_exploder/aero_exploder.rb', line 57

def reset
  @cursor         = Cursor.new(self)
  @text           = Font.new(18)
  @count_text     = Font.new(72)
  @game_over_text = Font.new(104)
  @score_text     = Font.new(36)
  # ======================================================================= #
  # === @game_over
  # ======================================================================= #
  @game_over = false
  # ======================================================================= #
  # === @n_bullets
  # ======================================================================= #
  @n_bullets = N_BULLETS
  # ======================================================================= #
  # === @time_then
  # ======================================================================= #
  @time_then = Time.now
  # ======================================================================= #
  # === @enemies
  # ======================================================================= #
  @enemies = []
  # ======================================================================= #
  # === @score
  # ======================================================================= #
  reset_the_score
end

#reset_the_scoreObject

#

reset_the_score

#


106
107
108
# File 'lib/games_and_rpg_paradise/gui/gosu/aero_exploder/aero_exploder.rb', line 106

def reset_the_score
  @score = 0
end

#runObject

#

run

#


243
244
245
# File 'lib/games_and_rpg_paradise/gui/gosu/aero_exploder/aero_exploder.rb', line 243

def run
  add_enemies
end

#updateObject

#

update

#


122
123
124
125
126
127
128
129
130
# File 'lib/games_and_rpg_paradise/gui/gosu/aero_exploder/aero_exploder.rb', line 122

def update
  @count_time = Time.now
  unless @count_time <= @time_then + 30
    @game_over = true
    @n_bullets = 0
  end
  @cursor.update(mouse_x, mouse_y)
  @enemies.each {|enemy| enemy.update(mouse_x, mouse_y)}
end