Make Your First Castle Game

By The Castle Team // 1-30-2019
In this short tutorial, we'll get oriented with Castle's basic workflow by creating a new project, drawing a circle, printing some logs, and moving the circle with the arrow keys. You'll need a text editor and a few minutes.

Learning to LÖVE

Castle uses a programming language called Lua, and a framework called LÖVE. If you're already familiar with LÖVE, you're in luck! Anything you can normally do with LÖVE will work in Castle.
If you aren't familiar with LÖVE, we'll go over some of the basics here. You might find the LÖVE API reference page useful to keep open while you work.

Creating a Castle Project

  1. If you don’t have it yet, download and open Castle.
  2. Inside Castle, click the button to create a New Project.
  3. You'll see some different options for starter templates on which to base your new project. For the purpose of this tutorial, choose blank.Creating a new Castle project
  4. When prompted, name your project. You can name it anything you want, but the rest of this tutorial assumes your project is called My Project.
  5. Castle will download some starter files for you. After it's done, go ahead and click the button to Open your new project.

Getting Oriented in Castle

Now that you've opened your new project, Castle should look something like this:
Most of the screen contains your new blank game. Since the blank template doesn't contain much graphics code, it's mostly just a black void right now, with some white text hovering in the middle.
The Logs View on the bottom of the screen is where you'll see development logs while you work.

Editing your Project

Go have a look at the directory where you created your project. Your project directory should contain two files: my-project.castle and main.lua.
Open up main.lua in a text editor of your choice. (Note: See this list if you'd like some recommendations for a text editor.)
The file called my-project.castle doesn't contain any code. Instead, it contains a few lines of configuration which tell Castle how to run your game. For example, you might notice it contains the line main: main.lua. You are welcome to change anything inside my-project.castle, but we won't cover it in depth in this tutorial. Instead, we'll focus solely on main.lua.

Drawing a Circle and Reloading

Let's draw a circle on screen. Inside main.lua, find the two lines that say love.graphics.print(...). Delete them and replace them with this:
love.graphics.setColor(0.4, 0.4, 1.0, 1.0)
love.graphics.circle("fill", 100, 100, 20, 32)
If you'd like to change the circle to be red, try replacing the first line with this:
love.graphics.setColor(1.0, 0.4, 0.4, 1.0)
Save your file and then click the Reload button in Castle. You can also use Cmd+R (macOS) or Ctrl+R (Windows) to reload.

Printing Logs

If you’d like to debug using logs, Castle has a built-in logger. You can show and hide the Logs View in Castle by pressing Cmd+J (Mac) or Ctrl+J (Windows). If you can't already see the Logs View, try pressing these keys now.
To print a log when the up arrow key is pressed, find the love.update(dt) method at the bottom of main.lua and change it to this:
function love.update(dt)
  if love.keyboard.isDown("up") then
    print("up arrow pressed!")
  end
end
Save main.lua, then Reload in Castle. Now try pressing the up arrow on your keyboard. You should see some logs appear.

Moving the Circle

Let's move the circle around with the arrow keys.
1. At the very top of main.lua, add this:
local playerPos = {
  x = 100,
  y = 100
}
2. Find the line with love.graphics.circle(...) and change it to this:
love.graphics.circle("fill", playerPos.x, playerPos.y, 20, 32)
3. Change the contents of love.update(dt) to this:
if love.keyboard.isDown("right") then 
    playerPos.x = playerPos.x + 256 * dt
  end
  if love.keyboard.isDown("left") then
    playerPos.x = playerPos.x - 256 * dt
  end
  if love.keyboard.isDown("up") then
    playerPos.y = playerPos.y - 256 * dt
  end
  if love.keyboard.isDown("down") then
    playerPos.y = playerPos.y + 256 * dt
  end
Save your revised main.lua, then Reload in Castle. Now try moving the circle around with the arrow keys.

That's it for now!

You should now have a simple program where you can move a circle with the arrow keys. What happens next is up to you!

Next Steps