Page 1 of 1

Love2d game questions

Posted: Thu Sep 30, 2021 11:45 am
by K20blegend
I am a new lua dev so I don't have much knowledge about lua or love2d, I wanted to make a shooter game I have made
the game its not great but I like it what I wanted help with is:

1) How to finalize my title screen: I have made a screen display on the start it looks good I have a play button but the button does not work.

2) I have made a circle as a target I wanted to know how to add an image in place of it, my game is space shooter based and I wanted to add an spaceship so yeah

I will be really thankful if you respond in a simpler manner

Re: Love2d game questions

Posted: Thu Sep 30, 2021 11:53 am
by veethree
When asking for help, It's always better to post your code. Then we can tailor our answers to your situation better.

For a button to work you need to know where it is, and how big it is. Then check if the mouse is over it when the mouse is pressed.

To replace the circle with an image, You load the desired image with love.graphics.newImage(), Then draw it with love.graphics.draw()

Re: Love2d game questions

Posted: Thu Sep 30, 2021 12:10 pm
by darkfrei
K20blegend wrote: Thu Sep 30, 2021 11:45 am 1) How to finalize my title screen: I have made a screen display on the start it looks good I have a play button but the button does not work.
You are need to check it the player clicks the button:

Code: Select all

function is_in_area (mx, my, x, y, w, h) -- mouse position and rectangle
	if (mx > x) and (mx < (x + w)) and
	   (my > y) and (my < (y + h)) then
		return true
	end
end

Code: Select all

buttons = {
	{name = "play", x=100, y=50, w=400, h=50, text = "Play"},
	{name = "quit", x=100, y=200, w=400, h=50, text = "Quit"},
}
function mousepressed (mx, my, button, istouch)
	for i, button in pairs (buttons) do
		if is_in_area (mx, my, button.x, button.y, button.w, button.h) then
			if button.name == "play" then
				-- change state for example as
				-- state = states.play
			elseif button.name == "quit" then
				love.event.quit()
			end
		end
	end
end

Re: Love2d game questions

Posted: Thu Sep 30, 2021 1:59 pm
by milon
Tip: Rather than run through massive IF blocks, it's better to store the button actions in a table indexed with the button name. Then it's just a matter of calling button.name() to trigger the appropriate response. (This goes for most things when you see giant IF blocks, btw.)

Re: Love2d game questions

Posted: Thu Sep 30, 2021 5:37 pm
by K20blegend
Thanks