Skip to content
Sign upLog in
← Back to Community

How to make a basic Text/Story Game in Python!

Profile icon
TsunamiOrSumth

How to make a basic text/story game in Python!

Introduction:

Text/story games are a great project to start with when you are first learning how to code. This tutorial will cover how to make a basic one in Python with scrolling text, coloured text, sleeping, options, and clearing the console.

Things to import:

import os
from os import system -- this is to clear the canvas
import time
from time import sleep -- for sleeping 😴
import sys -- for scrolling text
import random -- depending on your game, you might need random numbers.

The scrolling text function:

This will make the text scroll and space out the game better.
Make sure you indent it correctly!

def scrollTxt(text): for char in text: sys.stdout.write(char) sys.stdout.flush() time.sleep(0.1)

You can change sleep(0.1) to a number that suits your game.

When you have the function down, make sure to use scrollTxt() instead of print()!

Colors to get:

Colors are a great addition to a text game, it will make the text easier to read and less boring.

Here are some basic starter colors:

black = "\033[0;30m" purple = "\033[0;35m" blue = "\033[0;34m" green = "\033[0;32m" red = "\033[0;31m" yellow = "\033[0;33m" white = "\033[0;37m"

To use colors:

print(blue) scrollTxt("Im blue (daba dee ba da die)")

It will print "Im blue (daba dee ba da die)" except its blue!

Spacing out the game using clear and sleep:

Alright! Now you know how to print scrolling and coloured text. Now, we should space out the game, so it is easier to read and the console isnt messy.

First, sleeping.

To make the output sleep, use time.sleep(time).

For example:

print(white) scrollTxt("Joe: Pog") time.sleep(1) scrolltxt("Joette: Pog")

This will make joe say pog, then 1 second after joe finishes saying pog, joette will say pog.

Second, clearing.

To clear the console, use system('clear').

For example:

print(white) scrollTxt("Last time, on Epic Gamer Story (Pog): ") scrollTxt("Poopeepie: Pog") time.sleep(1) system('clear')

Options:

Options are pretty simple. You just use if and elif.

For example:

x = input(What will you do? Press 1 to eat cereal, Press 2 to eat toast.) if x == "1": scrollTxt("You ate cereal.") elif x == "2": scrollTxt("There was a bomb inside the toaster. You died.") else: scrollTxt("bruh actually answer something valid")

Text Game Example!

Ok! Now you should know how to make a basic text/story game. If you are still confused, ill put a short one with all the things I mentioned below.

import os from os import system import time from time import sleep import sys black = "\033[0;30m" purple = "\033[0;35m" blue = "\033[0;34m" green = "\033[0;32m" red = "\033[0;31m" yellow = "\033[0;33m" white = "\033[0;37m" def scrollTxt(text): for char in text: sys.stdout.write(char) sys.stdout.flush() time.sleep(0.1) print(red) scrollTxt("Previously on Pog Show Pog: \n") time.sleep(1) print(white) scrollTxt("Joe: Pog \n") time.sleep(3) system('clear') scrollTxt("You wake up. What do you do? \n") x = input("1. Go back to sleep, 2. Say pog") if x == "1": scrollTxt("You went back to sleep.") elif x == "2": print(red) scrollTxt("You died lol") else: scrollTxt("Invalid argument")

Thats it! Feel free to comment a question if you have one!

Voters
Profile icon
SamuelWang30
Profile icon
SamMaurer
Profile icon
PersonaOS
Profile icon
Borkify
Profile icon
Pcat1
Profile icon
datboicatguy
Profile icon
TKWMO
Profile icon
PlsDont
Profile icon
JacobSedgebeer
Profile icon
Dofni
Comments
hotnewtop
Profile icon
SawyerRuch

instead of os.system('clear'), import replit and you can do replit.clear()

Profile icon
SandwichBlamwich

hey, I found that using

def clear(): os.system('clear') def fprint(string): for letter in string: sys.stdout.write(letter) sys.stdout.flush() time.sleep(.09) print()

works better for typing text

Profile icon
CyberDaDev

heheh i love looking at old stuf even tho this is like 3 months old

Profile icon
PlsDont

dang this thing is cool I like it although i added like a bajillion things

Profile icon
JonahGress

How does the code know to print the colored text instead of printing the color's value?

Edit: I looked into it, I think it just gives all following text the color.

Profile icon
TsunamiOrSumth

@JonahGress
dont ask me, i didnt make python

Profile icon
JacobSedgebeer

thanks so much this is really gonna help me with my first project!

Profile icon
JacobSedgebeer

@JacobSedgebeer
another thing is all you have to do is just type import time you don't need the extra command to import sleep