Making your first game

2020-05-29T14:01:00.169Z 14 minutes

So, you've learnt programming? You've made some easy programs? Maybe even made a few bigger projects? It's about time for you guys to start making games. If you don't know programming, I encourage you to use google to look for programming tutorials to learn a beginner friendly language like Python. After you're done with that, come here again.

This tutorial is aimed at people who are `okay` at programming in C and are interested in making games. It's also aimed at people who are bored of their life and want to just read something random which involves an awesome orange demon.

My code is available in my github repository:  github.com/maheshbansod/getoutofhell
It has more features than what's covered here but you can refer to it anytime to get an idea of what to do.

If you wish to use a language other than C, I encourage you to do so. You'll just have to google a little bit if you're a beginner at that language.

So, let's get started.

We'll be creating a simple text-based game in this tutorial.

You've got to ask yourself what's a game though. You don't have to be technical about it. Think about what a game is for you. Is it something fun? something that excites you? something that fills the void in your life by giving a fake sense of victory? Whatever your definition of a game is, keep it in mind and add that parts of your definition in the game you're making.

A game keeps repeating these three things:

  1. Set/update the internal state.
  2. Show the user something based on some internal state/data
  3. Handle user's reaction and manipulate the internal data.

This is called a game loop. It's what drives your game.

We will start by creating small pieces of working code and gradually add to it consequently creating a complete game.

The game we will be making is a simple guessing game: The user has to guess the number stored in memory with the least amount of tries and our program will tell whether the number user guessed is greater or lesser than the number in memory.

Let's start with the story. We are going to show the user some text(since this is a text based game) conveying the story of the game. Every game needs a story(well, good games do). Try to make it fun and inviting, yet thrilling and dangerous. I encourage you to create your own story but it's fine if you copy the one below too.
So, create a file called...idk.. call it main.c or something i don't care. Make a main function and in the main function write a printf statement which tells your story:

printf("Due to a series of unfortunate events, your spouse
screamed at you and told you to go to hell. You understood
that they didn't mean it literally but you took the chance 
and set off on your journey to go to hell. You somehow already 
know the way.\nOn the entrance to hell stands a mysterious 
orangish figure..\nYou move closer and realise that you know the 
guy. It's Light the friendly demon. \"Hey buddy,\" you call out. 
You know that Light would never harm you - he's even got friendly 
in his name. He is kinda a jerk sometimes though and would probably
 ask you to solve a riddle or something to let you pass through.
\nYou move closer\n\"Dude, I'm not your buddy. You didn't accept 
my cookies the last time we met. I'm gonna kill you.\"\nYou - a 
brave adventurer - are not surprised at this turn of events. Plus 
you're confident that Light won't kill you.\nSo you approach him 
with your mighty sword and machine gun.\nLight stabs you with his 
claws and tears apart your intestines till you die.\nLight leaves 
the scene.\nYour spirit moves out of your body and reaches Hell.
\nCongratulations! you accomplished your goal!\nHell is just a 
long tunnel. You spend an eternity in Hell but eventually get 
bored. You walk forward with your spirit legs and feel more warmth.
 You realise that you were only sitting in a random tunnel this 
whole time(you still dead tho). Light appears out of nowhere and 
tells you that to return back to life you have to possess a body 
with a compatible ID. He tells you that an ID compatible with you 
is somewhere between 1 and 100. You'll feel either too hot or too 
cold for different IDs based on whether that ID is greater than 
the compatible ID or lesser than it. You think about it. Yep, it 
all makes sense for you.\n");

Now that we've set the stage. The next thing to do is to ask the user for some input - i.e. the number that the user will guess. It's better if you stick with your story and provide creative messages to the user.

printf("What ID to possess? ");
scanf("%d", &id);

Good! Don't forget to declare the variable id. Now that we've read user input it's time to handle it.

if(id > guess) {
  printf("You possess body %d and feel yourself burn. This is fine, you say and get a part of your spirit burnt off. You leave immediately.\n", id);
} else if(id < guess) {
  printf("You possess body %d and feel a chill in your head. You feel your spirit being frozen slowly. This is not a compatible body. You quickly move out.\n", id);
} else { //id=guess
  printf("Ahh.. your heart is cold. You feel your stomach burn. You're alive.\n You're alive.\nYou breathe a sigh of relief and get back to your spouse. They scream at you a lot and throw something metallic and hard at you. You jump and easily block it with your head. They say they missed you. You tell them you missed them too. They hug you and you hug back. It's the tightest hug you've ever recieved.\nYour bones shatter and you die.\n");
}

In the above code, we check if the user's number was greater than, lesser than or equal to the number stored in the memory and respond accordingly. The guess variable should be initialised to a random value from 0 to 100.

guess = rand()%100;

Include the file stdlib.h and compile and run the code.

If your run this code multiple times, you'll notice that the number in guess is always the same number. Let's change that. Put the following code before you've used the rand() function:

srand(time(NULL));

The function to generate a random number(rand()) uses a special variable called seed to generate the random number. If two people have the same seed, then they will generate the same sequence of random numbers. The function srand() allows you to set a seed. We have set the seed to the number of seconds passed since 00:00:00UTC Jan 1, 1970 using the time() function. Also, add the header file time.h for the time() function. Now every time the code is executed, the seed will be different and hence you'll get a different random number in guess.

So, at this point, you get a game with only one try to guess the correct ID. That makes winning purely on the basis of luck. We don't really mind luck, it's just the "purely" part that we do mind. We need to make it so that the user can use the info provided(hot or cold) and make another guess and keep going till the user guesses correctly. So let's put the relevant parts in a loop then.

while(1) {
  printf("What ID to possess? ");
  scanf("%d", &id);
  if(id > guess) {
    printf("You possess body %d and feel yourself burn. This is fine, you say and get a part of your spirit burnt off. You leave immediately.", id);
  } else if(id < guess) {
    printf("You possess body %d and feel a chill in your head. You feel your spirit being frozen slowly. This is not a compatible body. You quickly move out.\\n", id);
  } else {
    printf("Ahh.. your heart is cold. You feel your stomach burn. You're alive.\\n You're alive.\\nYou breathe a sigh of relief and get back to your spouse. They scream at you a lot and throw something metallic and hard at you. You jump and easily block it with your head. They say they missed you. You tell them you missed them too. They hug you and you hug back. It's the tightest hug you've ever recieved.\\nYour bones shatter and you die.\\n");
    break;
  }
}

Good! now that you've made an infinite loop, the user can play this game forever too! The game ends when the user makes the correct guess.

Now, let's see what else we can add to this. Woah! My friend Light just appeared out of nowhere and said that I should make it a little harder by setting a limit on how many times the user can guess. I told Light to shut up and mind his own business. I'm dead now.

I'm Light. So let's continue. We'll add a variable called turn which stores the number of turns the user got to choose the ID. This provides us with something to give a good score to the user for guessing early.

Declare this variable and initialize it to 0 and increment it by 1 in the loop. Then, check whether the turn no. is, say 7, 'cuz your spirit is made up of 7 spirit organs(I'm not making this up, it's real and don't look it up, just trust Light.) If it's 7 or more, then your spirit is gone and you've already lost. This is where your path ends so surrend your weapons and, wait, someone is stabbing me as I'm typing this. Oh no! How is he still alive? I thought you died. I gotta go guys. Make sure you remember to live. And don't forget m-.

Hi! Yes, so if you did everything so far, here's what your code would look like:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#define SPIRITORGANS_CNT 7
#define UPPERLIMIT 100

int main() {

  int id, guess, turn;
  
  srand(time(NULL));
  
  guess=rand()%UPPERLIMIT;
  turn = 0;
  
  //the story
  printf("Due to a series of unfortunate events, your spouse screamed at you and told you to go to hell. You understood that they didn't mean it literally but you took the chance and set off on your journey to go to hell. You somehow already know the way.\nOn the entrance to hell stands a mysterious orangish figure..\nYou move closer and realise that you know the guy. It's Light the friendly demon. \"Hey buddy,\" you call out. You know that Light would never harm you - he's even got friendly in his name. He is kinda a jerk sometimes though and would probably ask you to solve a riddle or something to let you pass through.\nYou move closer\n\"Dude, I'm not your buddy. You didn't accept my cookies the last time we met. I'm gonna kill you.\"\nYou - a brave adventurer - are not surprised at this turn of events. Plus you're confident that Light won't kill you.\nSo you approach him with your mighty sword and machine gun.\nLight stabs you with his claws and tears apart your intestines till you die.\nLight leaves the scene.\nYour spirit moves out of your body and reaches Hell. Congratulations! you accomplished your goal!\nHell is just a long tunnel. You spend an eternity in Hell but eventually get bored. You walk forward with your spirit legs and feel more warmth. You realise that you were only sitting in a random tunnel this whole time(you still dead tho). Light appears out of nowhere and tells you that to return back to life you have to possess a body with a compatible ID. He tells you that an ID compatible with you is somewhere between 1 and 100. You'll feel either too hot or too cold for different IDs based on whether that ID is greater than the compatible ID or lesser than it. You think about it. Yep, it all makes sense for you.\n");
  
  //the game loop
  while(1) {
    printf("\nWhat ID to possess? ", SPIRITORGANS\_CNT - turn);
    scanf("%d", &id);
    turn++;
    if(turn == SPIRITORGANS\_CNT) break;
    if(id > guess) {
      printf("You possess body %d and feel yourself burn. This is fine, you say and get a part of your spirit burnt off. You leave immediately.\n", id);
    } else if(id < guess) {
      printf("You possess body %d and feel a chill in your head. You feel your spirit being frozen slowly. This is not a compatible body. You quickly move out.\n", id);
    } else { //id=guess
      printf("Ahh.. your heart is cold. You feel your stomach burn. You're alive.\n You're alive.\nYou breathe a sigh of relief and get back to your spouse. They scream at you a lot and throw something metallic and hard at you. You jump and easily block it with your head. They say they missed you. You tell them you missed them too. They hug you and you hug back. It's the tightest hug you've ever recieved.\nYour bones shatter and you die.\nYou are a spirit with %d spirit organs. :)\n", SPIRITORGANS\_CNT - turn);
      break;
    }
  }
  
  if(turn >= 7) {
    printf("That is the perfect answer! You're so smart!\n.\n.\njk you ded\nWhat you looking at? You're gone now.\nLight collects your burnt and frozen organs and dissapears.\n");
  }
  
  return 0;
}

Hey, congratulations, you made it this far!

If you've never made a game in your life, this is a great achievement. Especially considering you had to go through all this and tolerate my horrid story and writing. Pat yourself on the back, go ahead.

So, there's one thing left to do. And I'm leaving it to you. You can ask the user if they want to play the game again and start the game again if they want to. They'd probably want to play the game again. Why wouldn't they? You are awesome and have made a great game. There's no reason for someone to not play it again.
I'll write about how to do this(without posting the code) at the end of this post.

You can add a lot more to this game. Use your creativity and think of ideas on how you'd improve this game.

I encourage the reader who followed the tutorial to make the code more modular. Seperate the code into different functions and similar functions in different files. Add comments to your code - preferably while coding. Learn about Makefiles. Create a github account if you don't have one and upload your first game on your github profile. Share the link of your repo on twitter and mention me(@BansodMahesh) for a <3 on your tweet.

So, here goes. Stop reading if you wanna do this yourself. Here's how you'd ask the user whether they want to play again.

Wrap your code within a loop(maybe a do-while loop?) and at the end just ask the user if they want to play again. The condition of the loop(whether the loop continues or breaks) depends on the response of the user.

I've put complete code in this tutorial in my GitHub repo:
github.com/maheshbansod/getoutofhell
You can refer to it if you face any difficulties.

Thanks for reading! I tried to keep it simple. Let me know if you like it and I might post more tutorials.

This article has been copied mostly as is from my original wordpress blog.

END
Copy