By volcom21pr
Date 2018-01-05 17:25
Edited 2018-01-05 19:11
Hello,
I am learning Ruby programming and how to use Gosu using a book but I had issues to create the bullets right in the middle of my spaceship game. From what I understand the bullets should use the position and angle of the spaceship as arguments to the initialize method but it
Could you please help me, how to accomplish this?
...........
main.rb
def button_down(id)
if id == Gosu::KbSpace
@bullets.push Bullet.new(self, @player.x, @player.y, @player.angle)
end
end
.........
player.rb
attr_reader :x, :y, :angle, :radius
def initialize(window)
@x = 400
@y = 450
@angle = 0
@image = Gosu::Image.new('images/ship.png')
@velocity_x = 0
@velocity_y = 0
@radius = 20
@window = window
end
.............
bullet.rb
class Bullet
SPEED = 5
def initialize(window, x, y, angle)
@x = x
@y = y
@direction = angle
@image = Gosu::Image.new('images/bullet.png')
@radius = 3
@window = window
end
def move
@x += Gosu.offset_x(@direction, SPEED)
@y += Gosu.offset_y(@direction, SPEED)
end
def draw
@image.draw(@x - @radius, @y - @radius, 1)
end
end
Loading...