Commit f6382e233a436c9dcc9a8e3a9f34d90cce2a87d1

Authored by Marius Hanne
1 parent bef0174967
Exists in master

pause game and restart after game over

Showing 1 changed file with 47 additions and 23 deletions Side-by-side Diff

... ... @@ -47,8 +47,6 @@
47 47 @space = CP::Space.new
48 48 @space.damping = 1.0
49 49 setup_collisions
50   - @objects = []
51   - @enemies = []
52 50 @queue = EventQueue.new
53 51 @clock = Clock.new
54 52 @clock.target_framerate = 60
... ... @@ -56,8 +54,6 @@
56 54 @background = Surface.load(File.join(data_dir, "images", "background.png"))
57 55 .zoom_to(@screen.size[0], @screen.size[1] * 2)
58 56 .to_display
59   - @bg_pos = @background.size[1] / 2
60   - @level = options[:level]
61 57 @state = :ready
62 58 @opts = opts
63 59 @steps = 0
64 60  
65 61  
66 62  
... ... @@ -89,16 +85,25 @@
89 85 @space.add_collision_func(:explosion, :ship, &nil)
90 86 end
91 87  
92   - def run
  88 + def start_game
  89 + @objects = []
  90 + @enemies = []
  91 + @bg_pos = @background.size[1] / 2
  92 + @level = options[:level]
93 93 @player = Player.new(self, p: [@screen.size[0] / 2, @screen.size[1] - 100])
94 94 @state = :running
95 95 @player.show
96   -
97 96 flash_text(text: "Level #{@level}", alpha: 150)
  97 + end
  98 +
  99 + def run
  100 + start_game
98 101 loop do
99 102 passed = @clock.tick
100   - @steps += passed
101   - @space.step(passed)
  103 + if @state != :pause
  104 + @steps += passed
  105 + @space.step(passed)
  106 + end
102 107  
103 108 @queue.enable_new_style_events
104 109 @queue.each do |event|
105 110  
106 111  
... ... @@ -114,22 +119,41 @@
114 119 [:escape, :q].include?(event.key)
115 120 end
116 121  
117   - @player.body.v += CP::Vec2.new(*BMA180.read) if options[:bma180]
  122 + if @keys_pressed.include?(:p)
  123 + if !@pause_pressed_at || Time.now - @pause_pressed_at > 1
  124 + if @state == :pause
  125 + @state = :running
  126 + else
  127 + @state = :pause
  128 + flash_text(text: "PAUSE", alpha: 150)
  129 + draw
  130 + end
  131 + @pause_pressed_at = Time.now
  132 + end
  133 + end
118 134  
119   - @player.fire if @keys_pressed.include?(:space) && @state == :running
  135 + if @state == :lost && @keys_pressed.include?(:return)
  136 + start_game
  137 + end
  138 +
  139 + if @state != :pause
  140 + @player.body.v += CP::Vec2.new(*BMA180.read) if options[:bma180]
  141 +
  142 + @player.fire if @keys_pressed.include?(:space) && @state == :running
  143 +
  144 + Enemy.generate(self) if @enemies.size < @level
  145 +
  146 + [:up, :down, :left, :right].each {|d|
  147 + @player.move(d) if @keys_pressed.include?(d) }
120 148  
121   - Enemy.generate(self) if @enemies.size < @level
122   -
123   - [:up, :down, :left, :right].each {|d|
124   - @player.move(d) if @keys_pressed.include?(d) }
125   -
126   - draw
127   -
128   - # TODO
129   - if @bg_pos.abs >= (@background.size[1] * 2 * @level) && @state == :running
130   - @level += 1
131   - flash_text(text: "Level #{@level}")
132   - @bg_pos = @background.size[1] / 2
  149 + draw
  150 +
  151 + # TODO
  152 + if @bg_pos.abs >= (@background.size[1] * 2 * @level) && @state == :running
  153 + @level += 1
  154 + flash_text(text: "Level #{@level}")
  155 + @bg_pos = @background.size[1] / 2
  156 + end
133 157 end
134 158 end
135 159 end