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 Castle Project.
  3. Pick a directory when prompted. Castle will create some files in this directory.

Getting Oriented

At this point, your project directory should contain two files: project.castle and main.lua.
  1. Make sure project.castle is running inside Castle. Castle opened it automatically when you created the project, so you’re probably already good to go.
  2. 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 Logs View on the bottom of the screen is where you'll see development logs while you work.
The file called 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 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 Delete them and replace them with this:, 0.4, 1.0, 1.0)"fill", 100, 100, 20, 32)
If you'd like to change the circle to be red, try replacing the first line with this:, 0.4, 0.4, 1.0)
Save your file, and then, with the Castle’s window focused, press Cmd+R on Mac, or Ctrl+R on Windows to reload and see the changes.

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!")
Save main.lua, Reload in Castle (Cmd or Ctrl + R), then 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 and change it to this:"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
  if love.keyboard.isDown("left") then
    playerPos.x = playerPos.x - 256 * dt
  if love.keyboard.isDown("up") then
    playerPos.y = playerPos.y - 256 * dt
  if love.keyboard.isDown("down") then
    playerPos.y = playerPos.y + 256 * dt
Save your revised main.lua, Reload in Castle (Cmd or Ctrl + R), then 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