Love - Moving an object

From TRCCompSci - AQA Computer Science
Revision as of 12:10, 4 June 2019 by Admin (talk | contribs)
Jump to: navigation, search

Requirements

You need to have followed the installation process for the Love engine.

You also need to have created a minimal game (ie a new folder, with a 'main.lua' file)

You need to have added this code to 'main.lua':

function love.load()

end

function love.update(dt)

end
 
function love.draw()

end

Create a Player Object

This tutorial will create a player object, if you wanted to create a complete game then you would need to create a class for the player. So before 'love.load()' add the following code:

player = {
   x = 100,
   y = 100,
   w = 10,
   h = 10,
   speed = 200
}

This essentially creates a structure called player, this structure has an X position of 100, a Y position of 100, a w(width) of 10, a h(height) of 10, and a speed of 200.

Drawing the player

We will just draw a rectangle at the players position, you could use an image instead or even some frame based animations. So in the draw section add the following code:

love.graphics.setColor(255, 255, 0,255)
love.graphics.rectangle('fill',player.x,player.y,player.w,player.h)