Game mechanics and steps

Poisonous cake

A game without end isn't fun, right? So let's put a bit of difficulty by introducing the Poisonous cake(s) If you remember well, we have in our loop for creation of cakes a type, which is "birthday" right now. Let's add another type, of poisonous. In our "load" callback, add the following :

cakes.types[2] = "poisonous"
cakes.images["poisonous"] = love.graphics.newImage('en/code/assets/poisonous.png')
cakes.dimensions["poisonous"] = {}
cakes.dimensions["poisonous"].width, cakes.dimensions["poisonous"].height = cakes.images["poisonous"]:getDimensions()

With this, poisonous cakes will display, but we want to add the appropriate behavior to it : loosing the game, not gaining points !

Game over

We're going to add a variable (a boolean) to the player, called "alive". On our "load" callback, add :

  player.alive = 1

When the player eat a poisonous cake, change the value of alive to 0. Adapt your previous code to increment your counter of points to the following

   if lecake.type == "birthday" then
     counter = counter + 100
     table.remove(cakes.instances, i)
   elseif lecake.type == "poisonous" then
     player.alive = 0
   end

Then add a condition over anything in our "update" callback to be done only if the player is alive. This way, we'll freeze the game on the latest image

   if player.alive == 1 then
    [everything we wrote in this callback previously]
  end

Finally, in order to inform the player that he lost, we display a "GAME OVER" in the "draw" callback. The condition will encapsulate our previous score display in order to give a more "memorable" effect of your game.

 if player.alive == 1 then
   love.graphics.printf("Score: "..counter, 10, 10, 9999)
 elseif player.alive == 0 then
   love.graphics.printf("GAME OVER", window.width/2, window.height/2, 9999)
   love.graphics.printf(counter.." points", (window.width/2), (window.height/2) + 18, 9999)
 end

We once again use the window's height value to center our text. The counter is positioned the same way plus the height of a line, allowing us to put it just under the "GAME OVER".

You should end up with the following code [en/code/game-over.lua]

results matching ""

    No results matching ""