Skip to content
Sign upLog in
This post is read-only. Explore Repls and connect with other creators on Community.View Community
The info in this post might be out of date, check out our docs instead. View docs
12

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

TsunamiOrSumth
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!

2 years ago

Voters

Comments

TopNew
1
SawyerRuch
SawyerRuch

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

8 months ago
1
SandwichBlamwich
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

11 months ago
1
CyberDaDev
CyberDaDev

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

2 years ago
1
PlsDont
PlsDont

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

2 years ago
1
JonahGress
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.

2 years ago
1
TsunamiOrSumth
TsunamiOrSumth

@JonahGress
dont ask me, i didnt make python

2 years ago
1
JacobSedgebeer
JacobSedgebeer

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

2 years ago
1
JacobSedgebeer
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

2 years ago
Load more