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

"You are in a dark room; learn how to make a text based game"

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

Problem

In chapter 45 of Learn Python the Hard Way, the author basically says: "Ok, go figure out how to make a text based game"

Before I get too far in this I just want to be sure I'm not doing anything stupid. The code as it is is fairly functional just not too many rooms.

I am running this under Windows:

``
from sys import exit
from random import randint
import curses,random
screen = curses.initscr()
screen.border(0)
width = screen.getmaxyx()[1]
height = screen.getmaxyx()[0]
size = width*height
char = [" ", ".", ":", "^", "*", "x", "s", "S", "#", "$"]
b = []

curses.start_color()
curses.init_pair(1,0,0)
curses.init_pair(2,1,0)
curses.init_pair(3,3,0)
curses.init_pair(4,4,0)
global x
x = 1

def cls():
screen.clear()
screen.border(0)
screen.addstr(20,1,">")
screen.refresh()

def info_special(message,message2):
screen.move(19,x)
screen.clrtoeol()
screen.addstr(19,x,message,curses.A_BOLD)
screen.addstr(message2)
screen.move(20,3)
screen.clrtoeol()
screen.border(0)
screen.refresh()

def info(message):
screen.move(19,x)
screen.clrtoeol()
screen.addstr(19,x,message)
screen.move(20,3)
screen.clrtoeol()
screen.border(0)
screen.refresh()

def help():
screen.move(19,x)
screen.clrtoeol()
string = "Commands:"
for j in commands:
string = "%s\n\t %s" % (string,j)
screen.addstr(10,x,string)
screen.move(20,3)
screen.clrtoeol()
screen.border(0)
screen.refresh()

def intro():
curses.curs_set(0)
screen.clear
welcome = """_ _ ____ __ ___ __ _ _ ____
/ )( \( __)( ) / __)/ \ ( \/ )( __)
\ /\ / ) _) / (_/\( (__( O )/ \/ \ ) _)
(_/\_)(____)\____/ \___)\__/ \_)(_/(____)
"""
dragon = """ ______________
,===:'.,
-._
:.---.__ -._
:. --. .

Solution

My main pointers are that the whole game should be a class, so you can have members (self.VARIABLE) and not have to pass them on to too many functions.

You should restructure your commands to be a dictionary, where the key is the command (as the user inputs it) and then a list or another dict (or class) which would have the displayed command, and then a pointer to its method of what it does.

This way when you modify commands dynamically based on location, you do not need a function for each location. It also lets you make a more robust game.

There are a few other minor concerns, but they are mostly opinions, and not "mistakes" that you are making or anything. Try to read about making things more generic, and about class inheritance.

You requested an example:

def attack(args):
    print "recieved attack command with args: %s" % args

def move(args):
    print "recieved move command with args: %s" % args

def inspect(args):
    print "recieved inspect command with args: %s" % args

commands = {
    'attack': {'description': "makes an attack", 'display': 'Attack', 'function': attack},
    'move': {'description': "moves somewhere", 'display': 'Move', 'function': move},
    'inspect': {'description': "inspect something", 'display': 'Inspect', 'function': inspect}}

def show_commands():
    for command, specs in commands.items():
        print "%s: %s, %s" % (command, specs['display'], specs['description'])

def get_command():
    user_input = raw_input('choose a command: ')
    command, args = user_input.split(' ', 1)
    if command in commands:
        commands[command]['function'](args)
    else:
        print 'command not recognized'


Simple usage:

>>> show_commands()
attack: Attack, makes an attack
move: Move, moves somewhere
inspect: Inspect, inspect something
>>> get_command()
choose a command: attack monster
recieved attack command with args: monster

Code Snippets

def attack(args):
    print "recieved attack command with args: %s" % args

def move(args):
    print "recieved move command with args: %s" % args

def inspect(args):
    print "recieved inspect command with args: %s" % args

commands = {
    'attack': {'description': "makes an attack", 'display': 'Attack', 'function': attack},
    'move': {'description': "moves somewhere", 'display': 'Move', 'function': move},
    'inspect': {'description': "inspect something", 'display': 'Inspect', 'function': inspect}}



def show_commands():
    for command, specs in commands.items():
        print "%s: %s, %s" % (command, specs['display'], specs['description'])

def get_command():
    user_input = raw_input('choose a command: ')
    command, args = user_input.split(' ', 1)
    if command in commands:
        commands[command]['function'](args)
    else:
        print 'command not recognized'
>>> show_commands()
attack: Attack, makes an attack
move: Move, moves somewhere
inspect: Inspect, inspect something
>>> get_command()
choose a command: attack monster
recieved attack command with args: monster

Context

StackExchange Code Review Q#20042, answer score: 4

Revisions (0)

No revisions yet.