Tutorial requests and ideas

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
rude
Administrator
Posts: 1051
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: Tutorial requests and ideas

Post by rude » Tue Feb 19, 2008 10:43 pm

Merkoth wrote:I guess it means I'm a douche who completely forgot about it. I'll try to upload what I have somewhere so you guys can give take a look at it.
Hehe, that's not what I meant. ^.^ Just wanted to know if I should remove it from the list.

Yes, I *am* still making the animation tutorial ... but The University of Doom is KILLING me at the moment.

User avatar
Merkoth
Party member
Posts: 186
Joined: Mon Feb 04, 2008 11:43 pm
Location: Buenos Aires, Argentina

Re: Tutorial requests and ideas

Post by Merkoth » Thu Feb 21, 2008 3:23 am

Sure I can, the only problem is that I'm not at home, and I have yet to polish it a little bit the tutorial itself. I'll upload it tomorrow if possible.

User avatar
rude
Administrator
Posts: 1051
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: Tutorial requests and ideas

Post by rude » Thu Feb 21, 2008 5:09 am

No rush. 8-)

User avatar
Merkoth
Party member
Posts: 186
Joined: Mon Feb 04, 2008 11:43 pm
Location: Buenos Aires, Argentina

Re: Tutorial requests and ideas

Post by Merkoth » Thu Feb 21, 2008 11:02 pm

So, here it is. It's pretty much the same as the current callbacks tutorial, but since it's already written it'd just dumb to keep it sitting here on my HD n_n

Using input

Capturing input events with LÖVE is really easy; in this tutorial we'll cover how to capture keyboard and mouse events using both object methods and callbacks.

We'll start our tutorial by putting together an almost empty LÖVE program:

Code: Select all

function load()
        love.graphics:setFont(love.objects:newFont(love.default_font, 12))
	text = “”
end

function update(dt)
end

function draw()
	love.graphics:draw( “text, 330, 300 )
end
Capturing keyboard events:

The most easy way to know if the user is pressing a key is calling the isDown method which has the following syntax:

Code: Select all

love.keyboard:isDown( key )
The key parameter is the constant value of the key we want to see if it's currently pressed. A simple example:

Code: Select all

if love.keyboard:isDown( love.key_space ) then
	text = “SPACE is being pressed!”
end
You can find the complete list of key constants here

The best place to perform this check is inside the update() callback: that way we're able to get input from the user and update our variables before drawing our stuff into the screen. So, our modified update() callback should look like this:

Code: Select all

function update(dt)
	if love.keyboard:isDown( love.key_space ) then
		text = “SPACE is being pressed!”
	end
end
While this is fine and dandy if we only need to know which key or keys are currently pressed, we might also need to specify different behaviors when a certain key is pressed and/or released. An elegant way of doing this is using the keyboard callbacks keypressed() and keyreleased(). They work in a similar way of the already known update() or draw() callbacks, executing our code every time that event is triggered. For example:

Code: Select all

function keypressed( key )
	if key == love.key_return then
		text = “RETURN is being pressed!”
	end
end

function keyreleased( key )
	if key == love.key_return then
		text = “RETURN has been released!”
	end
end
As you can see, these two callbacks will provide you a key variable which you can use to check if a given key has been pressed, released or both. Up to this point, our source file should look like this:

Code: Select all

function load()
        love.graphics:setFont(love.objects:newFont(love.default_font, 12))
        text = ""
end

function update(dt)
	if love.keyboard:isDown( love.key_space ) then
		text = “SPACE is being pressed!”
	end
end

function draw()
		love.graphics:draw( text, 330, 300 )
end

function keypressed( key )
	if key == love.key_return then
		text = “RETURN is being pressed!”
	end
end

function keyreleased( key )
	if key == love.key_return then
		text = “RETURN has been released!”
	end
end
Capturing mouse events:

So, we already know how to interact with our users through a keyboard. But what about that little rodent that sits on their desks? Well, mouse input works in a fairly similar way: we have an isDown() method and the mousepressed() and mousereleased() callbacks.

Let's add a few lines to our update() callback:

Code: Select all

if love.mouse:isDown(love.mouse_right) then
	text = “Mouse button right is pressed”
end
As you can see, it's very similar to the keyboard's isDown() method and, again, you can see the full list of mouse-related constants here. You can even check if the mouse wheel has been rolled up or down using this method.

We also have two handy methods to know the current position of the mouse pointer inside our game window: getX() and getY(). Each one will return the current coordinate of the mouse pointer. Let's see an example by adding these line to the beginning of our update() callback:

Code: Select all

mouse_x = love.mouse:getX()
mouse_y = love.mouse:getY()
And this line to our draw() callback:

Code: Select all

love.graphics:draw( “Mouse X: ”.. mouse_x .. “ Mouse Y: ” .. mouse_y, 10, 20 )
The mousepressed() and mousereleased() callbacks work in a very similar way as their keyboard counterparts:

Code: Select all

function mousepressed(x, y, button)
	if button == love.mouse_left then
		text = “Mouse button left is pressed”
	end
end

function mousereleased(x, y, button)
	if button == love.mouse_left then
		text = “Mouse button left is pressed”
	end
end
A cool feature of this callback is that you can know not only if a button has been pressed but also the position of the mouse pointer when the user pressed the button. This can be really useful if you need to put together some basic user interface elements like buttons or other objects that can interact with the mouse. A simple example:

Code: Select all

function mousepressed(x, y, button)
	if button == love.mouse_left then
                text = “Mouse button left is pressed at X:”..x.." Y: "..y
	end
end
Finally, we have yet another two useful mouse-related methods: setVisible() and isVisible(). The first one will let you hide or show the mouse pointer and the second one will obviously let you know if the mouse pointer is whether visible or not. Let's add even more code to our update() callback:

Code: Select all

if love.keyboard:isDown(love.key_h) then
	if love.mouse:isVisible then
		love.mouse:setVisible(false)
	else
		love.mouse:setVisible(true)
	end
end
In these few lines we check if the mouse pointer is visible or not and then we change its visibility: if it's visible we hide it and if it's already hidden we then show it. Fairly easy, isn't it?

-- END OF TUTORIAL
Teh screenshot:
Image

Teh files:
http://www.cloverpunk.com.ar/tehstuffs/love-tut1.zip

User avatar
rude
Administrator
Posts: 1051
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: Tutorial requests and ideas

Post by rude » Fri Feb 22, 2008 3:28 am

Nice work Merkoth ...
I've added it to the main page: http://love.sourceforge.net/?page=documentation ^^

User avatar
Merkoth
Party member
Posts: 186
Joined: Mon Feb 04, 2008 11:43 pm
Location: Buenos Aires, Argentina

Re: Tutorial requests and ideas

Post by Merkoth » Fri Feb 22, 2008 10:53 am

Thx rude, but it seems something broke:

Code: Select all

Warning: main(geshi.php): failed to open stream: No such file or directory in /home/groups/l/lo/love/htdocs/php/tutorial.php on line 4

Warning: main(): Failed opening 'geshi.php' for inclusion (include_path='') in /home/groups/l/lo/love/htdocs/php/tutorial.php on line 4

Fatal error: Cannot instantiate non-existent class: geshi in /home/groups/l/lo/love/htdocs/php/tutorial.php on line 5
i can has geshi plz? kthxby n_n

User avatar
rude
Administrator
Posts: 1051
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: Tutorial requests and ideas

Post by rude » Fri Feb 22, 2008 11:16 am

Now that's just creepy ... it got that error too now, but if I go back and try again, it works. Dare I say ... WTF?

User avatar
Merkoth
Party member
Posts: 186
Joined: Mon Feb 04, 2008 11:43 pm
Location: Buenos Aires, Argentina

Re: Tutorial requests and ideas

Post by Merkoth » Fri Feb 22, 2008 12:02 pm

It seems to work fine now... must be a broken tube on the intarwebz...
Anyway, thanks n_n

Green_Hell
Citizen
Posts: 94
Joined: Thu Feb 21, 2008 1:11 am

Debug info

Post by Green_Hell » Sat Feb 23, 2008 2:41 pm

Is it possible to print debug info somewhere? I've found stdout.txt but if i use print("hello") my message isn't there. I think it's very useful print debug info to stdout.txt or somewhere else. :|
>>I love LÖVE.<<

User avatar
rude
Administrator
Posts: 1051
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: Tutorial requests and ideas

Post by rude » Sat Feb 23, 2008 7:16 pm

Yeah ... need to do something about that. It would perhaps be best to have a way of printing debug info directly in the LÖVE Window. (And stdout, for that matter).

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 6 guests