Page 1 of 1

Ways for improving code?

Posted: Sun Feb 07, 2016 10:23 pm
by ZBoyer1000
I'm currently stuck on trying to find ways to improve the code I created. Is there any way to make parts of it smaller? :D
--W to change maps
--Up/Down to make a player jump

Re: Ways for improving code?

Posted: Mon Feb 08, 2016 6:19 am
by Davidobot
I'll personally get back to your code in a bit, but I just want to say that this book: Clean Code, is one of the most useful pieces of work I have ever read as regards to learning about code structure and maintenance. I highly recommend it and you can even find free PDFs floating around online.

EDIT:
Right off the bat, I can tell that you are creating quads every frame in player_state. Just create them once and use those.
Secondly, please indent your lines, it makes it infinitely more maintainable and readable:

Code: Select all

function player_state (id, rotatable, state) -- Player Textures
	for i,v in pairs (entity_list) do
		local block = entity_list[i]
		
		if block.entity_id == id then
			frame_x, frame_y = 0, 0
			
			if state == "Idle" then
				frame_x ,frame_y = 0, 0
			elseif state == "Idle2" then
				frame_x ,frame_y = 13, 0
			elseif state == "Crouch" then
				frame_x ,frame_y = 26, 0
			elseif state == "Crouch2" then
				frame_x ,frame_y = 39, 0
			elseif state == "Walk1" then
				frame_x ,frame_y = 0, 19
			elseif state == "Walk2" then
				frame_x ,frame_y = 13, 19
			elseif state == "Jump" then
				frame_x ,frame_y = 26, 19
			end
		
			frame = love.graphics.newQuad(frame_x, frame_y, block.w, block.h, img:getWidth(), img:getHeight())
			love.graphics.draw(img, frame, block.x, block.y, 0, -g_scale,g_scale,0,0)
		end
	end
end
Also, I'm not quite sure what your code is meant to do, so if you could elaborate, that would be great.

Re: Ways for improving code?

Posted: Mon Feb 08, 2016 11:13 pm
by ZBoyer1000
Thank you!