-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.rb
executable file
·677 lines (638 loc) · 23 KB
/
game.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
require 'gosu'
################################################
############# Classes declaration ##############
################################################
# --------------------------------------------------------------------------------------------------
# -------------------------------------- HEART -------------------------------------------------
# --------------------------------------------------------------------------------------------------
class Heart
SPEED = 2
attr_reader :x, :y, :angle, :radius
def initialize(window, x, y)
@radius = 25
@image = Gosu::Image.new("image/heart_shield.png") # Replace with your single image file
@window = window
@x = x
@y = y
end
# ----------------------------------------------
def draw
@image.draw(@x, @y, 0)
end
# ----------------------------------------------
def move
@y += SPEED
if @y > @window.height
@window.missiles.delete(self)
end
end
# ----------------------------------------------
def y
@y
end
end
# --------------------------------------------------------------------------------------------------
# -------------------------------------- MISSILE ---------------------------------------------------
# --------------------------------------------------------------------------------------------------
class Missile
SPEED = 3
attr_reader :x, :y, :angle, :radius
def initialize(window, x, y)
@radius = 30
@animation_frames = [] # Initialize the array to hold animation frames
5.times do |i|
frame = Gosu::Image.new("missile/missile_#{i + 1}.png") #21x75
@animation_frames.push(frame)
end
@window = window
@x = x
@y = y
@current_frame = 0 # Track the current frame of the animation
end
# ----------------------------------------------
def draw
@animation_frames[@current_frame].draw(@x, @y, 0)
@current_frame = (@current_frame + 1) % @animation_frames.length # Cycle through frames
end
# ----------------------------------------------
def move
@y += SPEED
if @y > @window.height
@window.missiles.delete(self)
end
end
# ----------------------------------------------
def y
@y
end
end
# ---------------------------------------------------------------------------------------------------
# -------------------------------------- INVADER ----------------------------------------------------
# --------------------------------------------------------------------------------------------------
class Invader
SPEED = 2.5
attr_reader :x, :y, :radius
def initialize(window, x, y)
@radius = 20
@x = rand(window.width - 2 * @radius) + @radius
@y = 0
@image = Gosu::Image.load_tiles('image/enemy4244.png', 42, 44)
@image_index = 0
end
def draw
if @image_index < @image.count
@image[@image_index].draw(@x - @radius, @y - @radius, 1)
@image_index += 1
else
@image_index = 0
end
end
def move
@y += SPEED
end
end
# ---------------------------------------------------------------------------------------------------
# -------------------------------------- INVADER1 ----------------------------------------------------
# --------------------------------------------------------------------------------------------------
class Invader1
SPEED = 2
attr_accessor :x, :y, :radius, :hp
def initialize(window, x, y)
@radius = 40
@x = rand(window.width - 2 * @radius) + @radius
@y = 0
@image = Gosu::Image.new('image/boss.png')
@hp = 5
end
def draw
@image.draw(@x - @radius, @y - @radius, 1)
end
def move
@y += SPEED
end
end
# --------------------------------------------------------------------------------------------------
# -------------------------------------- EXPLOSION ----------------------------------------------------
# --------------------------------------------------------------------------------------------------
class Explosion
attr_reader :finished
def initialize(window, x, y)
@x = x
@y = y
@radius = 30
@image = Gosu::Image.load_tiles('image/explosion2.png', 100, 100)
@finished = false
@image_index = 0
end
def draw
if @image_index < @image.count
@image[@image_index].draw(@x - @radius, @y - @radius, 2)
@image_index += 1
else
@finished = true
end
end
end
# --------------------------------------------------------------------------------------------------
# -------------------------------------- BULLET ----------------------------------------------------
# --------------------------------------------------------------------------------------------------
class Bullet
SPEED = 5
attr_reader :x, :y, :radius
def initialize(window, x, y, angle)
@x = x
@y = y
@direction = angle
@image = Gosu::Image.new('image/bullet.png')
@radius = 3
@window = window
end
def move
@x += Gosu.offset_x(@direction, SPEED)
@y += Gosu.offset_y(@direction, SPEED)
end
def draw
@image.draw(@x - @radius, @y - @radius, 1)
end
def onscreen?
right = @window.width + @radius
left = -@radius
top = -@radius
bottom = @window.height + @radius
@x > left and @x < right and @y > top and @y < bottom
end
end
# --------------------------------------------------------------------------------------------------
# -------------------------------------- PLAYER ----------------------------------------------------
# --------------------------------------------------------------------------------------------------
class Player
SPEED = 7
SHIP_WIDTH = 80
SHIP_HEIGHT = 80
VERTICAL_SPEED = 0.5
attr_reader :x, :y, :angle, :radius
def initialize(window)
@x = 350
@y = 480
@angle = 0
@radius = 30
@image = Gosu::Image.new('image/ship.png')
@direction = :up
end
def draw
@image.draw(@x, @y, 1)
end
def move_right
@x += SPEED
end
def move_left
@x -= SPEED
end
def move
if @direction == :up
@y -= VERTICAL_SPEED
if @y <= 467
@direction = :down
end
elsif @direction == :down
@y += VERTICAL_SPEED
if @y + SHIP_HEIGHT >= 550
@direction = :up
end
end
if @x + SHIP_WIDTH < 800 && @x > 0
# player can move
elsif @x <= 0
# player is at the leftmost position
@x = 0
elsif @x + SHIP_WIDTH >= 800
# player is at the rightmost position
@x = 800 - SHIP_WIDTH
end
end
end
# ------------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------------
################################################
################## Game Main ###################
################################################
class UFO_Hunters < Gosu::Window
INVADER_FREQUENCY = 5
def initialize
@window_width = 800
@window_height = 600
super(@window_width, @window_height, false)
self.caption = "UFO Hunters"
#Include images into game
@start_image = Gosu::Image.new("image/open_screen.png", tileable: true)
@background_image = Gosu::Image.new("image/galaxy.png", tileable: true)
@missile_start = Gosu::Image.new("image/missile.png", tileable: true)
@instructions_image = Gosu::Image.new("image/Instruction.png", tileable: true)
@credits_image = Gosu::Image.new("image/credits.png", tileable: true)
@plot_image = Gosu::Image.new("image/plot.png", tileable: true)
@scene = :start
@background_y = 0
@background_speed = 1.5
@info_font = Gosu::Font.new(20)
# ----------------------------------------------
# Animation in start screen
@hover_start = false
@hover_plot = false
@hover_instructions = false
@hover_credits = false
# ----------------------------------------------
#Include sound into game
@background_sound = Gosu::Song.new("sound/background_sound.mp3")
@background_sound.volume = 0.5
@background_sound.play(true)
@explosion_sound = Gosu::Sample.new("sound/explosion.mp3")
@shooting_sound = Gosu::Sample.new("sound/shoot.mp3")
@hover_sound = Gosu::Sample.new("sound/hover_sound.mp3")
@bonus_life = Gosu::Sample.new("sound/bonus_life.mp3")
end
# ----------------------------------------------
def initialize_game
@player = Player.new(self)
@invaders = []
@invaders1 = []
@bullets = []
@explosions = []
@missiles = []
@hearts = []
@scene = :game
@background_y = 0
@background_speed = 1.5
@invader_timer = 0
@invader_interval = 100
@background_image = Gosu::Image.new("image/galaxy.png", tileable: true)
@heart_image = Gosu::Image.new("image/heart.png")
@hearts_life = 3
@score = 0
@score_font = Gosu::Font.new(30)
@score_end_font = Gosu::Font.new(30)
end
# ----------------------------------------------
def initialize_end(status)
@end_sucess_image = Gosu::Image.new("image/end_sucess.png")
@end_fail_image = Gosu::Image.new("image/end_fail.png")
case status
when :hit_by_enemy
end
@scene = :end
@score_end_font = Gosu::Font.new(75)
end
def draw_end
if @score > 100
@end_sucess_image.draw(0, 0, 0)
else
@end_fail_image.draw(0, 0, 0)
end
@score_end_font.draw(@score, 385, 228, 1, 1, 1, Gosu::Color::WHITE)
end
# ----------------------------------------------
def draw
case @scene
when :start
draw_start
when :game
draw_game
when :end
draw_end
end
end
# ----------------------------------------------
def draw_start
# -------- Make the background image moving ---------
@background_image.draw(0, @background_y, 0, @window_width.to_f / @background_image.width, 1)
@background_image.draw(0, @background_y - @background_image.height, 0, @window_width.to_f / @background_image.width, 1)
@start_image.draw(0, 0, 0)
# -------- Draw missile in the start screen with hover animation ---------
if @hover_start
@missile_start.draw(15,306,0)
end
if @hover_plot
@missile_start.draw(15,355,0)
end
if @hover_instructions
@missile_start.draw(15,400,0)
end
if @hover_credits
@missile_start.draw(15,450,0)
end
# -------- Handle plot and instructions and credits window ---------
if @plot_visible
@plot_image.draw(0, 0, 0)
end
if @instructions_visible
@instructions_image.draw(0, 0, 0)
end
if @credits_visible
@credits_image.draw(0, 0, 0)
end
end
# ----------------------------------------------
def draw_game
@score_font.draw(@score, 30, 40, 1, 1, 1, Gosu::Color::WHITE)
@player.draw
# -------- Draw the invaders in the screen ---------
@invaders.each do |invader|
invader.draw
end
# -------- Draw the invaders1 in the screen ---------
@invaders1.each do |invader1|
invader1.draw
end
# -------- Draw the bullets in the screen ---------
@bullets.each do |bullet|
bullet.draw
end
# -------- Draw the explosion in the screen ---------
@explosions.each do |explosion|
explosion.draw
end
# -------- Make the backgr movin in Game screen ---------
@background_image.draw(0, @background_y, 0, @window_width.to_f / @background_image.width, 1)
@background_image.draw(0, @background_y - @background_image.height, 0, @window_width.to_f / @background_image.width, 1)
# -------- Draw remaining hearts(lives left) ---------
heart_spacing = 10
heart_start_x = self.width - (@heart_image.width + heart_spacing) * @hearts_life
heart_y = 20
@hearts_life.times do |i|
heart_x = heart_start_x + i * (@heart_image.width + heart_spacing)
@heart_image.draw(heart_x, heart_y, 0)
end
# -------- Draw the missiles falling down ---------
@missiles.each do |missile|
missile.draw
end
# -------- Draw the hearts falling down ---------
@hearts.each do |heart|
heart.draw
end
end
# ----------------------------------------------
def update
case @scene
when :start
update_start
when :game
update_game
end
end
# ----------------------------------------------
def button_down(id)
case @scene
when :start
button_down_start(id)
when :game
button_down_game(id)
when :end
button_down_end(id)
end
end
# ----------------------------------------------
def button_down_start(id)
if @hover_start
initialize_game
end
# -------- Make Instructions window appear ---------
if @hover_instructions
@instructions_visible = true
end
if @hover_return
@instructions_visible = false
end
# -------- Make Credit window appear ---------
if @hover_credits
@credits_visible = true
end
# -------- Return to home screen ---------
if @hover_return
@credits_visible = false
end
# -------- Make Plot window appear ---------
if @hover_plot
@plot_visible = true
end
# -------- Return to home screen ---------
if @hover_return
@plot_visible = false
end
end
# ----------------------------------------------
def button_down_game(id)
if id == Gosu::KbSpace
@bullets.push Bullet.new(self, @player.x + 37, @player.y + 25, @player.angle)
@shooting_sound.play
end
end
# ----------------------------------------------
def update_start
# -------- Animation for background ---------
@background_y += @background_speed
@background_y %= @background_image.height
# -------- Indicate START button ---------
# 85, 340 187, 361
if ((mouse_x > 85 and mouse_x < 187) and (mouse_y > 340 and mouse_y < 361))
@hover_start = true
else
@hover_start = false
end
# -------- Indicate PLOT button ---------
# 85, 388 166, 408
if ((mouse_x > 85 and mouse_x < 166) and (mouse_y > 388 and mouse_y < 408))
@hover_plot = true
else
@hover_plot = false
end
# -------- Indicate INSTRUCTIONS button ---------
# 85, 434 317, 456
if ((mouse_x > 85 and mouse_x < 317) and (mouse_y > 434 and mouse_y < 456))
@hover_instructions = true
else
@hover_instructions = false
end
# -------- Indicate CREDITS button ---------
# 85, 482 218, 503
if ((mouse_x > 85 and mouse_x < 218) and (mouse_y > 482 and mouse_y < 503))
@hover_credits = true
else
@hover_credits = false
end
# -------- Indicate RETURN button ---------
# 34,33 69, 90
if ((mouse_x > 34 and mouse_x < 69) and (mouse_y > 33 and mouse_y < 90))
@hover_return = true
else
@hover_return = false
end
end
# ----------------------------------------------
def update_game
# ------------ Player movement --------------
@player.move_right if button_down?(Gosu::KbRight)
@player.move_left if button_down?(Gosu::KbLeft)
@player.move
# ------------ Invader movement --------------
@invaders.each { |invader| invader.move }
@invader_timer += 1
if @invader_timer >= @invader_interval
@invaders.push Invader.new(self, 100, 100)
@invader_timer = 0
@invader_interval -= 1 if @invader_interval > 10 # Decrease the interval over time
end
# ------------ Invader1 movement --------------
@invaders1.each { |invader1| invader1.move }
@invader_timer += 0.3
if @invader_timer >= @invader_interval
@invaders1.push Invader1.new(self, 100, 100)
@invader_timer = 0
@invader_interval -= 1 if @invader_interval > 10 # Decrease the interval over time
end
# ------------ Bullet movement --------------
@bullets.each do |bullet|
bullet.move
end
# -------- Collision between INVADER & BULLET ---------
@invaders.dup.each do |invader|
@bullets.dup.each do |bullet|
distance = Gosu.distance(invader.x, invader.y, bullet.x, bullet.y)
if distance < invader.radius + bullet.radius
@invaders.delete invader
@bullets.delete bullet
@explosions.push Explosion.new(self, invader.x, invader.y)
@explosion_sound.play
@score += 1
# -----------------------------------------------------------------------------
# Create HEART & MISSILE dropping from the exploded invader's position randomly
random_number = rand
if random_number < 0.5
missile = Missile.new(self, invader.x, invader.y)
@missiles.push(missile)
elsif random_number < 0.85
heart = Heart.new(self, invader.x, invader.y)
@hearts.push(heart)
end
end
end
end
# -------- Collision between INVADER1 & BULLET ---------
@invaders1.dup.each do |invader1|
@bullets.dup.each do |bullet|
distance = Gosu.distance(invader1.x, invader1.y, bullet.x, bullet.y)
if distance < invader1.radius + bullet.radius
invader1.hp -= 1 # Add a hits counter to the invader
if invader1.hp == 0
@invaders1.delete(invader1)
@score += 5
end
@bullets.delete(bullet)
@explosions.push(Explosion.new(self, invader1.x, invader1.y))
@explosion_sound.play
end
end
end
# --------- Background moving ----------
@background_y += @background_speed
@background_y %= @background_image.height
# ----------- Missile moving down -----------
@missiles.each do |missile|
missile.move
@missiles.delete(missile) if missile.y > @window_height
end
# ----------- Heart moving down -----------
@hearts.each do |heart|
heart.move
@hearts.delete(heart) if heart.y > @window_height
end
# -------- Collision between INVADER & PLAYER ---------
@invaders.dup.each do |invader|
distance = Gosu.distance(invader.x, invader.y, @player.x, @player.y)
if distance < invader.radius + @player.radius
@invaders.delete(invader)
@explosions.push Explosion.new(self, invader.x, invader.y)
@explosion_sound.play
# ----------------------------------------------
# Reduce the number of hearts
@hearts_life -= 1
# ----------------------------------------------
# End screen indication
if @hearts_life.zero?
# Player has lost all hearts, end the game
@scene = :end
initialize_end(:hit_by_enemy)
else
# Create a new player instance
@player = Player.new(self)
end
end
end
# -------- Collision between INVADER1 & PLAYER ---------
@invaders1.dup.each do |invader1|
distance = Gosu.distance(invader1.x, invader1.y, @player.x, @player.y)
if distance < invader1.radius + @player.radius
@invaders1.delete(invader1)
@explosions.push Explosion.new(self, invader1.x, invader1.y)
@explosion_sound.play
# ----------------------------------------------
# Reduce the number of hearts
@hearts_life -= 2
# ----------------------------------------------
# End screen indication
if @hearts_life.zero?
# Player has lost all hearts, end the game
@scene = :end
initialize_end(:hit_by_enemy)
else
# Create a new player instance
@player = Player.new(self)
end
end
end
# -------- Collision between MISSILE & PLAYER ---------
@missiles.dup.each do |missile|
distance = Gosu.distance(missile.x, missile.y, @player.x, @player.y)
if distance < missile.radius + @player.radius
@missiles.delete(missile)
@explosions.push Explosion.new(self, missile.x, missile.y)
@explosion_sound.play
# Reduce the number of hearts
@hearts_life -= 1
if @hearts_life.zero?
# Player has lost all hearts, end the game
@scene = :end
initialize_end(:hit_by_enemy)
else
# Create a new player instance
@player = Player.new(self)
end
end
end
# -------- Collision between HEART & PLAYER ---------
@hearts.dup.each do |heart|
distance = Gosu.distance(heart.x, heart.y, @player.x, @player.y)
if distance < heart.radius + @player.radius
@hearts.delete(heart)
@bonus_life.play
@hearts_life += 1
end
end
end
# ----------------------------------------------
def missiles
@missiles
end
# ----------------------------------------------
def hearts
@hearts
end
def button_down_end(id)
if id == Gosu::KbP
initialize
elsif id == Gosu::KbQ
close
end
end
end
window = UFO_Hunters.new
window.show