Game mechanics

Objects: Cakes !

Let's start by creating the cakes global object, the same way we did for player.

cakes = {}

Then we need to initialize this cake object. In the "load" callback :

-- Number of cakes on the screen at the same time
cakes.maxSimultaneousCakes = 5
-- We initialize the different types of cakes : birthday first, then later poisonous
cakes.types = {}
cakes.types[0] = "birthday"
-- each type of cake has its own image
cakes.images = {}
cakes.images["birthday"] = love.graphics.newImage('assets/birthday.png')
-- we'll save our current cakes
cakes.instances = {}

Then let's create our cakes ! For each cake missing, we create a new one at the start of the screen, on the right. Each cake height and speed is random In the "update" callback, add the following code :

for i=(#cakes.instances) + 1, cakes.maxSimultaneousCakes do
  cakes.instances[i] = {}
  cakes.instances[i].x = window.width
  cakes.instances[i].y = math.random(0, window.height)
  cakes.instances[i].type = cakes.types[0];
end

We then reuse the window variable that we defined previously to give a position to each cakes, based upon the extreme values of our screen.

Display

Then let's display them. The same way we did for our player movements, we need to draw each cakes in the "draw" callback.

for i=1, #cakes.instances do
  local lecake = cakes.instances[i]
  love.graphics.draw(cakes.images[lecake.type], lecake.x, lecake.y)
end

Note that Lua start counting the array indexes at 1 instead of 0. If you run the game now, you'll see 5 cakes randomly positioned on the right side of the screen. Now we need them to get the left side !

Move

To make them move, we're going to simply "push" them the same. In the "update" callback, add the following code and replace the ??? by the value holding the horizontal value of the cake.

-- cakes movement
for i=#cakes.instances, 1, -1 do
  local lecake = cakes.instances[i]
  cakes.instances[i].??? = lecake.??? - dt * lecake.speed
end

This is what you should get by now

Eating

Collisions ! What would be a game without interactions with the different elements? We want our character to eat the cakes. It should remove the cake from the list of cake when the player touches a cake. If you noticed well, we used a loop starting at the end, and decrementing when moving the cakes The reason for that is because we're going to remove elements from the array, and we don't want to reorder the array after. Hence, when we move our cakes, we want to check if it collides. A collision means that the position of the cake is shared with the position of the player. Use the following function inside the loop to move the cakes :

if player.x < lecake.x + cakes.dimensions[lecake.type].width and
   player.x + player.width > lecake.x and
   player.y < lecake.y + cakes.dimensions[lecake.type].height and
   player.height + player.y > lecake.y then
     table.remove(cakes.instances, i)
end

Now, when the player is in contact with a cake, the cake disappear. Because our loop to create cake is in the "update" callback, the cake is recreated just after, in a new position. You should end up with the following code

Points

For every cake eaten, let's increase our counter ! Create a global variable, counter, like player and cakes. On "load" callback, give "counter" a value of 0. On "update" callback, when you eat a cake (when there is a collision with a cake), increase the counter !

  counter = counter + 100

Now, let's display the counter, on the top left In the "draw" callback, use the following snippet to display your score :

love.graphics.printf("Score: "..counter, 10, 10, 9999)

When running the game, it may be a bit small, so let's increase the size. In the "load" callback, set a new Font size :

love.graphics.setFont(love.graphics.newFont(25))

In the end, you should have the following code

results matching ""

    No results matching ""