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

Character creator for a role-playing game

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

Problem

Goal:


Write a character creator program for a role-playing game. The player
should be given a pool of 30 points to spend on four attributes:
strength, health, wisdom, and dexterity. The player should be able to
spend points from the pool on any attribute and should also be able to
take points from an attribute and put them back into the pool.


Python Programming for the Absolute Beginner by Michael Dawson

My attempt:

```
print "create a character! you have points to assign to strength, health, wisdom, and dexterity."
name=raw_input("what's your character's name? ")
points=30
attributes=("health", "strength", "wisdom", "dexterity")
strength=0
health=0
wisdom=0
dexterity=0
while True:
print
print "you have", points, "points left."
print \
"""
1-add points
2-take points
3-see points per attribute
4-exit
"""
choice=raw_input("choice: ")
if choice=="1":
attribute=raw_input("which attribute? strength, health, wisdom, or dexterity? ")
if attribute in attributes:
add=int(raw_input("how many points? "))
if add0:
if attribute=="strength":
strength+=add
print name, "now has", strength, "strength points."
elif attribute=="health":
health+=add
print name, "now has", health, "health points."
elif attribute=="wisdom":
wisdom+=add
print name, "now has", wisdom, "wisdom points."
elif attribute=="dexterity":
dexterity+=add
print name, "now has", dexterity, "dexterity points."
points-=add
else:
print "invalid number of points."
else:
print "invalid attribute."
elif choice=="2":
attribute=raw_input("which attribute? strength, health, wisdom, or dexterity? ")
if attribute in attrib

Solution

One of the first things I would do is try and group your information into neater packages, rather than having a bunch of free variables. I assume you're not too familiar with classes, but try putting your character's attributes into a data structure like a List, or even better - a Dictionary:

attributes = {'health': 0, 'strength': 0, 'wisdom': 0, 'dexterity': 0 }


If you want to change attributes, you can then do

attributes['health'] = some_value


or to increment,

attributes['health'] += some_value


Secondly, the best way to improve your program is to make it more readable by splitting your code up into functions. For instance, the code below is an example of what it may look like if you took some of the stat changing logic out of the main program:

if choice=="1":
    stat = input("Which stat? ")
    modifier = input("By how much? ")
    add_stats(modifier, stat)
elif choice=="2":
    stat = input("Which stat? ")
    modifier = input("By how much? ")
    take_stats(modifier, stat)
elif choice=="3":
    print(show_player_stats())


In general, any where you find yourself getting "too deep" in nests of if statements and loops, or you find yourself repeating your code, try and break it out into a function. Bear in mind this is a crude example, and you'll have to develop your own solution, but in terms of readability it's a vast improvement.

I've made a start on a version of your game using my own approach, although I haven't implemented all of it (you can't subtract points for instance). However, you can probably already see where the program has improved on things.

Things to note about my version compared to yours:

  • There is commenting (although basic). Commenting code is a MUST, even if it's for yourself. It will help you understand your own code and help anyone else who uses it, even if it's just to show different parts of the program.



  • The code is broken down into functions, which improves readability and allows you to re-use bits of code in the future. For instance, every time you want to print your character's info, you just call print_character()!



  • The code is neater - I'm packing information into a data structure, strings are formatted with linebreaks, logic is compartmented into smaller, manageable chunks.



However, I have used some Python you may not be familiar with, like the keys() method. But it's important to go through the code and try and work out what's happening. This will help to expose you to the "Python" way of doing things.

##### GAME FUNCTIONS #####

def add_character_points():
    attribute = raw_input("\nWhich attribute? Strength, Health, Wisdom or Dexterity?\n")

    if attribute in my_character.keys():
        amount = int(raw_input("By how much?"))

        if (amount > my_character['points']) or (my_character['points'] <= 0):
            print "Not enough points!"
        else:
            my_character[attribute] += amount
            my_character['points'] -= amount
    else:
        print "\nThat attribute doesn't exist!\n"

def print_character():
    for attribute in my_character.keys():
        print attribute, " : ", my_character[attribute]

##### MAIN FUNCTION #####

my_character = {'name': '', 'strength': 0, 'health': 0, 'wisdom': 0, 'dexterity': 0, 'points': 20}
running = True

print "Create a character! You have points to assign to strength, health, wisdom, and dexterity."

my_character['name'] = raw_input("What is your character's name? ")

while running:
    print "\nYou have ", my_character['points'], " points left.\n"
    print "1. Add points\n2. Remove points\n3. See current attributes\n4. Exit\n"

    choice = raw_input("Choice:")

    if choice == "1":
        add_character_points()
    elif choice == "3":
        print_character()
    elif choice == "4":
        running = False         
    else:
        pass

Code Snippets

attributes = {'health': 0, 'strength': 0, 'wisdom': 0, 'dexterity': 0 }
attributes['health'] = some_value
attributes['health'] += some_value
if choice=="1":
    stat = input("Which stat? ")
    modifier = input("By how much? ")
    add_stats(modifier, stat)
elif choice=="2":
    stat = input("Which stat? ")
    modifier = input("By how much? ")
    take_stats(modifier, stat)
elif choice=="3":
    print(show_player_stats())
##### GAME FUNCTIONS #####

def add_character_points():
    attribute = raw_input("\nWhich attribute? Strength, Health, Wisdom or Dexterity?\n")

    if attribute in my_character.keys():
        amount = int(raw_input("By how much?"))

        if (amount > my_character['points']) or (my_character['points'] <= 0):
            print "Not enough points!"
        else:
            my_character[attribute] += amount
            my_character['points'] -= amount
    else:
        print "\nThat attribute doesn't exist!\n"


def print_character():
    for attribute in my_character.keys():
        print attribute, " : ", my_character[attribute]


##### MAIN FUNCTION #####

my_character = {'name': '', 'strength': 0, 'health': 0, 'wisdom': 0, 'dexterity': 0, 'points': 20}
running = True

print "Create a character! You have points to assign to strength, health, wisdom, and dexterity."

my_character['name'] = raw_input("What is your character's name? ")

while running:
    print "\nYou have ", my_character['points'], " points left.\n"
    print "1. Add points\n2. Remove points\n3. See current attributes\n4. Exit\n"

    choice = raw_input("Choice:")

    if choice == "1":
        add_character_points()
    elif choice == "3":
        print_character()
    elif choice == "4":
        running = False         
    else:
        pass

Context

StackExchange Code Review Q#8285, answer score: 8

Revisions (0)

No revisions yet.