[SOLVED]How to draw an image in a table?

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
Taaaaum
Prole
Posts: 2
Joined: Thu Jan 05, 2017 2:16 am

[SOLVED]How to draw an image in a table?

Post by Taaaaum »

Well ... I'm very new to game development and I started this a few hours ago to be honest. I've been reading docs in Lua and Love docs to try to produce something and what I'm trying to do is a simple board and I'd like to put a chunk inside the table index. The following code is showing you ... but the image is positioning at the edge of the window.

What am I trying to do is ok or is there another easy way out of it?

Code: Select all

Tabuleiro = {}
vazio = nil
casax, casay = 0,0;
Piece1= {x = 0, y = 0, img = nil}

function IniciaTabuleiro()
	for i = 1, 10 do
		Tabuleiro[i] = {}
		for j = 1, 10 do
			Tabuleiro[i][j] = 0 -- Inicia o tabuleiro
		end
	end
	
end 

function love.load(args)	
		vazio = love.graphics.newImage('vazio.png') -- Salva img na memoria
		Piece1.img = love.graphics.newImage('aviao1.png')
		
		IniciaTabuleiro()
		
end

function love.update(dt)
	if love.keyboard.isDown('escape') then
		love.event.push('quit') -- Fecha quando aperta ESC
	end
	Tabuleiro.x = 5
	Tabuleiro.y = 5
end

function love.draw(dt) 
	for x = 1, 10 do
		for y = 1, 10 do
					
					Tabuleiro[x][y] = love.graphics.draw(vazio, y * 50 - 50, x * 50 - 50) -- Desenha o tabuleiro
					
	
		end
	end

	Tabuleiro[2][3] = love.graphics.draw(Piece1.img, 450, 400)	
end
Last edited by Taaaaum on Sat Jan 28, 2017 1:39 am, edited 1 time in total.
nyenye
Citizen
Posts: 62
Joined: Fri Dec 02, 2016 1:44 pm

Re: How to draw an image in a table?

Post by nyenye »

Hii, welcome to the forums.

From what I see you are trying to make some sort of chess game? What I'd do is draw the empty board as a whole image. Then on your matrix you keep note of your pieces and theyr position, and draw each one on top of the board.

You will have to know width and height of the cells and draw acordingly.

Hope it helps.
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to draw an image in a table?

Post by zorg »

First, a tip: Posting english code snippets amounts for faster/better help. :3

Now, onto the issues: (from smaller to bigger)
- anything = nil will only ever be a placeholder, it doesn't make sense to have it, unless you want a visual reminder of those variables.
- Same with someTable.somefield = nil
- You don't -technically- need to initialize the inner tables to 0, only those that hold tables themselves, but you might have a good reason to do it anyway.

- Why are you setting Tabuleiro.x and y to 5 every update?

- love.graphics.draw draws an image on the screen, it has no return value. You'd either want to store images in the table slots, or some other data that can tell the drawing code what to draw in love.draw. I'm guessing you want to do the latter.

So, here's a bit of a rewrite; not saying it'll work, i haven't tested it myself, but it should show how things would work more correctly.

Code: Select all

-- Use locals, then it makes sense to have nil variables.
local Tabuleiro = {}
local vazio -- don't even need the explicit "= nil" written out! :3
local casax, casay = 0,0;
local Piece1 = {x = 0, y = 0, img = nil}

function IniciaTabuleiro()
   for i = 1, 10 do
      Tabuleiro[i] = {}
      -- As long as Tabuleiro[i][j] is not a table you'd accidentally index into, you don't need to initialize it.
      -- But since you have an empty image...
      Tabuleiro[i][j] = vazio
   end
   
end 

function love.load(args)   
      vazio = love.graphics.newImage('vazio.png') -- Salva img na memoria
      Piece1.img = love.graphics.newImage('aviao1.png')
      IniciaTabuleiro()
      Tabuleiro[2][3] = Piece1.img
end

function love.update(dt)
   if love.keyboard.isDown('escape') then
      love.event.push('quit') -- Fecha quando aperta ESC
   end
end

function love.draw(dt) 
   for x = 1, #Tabuleiro do -- This way you get the max number from the table itself.
      for y = 1, #Tabuleiro[x] do -- Same here
         love.graphics.draw(Tabuleiro[x][y], (y-1) * 50, (x-1) * 50) -- Desenha o tabuleiro
      end
   end  
end
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Taaaaum
Prole
Posts: 2
Joined: Thu Jan 05, 2017 2:16 am

Re: How to draw an image in a table?

Post by Taaaaum »

Thanks zorg and nyenye, you too were very fast and helpfull.


- Why are you setting Tabuleiro.x and y to 5 every update? It's easy to answer, I was TOTALLY drunk of sleep, and just doing things without think.

- I knew that I can store images in table because Lua properties is very dinamic about tables, I just don't knew how to do and you made it clear so now I can sleep early today.
Post Reply

Who is online

Users browsing this forum: Amazon [Bot] and 63 guests