Thursday, September 24, 2020

Simple Platformer - part 5

Getting started with Love2d - part 5

Introduction

Welcome to part 5 of my Love2d tutorial. In this part we are going to add tiles to the game.

Adding tiles

The first thing we need to do is to create three tile pieces. For this i used GIMP and made three simple squares.
Next create a directory called images and place the three tile pieces there.

Tile 1
Tile 2
Tile 3

The next thing to do is to create a file Tiles.lua.
Inside this file, the first thing to do is to create a tile map.
This is a 2d array using numbers to represent location of the tiles on the map

  • 0 represents empty space
  • 1 represents tile 1
  • 2 is tile 2
  • 3 is tile 3
Tile Map

For the next part, the steps are:

  1. Create an Icon object to hold icons. it should store:
    • icon for the tile created with love.graphics.newImage()
    • width of icon using getWidth()
    • height of icon. Use the function getHeight()
  2. Create Icon list.This is a list to hold the Icon objects.
  3. Create a function which populates the icon list with the three icons tile1, tile2, and tile3.
  4. A function to loop through the tile map and print each tile.
    • We use a nested for loop to loop through the tile map.
    • If the tile isn't zero then we print the corresponding icon from icon_list using the number in Tile_Map.
Tile objects and Tile list

Modifying main.lua

We now need to make a few changes to main. We need to call our new code and we clean up a few things we previously added.

New stuff to add

  • Icon_List:populate we call the populate function in our love.load() function.
  • drawTileMap() We add this function to love.draw() in order to draw the tiles on each update.

Cleaning up older stuff

We now clean up some of our older code to improve our main.lua file.

  • we change our player construct function to use an image icon using love.graphics.newImage().
  • we change our enemy construct function to use an image icon directly.
  • we change the values for WIDTH and HEIGHT to accommodate our new tile map.
updated main.lua

Updating Characters.lua

We now make a few changes to our Characters.lua file.

  • o.icon = icon we set the objects icon to the icon we pass to it. No more calling love.graphics.newImage()
  • o.y = y - o.oy we adjust the y position to account for the y offset.
Updated Characters.lua

No comments:

Post a Comment