Game Development Reference
In-Depth Information
23 end
24
25 def button_down ( id )
26 end
27 end
This class provides GameState.switch , that will change the state for our
Gosu::Window , and all enter and leave methods when appropriate. These methods
will be useful for things like switching music.
Notice that Gosu::Window is accessed using global $window variable, which will be
considered an anti-pattern by most good programmers, but there is some logic behind this:
1.
There will be only one Gosu::Window instance.
2.
It lives as long as the game runs.
3.
It is used in some way by nearly all other classes, so we would have to pass it
around all the time.
4.
Accessing it using Singleton or static utility class would not give any clear
benefits, just add more complexity.
Chingu , another game framework built on top of Gosu, also uses global $window , so it's
probably not the worst idea ever.
We will also need an entry point that would fire up the game and enter the first game state
- the menu.
03-prototype/main.rb
1 require 'gosu'
2 require_relative 'states/game_state'
3 require_relative 'states/menu_state'
4 require_relative 'states/play_state'
5 require_relative 'game_window'
6
7 module Game
8 def self . media_path (file)
9
File . join( File . dirname( File . dirname(
10
__FILE__ )), 'media' , file)
11 end
12 end
13
14 $window = GameWindow . new
Search WWH ::




Custom Search