Regarding placing/removing blocks in game.
Posted: Mon Oct 10, 2011 4:16 pm
So I've got this code set up for a game where you can place/remove blocks by using the left and right click keys while hovering over them, similar to minecraft and terraria, and I was wondering if I'm doing this in the best possible way or if I'm over complicating the process. What I'm doing DOES work though.
Sample code as to what I'm doing:
Alright, this code makes it so that a variable, called 'aoff', changes whenever the mouse is over a certain x/y area and the user clicks.
Now, that variable is put to use later. (obviously inside a love.draw() function.) If the variable is 2, then it shows it. (2 is the default.) If the variable is 1, which it's changed to when the user left clicks the area, the program just doesn't show the block. The rest is just for changing the block color, which for now, every block is based on the 'blockcolor' variable which changes when the user presses 'r'.
The issue with this is that I have to do the code over and over again for every possible block. It's not like I won't do that or I'm too lazy , but I'd just like to know if a superior method exists.
Lastly, some screenshots showing blocks. =)
I'd love some feedback so I can improve. Thanks for reading, regardless!
Sample code as to what I'm doing:
Code: Select all
function love.mousereleased(x, y, button)
if ((x > 0 and x < 21) == true) and ((y > 279 and y < 301) == true) and button == "l" then
aoff = 1
elseif ((x > 0 and x < 21) == true) and ((y > 279 and y < 301) == true) and button == "r" then
aoff = 2
end
Code: Select all
if aoff == 2 then
if blockcolor == "a" then
tiles.a = love.graphics.draw(water,0 , 280)
elseif blockcolor == "b" then
tiles.a = love.graphics.draw(redb, 0, 280)
elseif blockcolor == "c" then
tiles.a = love.graphics.draw(greenb, 0, 280)
end
end
The issue with this is that I have to do the code over and over again for every possible block. It's not like I won't do that or I'm too lazy , but I'd just like to know if a superior method exists.
Lastly, some screenshots showing blocks. =)
I'd love some feedback so I can improve. Thanks for reading, regardless!