• # Make a Shooter in Lua/Love2D - Setup and Player Movement

General and Gameplay Programming

Hello! This will be the first in a series of posts on developing a simple shoot 'em up game using the Lua programming language and the Love2D framework. I decided to make this game as a bit of a break from another game project that I've been working on for a while. I chose Lua and Love2D due to their focus on simplicity and efficiency. I will be writing these posts as I learn.

## Getting started

In order to run the game we are making, you need to install the Love2D framework from https://love2d.org. You might also want to use an IDE or a plugin for your favorite text editor to get things such as autocomplete and syntax checking. Here is a list of such [tools](http://www.gamefromscratch.com/post/2016/01/25/Editors-and-IDEs-for-Lua-and-Love-Development.aspx). Personally I went with the Atom Editor plugin, since Atom is currently my favorite text editor.

## Let's write some code!

After we have our tools set up, the first thing we'll do is simply create a folder containing a file named main.lua. This is where the Love2D executable will look for the game code's entry point. This is where we will put all our code for now.

We will start by writing three very important functions:

• love.draw()
• love.update()

These functions are called by the Love2D engine and are required to get any of our code to run at all. The load() function is called exactly once, when the game is started. The draw() function is called continuously once the game is running and is where any graphics code should be placed. The update() function is also called continuously and is where the state of the game should be updated.

function love.load()
xPos = 0
yPos = 0
playerWidth = 64
playerHeight = 64
playerSpeed = 200
submarineImage = love.graphics.newImage("resources/images/submarine.png")
end

The load() function is the perfect place for all kinds of initialization code. We start by defining some integer values for the position, size and speed of the player. We then load an image located on the hard drive - I'm using [this little pixel art submarine I drew](https://github.com/jeansberg/GreatDeep/blob/master/resources/images/submarine.png). This image will represent the player in our game. Note that Lua is a dynamically typed language so we don't need to declare any types for these variables.

### love.draw()

function love.draw()
love.graphics.draw(submarineImage, xPos, yPos, 0, 2, 2)
end

For now, our draw() function will contain a single line of code to draw the image at the player's current position. Normally you would only need the image, x-position and y-position parameters. The three last parameters are there because I wanted to make my tiny 32x32 pixel image a little bit bigger. The 0 means that the image will not be rotated and the 2s mean that the image's width and height are doubled.

### love.update()

function love.update(dt)
downUp = love.keyboard.isDown("down") or love.keyboard.isDown("up")
leftRight = love.keyboard.isDown("left") or love.keyboard.isDown("right")

speed = playerSpeed
if(downUp and leftRight) then
speed = speed / math.sqrt(2)
end

if love.keyboard.isDown("down") and yPos<love.graphics.getHeight()-playerHeight then
yPos = yPos + dt * speed
elseif love.keyboard.isDown("up") and yPos>0 then
yPos = yPos - dt * speed
end

if love.keyboard.isDown("right") and xPos<love.graphics.getWidth()-playerWidth then
xPos = xPos + dt * speed
elseif love.keyboard.isDown("left") and xPos>0 then
xPos = xPos - dt * speed
end
end

The update() function will contain code for listening to keyboard input and moving the player around. You probably noticed that this function takes a parameter called dt. This is passed to the function by the Love2D engine and indicates how much time has passed since the last update() call. This is used as a multiplier to determine how much our variables should change. Without this adjustment, the speed of our game would directly depend on the speed of the computer running it!

#### Input handling

We begin by using the built in keyboard.isDown() function to check if any arrow keys are currently being pressed. We store this information in two variables. One will indicate whether the down or up key is being pressed. The other one will do the same for the left and right keys. If both of these values are true, it means the player will be moving diagonally. In that case we need to divide the speed value with the square root of two before we apply it to the x- and y-positions. Otherwise diagonal movement would be much faster than horizontal or vertical movement.

#### Movement constraints

We then check whether the down or up key is being pressed again (not the most optimal code, but sufficient for a tutorial!). If the down key is being pressed, we increment the y-position, since the y-coordinates start at 0 at the top of the screen. We use dt as a multiplier as mentioned above. It's not enough to merely check the player input however. We don't want the submarine to disappear below the screen, so we make sure the yPos variable is never greater than the height of the screen minus the height of the player (the player width and height are 64 pixels since I doubled the width and height of the image when drawing it). Similarly, we want to make sure that the submarine doesn't go above the screen so we check that yPos is greater than zero. The code for horizontal movement is very similar.

## Conclusion

You can now use love.exe which comes with the Love2D install to run the game! Just make sure you give it the path to the folder containing your main.lua file as a parameter. You should be able to control the submarine with the arrow keys on your keyboard. If you're on a laptop without arrow keys, simply change the "down", "up", "left" and "right" parameters to some letter keys.

In the following post we will take a look at some shooting, so stay tuned!

Report Article

## User Feedback

There are no comments to display.

## Create an account

Register a new account

• 0
• 0
• 25
• 0
• 1

• 13
• 26
• 10
• 11
• 44
• ### Similar Content

• Hi everyone,
Me and my friend are working on an 2d action shooting game.
You can check techno demo of core mechanic here: https://spidamoo.itch.io/bunny
So, as you can see, we really need an artist here. (And probably for other projects after)

• I am curious, if anyone would be interested in an RPG Adventure in a visual novel art style?
I loved Doki Doki and if I could create something in an RPG element, that would be THE BEST.
I am a complete noob however, that is the thing. Like I just started adventuring into coding two weeks ago. I love it so far.
I think I may be addicted. oof. Which is why I want to create something I have that itch. lol.
Basically if anyone who wants to pitch in for free, or not I'd be glad to include them in the credits section.
Also, I'd love to get the community involved in this, to create more fun RPG-esk things if that makes sense.
Where would I go for that?
• By TheMode
Before starting: I'm looking for a Java developer who want to help me improving my game engine and then, create a game that I will describe
My goal is to create a "fight arena" multiplayer game similar to xblaster (only how they managed arena, I do not want robot stuff)
For people who do not know this game, let me explain how I'm inspired by it
You log in the game, you can enter an arena at any time, in the arena, there are 4 portals where you can go and enter another arena, there can be a maximum of 4players in the same arena, they have to fight each other in order to get money to improve their characters.
I won't describe it any longer, I've much more ideas about the game.
I already done the server architecture, I have a Game Engine (the client side), but there are still things to do on it, that's why I'm looking for another developer to help me if the game also look interesting for you
Here the version of the engine:
https://github.com/TheMode911/ProneusEngineV2/blob/master/src/main/java/fr/proneus/engine/demo/DemoState.java
Discord: TheMode#3487

• In MonoGame I'm writing a shader for lighting and shadows in a 2D Platformer.

A shadow will be drawn for each character for each light that hits said character. Because shadows from different lights can overlap the shadows are drawn to a texture where each pixel is a bitfield where each bit tells you if the pixel was reflected by a given light. In the lighting shader for each light, it only applies light if the bit for that light is not set at the given pixel.

In order to not make for example 40 draw calls to draw 40 shadows if 40 lights overlapped a character, I batch shadows together into a VertexBuffer with the data specifying which light created the given shadow. In the shadow shader, it samples the render target I am drawing the shadows to and sets its own bit. My problem is that the changes from the previous shadows in the same batch aren't applied to the render target until after the draw call has completed. This results in the bitfield getting overwritten by shadows from other lights.

If I could somehow sample the back buffer this wouldn't be a problem. Is there any way I can fix this without making a draw call for each shadow?

• Hey Folks!!!

We have a great news for you, we just released a Demo that is our last version of the game before the Early Access on Steam! This is Version 0.9d. The game had several modifications, We hope you like it!
http://playriseofages.com/demo-0-9d/

×