Making a tile map

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
KrAnKy
Prole
Posts: 4
Joined: Sat Nov 07, 2009 11:02 am

Making a tile map

Post by KrAnKy » Sat Nov 07, 2009 11:05 am

OK i am a new to love , show me some love!

Im trying to make a 2d tile map and i have all of the graphics , im using 16x16 tiles but i cant seem to get it working.. whats wrong with the code im using?

I also did make my own graphics , tex0-3.jpg..
I downloaded the source code from the website of the tutorial and it didnt work either..

Code: Select all

-- our tiles
   tile = {}
   for i=0,3 do
      tile[i] = love.graphics.newImage( "gfx/tex"..i..".jpg" )
   end
   
   love.graphics.setFont(love.graphics.newFont(love.default_font, 12))
   
   -- map vaiables
   map_w = 20
   map_h = 20
   map_x = 0
   map_y = 0
   map_offset_x = 30
   map_offset_y = 30
   map_display_w = 14
   map_display_h = 10
   tile_w = 16
   tile_h = 16

map={
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 1, 0, 0, 2, 2, 2, 0, 3, 0, 3, 0, 1, 1, 1, 0, 0, 0, 0, 0},
   { 0, 1, 0, 0, 2, 0, 2, 0, 3, 0, 3, 0, 1, 0, 0, 0, 0, 0, 0, 0},
   { 0, 1, 1, 0, 2, 2, 2, 0, 0, 3, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
   { 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0},
   { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 2, 2, 2, 0, 3, 3, 3, 0, 1, 1, 1, 0, 2, 0, 0, 0, 0, 0, 0},
   { 0, 2, 0, 0, 0, 3, 0, 3, 0, 1, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0},
   { 0, 2, 0, 0, 0, 3, 0, 3, 0, 1, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0},
   { 0, 2, 2, 2, 0, 3, 3, 3, 0, 1, 1, 1, 0, 2, 2, 2, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
}


function draw_map()
   for y=1, map_display_h do
      for x=1, map_display_w do                                                        
         love.graphics.draw(
            tile[map[y+map_y][x+map_x]],
            (x*tile_w)+map_offset_x,
            (y*tile_h)+map_offset_y )
      end
   end
end

function update( dt )
   
   if love.keyboard.isDown( love.key_up ) then
      map_y = map_y-1
   end
   if love.keyboard.isDown( love.key_down ) then
      map_y = map_y+1
   end
   
   if love.keyboard.isDown( love.key_left ) then
      map_x = map_x-1
   end
   if love.keyboard.isDown( love.key_right ) then
      map_x = map_x+1
   end
   
   -- check boundaries
   if map_x < 0 then
      map_x = 0
   end

    if map_x > map_w-map_display_w then
      map_x = map_w-map_display_w
    end
   
    if map_y < 0 then
      map_y = 0
    end
    if map_y > map_h-map_display_h then
      map_y = map_h-map_display_h
    end
   
end


User avatar
Avalon
Prole
Posts: 49
Joined: Sat Sep 12, 2009 11:37 am

Re: Making a tile map

Post by Avalon » Sat Nov 07, 2009 11:32 am

tex0-3.jpg

tile = love.graphics.newImage( "gfx/tex"..i..".jpg" )

I don't see a hyphen in that newImage code...

User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: Making a tile map

Post by TechnoCat » Sat Nov 07, 2009 1:57 pm

Avalon wrote:tex0-3.jpg

tile = love.graphics.newImage( "gfx/tex"..i..".jpg" )

I don't see a hyphen in that newImage code...

He probably meant tex0-3.jpg as in tex0.jpg, tex1.jpg, tex2.jpg, tex3.jpg

User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Making a tile map

Post by bartbes » Sat Nov 07, 2009 2:57 pm

Can you tell us what's wrong, do you get an error, do you get nothing, what happens?

User avatar
Avalon
Prole
Posts: 49
Joined: Sat Sep 12, 2009 11:37 am

Re: Making a tile map

Post by Avalon » Sat Nov 07, 2009 3:18 pm

TechnoCat wrote:
Avalon wrote:tex0-3.jpg

tile = love.graphics.newImage( "gfx/tex"..i..".jpg" )

I don't see a hyphen in that newImage code...

He probably meant tex0-3.jpg as in tex0.jpg, tex1.jpg, tex2.jpg, tex3.jpg


My slightly hungover brain appreciates the clarification. >_>;

User avatar
kalle2990
Party member
Posts: 245
Joined: Sat Sep 12, 2009 1:17 pm
Location: Sweden

Re: Making a tile map

Post by kalle2990 » Sat Nov 07, 2009 3:54 pm

He have 4 images, and they are tex0 to tex3 (.jpg). I didn't realize that until now ^^

User avatar
napco
Party member
Posts: 129
Joined: Fri Jun 12, 2009 9:28 pm
Location: Ital... ehm...

Re: Making a tile map

Post by napco » Sat Nov 07, 2009 6:03 pm

You have to rename function "map_draw" to "draw", or to add:

Code: Select all

function draw()
        draw_map()
end
I've made a lot of tile map engines to train with love. If you've got problems maybe i can pass you a sample!

KrAnKy
Prole
Posts: 4
Joined: Sat Nov 07, 2009 11:02 am

Re: Making a tile map

Post by KrAnKy » Tue Nov 10, 2009 4:16 am

It still just shows a black screen

Post Reply

Who is online

Users browsing this forum: No registered users and 29 guests