Page 1 of 1

A little drawing tool

Posted: Mon Jun 19, 2017 4:26 am
by TheMajesticMagikarp
This is my first post on the Love2D forum, so please excuse my lack of ability to create a neat post. I've been working on a little "code doodle" that will place a circle at the point of the mouse when the user clicks. The person can click in multiple places and the circles will connect with lines. I am new to programming and I am learning how to use tables and arrays. I was hoping to get feedback on my use of the table in this project. I was only able to achieve my goal by using the table. If there is a better way to achieve this I would like to know. Here is the code for the project:

Code: Select all

function love.load()
circle = {}
cN = -1 --Variable stands for Circle Number,  it adds two to each circle. I use it to keep track of amount of circles on the screen and looping
end

function love.mousepressed(x, y, button, istouch)
   if button == 1 then
      printx = x
      printy = y
	  print(x)
	  print(y)
	  cN = cN + 2
	  table.insert(circle, 1, printy)
	  table.insert(circle, 1, printx)
   end
  end

 function love.draw()
	 if cN > 0 then
		 for i=1,cN,2 do
			love.graphics.circle("fill", circle[i], circle[i + 1], 2)
		 end
		 for i=1,cN,2 do
			 if circle[i+2] ~= nil then
				love.graphics.line(circle[i],circle[i+1],circle[i+2],circle[i+3])
			 end
		 end
	 end
 end
I appreciate all feedback. Thanks! :awesome:

Re: A little drawing tool

Posted: Mon Jun 19, 2017 8:50 am
by zorg
Hi and welcome to the forums!

You can nest tables inside other tables too.

Code: Select all

local circle = {}

love.mousepressed = function(x,y,b)
  if b == 1 then
    circle[#circle+1] = {x,y}
  end
end

love.draw = function()
  if #circle > 0 then love.graphics.circle('fill', circle[1][1]) end
  for i=1, #circle-1 do
    love.graphics.line(circle[i][1], circle[i][2], circle[i+1][1], circle[i+1][2])
    love.graphics.circle('fill', circle[i+1][1])
  end
end

Re: A little drawing tool

Posted: Mon Jun 19, 2017 4:51 pm
by TheMajesticMagikarp
That makes more sense to group the coordinates together in the table.