Any tip to optimize my code?

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
User avatar
andropof91h
Prole
Posts: 3
Joined: Sun Jul 03, 2016 10:21 am

Any tip to optimize my code?

Post by andropof91h »

Hi,
i've started learning Lua and Love about 4 days ago, so sorry if my code is bad.
Today i wrote this code

Code: Select all

function love.load()

	love.graphics.setBackgroundColor(50,50,50)

	AltezzaFinestra = love.graphics.getHeight()
	LarghezzaFinestra = love.graphics.getWidth()

	Font1 = love.graphics.newFont( "pcsenior.ttf", 16 )

	--Testo

	Testo = ""

	LunghezzaMassima = 3

	--Pulsante1

	FraseCasella1="Il valore inserito è: "..Testo

	LarghezzaPulsante1=0
	AltezzaPulsante1=0

	XPulsante1 = (LarghezzaFinestra/2)-(LarghezzaPulsante1/2)
	YPulsante1 = (AltezzaFinestra/2)-(AltezzaPulsante1/2)

	ColorePulsante1=150

	ValoreStandardAggiunto1=10

	--Pulsante2

	FraseCasella2=Testo

	LarghezzaPulsante2=150
	AltezzaPulsante2=25

	ColorePulsante2=120

	ValoreStandardAggiunto2=10

	
	------------------
	--Testo accettato

	TestoAccettato={"1","2","3","4","5","6","7","8","9"}
	Numeri=9

	------------------
	Pressione=0
	Errore = false

end

function love.textinput(TestoInserito)
	
	
		Verifica = ""
		Verifica = Verifica..TestoInserito

		Numeri2=Numeri

		Controllo = false

		while Controllo == false do
			Numeri2=(Numeri2-1)
			if Verifica == TestoAccettato[Numeri2] then
				Controllo = true
				Errore = false
			end
			if Numeri2 == 0 then
				Controllo = true
				Errore = true
			end


		end

		

		if Errore == false then
			
			Testo = Testo..Verifica
			if string.len(Testo)>LunghezzaMassima then
			Testo=""
		end
			
		end
	
			
end

	

	



function love.mousepressed(x, y, button, istouch)
   if button == 1 and x>XPulsante1 and x<XPulsante1+LarghezzaPulsante1 and y>YPulsante1 and y<YPulsante1+AltezzaPulsante1 then
   		
   		Pressione=Pressione+1
		Attivato=true
		if (Pressione%2==1) then
			Testo=""
		end
		
   end
end

function love.draw()


	--Pulsante1

		
	FraseCasella1="Il valore inserito è: "..Testo

	LarghezzaFraseCasella1=Font1:getWidth(FraseCasella1)
	AltezzaFraseCasella1=Font1:getHeight(FraseCasella1)

	LarghezzaPulsante1=LarghezzaFraseCasella1+ValoreStandardAggiunto1
	AltezzaPulsante1=AltezzaFraseCasella1+ValoreStandardAggiunto1

	XPulsante1 = (LarghezzaFinestra/2)-(LarghezzaPulsante1/2)
	YPulsante1 = (AltezzaFinestra/2)-(AltezzaPulsante1/2)


	love.graphics.setColor(ColorePulsante1,ColorePulsante1,ColorePulsante1)
	love.graphics.rectangle("fill", XPulsante1, YPulsante1,LarghezzaPulsante1 , AltezzaPulsante1)
	love.graphics.setColor(225,225,225)

	TestoFraseCasella1 = love.graphics.newText( Font1, FraseCasella1 )
	love.graphics.draw(TestoFraseCasella1, (LarghezzaFinestra/2)-(LarghezzaFraseCasella1/2), (AltezzaFinestra/2)-(AltezzaFraseCasella1/2) )


	--Pulsante2

	if (Pressione%2==1) then


		FraseCasella2=Testo


		love.keyboard.setTextInput( true )

		ColorePulsante1=120

		LarghezzaFraseCasella2=Font1:getWidth(FraseCasella2)
		AltezzaFraseCasella2=Font1:getHeight(FraseCasella2)

		LarghezzaPulsante2=LarghezzaFraseCasella2+ValoreStandardAggiunto2
		AltezzaPulsante2=AltezzaFraseCasella2+ValoreStandardAggiunto2

		XPulsante2 = (LarghezzaFinestra/2)-(LarghezzaPulsante2/2)
		YPulsante2 = (AltezzaFinestra/2)-(AltezzaPulsante2-90)


		love.graphics.setColor(ColorePulsante2,ColorePulsante2,ColorePulsante2)
		love.graphics.rectangle("fill", XPulsante2, YPulsante2,LarghezzaPulsante2 , AltezzaPulsante2)
		love.graphics.setColor(225,225,225)

		TestoFraseCasella2 = love.graphics.newText( Font1, FraseCasella2 )
		love.graphics.draw(TestoFraseCasella2, XPulsante2+(ValoreStandardAggiunto2/2), YPulsante2+(ValoreStandardAggiunto2/2))



	elseif (Pressione%2==0) then

		love.keyboard.setTextInput( false )
		ColorePulsante1=150

	end





end
It works.... but for what it does it's very long and hard to read...
~Can anyone give me any advice on how to make a cleaner code?
~Is it well optimized?
~Any example of code which does the same things?

For any doubt just ask me and i'll reply.
Waiting for your kind reply, and sorry if it might look like a useless topic, but I hadn't found anything about this kind of subject...
Attachments
Test.love
(6.8 KiB) Downloaded 59 times
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Any tip to optimize my code?

Post by s-ol »

Start with formatting. I havent looked at the .love itself so maybe it happened during pasting, but your indentation is messed up. Indentation is extremely important for well readable code, and having readable code is an important first step towards clean, structured code. The same goes for generally making code readable. For example putting spaces around the = in an assignment, or around an operator like + or ==. The better you can "see" the code, the better you can focus on the actual stuff it is doing. It is even more important when showing the code to others who have to figure out both "layers" of the code at once basically.

Similarily, naming of variables is important, but a very subjective thing. For example even though I speak german natively I program entirely in english, even in private projects that are never meant to be seen by anyone else. Reading your code above is near impossible because I dont understand the variable names. I also try to not use numbers in named anr instead give context (color1 + color2 vs current_color + next_color for example). I also write variable_names_like_this and class-like tcings LikeThis but again, this is very subjective. consistency is key, the system used isn't that important.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Post Reply

Who is online

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