1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Rep Power

    Lightbulb Newb - lots of questions about text RPG game

    Hi, I had another thread earlier on which I asked a few questions but was getting quite off topic.

    1st question I have is, if you have a big line of code, can you enclose it all in brackets and use it again later?

    I made this fight sequence for a text based rpg I am doing and what I basically want to do is write

    fight_seq = (-insert huge chunk of code here...-)

    so I can bring it in any time I want it in the game instead of writing the whole thing out?

    here is my code:

    # cb v5.0
    # I just use the turn counter as a way of setting rounds for a fight.
    # Not applicable here because it is huge and the fight will never last that long.
    #The player presses the enter key to progress through the fight.
    #You can press 1 to heal 5 health
    import random
    turn_counter = 0
    health = 25
    creep_health = 10
    health_potion = 1
    while turn_counter < 100:
        damage = random.randint(0,3)
        creep_damage = random.randint(0,3)
        health -= creep_damage
        creep_health -= damage
        turn_counter += 1
        if health <= 0: health = 0
        if creep_health <= 0: creep_health = 0
        print("You swing your fist to hit [" + str(damage) +"] damage.")
        print("The creep swipes to hit (" + str(creep_damage) + ") damage.")
        print("Your HP is [" + str(health) + "] and creeps HP is (" + str(creep_health) + ").\n")
        if health <= 0 or creep_health <= 0:
        answer = input()
        if answer == "1":
            if health_potion >= 1:
                health_potion -= 1
                health += 5
                print("\n-You drink a health potion. \n-Your HP is now",
                      str(health) + ". \n-You have", health_potion, "health potions left.\n")
                print("\nYou are out of health potions!\n")
    print("\n\nend of battle.")
    input("Press Enter to exit....")

    thanks in advance.
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Rep Power
    Yes, of course you can. Write the program rpg.py.m4 , preprocess it with m4 (link) into rpg.py , then run your program with python. I've found that technique especially useful with bison programs in which the source code can be quite repetitive. And, since bison is a mix of c fragments with grammar rules, you can't just apply cpp macros. Often it happens that the "big chunk of code" depends on something. Say, perhaps, the values of some local variables. m4 allows for this---the macros take arguments that are duly substituted, possibly recursively.

    The clever designers of python considered that you might have a "big chunk of code" destined for reuse. They realized the difficulty of reading a clumsy program with repeated "big chunk of code". And, having made the mistake once or twice themselves, considered the well being of future programmers who would have to maintain the mess when, for whatever reason, the code changes. And so they included functions, classes, and methods to alleviate the problem. Methods are functions belonging to an object with easy access to relevant data.

    You yourself have used functions, guiding the action with information you supplied! Don't believe this? AHAH! Look here.
    print("You swing your fist to hit [" + str(damage) +"] damage.")
    But how, you ask, can I create my own reusable chunk of code? How can I make my own function??? READ HERE, then continue your study with classes, methods, and attributes.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo