0

So i'm trying to make a very simple text-based RPG as my semester project for my programming class. I just recently learned (which is probably pretty apparent by my code) how to define classes and felt they would work much better than a function.

However, i'm having trouble with the 'character' class. Instead of having the player name predefined, I want the user to be able input their own name, which i've done in the 'Intro' function. Now my problem is taking the variable 'pName' and setting as the player's name, which I havent been able to do.

My questions are: 1. Can I do this? (use a function variable as a class attribute?) 2. Is there a better, more efficient way of doing this? And 3. Is there any additional information you guys could give me about classes or about how I can go about finishing this program?

Anything is well-appreciated and thanks in advance for the help!

import random, time

#I'm not at all hellbent on keeping this same format, it's just
#the way i've been taught and i'm most comfortable with.

def Intro():
    print('Welcome puny warrior, to my Dungeon of DOOM, and stuff.')
    pName = input('Tell me your name, puny warrior: ')
    playResponse = input('Do you want to begin puny warrior who calls himself ' + pName + '? Y/N: ')
    playResponse = playResponse.upper()
    if playResponse[0:1] == 'Y':
        pass
    else:
        print('You die now', pName)

class character(object):
    def __init__(self, name, health, attack):
        self.name = name
        self.health = health
        self.attack = attack

#this part obviously doesn't work, but I decided to leave it as a visual aid
player = character(pName, 25, 5)

#should I just make this class a child of the 'character' class?
class foes(object):
    def __init__(self, name, health, attack):
        self.name = name
        self.health = health
        self.attack = attack

zombie = foes('Zombie', 10, 3)
dragon = foes('Dragon',20, 5)
skeleton = foes('Skeleton', 8, 4)
YourPalAl
  • 3
  • 1
  • u can inherit the character class to foes or u can have on class and add a variable called type.. to it as type maybe foes or playes – sundar nataraj Apr 17 '14 at 07:08

3 Answers3

1

You are trying to call internal Intro() variable - fast fix could be like this:

def Intro():
    print('Welcome puny warrior, to my Dungeon of DOOM, and stuff.')
    pName = input('Tell me your name, puny warrior: ')
    playResponse = input('Do you want to begin puny warrior who calls himself ' + pName + '? Y/N: ')
    playResponse = playResponse.upper()
    if playResponse[0:1] == 'Y':
        pass
    else:
        print('You die now', pName)
    return pName

class character(object):
    def __init__(self, name, health, attack):
        self.name = name
        self.health = health
        self.attack = attack

#this part obviously doesn't work, but I decided to leave it as a visual aid

player = character(Intro(), 25, 5)

#should I just make this class a child of the 'character' class?

class foes(object):
    def __init__(self, name, health, attack):
        self.name = name
        self.health = health
        self.attack = attack

zombie = foes('Zombie', 10, 3)
dragon = foes('Dragon',20, 5)
skeleton = foes('Skeleton', 8, 4)
hegelsturm
  • 71
  • 1
  • 6
  • Thanks for the quick reply! It's not quite what I wanted to have to do but for this purpose it works for how I need it – YourPalAl Apr 17 '14 at 14:02
0

I'm not really sure what your question is. There's no difference to Python between using a variable like pName and a string like "Zombie" in a class instantiation. The only thing wrong with your code is that you're doing that instantiation outside Intro(), so pName is not defined.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • How would I go about fixing that? – YourPalAl Apr 17 '14 at 07:29
  • Well, that depends on the rest of your program. You haven't shown the code that actually calls Intro, or the bit that does anything with the objects you've instantiated. You could perhaps return `pName` from Intro, and use it in the calling function to do the instantiation: or move that instantiation into Intro itself, and return that object from there. – Daniel Roseman Apr 17 '14 at 07:52
0

Some Pointers

  • Take a look at PEP8, the Style Guide for Python.

  • Use the Character Class to define both the player, and foes.

  • Implement a main() method and use it appropriately. For more information about this topic, visit this discussion.

  • Make use of Python dictionaries, as they are very powerful.


#!/usr/bin/env python


"""Simple, text-based RPG."""


import random  # Generate pseudo-random numbers.
import time  # Time access and conversions.


class Character(object):
    """Define the character."""

    def __init__(self, name, health, attack):

        self.name = str(name)  # Expecting a string.

        self.health = int(health)  # Expecting an integer.

        self.attack = int(attack)  # Expecting an integer.

    def properties(self):
        """Returns dictionary containing the character properties."""

        # Because using dictionaries is awesome.
        characteristics = {
            'name': self.name,
            'health': self.health,
            'attack': self.attack
        }

        return characteristics  # Returns a dictionary


def get_player():
    """Acquire the name of the player, and begin the game."""

    # Newline characters should improve readability on the command line.
    print('\nWelcome puny warrior, to my Dungeon of DOOM, and stuff.\n')

    # Variable names should be easy to read and understand.
    player_name = input(
        """
        Welcome puny warrior, to my Dungeon of DOOM... and stuff.

        Tell me your name: """
    )

    # Get and store the player's response.
    player_response = input(
        """
        Do you want to begin puny warrior who calls himself %s? (Y/N): \
        """ % player_name
        )

    if player_response.upper() == "Y":
        pass

    else:
        print('\nYou die now\n', player_name)

    return player_name


def score():
    """Assign a score between 1 and 100."""

    return random.randint(1, 100)


def main():
    """Where the automagic happens."""

    # Assuming you want random integers.
    player = Character(get_player(), score(), score()).properties()
    zombie = Character('Zombie', score(), score()).properties()
    dragon = Character('Dragon', score(), score()).properties()
    skeleton = Character('Skeleton', score(), score()).properties()

    # Since you're working with a dictictionary, you can now do things like:
    print(player['name'])
    print(zombie['name'], zombie['health'])
    print("%s: %s, %s" % (dragon['name'], dragon['health'], dragon['attack']))


# The correct methodology.
if __name__ == "__main__":
    main()
Community
  • 1
  • 1
Johnny
  • 2,088
  • 1
  • 14
  • 10