Ways for improving code?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
ZBoyer1000
Prole
Posts: 39
Joined: Sat Nov 28, 2015 10:13 am

Ways for improving code?

Post 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
Attachments
Game Experiment4.LOVE
(991.43 KiB) Downloaded 60 times
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: Ways for improving code?

Post 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.
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
ZBoyer1000
Prole
Posts: 39
Joined: Sat Nov 28, 2015 10:13 am

Re: Ways for improving code?

Post by ZBoyer1000 »

Thank you!
Post Reply

Who is online

Users browsing this forum: darkfrei and 247 guests