TILE GAME!

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.
User avatar
baconhawka7x
Party member
Posts: 491
Joined: Mon Nov 21, 2011 7:05 am
Location: Oregon, USA
Contact:

TILE GAME!

Post by baconhawka7x »

So I've been reading through the tile-based-scrolling for a while now. And theres only a few parts that i don't understand.

Like.

Code: Select all

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
I started out using applescript making stupid little dialog games. So im familiar with "if then" statements. I'm very unfamiliar with "for do" statements.

For example i Don't understand in the line..

Code: Select all

for y=1, map_display_h do
So is it saying that 1 can repeat "map_display_h" number of times? (map_display_h = 10 by the way)

also i really dont understand the line

Code: Select all

tile[map[y+map_y][x+map_x]], 
I really would like to make tile games. please help me!
All Help Is Apreciated!:D


P.S. how would i use more then 1 kind of tile? for example, Lets say i wanted grass, and stone?
Last edited by baconhawka7x on Thu Dec 08, 2011 3:23 pm, edited 3 times in total.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: tile game.

Post by tentus »

The space is there just to make it more legible. The following code is all the same, for example:

Code: Select all

for i=1,bar do foo(i) end
--
for i=1, bar do 
  foo(i )
end
--
for i = 1, bar do
         foo( i )
end
Anyhow, a "for do" statement is a loop, where a number increments (or decrements) each time it happens. Whatever is inside the loop (before the matching end) will be executed a specified number of times. So, in the above examples, foo() will be called bar number of times... let's say five times. You could write the code like this instead:

Code: Select all

foo(1)
foo(2)
foo(3)
foo(4)
foo(5)
But that's really wordy, especially if bar is a much bigger number, say 100 or 1000.

In your case the for loops are going through each row and then column in a grid, whose size is determined by map_display_h and map_display_w.
Kurosuke needs beta testers
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: tile game.

Post by kikito »

I recommend you to give a look to my love tile tutorial. There's a chapter 0 with basic concepts, and a section there explaining "the for do statements" (also called "loops").
When I write def I mean function.
User avatar
baconhawka7x
Party member
Posts: 491
Joined: Mon Nov 21, 2011 7:05 am
Location: Oregon, USA
Contact:

Re: tile game.

Post by baconhawka7x »

kikito wrote:I recommend you to give a look to my love tile tutorial. There's a chapter 0 with basic concepts, and a section there explaining "the for do statements" (also called "loops").
Thank you! I'm Still reading, but it is defidently the greatest tutorial about love i've ever read!:D
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: TILE GAME!

Post by Nixola »

Compliments for your tutorial, I (finally) understood the use of "for ... do"!
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: TILE GAME!

Post by kikito »

Thanks! ^^

I will try to finish it ... in the future. Right now I have just too much stuff going on.
When I write def I mean function.
User avatar
Jack5500
Party member
Posts: 148
Joined: Wed Dec 07, 2011 8:38 pm
Location: Hamburg, Germany

Re: TILE GAME!

Post by Jack5500 »

This is the best tutorial ever. Please finish it. It helped me a lot!
User avatar
Kazagha
Prole
Posts: 40
Joined: Tue Oct 04, 2011 7:05 am

Re: TILE GAME!

Post by Kazagha »

Jack5500 wrote:This is the best tutorial ever. Please finish it. It helped me a lot!
Ding Dong for that; http://whiteninjacomics.com/comics/munchkins.shtml
nunix
Prole
Posts: 28
Joined: Fri Jan 30, 2009 4:38 am

Re: TILE GAME!

Post by nunix »

So here's a question. In the basics/loops, there are these two blocks of sample code. It seems like if you put them into a main.lua file it should run, but it doesn't; love spits out an error.

Code: Select all

function love.load()
  MyGlobalTable = { 
    { 'Stuff inside a sub-table', 'More stuff inside a sub-table' },
    { 'Stuff inside the second sub-table', 'Even more stuff' }
  }
end

function love.draw()
  for i=1,#MyGlobalTable do
    local subtable = MyGlobalTable[i]
    for j=1,#subtable do   -- nested loop!
      love.graphics.draw(subtable[j], 100 * i, 50 * j)
    end
  end
end
Error is:

[C]: in function 'draw'
main.lua:12:in function 'draw'
[C]: in function 'xpcall'

What's going on? I feel like I almost have a handle on this (and it's definitely the best tile tutorial I've read so far) but I needed to play with some of the sample code to really get it, and this doesn't work. =/
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: TILE GAME!

Post by bartbes »

I spot nothing wrong (but I might be overlooking something), but what I can tell, is that you didn't post the error, but instead the stack trace, useful, but not in this case. Error, please!
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests