ippa-chingu 0.4.8 → 0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +5 -0
- data/README.rdoc +43 -23
- data/chingu.gemspec +4 -4
- data/examples/example1.rb +2 -0
- data/examples/example2.rb +63 -33
- data/examples/example4.rb +16 -3
- data/examples/example5.rb +3 -3
- data/examples/example6.rb +1 -40
- data/examples/example7.rb +9 -8
- data/examples/example8.rb +109 -0
- data/examples/example9.rb +83 -0
- data/lib/chingu.rb +17 -2
- data/lib/chingu/actor.rb +15 -0
- data/lib/chingu/animation.rb +27 -2
- data/lib/chingu/basic_game_object.rb +140 -0
- data/lib/chingu/core_extensions.rb +15 -5
- data/lib/chingu/effects.rb +1 -1
- data/lib/chingu/fpscounter.rb +0 -1
- data/lib/chingu/game_object.rb +85 -155
- data/lib/chingu/game_state.rb +4 -3
- data/lib/chingu/game_state_manager.rb +3 -4
- data/lib/chingu/game_states/debug.rb +43 -0
- data/lib/chingu/game_states/fade_to.rb +2 -2
- data/lib/chingu/helpers.rb +2 -0
- data/lib/chingu/parallax.rb +2 -2
- data/lib/chingu/particle.rb +5 -4
- data/lib/chingu/traits/collision_detection.rb +33 -0
- data/lib/chingu/traits/deprecated_module_visual.rb +108 -0
- data/lib/chingu/traits/deprecated_visual.rb +100 -0
- data/lib/chingu/traits/effect.rb +80 -0
- data/lib/chingu/traits/input.rb +15 -0
- data/lib/chingu/traits/velocity.rb +59 -0
- data/lib/chingu/window.rb +18 -3
- metadata +16 -4
data/History.txt
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
=== 0.5 / 2009-10-7
|
2
|
+
Big refactor of GameObject. Now has BasicGameObject as base.
|
3
|
+
A first basic "trait"-system where GameObject "has_traits :visual, :velocity" etc.
|
4
|
+
Tons of enhancements and fixes. Speed optimization. More examples.
|
5
|
+
|
1
6
|
=== 0.4.5 / 2009-08-27
|
2
7
|
Tons of small fixes across the board.
|
3
8
|
Started on GFX Helpers (fill, fill_rect, fill_gradient so far).
|
data/README.rdoc
CHANGED
@@ -3,6 +3,8 @@ http://github.com/ippa/chingu/tree/master
|
|
3
3
|
|
4
4
|
DOCUMENTATION: http://rdoc.info/projects/ippa/chingu
|
5
5
|
|
6
|
+
Ruby 1.9.1 is recommended. Also works with 1.8.7+.
|
7
|
+
|
6
8
|
This is an early preview, a lot of functionality is still missing!
|
7
9
|
It's also in a state of flux while I decide on core-naming etc.
|
8
10
|
|
@@ -13,7 +15,7 @@ It's also in a state of flux while I decide on core-naming etc.
|
|
13
15
|
|
14
16
|
== DESCRIPTION
|
15
17
|
Game framework built on top of the OpenGL accelerated game lib Gosu.
|
16
|
-
It adds simple yet
|
18
|
+
It adds simple yet powerful game states, prettier input handling, deployment safe asset-handling, a basic re-usable game object and automation of common task.
|
17
19
|
|
18
20
|
|
19
21
|
== THE STORY
|
@@ -34,11 +36,16 @@ Chingu consists of the following core classes:
|
|
34
36
|
The main window, use it at you use Gosu::Window.
|
35
37
|
|
36
38
|
=== Chingu::GameObject
|
37
|
-
Use for all your in game objects. The player, the enemies, the bullets, the powerups, the loot laying around.
|
39
|
+
Use this for all your in game objects. The player, the enemies, the bullets, the powerups, the loot laying around.
|
38
40
|
It's very reusable and doesn't contain any game-logic (that's up to you!). Only stuff to put it on screen a certain way.
|
39
41
|
It also gives you a couple of bonuses with chingu, as automatic updates/draws and easier input-mapping.
|
40
42
|
Has either Chingu::Window or a Chingu::GameState as "owner".
|
41
43
|
|
44
|
+
=== Chingu::BasicGameObject
|
45
|
+
For those who think GameObject is a too little fat, there's BasicGameObject (GameObject inherits from BasicGameObject).
|
46
|
+
BasicGameObject is just an empty frame (no x,y,image accessors or draw-logic) for you to build on.
|
47
|
+
It _can_ be extended with Chingus trait-system though. and it belong to either $window or a game state as BasicObject.
|
48
|
+
|
42
49
|
=== Chingu::Text
|
43
50
|
Makes use of Gosu::Font more rubyish and powerful.
|
44
51
|
In it's core, another Chingu::GameObject + Gosu::Font.
|
@@ -55,6 +62,12 @@ If using game states, the flow of draw/update/button_up/button_down is:
|
|
55
62
|
Chingu::Window --> Chingu::GameStateManager --> Chingu::GameState.
|
56
63
|
For example, inside game state Menu you call push_game_state(Level). When Level exists, it will go back to Menu.
|
57
64
|
|
65
|
+
=== Chingu::Animation
|
66
|
+
Load and interact with tile-based animations. loop, bounce and access invidual frame(s) easily.
|
67
|
+
An "@image = @animation.next!" in your Player#update is usually enough to get you started!
|
68
|
+
|
69
|
+
== Chingu::Parallax
|
70
|
+
A class for easy paralaxxscrolling. See example3.rb for more.
|
58
71
|
|
59
72
|
== THE BASICS
|
60
73
|
|
@@ -113,7 +126,7 @@ Chingu doesn't change any fundamental concept of Gosu, but it will make the abov
|
|
113
126
|
#
|
114
127
|
# We use Chingu::Window instead of Gosu::Window
|
115
128
|
#
|
116
|
-
class Game < Chingu
|
129
|
+
class Game < Chingu::Window
|
117
130
|
def initialize
|
118
131
|
super # This is always needed if you want to take advantage of what chingu offers
|
119
132
|
#
|
@@ -222,7 +235,7 @@ Another more complex example:
|
|
222
235
|
# Pressing ESC would call Play#close
|
223
236
|
# Holding down LEFT would call Play#move_left on every game iteration
|
224
237
|
# Holding down RIGHT would call Play#move_right on every game iteration
|
225
|
-
#
|
238
|
+
# Releasing SPACE would call Play#fire
|
226
239
|
#
|
227
240
|
|
228
241
|
class Play < Chingu::GameState
|
@@ -242,14 +255,14 @@ In Gosu the above code would include code in button_up(), button_down() and a ch
|
|
242
255
|
|
243
256
|
Every symbol can be prefixed by either "released_" or "holding_" while no prefix at all defaults to pressed once.
|
244
257
|
|
245
|
-
So, why not :up_space or :
|
258
|
+
So, why not :up_space or :release_space instead of :released_space?
|
246
259
|
+:up_space+ doesn't sound like english, :release_space sounds more like a command then an event.
|
247
260
|
|
248
261
|
|
249
262
|
Or +:hold_left+ or :down_left instead of :holding_left?
|
250
263
|
:holding_left sounds like something that's happening over a period of time, not a single trigger, which corresponds well to how it works.
|
251
264
|
|
252
|
-
And with the default :space => :something
|
265
|
+
And with the default :space => :something you would imagine that :something is called once. You press :space once, :something is executed once.
|
253
266
|
|
254
267
|
|
255
268
|
=== GameState / GameStateManager
|
@@ -283,7 +296,7 @@ Game states aren't complicated. In Chingu a GameState is a class that behaves mo
|
|
283
296
|
|
284
297
|
end
|
285
298
|
|
286
|
-
Looks familiar
|
299
|
+
Looks familiar yet?
|
287
300
|
You can activate the above game state in 2 ways
|
288
301
|
|
289
302
|
class Game < Chingu::Window
|
@@ -309,15 +322,15 @@ Another example:
|
|
309
322
|
def initialize
|
310
323
|
#
|
311
324
|
# We start by pushing Menu to the game state stack, making it active as the only state on stack.
|
312
|
-
# :setup => :false which will skip setup() from
|
325
|
+
# :setup => :false which will skip setup() from being called (standard when switching to a new state)
|
313
326
|
#
|
314
327
|
push_game_state(Menu, :setup => false)
|
315
328
|
|
316
329
|
#
|
317
|
-
# We push another game state to the stack, Play. We now have 2 states, which active
|
330
|
+
# We push another game state to the stack, Play. We now have 2 states, which active being first / active.
|
318
331
|
#
|
319
|
-
# :finalize => false will skip setup() from
|
320
|
-
# that's
|
332
|
+
# :finalize => false will skip setup() from being called on game state
|
333
|
+
# that's being pushed down the stack, in this case Intro.setup().
|
321
334
|
#
|
322
335
|
push_game_state(Play, :finalize => false)
|
323
336
|
|
@@ -340,7 +353,7 @@ Another example:
|
|
340
353
|
|
341
354
|
A GameState in Chingu is just a class with the following instance methods:
|
342
355
|
|
343
|
-
* initialize() - called only once with push_game_state(Intro) but
|
356
|
+
* initialize() - called only once with push_game_state(Intro) but every time with push_game_state(Intro.new)
|
344
357
|
* setup() - called each time the game state becomes active.
|
345
358
|
* button_down(id) - Called when a button is down
|
346
359
|
* button_up(id) - Called when a button is released
|
@@ -380,13 +393,13 @@ Chingus inputhandler will detect that Menu is a gamestate-class, create a new in
|
|
380
393
|
|
381
394
|
=== Assets / Paths
|
382
395
|
|
383
|
-
You might wonder why this is
|
396
|
+
You might wonder why this is necessary in the straight Gosu example:
|
384
397
|
ROOT_PATH = File.dirname(File.expand_path(__FILE__))
|
385
398
|
@image = Image.new(File.join(ROOT_PATH, "media", "player.png"))
|
386
399
|
|
387
400
|
It enables you to start your game from any directory and it will still find your assets (pictures, samples, fonts etc..) correctly.
|
388
401
|
For a local development version this might not be important, you're likely to start the game from the games root-dir.
|
389
|
-
But as soon as you try to deploy (for example to windows with OCRA - http://github.com/larsch/ocra/tree/master) you'll run into trouble of you
|
402
|
+
But as soon as you try to deploy (for example to windows with OCRA - http://github.com/larsch/ocra/tree/master) you'll run into trouble of you don't do it like that.
|
390
403
|
|
391
404
|
Chingu solves this problem behind the scenes for the most common assets. The 2 lines above can be replaced with:
|
392
405
|
Image["player.png"]
|
@@ -430,20 +443,27 @@ Since Chingu::Window is just Gosu::Window + some cheese you can do your $window.
|
|
430
443
|
See http://www.libgosu.org/rdoc/classes/Gosu/Window.html for a full set of methods.
|
431
444
|
|
432
445
|
== TODO:
|
446
|
+
* add :padding and :align => :topleft etc to class Text
|
447
|
+
* rename Chingu::Window so 'include Chingu' and 'include Gosu' wont make Window collide
|
448
|
+
* (done) BasicObject vs GameObject vs ScreenObject => Became BasicGameObject and GameObject
|
449
|
+
* (50%) some kind of componentsystem for GameObject (which should be cleaned up)
|
450
|
+
* (done) scale <--> growth parameter. See trait "effect"
|
451
|
+
* (done) Enemy.all ... instead of game_objects_of_type(Enemy) ? could this be cool / understandable?
|
452
|
+
* (done) Don't call .update(time) with timeparameter, make time available thru other means when needed.
|
453
|
+
* (10% done) debug screen / game state.. check out shawn24's elite irb sollution :)
|
433
454
|
* (done) Complete the input-definitions with all possible inputs (keyboard, gamepad, mouse)!
|
434
455
|
* (done) Complete input-stuff with released-states etc
|
435
456
|
* (done) More gfx effects, for example: fade in/out to a specific color (black makes sense between levels).
|
436
457
|
* (posted request on forums) Summon good proven community gosu snippets into Chingu
|
437
458
|
* (done) Generate docs @ ippa.github.com- http://rdoc.info/projects/ippa/chingu !
|
438
459
|
* (done) A good scene-manager to manage welcome screens, levels and game flow- GameStateManager / GameState !
|
439
|
-
* More docs
|
440
460
|
* (20% done) make a playable simple game in examples\ that really depends on game states
|
441
461
|
* (done) Make a gem- first gem made on github
|
442
462
|
* (done) Automate gemgenning rake-task even more
|
443
463
|
* More examples when effects are more complete
|
444
464
|
* class ChipmunkObject
|
445
|
-
* class Actor/MovingActor with maybe
|
446
|
-
* (
|
465
|
+
* class Actor/MovingActor with maybe a bit more logic then the basic GameObject. Would ppl find is useful?
|
466
|
+
* (60% done) Spell check all docs, sloppy spelling turns ppl off. tnx jduff ;).
|
447
467
|
* Tests
|
448
468
|
* (done) Streamline fps / tick code
|
449
469
|
* (done) Encapsulate Font.new / draw_rot with a "class Text < GameObject"
|
@@ -452,7 +472,7 @@ See http://www.libgosu.org/rdoc/classes/Gosu/Window.html for a full set of metho
|
|
452
472
|
* A more robust game state <-> game_object system to connect them together.
|
453
473
|
* (50% done) Get better at styling rdocs
|
454
474
|
* (done) all �gamestate� ? �game state� ? it's "game state"
|
455
|
-
* intergrate MovieMaker
|
475
|
+
* (skipping) intergrate MovieMaker - solve this with traits instead.
|
456
476
|
* FIX example4: :p => Pause.new would Change the "inside_game_state" to Pause and make @player belong to Pause.
|
457
477
|
|
458
478
|
== WHY?
|
@@ -467,11 +487,11 @@ See http://www.libgosu.org/rdoc/classes/Gosu/Window.html for a full set of metho
|
|
467
487
|
* Don't separate too much from Gosus core-naming
|
468
488
|
|
469
489
|
== CREDITS:
|
470
|
-
Jacius of Rubygame (For doing cool stuff that's well documented as re-usable). So far rect.rb and named_resource.rb is taken from Rubygame.
|
471
|
-
|
472
|
-
|
473
|
-
|
490
|
+
* Jacius of Rubygame (For doing cool stuff that's well documented as re-usable). So far rect.rb and named_resource.rb is taken from Rubygame.
|
491
|
+
* Jduff for input / commits.
|
492
|
+
* Jlnr,Philymore,Shawn24,JamesKilton for constructive feedback/discussions.
|
474
493
|
|
475
494
|
== REQUIREMENTS:
|
476
495
|
* Gosu latest version
|
477
|
-
* Ruby 1.8 (
|
496
|
+
* Ruby 1.8.7+ (Works with 1.9+ as well!)
|
497
|
+
|
data/chingu.gemspec
CHANGED
@@ -2,16 +2,16 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{chingu}
|
5
|
-
s.version = "0.
|
5
|
+
s.version = "0.5"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["ippa"]
|
9
|
-
s.date = %q{2009-
|
9
|
+
s.date = %q{2009-09-07}
|
10
10
|
s.description = %q{Game framework built on top of the OpenGL accelerated game lib Gosu.
|
11
|
-
It adds simple yet
|
11
|
+
It adds simple yet powerful game states, prettier input handling, deployment safe asset-handling, a basic re-usable game object and automation of common task.}
|
12
12
|
s.email = ["ippa@rubylicio.us"]
|
13
13
|
s.extra_rdoc_files = ["History.txt", "Manifest.txt"]
|
14
|
-
s.files = ["History.txt", "Manifest.txt", "README.rdoc", "Rakefile", "chingu.gemspec", "examples/example1.rb", "examples/example2.rb", "examples/example3.rb", "examples/example4.rb", "examples/example5.rb", "examples/example6.rb", "examples/example7.rb", "examples/media/Parallax-scroll-example-layer-0.png", "examples/media/Parallax-scroll-example-layer-1.png", "examples/media/Parallax-scroll-example-layer-2.png", "examples/media/Parallax-scroll-example-layer-3.png", "examples/media/background1.png", "examples/media/fire_bullet.png", "examples/media/fireball.png", "examples/media/particle.png", "examples/media/ruby.png", "examples/media/spaceship.png", "examples/media/stickfigure.bmp", "examples/media/stickfigure.png", "examples/media/video_games.png", "lib/chingu.rb", "lib/chingu/animation.rb", "lib/chingu/assets.rb", "lib/chingu/core_extensions.rb", "lib/chingu/effects.rb", "lib/chingu/fpscounter.rb", "lib/chingu/game_object.rb", "lib/chingu/game_state.rb", "lib/chingu/game_state_manager.rb", "lib/chingu/game_states/fade_to.rb", "lib/chingu/game_states/pause.rb", "lib/chingu/gfx_helpers.rb", "lib/chingu/helpers.rb", "lib/chingu/input.rb", "lib/chingu/named_resource.rb", "lib/chingu/parallax.rb", "lib/chingu/particle.rb", "lib/chingu/rect.rb", "lib/chingu/text.rb", "lib/chingu/window.rb"]
|
14
|
+
s.files = ["History.txt", "Manifest.txt", "README.rdoc", "Rakefile", "chingu.gemspec", "examples/example1.rb", "examples/example2.rb", "examples/example3.rb", "examples/example4.rb", "examples/example5.rb", "examples/example6.rb", "examples/example7.rb", "examples/example8.rb", "examples/example9.rb", "examples/media/Parallax-scroll-example-layer-0.png", "examples/media/Parallax-scroll-example-layer-1.png", "examples/media/Parallax-scroll-example-layer-2.png", "examples/media/Parallax-scroll-example-layer-3.png", "examples/media/background1.png", "examples/media/fire_bullet.png", "examples/media/fireball.png", "examples/media/particle.png", "examples/media/ruby.png", "examples/media/spaceship.png", "examples/media/stickfigure.bmp", "examples/media/stickfigure.png", "examples/media/video_games.png", "lib/chingu.rb", "lib/chingu/actor.rb", "lib/chingu/animation.rb", "lib/chingu/assets.rb", "lib/chingu/basic_game_object.rb", "lib/chingu/core_extensions.rb", "lib/chingu/effects.rb", "lib/chingu/fpscounter.rb", "lib/chingu/game_object.rb", "lib/chingu/game_state.rb", "lib/chingu/game_state_manager.rb", "lib/chingu/game_states/debug.rb", "lib/chingu/game_states/fade_to.rb", "lib/chingu/game_states/pause.rb", "lib/chingu/gfx_helpers.rb", "lib/chingu/helpers.rb", "lib/chingu/input.rb", "lib/chingu/named_resource.rb", "lib/chingu/parallax.rb", "lib/chingu/particle.rb", "lib/chingu/rect.rb", "lib/chingu/text.rb", "lib/chingu/traits/collision_detection.rb", "lib/chingu/traits/deprecated_module_visual.rb", "lib/chingu/traits/deprecated_visual.rb", "lib/chingu/traits/effect.rb", "lib/chingu/traits/input.rb", "lib/chingu/traits/velocity.rb", "lib/chingu/window.rb"]
|
15
15
|
s.homepage = %q{http://github.com/ippa/chingu/tree/master}
|
16
16
|
s.rdoc_options = ["--main", "README.rdoc"]
|
17
17
|
s.require_paths = ["lib"]
|
data/examples/example1.rb
CHANGED
data/examples/example2.rb
CHANGED
@@ -6,6 +6,8 @@ include Gosu
|
|
6
6
|
# A little more complicated example where we do our own #update and #draw code.
|
7
7
|
# We also add another Actor - a bullet fired with space.
|
8
8
|
#
|
9
|
+
# Also tests out the Debug game state.
|
10
|
+
#
|
9
11
|
class Game < Chingu::Window
|
10
12
|
def initialize
|
11
13
|
#
|
@@ -14,45 +16,16 @@ class Game < Chingu::Window
|
|
14
16
|
#
|
15
17
|
super
|
16
18
|
|
17
|
-
|
18
|
-
@player.input = { :holding_left => :move_left,
|
19
|
-
:holding_right => :move_right,
|
20
|
-
:holding_up => :move_up,
|
21
|
-
:holding_down => :move_down,
|
22
|
-
:space => :fire,
|
23
|
-
:escape => :exit
|
24
|
-
}
|
25
|
-
end
|
26
|
-
|
27
|
-
#
|
28
|
-
# If we want to add extra graphics drawn just define your own draw.
|
29
|
-
# Be sure to call #super for enabling Chingus autodrawing of Actors.
|
30
|
-
# Putting #super before or after the background-draw-call really doesn't matter since Gosu work with "zorder".
|
31
|
-
#
|
32
|
-
def draw
|
33
|
-
# Raw Gosu Image.draw(x,y,zorder)-call
|
34
|
-
Image["background1.png"].draw(0, 0, 0)
|
35
|
-
super
|
19
|
+
push_game_state(Play.new)
|
36
20
|
end
|
37
|
-
|
38
|
-
#
|
39
|
-
# Gosus place for gamelogic is #update in the mainwindow
|
40
|
-
#
|
41
|
-
# A #super call here would call #update on all Chingu::Actors and check their inputs, and call the specified method.
|
42
|
-
#
|
43
|
-
def update
|
44
|
-
|
45
|
-
### Your own gamelogic here
|
46
|
-
super
|
47
|
-
self.caption = "FPS: #{self.fps} milliseconds_since_last_tick: #{self.milliseconds_since_last_tick}"
|
48
|
-
end
|
49
|
-
|
50
21
|
end
|
51
22
|
|
52
23
|
#
|
53
24
|
# Our Player
|
54
25
|
#
|
55
26
|
class Player < Chingu::GameObject
|
27
|
+
has_trait :input
|
28
|
+
|
56
29
|
def initialize(options = {})
|
57
30
|
super
|
58
31
|
@image = Image["spaceship.png"]
|
@@ -78,10 +51,67 @@ class Bullet < Chingu::GameObject
|
|
78
51
|
end
|
79
52
|
|
80
53
|
# Move the bullet forward
|
81
|
-
def update
|
54
|
+
def update
|
82
55
|
@y -= 2
|
83
56
|
end
|
84
57
|
|
85
58
|
end
|
86
59
|
|
60
|
+
class Play < Chingu::GameState
|
61
|
+
|
62
|
+
def initialize
|
63
|
+
super
|
64
|
+
@player = Player.new(:x => 200, :y => 200)
|
65
|
+
@player.input = { :holding_left => :move_left,
|
66
|
+
:holding_right => :move_right,
|
67
|
+
:holding_up => :move_up,
|
68
|
+
:holding_down => :move_down,
|
69
|
+
:space => :fire,
|
70
|
+
:escape => :exit,
|
71
|
+
}
|
72
|
+
self.input = { :f1 => :debug }
|
73
|
+
end
|
74
|
+
|
75
|
+
def debug
|
76
|
+
#puts "--------"
|
77
|
+
#GameObject.all.each do |game_object|
|
78
|
+
# puts game_object.class
|
79
|
+
#end
|
80
|
+
#return
|
81
|
+
|
82
|
+
push_game_state(Chingu::GameStates::Debug)
|
83
|
+
end
|
84
|
+
|
85
|
+
#
|
86
|
+
# If we want to add extra graphics drawn just define your own draw.
|
87
|
+
# Be sure to call #super for enabling Chingus autodrawing of Actors.
|
88
|
+
# Putting #super before or after the background-draw-call really doesn't matter since Gosu work with "zorder".
|
89
|
+
#
|
90
|
+
def draw
|
91
|
+
# Raw Gosu Image.draw(x,y,zorder)-call
|
92
|
+
Image["background1.png"].draw(0, 0, 0)
|
93
|
+
super
|
94
|
+
end
|
95
|
+
|
96
|
+
#
|
97
|
+
# Gosus place for gamelogic is #update in the mainwindow
|
98
|
+
#
|
99
|
+
# A #super call here would call #update on all Chingu::Actors and check their inputs, and call the specified method.
|
100
|
+
#
|
101
|
+
def update
|
102
|
+
Bullet.destroy_if { |bullet| bullet.outside_window? }
|
103
|
+
#Bullet.destroy_if(&:outside_window?)
|
104
|
+
|
105
|
+
#
|
106
|
+
# Bullet.each_collision(Enemy) do |bullet, enemy|
|
107
|
+
# enemy.collision_with(bullet)
|
108
|
+
# bullet.destroy!
|
109
|
+
# end
|
110
|
+
#
|
111
|
+
|
112
|
+
super
|
113
|
+
$window.caption = "FPS: #{$window.fps} - milliseconds_since_last_tick: #{$window.milliseconds_since_last_tick} - game objects# #{current_game_state.game_objects.size}"
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
87
117
|
Game.new.show
|
data/examples/example4.rb
CHANGED
@@ -40,6 +40,8 @@ end
|
|
40
40
|
|
41
41
|
# Our Player
|
42
42
|
class Player < Chingu::GameObject
|
43
|
+
has_trait :input
|
44
|
+
|
43
45
|
def initialize(options = {})
|
44
46
|
super
|
45
47
|
@image = Image["spaceship.png"]
|
@@ -51,7 +53,9 @@ class Player < Chingu::GameObject
|
|
51
53
|
def move_down; @y += 1; end
|
52
54
|
|
53
55
|
def fire
|
54
|
-
Bullet.new(:x => @x, :y => @y)
|
56
|
+
Bullet.new(:x => @x - 20, :y => @y)
|
57
|
+
Bullet.new(:x => @x, :y => @y)
|
58
|
+
Bullet.new(:x => @x + 20, :y => @y)
|
55
59
|
end
|
56
60
|
end
|
57
61
|
|
@@ -62,7 +66,7 @@ class Bullet < Chingu::GameObject
|
|
62
66
|
@image = Image["fire_bullet.png"]
|
63
67
|
end
|
64
68
|
|
65
|
-
def update
|
69
|
+
def update
|
66
70
|
@y -= 2
|
67
71
|
end
|
68
72
|
end
|
@@ -113,7 +117,7 @@ class Level < Chingu::GameState
|
|
113
117
|
:holding_right => :move_right,
|
114
118
|
:holding_up => :move_up,
|
115
119
|
:holding_down => :move_down,
|
116
|
-
:
|
120
|
+
:space => :fire}
|
117
121
|
|
118
122
|
#
|
119
123
|
# The input-handler understands gamestates. P is pressed --> push_gamegate(Pause)
|
@@ -122,6 +126,12 @@ class Level < Chingu::GameState
|
|
122
126
|
self.input = {:p => Pause, :r => lambda{ current_game_state.setup } }
|
123
127
|
end
|
124
128
|
|
129
|
+
def update
|
130
|
+
super
|
131
|
+
Bullet.destroy_if {|bullet| bullet.outside_window? }
|
132
|
+
$window.caption = "FPS: #{$window.fps} - GameObjects: #{game_objects.size}"
|
133
|
+
end
|
134
|
+
|
125
135
|
#
|
126
136
|
# setup() is called each time you switch to the game state (and on creation time).
|
127
137
|
# You can skip setup by switching with push_game_state(:setup => false) or pop_game_state(:setup => false)
|
@@ -130,6 +140,7 @@ class Level < Chingu::GameState
|
|
130
140
|
#
|
131
141
|
def setup
|
132
142
|
# Place player in a good starting position
|
143
|
+
Bullet.clear
|
133
144
|
@player.x = $window.width/2
|
134
145
|
@player.y = $window.height - @player.image.height
|
135
146
|
end
|
@@ -138,6 +149,8 @@ end
|
|
138
149
|
#
|
139
150
|
# SPECIAL GAMESTATE - Pause
|
140
151
|
#
|
152
|
+
# NOTICE: Chingu now comes with a predefined Chingu::GameStates::Pause that works simular to this!
|
153
|
+
#
|
141
154
|
class Pause < Chingu::GameState
|
142
155
|
def initialize(options)
|
143
156
|
super
|
data/examples/example5.rb
CHANGED
@@ -55,7 +55,7 @@ class State1 < Chingu::GameState
|
|
55
55
|
@spinner_index = 0.0
|
56
56
|
end
|
57
57
|
|
58
|
-
def update
|
58
|
+
def update
|
59
59
|
@spinner_index += 0.1
|
60
60
|
@spinner_index = 0 if @spinner_index >= @spinner.size
|
61
61
|
end
|
@@ -71,7 +71,7 @@ class State2 < Chingu::GameState
|
|
71
71
|
@ticks = 0.0
|
72
72
|
end
|
73
73
|
|
74
|
-
def update
|
74
|
+
def update
|
75
75
|
@ticks += 0.01
|
76
76
|
@factor = 1.5 + Math.sin(@ticks).to_f
|
77
77
|
end
|
@@ -88,7 +88,7 @@ class State3 < Chingu::GameState
|
|
88
88
|
@ticks = 0.0
|
89
89
|
end
|
90
90
|
|
91
|
-
def update
|
91
|
+
def update
|
92
92
|
@ticks += 0.01
|
93
93
|
@factor = 1.5 + Math.sin(@ticks).to_f
|
94
94
|
end
|