Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / bullets are not created right in the middle of the ship
- - 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
Parent - - By RunnerPack Date 2018-01-05 22:27
Image#draw puts the upper left corner of the image at (x, y). Are you taking this into account for both the player ship and the bullets?

(Also, this isn't the right forum for posting questions. It's really for finished projects you'd like to show off.)
Parent - By jlnr (dev) Date 2018-01-08 10:02
Correct, moving this thread.
Parent - - By kyonides Date 2018-01-05 22:40
Did you know that you no longer need to pass a window as an argument? Or are you working with multiple windows in your game project?
Parent - By jlnr (dev) Date 2018-01-08 10:02
Even if you were working with multiple windows, the OpenGL context is shared. Passing a window into game objects is only necessary right now if they need access to its title or mouse position.
Up Topic Gosu / Gosu Exchange / bullets are not created right in the middle of the ship

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill