In this tutorial, we'll cover loading an image and rendering it onto the screen. If you aren't familiar with the Squirrel programming language, you can check out this tutorial for a primer.

Create a new text file and name it helloworld.nut. Make sure you give it the right extension so that it works with XYG. Next, associate the file with XYG using your system's "open with" option. It is recommended you install Notepad++ or Atom to edit your scripts with, as these two editors come with a convenient option to edit by right clicking the file itself.

In the same folder of your source file, create a new folder named res. It's a good idea to keep your game's resources in a separate folder for neatness and organization. Save this image to that folder:

Midi Sprite

This is Midi. He'll be helping us with our lessons for now.

Open up helloworld.nut and type in the following code:

local midi = newSprite("res/sprMidiO.png", 22, 22, 1, 1, 11, 16, 0);

	if(keyPress(k_escape)) break;
	drawSprite(midi, 0, 160, 120);

Let's look at this one line at a time. On the first line, we load Midi's sprite and assign it to a variable called midi. What's happening here is we're telling a new variable, declared using the word local, to store the result of whatever expression is on the right side of the = sign. In this case, it is the number returned by newSprite that tells us what slot XYG is storing the sprite in when it loads it. We'll go more in depth with this function later, but for now, use the numbers provided for the rest of the arguments, which are the values you put between the parentheses after the function's name.

Next, we make a while loop. This kind of loop will run over and over again until the expression in its paretheses becomes false. Since true is an unconditional statement, it will never be false. This creates an infinite loop.

We are able to escape this infinite loop by having a break check. This is done by using an if statement, which works by evaluating the expression in its parentheses, and if that expression returns true or non-zero, then the statement immediately following it will be executed. In this case, it checks for when the escape key has been pressed, and when it has been pressed, it calls break, which tells the game to leave the current loop.

Now for the meat of the sandwich, a call to drawSprite. We don't need to remember the number of the sprite we loaded ourselves because midi stores that for us. We can pass that as the first argument instead. Zero is the frame number, and the last two numbers are coordinates on the screen.

Lastly, the bottom slice of bread, which is the most important part of all, is the call to update. Without this function, your game would freeze until you ended the process. update does two very important things: it redraws the screen so that everything you've done will show up, and it refreshes the input buffer, which is what functions like keyPress need to work. It's simply the most important function you will ever use in XYG.

Try running the nut. If all goes well, you will see Midi standing in an endless void, staring at you, unmoving, not the slightest bit bothered that he alone exists in an otherwise empty universe.

Midi in the Void

Don't worry; we'll be fixing that later. For now, be happy that you've completed your very first program in XYG!

Search results