Class: Spacewar::Game

Inherits:
Chingu::Window
  • Object
show all
Defined in:
lib/spacewar/spacewar.rb

Instance Method Summary collapse

Constructor Details

#initializeGame

Returns a new instance of Game.



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
# File 'lib/spacewar/spacewar.rb', line 19

def initialize
  super(Spacewar::SCREEN_WIDTH, Spacewar::SCREEN_HEIGHT)
  
  Image.autoload_dirs << File.join(ROOT, 'media')
  
  self.input = {:esc => :exit}
  
  @planet = Planet.create

  @player1 = Player.create({:holding_right => :turn_right,
                            :holding_left => :turn_left,
                            :holding_up => :accelerate},
                            :zorder => 2,
                            :x => 180,
                            :y => 240,
                            :image => 'Starfighter_p1.bmp')
  
  @player2 = Player.create({:holding_d => :turn_right,
                            :holding_a => :turn_left,
                            :holding_w => :accelerate},
                            :zorder => 2,
                            :x => 400,
                            :y => 240,
                            :image => 'Starfighter_p2.bmp')
  @score = 0
  @score_text = Chingu::Text.create("Score: #{@score}", :x => 10, :y => 10, :zorder => 55, :size=>20)
end

Instance Method Details

#updateObject



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
# File 'lib/spacewar/spacewar.rb', line 47

def update
  super

  if rand(100) < 4 && Star.all.size < 25
    Star.create
  end

  #
  # Collide @player with all instances of class Star
  #
  @player1.each_collision(Star) do |player, star| 
    star.destroy
    @score+=10
  end

  @player2.each_collision(Star) do |player, star|
    star.destroy
    @score-=5
  end

  @player1.adjust_gravity(@planet)
  @player2.adjust_gravity(@planet)

  Star.all.each{|star| star.adjust_gravity(@planet)}

  @player1.each_collision(Player) do |player1, player2|
    if player2 == @player2
      player1.destroy
      player2.destroy
      @player1 = Player.create({:holding_right => :turn_right,
                                :holding_left => :turn_left,
                                :holding_up => :accelerate},
                                :zorder => 2,
                                :x => 180,
                                :y => 240,
                                :image => 'Starfighter_p1.bmp')
      @player2 = Player.create({:holding_d => :turn_right,
                                :holding_a => :turn_left,
                                :holding_w => :accelerate},
                                :zorder => 2,
                                :x => 400,
                                :y => 240,
                                :image => 'Starfighter_p2.bmp')
    end
  end

  @planet.each_collision(Player) do |planet, player| 
    player.destroy
    @score-=10000
  end

  @score_text.text = "Score: #{@score}"
  self.caption = "Chingu Game - " + @score_text.text
end