Early LÖVE code snippets? For learning?

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.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Early LÖVE code snippets? For learning?

Post by tentus »

heartmonster wrote:Aaah! Thank you guys so much! This is more helpful than you can imagine. So much of the code out there is like 100 steps above what I can comprehend at this level.

Anyway, I made something. This is my first ever programmed thing I've ever made so it's very simple. But I'm happy with it. :D
Here's something you might find useful: an function that checks if two rectangles are overlapping. Or, more correctly, it tests if they're not overlapping and then returns the opposite. Just give it the coords and dimensions of the first rectangle, and then the coords and dimensions of the second.

Code: Select all

function overlap(x1,y1,w1,h1, x2,y2,w2,h2)
	return not (x1+w1 < x2 or x2+w2 < x1 or y1+h1 < y2 or y2+h2 < y1)
end
Kurosuke needs beta testers
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Early LÖVE code snippets? For learning?

Post by Robin »

Hot damn! Pretty good for a first one. ;)

You could make escape quit the game by adding this function at the bottom of your main.lua:

Code: Select all

function love.keypressed(key)
    if key == 'escape' then
        love.even.push 'q'
    end
end
Help us help you: attach a .love.
User avatar
heartmonster
Prole
Posts: 8
Joined: Sun Sep 04, 2011 3:26 am
Location: New York, NY

Re: Early LÖVE code snippets? For learning?

Post by heartmonster »

Here's something you might find useful: an function that checks if two rectangles are overlapping. Or, more correctly, it tests if they're not overlapping and then returns the opposite. Just give it the coords and dimensions of the first rectangle, and then the coords and dimensions of the second.
Awesome! Thanks, I will def be using that.
You could make escape quit the game by adding this function at the bottom of your main.lua:
That too. :P

Oh, also I have a question, if anyone is still paying attention to this thread--

If I want the player to be able to drag and drop an object (a shape or image or whatev.), what is the best way to do that? I mean I know theoretically one could do this:

Code: Select all

if love.mouse.getX() > thing.x 
and love.mouse.getX() < thing.x+thing.width 
and love.mouse.getY() > thing.y 
and love.mouse.getY() < thing.y+thing.height 
and love.mouse.isDown("l") then
...
end
BUT first of all that is like the most cumbersome thing ever. And second it only half works--in that if you move the mouse too quickly you lose the item you're trying to drag.

I'm sure there must be an easier and less failing way to do this! Thank you!!! ^^
Rad3k
Citizen
Posts: 69
Joined: Mon Aug 08, 2011 12:28 pm

Re: Early LÖVE code snippets? For learning?

Post by Rad3k »

heartmonster wrote:If I want the player to be able to drag and drop an object (a shape or image or whatev.), what is the best way to do that?
You can use love.mousepressed - inside the callback you check if the pointer is over any object, and if it is, assign this object to some variable (let's say, "grabbed"). Then, until the mouse is released (use love.mousereleased or check with love.mouse.isDown each frame), you just keep moving the "grabbed" object to where the pointer is. When you detect that mouse button is released, just unset the "grabbed" variable. I don't know if it's the best way, but it's simple and works well.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Early LÖVE code snippets? For learning?

Post by BlackBulletIV »

I was going to post a reply to your question, but I thought I'd turn it into a tutorial: http://nova-fusion.com/2011/09/06/mouse ... in-love2d/
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: Early LÖVE code snippets? For learning?

Post by miko »

heartmonster wrote:Can people who know what they're doing with LÖVE (you forum regulars!) please post some of their early code examples? Like if you coded a basic Snake or Space Invaders clone, for example. If you could just throw the code down, that would be amazing, and REALLY REALLY helpful to me and probably other beginners like me.

Thanks so much! Look forward to seeing your stuff. :)
Great idea! Here is (one of) my first tries. It was never finished, but at least is somewhat playable ;)
Attachments
MoonLander.love
(137.46 KiB) Downloaded 107 times
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
User avatar
heartmonster
Prole
Posts: 8
Joined: Sun Sep 04, 2011 3:26 am
Location: New York, NY

Re: Early LÖVE code snippets? For learning?

Post by heartmonster »

BlackBulletIV wrote:I was going to post a reply to your question, but I thought I'd turn it into a tutorial: http://nova-fusion.com/2011/09/06/mouse ... in-love2d/
Amazing! This forum is the best. Thanks so much, and I posted a question about that in your topic about it so... Yes. No need for redundancy here. :monocle:
miko wrote:Great idea! Here is (one of) my first tries. It was never finished, but at least is somewhat playable
Thanks! And yeah I will be looking at your code, the program looks appropriately simple-but-with-excellent-things-happening-in-it. Which is apparently a new term I just invented.
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: Early LÖVE code snippets? For learning?

Post by miko »

heartmonster wrote:Thanks so much! Look forward to seeing your stuff. :)
I have just written another game for you - search for Minefield in http://love2d.org/forums/viewtopic.php?f=5&t=3605
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 62 guests