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
#Copyright (C) 2006 by Han Dao
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#This is the game SpaceAceFighter
#You can contact the author at wikipediankiba@gmail.com

class Controller
  def initialize main
    @main = main
    @warrior = Fighter.new
    @weapon = Weapon.new
    @target = Enemy.new
    @delete = CollideDetector.new(@weapon,@warrior,@target)
    controller()
  end
  def controller
    q = Rubygame::Queue.instance()
    x = 0
    while 1
      q.get.each do |e|
        case e
          when Rubygame::KeyDownEvent
            case e.key
              when Rubygame::K_ESCAPE
                exit
              when Rubygame::K_LEFT
                x = -3
              when Rubygame::K_RIGHT
                x = 3
              when Rubygame::K_S
                x = 0
              when Rubygame::K_SPACE
                @weapon.generate(@warrior.x,@warrior.y,true)
            end
        end
      end
      execute(x)
    end
  end
  def execute x
    @main.screen.flip()
    @main.background.blit(@main.screen,[0,0])
    @warrior.pos(x)
    @warrior.draw(@main.screen)
    @weapon.movement()
    @weapon.draw(@main.screen)
    enemy()
    determine = @delete.detect()
    if determine == false
      GameOver.new(@main)
      exit
    end
  end
  def enemy
    if rand(51) == 0
      @target.generate(rand(801),0)
    end
    if rand(21) == 0
      x , y = @target.return
      if Integer === x
        @weapon.generate(x,y)
      end
    end
    @target.draw(@main.screen)
    @target.movement()
  end
end