Introduction
Welcome to part 4 of my ongoing series about game dev using Love2d.
In this part we will expand upon the work we did previously.
First we will slightly alter how we draw images to screen.
Then we will better organize our code to move the player.
Lastly we will add the ability to run into enemies and destroy them.
Working with offsets
The typical way that love2d draws an image onto screen is to use x,y coordinates.
So far the way we have been using them, love2d has been using x,y as the upper left corner of the image.
We would like to change this so that x,y refer to the center of the image.
We will place the offsets in two variables in CHARACTER
class as ox
, and oy
.
We will set the value to these variables using the love2d functions getHeight()
and getWidth()
We will now update our draw function to accommodate these offsets.
As you can see from the image below, .draw()
now has a bunch of new arguments.
If you want to know what each of them are, please see the Love2d reference
You will now see that the program draws the player character a bit off screen.
You can fix this by adjusting the x and y values you use when creating him.
You will also notice that our bounds checking is slightly off now. Don't worry, we will soon fix this.
Improving movement
The first thing we are going to do is break up our current keypress function into smaller functions.
We are going to create one function per direction: up,down,left,and right.
Since no other object in the game uses these functions, we make them belong to PLAYER
class.
We place these functions in our Characters.lua file.
We can now clean up our keypress function.
We can simply call the corresponding function to move player that direction.
If you test it out, this now works just as before, but much better organized.
But wouldn't it be nice to be able to move around by holding down the key?
I think it would, so we can replace our love.keypress()
by a new function.
We make our keyboardInput()
and use the function love.keyboard.isDown("key")
.
After making keyboardInput
we call it at the end of love.update()
You should now be able to move the player character around by holding down a movement key.
The speed at which he moves is governed by the speed
variable inside of CHARACTER
class.
We next need to slightly adjust our enemy move function to account for the offsets.
Once that is done you should now see the game working as it should.
Collision detection
We are now going to add the ability to collide into an enemy.
When player character collides into an enemy, the enemy is removed from the game.
We need to check for four conditions to account for a collision:
- player's head is above the enemy's feet.
- The player's feet are below the enemy's head.
- Player's left side is to the left of the enemy's right side.
- Player's right side is to the right of the enemy's left side.
If you walk through these four steps, maybe even draw out a diagram, you will see that it accounts for a collision between the player and enemy.
We will add a function in Character.lua to account for this.
We make this function belong to ENEMY
class.
You can see that it checks for a collision and returns true if player and enemy collide.
Now that we can check for a collision between a player and a single enemy, we need to expand this for all enemies.
We accomplish this by writing a function in main.lua to loop through ENEMY_LIST
.
This functions checks for collision with the player on each and every enemy in ENEMY_LIST
.
If a collision is detected with an enemy, we remove that enemy object from ENEMY_LIST
.
Once we have this function, we need to call it at the bottom of our love.update()
function.
With everything in place we should now be able to touch an enemy and have them removed from the game.
Final remarks
Thank you for checking our part 4 of my series on Love2d.
Continue on to part 5
As always, you can get the complete code from github.
No comments:
Post a Comment