Difference between revisions of "User:Kraftman"

Line 100: Line 100:
 
end
 
end
 
</source>
 
</source>
 +
 +
So now the alien bounces left and right off the screen.

Revision as of 21:48, 21 May 2011

Getting Started

This is a basic guide to LOVE, focused on getting you stuck in as fast as possible. Everyone starts from a different background, so find where you belong and start from there:

New to Coding

- to do

New to Lua

- to do

New to LOVE

Coding in LOVE

Lets dive straight in and get something on the screen: Create a folder (name it whatever you want) Create a text file inside the folder, and rename it "main.lua"

insert the following into main.lua:

function love.draw()
  love.print("Hello World", 20, 40)
end

Drag the entire folder onto the LOVE exe or shortcut, and your first program should run!

love.draw() is a function that is called every frame, and so it is use to draw each object. In this first example, the love.print function prints the desired text to the page, with the coordinates specified. the x and y coordinates are from the top left corner of the screen.

Printing text is great, but how about showing some cool graphics?

Lets load an image into the game:

Place any image you want into the folder. Use this one if you want. Rename the file something simple and appropriate, eg alien.

change the code in main.lua to

Alien = love.graphics.newImage("Alien.jpg")

function love.draw()
    love.draw(Alien, 50, 50)
end

Ta da! your image should now be sitting happily in the middle of the screen. As exciting as this is, I'm sure you'll want to do a little more with love, for example, making the alien move.

To do this, we need the x and y positions of the alien to vary with time. One way to do this would be to create x and y variables, and vary them every frame. Since love.draw() is called every frame, it seems logical to use this to add small increments to x and y, making the alien appear to move. However, the amount of time between can vary a lot depending on the amount of processing needed to be done, which would make the speed of the alien appear to vary. To account for this we need to use love.update(dt) you'll notice that unlike love.draw(), love.update(dt) passes a variable, dt, which is the amount of time that has passed since the last frame was drawn. By setting a constant speed and multiplying it by by the time since the last frame, the objects movement whiel appear constant despite any framerate lags or spikes.

So will that block of text over, lets see the code:


Alien = love.graphics.newImage("Alien.jpg")
X = 50
Y = 50
SPEED = 10

function love.update(dt)
 X = X + dt*SPEED
end

function love.draw()
    love.draw(Alien, X, Y)
end

The alien should shoot off the side off the screen, never to be seen again!

I guess you're growing pretty fond of your pet alien by now, and don't want it to escape, so lets trap it inside the window.

We can find out the height and width of the window by using love.graphics.getWidth() and love.graphics.getHeight() which return the size in pixels. As the alien moves, we need to check if its reached the edge of the window, and change its direction if it does, so it cant escape.


Alien = love.graphics.newImage("Alien.jpg")
X = 50
Y = 50
height = love.graphics.getHeight()
width = love.graphics.getWidth()
reversex = true
SPEED = 10

function love.update(dt)
    if X < 0 or X > width then
        reversex = not reversex
    end

    if reversex then
        X = X - dt*SPEED
    else
        X = X + dt*SPEED
    end
end

function love.draw()
    love.draw(Alien, X, Y)
end

So now the alien bounces left and right off the screen.