A little drawing tool

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
TheMajesticMagikarp
Prole
Posts: 8
Joined: Tue Apr 11, 2017 12:11 am

A little drawing tool

Post 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:
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: A little drawing tool

Post 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
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.
TheMajesticMagikarp
Prole
Posts: 8
Joined: Tue Apr 11, 2017 12:11 am

Re: A little drawing tool

Post by TheMajesticMagikarp »

That makes more sense to group the coordinates together in the table.
Post Reply

Who is online

Users browsing this forum: No registered users and 94 guests