Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / drawing tiles with Gosu... HELP
- - By cloudyyard Date 2015-12-14 20:23
Hello everyone!,
i want to make a 2d minecraft-like plataformer game,
i make this game with Quby, an Ruby-Like programing language, but
i like too much Ruby, and a want to create my game with gosu,
but my problem is:

i want to make a tile image with all Minecraft blocks,
and i use a hash for add the block in the game
like that:

$tile = {
  :air => [0,0]
}

and the air blocks was in the firt tile
of the Tileset!

but the gosu draws is:

image.draw(x, y)

i need this one:

drawImage( image, srcX, srcY, srcWidth, srcHeight, x, y, width, height, isCentred=false )

this class i want to draw:

class Block
    def new(id, x, y)
      @size = 16
        @id = id
        @x = x
        @y = y
        @w = @size
        @h = @w
    end
    def getId()
        return @id
    end
    def setId(id)
        @id = id
    end
    def getX()
        return @x
    end
    def getY()
        return @y
    end
    def render()
        if @id != $blocks[:air]
            drawImage( $tile,  @id[0]*@size, @id[1]*@size, @size, @size, @x, @y, @w, @h )
        end
    end
end

help me Please!,
thanks.
Parent - - By cloudyyard Date 2015-12-14 22:35
Hello? someone can help-me?
Parent - - By aktivb Date 2015-12-15 01:21
Instead of storing where the tile is on the image, use Gosu::Image.load_tiles and store the image as tiles/image objects.
Then instead of passing the coordinates of the tile and where to draw it to a function drawImage(..., x, y), you just pass
where to draw the tile to the tile itself, tile.draw(x, y)
Parent - - By cloudyyard Date 2015-12-15 20:42
but i want to make my game with a tileset,
is just 1 Image, with all the blocks,
and i just add a block with:

$ids = {
:air => [0, 0]
}

understood?
Parent - - By cloudyyard Date 2015-12-15 21:00
Ok!, now have a new Problem!...

code:
require 'gosu'

$size = 16

$tile = Gosu::Image.new("res/tile.png", :tileable => true)

$ids = {
  :air => [0, 0],
  :dirt => [4, 0]
}

$w = 30
$h = $w

$level = Level.new(30, 30)

class Block
  def initialize(id, x, y)
    @id = id
    @x = x
    @y = y
  end
  def getId()
    return @id
  end
  def setId(id)
    @id = id
  end
  def tick()
  end
  def render()
    $tile.draw((@id[0]*$size), (@id[1]*$size), 0)
  end
end

def Level
  def initialize(w, h)
    @w = w
    @h = h
    @map = []
   
    initLevel()
  end
  def initLevel()
    @w.times() do |x|
      @map[x] = []
      @h.times() do |y|
        @map[x][y] = Block.new($ids[:air], x*$size, y*$size)
      end
    end
  end
  def tick()
  end
  def render()
    @w.times() do |x|
      @h.times() do |y|
        @map[x][y].render()
      end
    end
  end
end

class GameWindow < Gosu::Window
  def initialize()
    super(480, 480, false)
    self.caption = "Pix2D"
  end
   
  def update()
    $level.tick()
  end
 
  def draw()
    $level.render()
  end
 
  def needs_cursor?
    true
  end
end

def fillRect(x, y, width, height, t, color)
  draw_quad(x+t-width, y+t-height, color, x+t+width, y+t-height, color, x+t-width, y+t+height, color, x+t+width, y+t+height, color, 0)
end

GameWindow.new.show

ERROR:

main.rb:15:in `<main>': uninitialized constant Level (NameError)
Parent - - By RunnerPack Date 2015-12-15 21:54
def Level should be class Level
Parent - - By cloudyyard Date 2015-12-15 22:17
Nope!, i have changed but the Terminal say the same error...
Parent - - By cloudyyard Date 2015-12-16 00:08
ok, now works, but is not working corectly!, the window show the full tile image...
Parent - - By lol_o2 Date 2015-12-16 11:55
You need to use Image.load_tiles(image_name, width, height)
It returns and array of images, with width/height being the size of your tile.

If you make it like this: $tile = Image.load_tiles("res/tile.png", 16, 16)
Then drawing first tile goes like this: $tile[0].draw(pos_x, pos_y, some_z) for first tile.

If you want to access tiles by their row/column number, it's just calculating tile index: tile_id = x % columns + y * columns, where x/y is the tile you want to get and columns is number of columns in your tileset.
Parent - - By cloudyyard Date 2015-12-16 19:13
you can make a simple example? please...
i don't understood...
Parent - - By lol_o2 Date 2015-12-16 20:23
Let's say you have tileset like this:


To load it, use: my_tileset = Image.new("tileset.png", 32, 32)
my_tileset will be array containing tiles. This is how Gosu organizes it:


So if you want to draw for example the center tile, you do it like this: my_tileset[4].draw(tile_x, tile_y, z)

But you probably don't want to use direct indexes. So you have to use the formula I posted earlier.

Let's say you want to access bottom-left border tile. It's position is (0, 2)
You have: x = 0, y = 2, and the number of columns is 3. (number of columns is also number of tiles in top row)
And use the formula: index = (x % columns + y * columns) = (0 % 3 + 2 * 3) = 6
And if you look at the image, 6 is the tile we wanted to retrieve.

Hope it's clear enough.
Parent - - By cloudyyard Date 2015-12-16 20:40
you can make an example with Code, a simple game with a tile?,
i'm from brazil, and i don't speak and read english good...
Parent - - By cloudyyard Date 2015-12-16 20:43
i understood the first part...
like that:
require 'gosu'

$tile = Gosu::Image.load_tiles("res/tile.png", 16, 16)

class GameWindow < Gosu::Window
  def initialize()
    super(480, 480, false)
    self.caption = "Pix2D"
  end
  
  def update()
    #$level.tick()
  end

  def draw()
    #$level.render()
    $tile[0].draw(0,0,0)
  end

  def needs_cursor?
    true
  end
end

GameWindow.new.show
Parent - By cloudyyard Date 2015-12-16 20:46
but i want to make:
@map[2][0].setId(block[:stone])

like that!.
Parent - - By lol_o2 Date 2015-12-16 20:52 Edited 2015-12-16 21:09
require 'gosu'

$tile = Gosu::Image.load_tiles("res/tile.png", 16, 16)

class GameWindow < Gosu::Window
  def initialize()
    super(480, 480, false)
    self.caption = "Pix2D"
  end
  
  def update()
    #$level.tick()
  end

  def draw()
    #$level.render()
    $tile[ get_tile_index(0, 0) ].draw(0,0,0) #replace 0,0 with the indexes you want to use
  end

  def needs_cursor?
    true
  end

  def get_tile_index(x,y)
    columns = $tile.width / 16
    return x % columns + y * columns
  end
end

GameWindow.new.show
Parent - By cloudyyard Date 2015-12-16 21:22
Ok, Ok, Ok!, now i understood!!!,
now work!!, the problem was very simple!,
i try with:
$tile[0].draw(@x, @y, 0)
and now works!!

Thanks for all the help!!
Up Topic Gosu / Gosu Exchange / drawing tiles with Gosu... HELP

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill