How to make a basic Text/Story Game in Python!
h
TsunamiOrSumth (500)

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!

You are viewing a single comment. View All
JacobSedgebeer (2)

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

JacobSedgebeer (2)

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