Love2d game questions

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
K20blegend
Prole
Posts: 2
Joined: Thu Sep 30, 2021 11:33 am

Love2d game questions

Post 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
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: Love2d game questions

Post 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()
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: Love2d game questions

Post 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
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: Love2d game questions

Post 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.)
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
K20blegend
Prole
Posts: 2
Joined: Thu Sep 30, 2021 11:33 am

Re: Love2d game questions

Post by K20blegend »

Thanks
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 8 guests