HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Run loop for a text-based adventure game

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
textloopadventuregameforbasedrun

Problem

I am programming a graphical text adventure using python. The game is playable, but I am not sure whether the code is well-suited for newcomers (code smells / quirks / general readability).

Here's the main script:

import time
import sys

import printer
from rooms import *
from _player import Player

VER = 0.01

SCENES = {0 : Lobby(),
          1 : LivingRoom(),
          2 : Bathroom()}

player = Player()

def run(start_at=None):
    """Starts the game"""

    if not start_at:
        scene = SCENES[0]
    else:
        scene = SCENES[start_at]
    scene.init_player(player)
    while 1:
        new_scene = scene.run()
        if new_scene != None:
            scene = SCENES[new_scene]
            scene.init_player(player)

if __name__ == "__main__":
    run()

Solution

Avoid wildcard imports. Import from rooms just what you need, named explicitly.

while 1 is an unnatural way of writing while True

Don't use != or == with None. Use is not None and is None.

The scene.run call that returns another scene object is not very intuitive. Functions are best named after what they do and/or what they return. In this example "run" is a pretty vague term, it could mean anything. If it transitions the game state to the next scene, I think scene.next_scene would be more intuitive.

Do you really need SCENES ? How about just passing around scene instances? This approach using a global dictionary of index to instance mappings seems fishy.

Context

StackExchange Code Review Q#93147, answer score: 7

Revisions (0)

No revisions yet.