Thursday, August 27, 2020

Simple Platformer - Part 3

Getting started with Love2d - part 3

Introduction

This is part 3 of my ongoing series on making a simple platformer using Love2d.
For this part we are going to continue our adventure into OOP and further improve our code.
We will make a dedicated player class,start doing some polymorphism, and create some more enemies.

get organized

The first thing we will do is get better organized.
In the directory where main.lua is, make a new directory named 'images'
We will now put our character images here to help keep us organized.
In the code where we load images, we need to change 'player.png' to '/images/player.png'
Do the same for the enemy image as well.

making a player class

The next thing we need to do is create a player class. This is identical to how we made an enemy class
So far we have no unique variables or functions for player but we will change that later on.

Player class
player class

Now that we have a player class we need a constructor functions.
This is also similar to what we did with enemy class.

constructor for player objects
player constructor

We now need to change one line in main.lua to make player a PLAYER object.

updates love.load()
making player object in main.lua

We now have a working separate class for our player

Making multiple enemies

We will now make multiple enemies in our game.
To easily handle this we are going to create a table to hold enemy objects.
And to make this even easier, we make a function to populate this list for us.
So we have ENEMY_LIST to hold our enemies and makeEnemies() to populate our table with enemy objects

tabel to hold multiple enemy objects.
making list of enemies

We now have multiple enemies in our game. This can easily be expanded to make as many as we want.
Notice that the i*100 in our function just places the enemies 100 pixels a part.
Also notice that the WIDTH has been increased to accommodate these new enemies.

Drawing characters

We will now go further with our OOP adventure.
To do this we need to create a function to draw character objects to screen.
Since both player and enemy are subclasses of character, this function will work for both of them.
Hurray for polymorphism.

function to draw character to screen
function to draw character objects to screen

We now need to make a function to go through enemy_list and print each enemy to screen.
To accomplish this we just need a loop to go through enemy_list, pretty simple stuff.
Notice we make use of our newly made draw function.

loop through enemy_list and draw each enemy to screen
function to draw enemies to screen

Now that we have our function to draw objects to screen and a loop to go through each enemy, we need to update love.draw().
With just these two lines we can now print both the player and all the enemies to screen on each frame.

updated love.draw()
updated our love.draw() function

We now have the capability to print a player and any number of enemies to screen.
so far the enemies don't do anything, but we are about to change that.

Moving Enemies

We will now make our enemies move back and forth as we previously did with a single enemy.
To do this we need a function to go through ENEMY_LIST.
With each enemy object in the loop we call the move function which we previous made

function to move enemies
function to move each enemy on screen

We now need to update our code in love.update to call this new function.

updated love.update()
updated love.update()

we should now have three enemies moving across the screen and a player who can also move around

The game so far.
Enemies moving around

Final remarks

With just a little bit of reorganizing and adding a tiny bit to our previous code we can now:

  • make multiple enemies in our game.
  • print them all to screen.
  • have each enemy move independent of each other.

It may not seem much, but it is progress we are making on our game.
The full code for this part is available on github
Thank you for checking out part 3 of my series on game dev.continue to part 4

No comments:

Post a Comment