Difference between revisions of "Love - Moving an object"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Drawing the player)
 
Line 41: Line 41:
 
love.graphics.setColor(255, 255, 0,255)
 
love.graphics.setColor(255, 255, 0,255)
 
love.graphics.rectangle('fill',player.x,player.y,player.w,player.h)
 
love.graphics.rectangle('fill',player.x,player.y,player.w,player.h)
 +
</syntaxhighlight>
 +
 +
Everytime the draw method is called, the color will be set to yellow, and then draw the rectangle for the player.
 +
 +
=Movement=
 +
The update section will be run multiple times during the game, so this is the ideal section to add the movement code. After update is run the draw method will be immediately called, it is better to keep draw just to drawing the game and update for everything else.
 +
 +
So in the update section add the following for left and right movement:
 +
<syntaxhighlight lang=lua>
 +
if love.keyboard.isDown('a') then
 +
  player.x = player.x - dt * player.speed
 +
elseif love.keyboard.isDown('d') then
 +
  player.x = player.x + dt * player.speed
 +
end
 +
</syntaxhighlight>
 +
 +
You should then deal with up and down movement, so add:
 +
<syntaxhighlight lang=lua>
 +
if love.keyboard.isDown('w') then
 +
  player.y = player.y - dt * player.speed
 +
elseif love.keyboard.isDown('s') then
 +
  player.y = player.y + dt * player.speed
 +
end
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 12:18, 4 June 2019

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)

Everytime the draw method is called, the color will be set to yellow, and then draw the rectangle for the player.

Movement

The update section will be run multiple times during the game, so this is the ideal section to add the movement code. After update is run the draw method will be immediately called, it is better to keep draw just to drawing the game and update for everything else.

So in the update section add the following for left and right movement:

if love.keyboard.isDown('a') then 
   player.x = player.x - dt * player.speed 
elseif love.keyboard.isDown('d') then 
   player.x = player.x + dt * player.speed 
end

You should then deal with up and down movement, so add:

if love.keyboard.isDown('w') then 
   player.y = player.y - dt * player.speed
elseif love.keyboard.isDown('s') then 
   player.y = player.y + dt * player.speed 
end