Class: Spacewar::Player

Inherits:
Chingu::GameObject show all
Defined in:
lib/spacewar/spacewar.rb

Instance Method Summary collapse

Methods inherited from Chingu::GameObject

#adjust_gravity

Constructor Details

#initialize(input, options = {}) ⇒ Player

Returns a new instance of Player.



131
132
133
134
135
136
# File 'lib/spacewar/spacewar.rb', line 131

def initialize(input, options={})
  super(options)
  @image = Image[options[:image]]
  self.input = input
  self.max_velocity = 1
end

Instance Method Details

#accelerateObject



138
139
140
141
# File 'lib/spacewar/spacewar.rb', line 138

def accelerate
  self.velocity_x += Gosu::offset_x(self.angle, 0.5) * self.max_velocity
  self.velocity_y += Gosu::offset_y(self.angle, 0.5) * self.max_velocity
end

#turn_leftObject



148
149
150
151
# File 'lib/spacewar/spacewar.rb', line 148

def turn_left
  # The same can be achieved without trait 'effect' as: self.angle -= 4.5
  rotate(-4.5)
end

#turn_rightObject



143
144
145
146
# File 'lib/spacewar/spacewar.rb', line 143

def turn_right
  # The same can be achieved without trait 'effect' as: self.angle += 4.5
  rotate(4.5)
end

#updateObject



153
154
155
156
157
158
159
# File 'lib/spacewar/spacewar.rb', line 153

def update
  self.velocity_x *= 0.99 # dampen the movement
  self.velocity_y *= 0.99

  @x %= $window.width # wrap around the screen
  @y %= $window.height
end