forked from ippa/chingu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
game1.rb
670 lines (537 loc) · 16.8 KB
/
game1.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
#!/usr/bin/env ruby
#
#
# A simple game in Chingu, using GameState, GameObject, Paralaxx, has_traits etc
#
#
require 'rubygems' rescue nil
$LOAD_PATH.unshift File.join(File.expand_path(__FILE__), "..", "..", "lib")
require 'chingu'
require 'texplay' # adds Image#get_pixel
require 'optparse'
include Gosu
include Chingu
class Game < Chingu::Window
def initialize
$debug = false
ARGV.each do |a|
if a == "-debug"
$debug = true
end
end
super(1000,400,false)
self.input = { :escape => :exit }
retrofy
switch_game_state(Level)
end
end
#
# GAME STATE: LEVEL
#
class Level < Chingu::GameState
has_trait :timer
def initialize(options = {})
super
@parallax = Parallax.create(:rotation_center => :top_left, :zorder => 0)
@parallax << { :image => "city2.png", :damping => 2, :zorder => 0}
@parallax << { :image => "city1.png", :damping => 1, :zorder => 1000}
@player = Player.create(:x => 30, :y => 10, :zorder=> 500)
@bg1 = Color.new(0xFFCE28FF)
@bg2 = Color.new(0xFF013E87)
@text = Text.create("Score:", :size => 20, :x => 30, :y => 370, :zorder => 1001) #initialize score string
@text1 = Text.create("0", :size => 20, :x => 90, :y => 370, :zorder => 1001) #initialize score value to 0
end
#
# This is called each time this GameState is switched/pushed/poped to.
#
def setup
# Remove all lingering game objects
Enemy.destroy_all
Bullet.destroy_all
@player.score = 0
@player.x = 10
@player.y = 100
@parallax.camera_x = 0
@total_game_ticks = 10000#100000
@timer = 100
@total_ticks = 0
end
#
# The foremost layer in our parallax scroller is the collidable terrain
#
def solid_pixel_at?(x, y)
begin
@parallax.layers.last.get_pixel(x, y)[3] != 0
rescue
puts "Error in get_pixel(#{x}, #{y})"
end
end
def update
super
# Move the level forward by increasing the parallax-scrollers camera x-coordinate
@parallax.camera_x += 1
# Remove all objects outside screen
game_objects.destroy_if { |game_object| game_object.respond_to?("outside_window?") && game_object.outside_window? }
# Collide bullets with terrain
Bullet.select { |o| solid_pixel_at?(o.x, o.y)}.each { |o| o.die }
# Collide player with terrain
push_game_state(GameOver) if solid_pixel_at?(@player.x, @player.y)
# Collide player with enemies and enemy bullets
@player.each_bounding_circle_collision(Enemy) do |player, enemy|
enemy.die
push_game_state(GameOver)
end
Bullet.each_bounding_circle_collision(Enemy) do |bullet, enemy|
bullet.die
if enemy.hit_by(bullet)
@player.score += 20
@text1.destroy! #destroy old score
@text1 = Text.create(@player.score, :size => 20, :x => 90, :y => 370, :zorder => 1001 ) #update the new score
end
end
Bullet.each_bounding_circle_collision(EnemyPlane) do |bullet, enemy|
bullet.die
if enemy.hit_by(bullet)
@player.score += 20
@text1.destroy! #destroy old score
@text1 = Text.create(@player.score, :size => 20, :x => 90, :y => 370, :zorder => 1001 ) #update the new score
end
end
# clean up objects
game_objects.destroy_if { |object| object.outside_window? || object.color.alpha == 0 }
@timer = @timer * 0.9999
@total_ticks += 1
if @total_ticks > @timer
Enemy.create(:x => $window.width, :y => rand(300), :zorder => (rand(20) * 100))
@total_ticks = 0
end
if @player.score > 100
switch_game_state(Level2)
end
if($debug)
$window.caption = "City Battle! - Player x/y: #{@player.x}/#{@player.y} - Score: #{@player.score} - FPS: #{$window.fps} - game objects: #{game_objects.size}"
else
$window.caption = "City Battle! - Score: #{@player.score} - FPS: #{$window.fps} - game objects: #{game_objects.size}"
end
end
def draw
fill_gradient(:from => @bg2, :to => @bg1)
@parallax.draw
super
end
end
#
# GAME STATE: LEVEL
#
class Level2 < Chingu::GameState
has_trait :timer
def initialize(options = {})
super
@parallax = Parallax.create(:rotation_center => :top_left, :zorder => 0)
@parallax << { :image => "city1.png", :damping => 2, :zorder => 0}
@parallax << { :image => "city3.png", :damping => 1, :zorder => 1000}
@player = Player.create(:x => 30, :y => 10, :zorder=> 500)
@bg1 = Color.new(0xFFCE28FF)
@bg2 = Color.new(0xFF013E87)
@text = Text.create("Score:", :size => 20, :x => 30, :y => 370, :zorder => 1001) #initialize score string
@text1 = Text.create("0", :size => 20, :x => 90, :y => 370, :zorder => 1001) #initialize score value to 0
end
#
# This is called each time this GameState is switched/pushed/poped to.
#
def setup
# Remove all lingering game objects
Enemy.destroy_all
Bullet.destroy_all
@player.score = 0
@player.x = 10
@player.y = 100
@parallax.camera_x = 0
@total_game_ticks = 10000#100000
@timer = 100
@total_ticks = 0
end
#
# The foremost layer in our parallax scroller is the collidable terrain
#
def solid_pixel_at?(x, y)
begin
@parallax.layers.last.get_pixel(x, y)[3] != 0
rescue
puts "Error in get_pixel(#{x}, #{y})"
end
end
def update
super
# Move the level forward by increasing the parallax-scrollers camera x-coordinate
@parallax.camera_x += 1.5
# Remove all objects outside screen
game_objects.destroy_if { |game_object| game_object.respond_to?("outside_window?") && game_object.outside_window? }
# Collide bullets with terrain
Bullet.select { |o| solid_pixel_at?(o.x, o.y)}.each { |o| o.die }
# Collide player with terrain
push_game_state(GameOver) if solid_pixel_at?(@player.x, @player.y)
# Collide player with enemies and enemy bullets
@player.each_bounding_circle_collision(Enemy) do |player, enemy|
enemy.die
push_game_state(GameOver)
end
# Collide player with enemies and enemy bullets
@player.each_bounding_circle_collision(EnemyPlane) do |player, enemy|
enemy.die
push_game_state(GameOver)
end
Bullet.each_bounding_circle_collision(Enemy) do |bullet, enemy|
bullet.die
if enemy.hit_by(bullet)
@player.score += 20
@text1.destroy! #destroy old score
@text1 = Text.create(@player.score, :size => 20, :x => 90, :y => 370, :zorder => 1001 ) #update the new score
end
end
Bullet.each_bounding_circle_collision(EnemyPlane) do |bullet, enemy|
bullet.die
if enemy.hit_by(bullet)
@player.score += 20
@text1.destroy! #destroy old score
@text1 = Text.create(@player.score, :size => 20, :x => 90, :y => 370, :zorder => 1001 ) #update the new score
end
end
Bullet.each_bounding_circle_collision(EnemyPlane) do |bullet, enemy|
bullet.die
if enemy.hit_by(bullet)
@player.score += 20
@text1.destroy! #destroy old score
@text1 = Text.create(@player.score, :size => 20, :x => 90, :y => 370, :zorder => 1001 ) #update the new score
end
end
@timer = @timer * 0.9999
@total_ticks += 1
if @total_ticks > @timer
@select=rand(2)
if @select==1
Enemy.create(:x => $window.width, :y => rand(300), :zorder => (rand(20) * 100))
else
EnemyPlane.create(:x => $window.width, :y => rand(300), :zorder => (rand(20) * 100))
end
@total_ticks = 0
end
if @player.score > 100
switch_game_state(Done)
end
# clean up objects
game_objects.destroy_if { |object| object.outside_window? || object.color.alpha == 0 }
if($debug)
$window.caption = "City Battle! - Player x/y: #{@player.x}/#{@player.y} - Score: #{@player.score} - FPS: #{$window.fps} - game objects: #{game_objects.size}"
else
$window.caption = "City Battle! - Score: #{@player.score} - FPS: #{$window.fps} - game objects: #{game_objects.size}"
end
end
def draw
fill_gradient(:from => @bg2, :to => @bg1)
@parallax.draw
super
end
end
#
# OUR PLAYER
#
class Player < GameObject
has_traits :velocity, :collision_detection, :timer
has_trait :bounding_circle, :scale => 0.50, :debug => false
attr_accessor :score
def setup
@image = Image["plane.png"]
self.input = {
:holding_left => :left,
:holding_right => :right,
:holding_up => :up,
:holding_down => :down,
:holding_space => :fire }
@max_velocity = 2
@score = 0
@cooling_down = false
end
def up
self.velocity_y = -@max_velocity
end
def down
self.velocity_y = @max_velocity
end
def right
self.velocity_x = @max_velocity
end
def left
self.velocity_x = -@max_velocity
end
def fire
return if @cooling_down
@cooling_down = true
after(100) { @cooling_down = false }
Bullet.create(:x => self.x, :y => self.y)
Sound["laser.wav"].play(0.1)
end
def update
self.velocity_y *= 0.6
self.velocity_x *= 0.6
@x = @last_x if @x < 0 || @x > $window.width
@y = @last_y if @y < 0 || @y > $window.height
@last_x, @last_y = @x, @y
end
end
#
# OUR PLAYERS BULLETS
#
class Bullet < GameObject
has_traits :timer, :collision_detection, :velocity
attr_reader :status, :radius
def setup
@image = Image["bullet.png"]
self.velocity_x = 10
@status = :default
@radius = 3
end
def die
return if @status == :dying
Sound["bullet_hit.wav"].play(0.2)
@status = :dying
during(50) { @factor_x += 1; @factor_y += 1; @x -= 1; }.then { self.destroy }
end
end
#
# ENEMY BULLET
#
class EnemyBullet < Bullet
def setup
@image = Image["enemy_bullet.png"]
self.velocity_x = -3
end
end
class Explosion < GameObject
has_traits :timer
def initialize(options)
super
unless defined?(@@image)
@@image = TexPlay::create_blank_image($window, 100, 100)
@@image.paint { circle 50,50,49, :fill => true, :color => [1,1,1,1] }
end
@image = @@image.dup if @image.nil?
self.rotation_center = :center
during(100) { self.alpha -= 30}.then { destroy }
end
def self.create_image_for(object)
width = height = (object.image.width + object.image.height) / 2
explosion_image = TexPlay::create_blank_image($window, 100, 100)
explosion_image.paint { circle 50,50,49, :fill => true, :color => [1,1,1,1] }
return explosion_image
end
end
class Shrapnel < GameObject
has_traits :timer, :effect, :velocity
#def initialize(options)
# super
def setup
self.rotation_rate = 1 + rand(10)
self.velocity_x = 4 - rand(8)
self.velocity_y = 4 - rand(10)
self.acceleration_y = 0.2 # gravity = downards acceleration
self.rotation_center = :center
@status = :default
end
def self.create_image_for(object)
image = object.image
width = image.width / 5
height = image.width / 5
shrapnel_image = TexPlay::create_blank_image($window, width, height)
x1 = rand(image.width/width)
y1 = rand(image.height/height)
shrapnel_image.paint { splice image,0,0, :crop => [x1, y1, x1+width, y1+height] }
return shrapnel_image
end
def die
destroy
end
end
#
# OUR ENEMY SAUCER
#
class Enemy < GameObject
has_traits :collision_detection, :timer
attr_reader :radius
def setup
@velocity = options[:velocity] || 2
@health = options[:health] || 100
@anim = Animation.new(:file => "media/saucer.png", :size => [32,13], :delay => 100)
@image = @anim.first
@radius = 5
@black = Color.new(0xFF000000)
@status == :default
@fireball_animation = Chingu::Animation.new(:file => media_path("fireball.png"), :size => [32,32])
#
# Cache explosion and shrapnel images (created with texplay, not recomended doing over and over each time)
#
@@shrapnel_image ||= Shrapnel.create_image_for(self)
@@explosion_image ||= Explosion.create_image_for(self)
end
def hit_by(object)
return if @status == :dying
#
# During 20 millisecons, use Gosus :additive draw-mode, which here results in a white sprite
# Classic "hit by a bullet"-effect
#
during(20) { @mode = :additive; }.then { @mode = :default }
@health -= 20
if @health <= 0
die
return true
else
return false
end
end
def fire
EnemyBullet.create(:x => self.x, :y => self.y)
end
def die
#
# Make sure die() is only called once
#
return if @status == :dying
@status = :dying
#
# Play our explosion-sound file
# Create an explosion-object
# Create some shrapnel-objects
#
Sound["explosion.wav"].play(0.3)
#Explosion.create(:x => @x, :y => @y, :image => @@explosion_image )
5.times{ Chingu::Particle.create( :x => @x,
:y => @y,
:animation => @fireball_animation,
:scale_rate => +0.05,
:fade_rate => -10,
:rotation_rate => +1,
:mode => :default
)
@x -= @velocity
}
5.times { Shrapnel.create(:x => @x, :y => @y, :image => @@shrapnel_image)}
#
# During 200 ms, fade and scale image, then destroy it
#
@color = @black
@color.alpha = 50
during(200) { @factor_x += 0.5; @factor_y += 0.5; @x -= 1; @color.alpha -= 1}.then { self.destroy }
end
def update
return if @status == :dying
@image = @anim.next
@x -= @velocity
end
end
#
# OUR ENEMY Plane
#
class EnemyPlane < GameObject
has_traits :collision_detection, :timer
attr_reader :radius
def setup
@velocity = options[:velocity] || 2
@health = options[:health] || 100
@anim = Animation.new(:file => "media/enemy_plane.png", :size => [32,20], :delay => 100)
@image = @anim.first
@radius = 5
@black = Color.new(0xFF000000)
@status == :default
@fireball_animation = Chingu::Animation.new(:file => media_path("fireball.png"), :size => [32,32])
#
# Cache explosion and shrapnel images (created with texplay, not recomended doing over and over each time)
#
@@shrapnel_image ||= Shrapnel.create_image_for(self)
@@explosion_image ||= Explosion.create_image_for(self)
end
def hit_by(object)
return if @status == :dying
#
# During 20 millisecons, use Gosus :additive draw-mode, which here results in a white sprite
# Classic "hit by a bullet"-effect
#
during(20) { @mode = :additive; }.then { @mode = :default }
@health -= 20
if @health <= 0
die
return true
else
return false
end
end
def fire
EnemyBullet.create(:x => self.x, :y => self.y)
end
def die
#
# Make sure die() is only called once
#
return if @status == :dying
@status = :dying
#
# Play our explosion-sound file
# Create an explosion-object
# Create some shrapnel-objects
#
Sound["explosion.wav"].play(0.3)
#Explosion.create(:x => @x, :y => @y, :image => @@explosion_image )
5.times{ Chingu::Particle.create( :x => @x,
:y => @y,
:animation => @fireball_animation,
:scale_rate => +0.05,
:fade_rate => -10,
:rotation_rate => +1,
:mode => :default
)
@x -= @velocity
}
5.times { Shrapnel.create(:x => @x, :y => @y, :image => @@shrapnel_image)}
#
# During 200 ms, fade and scale image, then destroy it
#
@color = @black
@color.alpha = 50
during(200) { @factor_x += 0.5; @factor_y += 0.5; @x -= 1; @color.alpha -= 1}.then { self.destroy }
end
def update
return if @status == :dying
@image = @anim.next
@x -= @velocity
end
end
#
# GAME STATE: GAME OVER
#
class GameOver < Chingu::GameState
def setup
@text = Text.create("GAME OVER (ESC to quit, RETURN to try again!)", :size => 40, :x => 30, :y => 100)
self.input = { :esc => :exit, :return => :try_again }
@layover = Color.new(0x99000000)
end
def draw
super
previous_game_state.draw
fill(@layover)
end
def try_again
pop_game_state # pop back to our playing game state
end
end
#
# GAME STATE: GAME OVER
#
class Done < Chingu::GameState
def setup
@text = Text.create("You made it! Score #{@score} (ESC to quit, RETURN to try again!)", :size => 40, :x => 30, :y => 100)
self.input = { :esc => :exit, :return => :try_again}
end
def try_again
switch_game_state(Level)
end
end
Game.new.show