Game Creation: The Basics

Started by Jubal, March 18, 2012, 04:36:16 PM

Previous topic - Next topic

Jubal

Okay, this is going to focus on one of the things people find hardest to grapple with when starting programming. It's not usually syntax, or the concepts behind the syntax. It's how that syntax can be used to fit together into a game. I'll use Python for this tutorial, but the concepts are more important than the syntax and this can be used for many languages. I'm basically going to examine how you can use inputs to make a really simple text-based RPG.

WHILE AND IF - Using them together
The while loop is the most important thing in making a simple shell-based python program run properly. You can make something that runs once and spews out some results without it, but for a program that's going to keep running you need to, well, keep running it. You stop running it, simply put, by getting out of the while loop again. The if statement, when used along with this, is the other basic part of your kit. If lets you react to user inputs or random inputs, meaning it's a very powerful tool for resolving actions. So for a program that just turns itself off:

a = 1
while a == 1:
     action = input('What do you want to do?')
     if action == 'quit':
          a = 0

The above code will keep prompting you for something to do until you tell it to quit, at which point the program closes. Now you really have the majority of what you need - you can get the player to type commands, and use an if statement to respond to them. Using the print command and a couple of variables we can quite easily get:

location = 1
a = 1
while a == 1:
     action = input('What do you want to do?')
     if action == 'quit':
          a = 0
     if location == 1:
          if action == 'look':
               print ('You are in a forest. There is a path going south.')
          elif action == 'go south':
               print ('You go south.')
               location = 2
          else:
               print ('You can't do that.')
     elif location == 2:
          if action == 'look':
               print ('You are in a forest. There is a path going north.')
          elif action == 'go north':
               print ('You go north.')
               location = 1
          else:
               print ('You can't do that.')

And we suddenly have something that looks like a game. The if, elif, and else statements and their order are all very important here. Because we put the 'quit' statement outside the location checkers, it means we can use it anywhere. The ones inside the location checkers are specific to that location; only if the location variable is equal to that value will the game check through the list of possible actions that can be done there. The else statement is useful because it's a catch-all - whatever the user has put, it gives them some sort of response.

Now, there are a few more tools of the trade you need...

RANDOM NUMBERS
All good games are built around random number generators. Of course, random number generators are not wholly random; they have predefined list of numbers which have been tested beforehand to ensure they have no underlying bias. This is in fact better than "really random" numbers, which could potentially give a massively long string of incredibly improbable results and wreck your statistically worked out game idea. Anyway, that little point aside, the fundamental fact is that totally predictable games are duller than dull.

For python, you need to import a random number generating module. Just put at the top of your code:
import random
And you have access to the function. If you forget to do this, your program will crash out when you try and generate a random number.

To do that, just do this:
number = random.random()
Where number is a variable. The random.random() function will create a number between 0 and 1. To get it random up to a bigger number, just multiply it up. Using the int(variable) function will round your number down to the nearest integer, for when you only want to deal with whole numbers.

VARIABLE TYPES
Just as some quick notes: to turn an integer into a string, use string = str(variable). This will let you use your variables in print commands.
Consider making your initial input all uppercase with action = str.upper(action). This means your inputs won't be case sensitive (though you need to remember that all the if statements will then have to check for inputs in all caps).

Those basics should let you start work on making a very simple game.

THINGS TO TRY
- Make a simple program where you can walk to different rooms in a house and look at the things there - consider adding NPCs who give information if you use a talk command, just like the look command.
- Make an oracle that you can ask yes/no questions to with a randomised answer
- Make a simple guessing game where the computer picks a random number, the player guesses a number, and depending on the computer's random number the player wins/loses money and gets a readout of their current total.
The duke, the wanderer, the philosopher, the mariner, the warrior, the strategist, the storyteller, the wizard, the wayfarer...

Othko97

Thanks for this, I've been trying to think of some ideas for programs for a while now; I've also wanted to make a text based game for a while, but didn't quite know how to fit it together.  ;D
I am Othko, He who fell from the highest of places, Lord of That Bit Between High Places and Low Places Through Which One Falls In Transit Between them!


Jubal

The duke, the wanderer, the philosopher, the mariner, the warrior, the strategist, the storyteller, the wizard, the wayfarer...