Subscribe to Akasen's Blog        RSS Feed
-----

I learn Python and Programming by Working on a Text-Based RPG: Entry 2

Icon 2 Comments
So with entry 2, I will be posting up all the source in much of the files I have. I will attempt to explain some things in regards to some source although I may not get to it altogether.

I will say right here that I did not at ALL get to these points by myself. I was following a youtube tutorial on RPG's from a user named QuadRaxi. His tutorial is incomplete though and has lead me to use what I felt was necessary from his videos and use it in this. I looking over the code, the Character files are generally derived from what he had in his videos and the Item file has some as well although I am hoping to figure out how to work in items by myself.

File: main
from characters.player import *
from characters.enemy import *
from enemy.enemy import *
import random

player = Player('Default', 10, 10, 10, 10, 5, 5)

enemy = Enemy('DUMMY', 5, 5, 5, 2, 2)

player.name = raw_input('Type your name: ')
print''
print '>> Then let me throw your enemy at you ' +player.name+'!\n'

print '>> And so the video game threw at you a ' +enemy.name+'.\n'

#If the player is to die, this function is used at the moment to end the game
def game_over():
        print '>> Game Over! You died to a ' +enemy.name+ '!\n'
        raw_input('Press Enter to continue...')

#Uf the players enemy dies, this function is also used to end the game
def game_win():
        print '>> A winner is you! You defeated a ' +enemy.name+ '!\n'
        raw_input('Press Enter to continue...')

while True:
        line = ''
        while line != 'attack':
                #Player will fight against enemy
                line = raw_input('>> What do? \n\n>> ').lower()
                print ''
                if line == 'help':
                    print ('>> At the moment, all you can do is attack.'
                           +' GO FORTH WITH ALL YOUR MIGHT!\n')
                if line == 'run':
                    print '>> You feel a smack across the face\n'

                #This line of code allows the player to deal damage.
                #If the damage is ever less than zero, the enemy is dealt no
                #damage.
                #Damage is rolled randomly based on player strength.
                #Strength also goes against an enemies stamina.
                #A future implementation is to use a To Hit system
                #to see if a player hits.
                if line == 'attack':
                    player_damage = (random.randint(1, player.strength)
                                     - enemy.stamina)
                    
                    if player_damage < 0:
                        player_damage = 0
                        print '>> '+enemy.name+ ' shrugs off your attack.\n'
                        
                    else:
                        print ('>> You deal ' +str(player_damage)+ ' to the '
                               +str(enemy.name)+ '.\n')
                        enemy.health -= player_damage
        
        #Declaring enemy and player death requirements
        if enemy.health <= 0 and player_damage >= enemy.health + 10 :
                print ('>> You kill the DUMMY. You have completely obliterated '
                       +'it you cold hearted monster.\n')
                game_win()
                break
        elif enemy.health <= 0:
                print '>> You killed a DUMMY. You monster.\n'
                game_win()
                break

        if player.health <= 0:
                game_over()
                break

        #Enemy turn
        enemy_damage = random.randint(1, enemy.strength) - player.stamina

        if enemy_damage < 0:
                enemy_damage = 0
                print '>> You shrug off the enemy attack.\n'

        else:
                print '>> You have been dealt ' +str(enemy_damage)+ '.\n'

        player.health -= enemy_damage



#In regards to this main file, it was at one point the test bed for how to work combat into the game. After I was done implementing what I felt to be an okay system and understanding of how complex even a rudimentary system would be, I set sights on working to add items to the game. That is when I immediately realized a certain bug that could possibly happen where in which a player could possibly gain infinite health at with potions. I immediately set out to get an answer to my problem and thanks to 'atraub', I was able to understand Classes a bit more and what I was doing wrong.


File: character
#This is the character template that PC's and NPC's have.
#NPC's and the PC will have their own variables and attributes
#to further differentiate them.

class Character(object):
    def __init__(self, name, max_health, health, strength, stamina, toHit):
        self.name = name
        self.max_health = max_health
        self.health = health
        self.strength = strength
        self.stamina = stamina
        self.toHit = toHit
        if health >= max_health:
            
           health = max_health

    #def __setattr__(self, name, value):
        #self.__dict__[name] = self.health
    
    def recover_health(self, amount):
        if self.health + amount > self.max_health:
            self.health = self.max_health
        else:
            self.health += amount

    def attack(self, args):
        if line == attack:
            enemy.damage -= self.damage



#I realize that I seem to have added a function called 'attack' which contains an attribute called 'args'. I have checked back to the files that I have from when I watched QuadRaxi's work and I have no clue what 'args' was meant for. I will have to look into the files further and see if it has meaning in another file that I seem to be over looking.

File: player
from character import *

#This is the playr file. At the moment, the player only has intelligence.
#If I really did go on to do an rpg, a morality attribute may be added

class Player(Character):
    def __init__(self, name, max_health, health,
                 strength, stamina, toHit, intelligence):
        Character.__init__(self, name, max_health,
                           health, strength, stamina, toHit)
        self.intelligence = intelligence



File: enemy
from character import *

class Enemy(Character):
    def __init__(self, name, max_health, health, strength, stamina, toHit):
        Character.__init__(self, name, max_health,
                           health, strength, stamina, toHit)



File: enemy (file list)
#This file contains a list of enemies
from characters.player import *
from characters.enemy import *

def Cat():
    enemy = Enemy('Cat', 10, 10, 10, 10, 10)

def Demon():
    enemy = Enemy('Demon', 10, 10, 10, 10, 10)



#I still don't know how to work this yet but in theory, if the game is set to 'Demon' I should fight an enemy called 'Demon'. 'Cat' should also lead to 'Cat' in of itself. Don't ask WHY I even set cat. Animal cruelty is a big no people, don't do it bro.

File: item
#Items template

#Defines the basics of an item
class Item(object):
    def __init__(self, name, value, quantity = 1):
        self.name = name
        self.value = value
        self.quantity = quantity

#The Container is something akin to inventory where in which
#items are kept in.
class Container(object):
    def __init__(self, name):
        self.space = ()

    def add_item(self, name, quantity = 1):
        self.name = name
        self.add = self.append(space)
        



#Simply put, what I need is to simply allow for the addition of items into a player inventory. I also wanted it so that enemies had an inventory they could check for items that they may be holding. Future thinking would dictate that the need for a player to search or automatically gain the enemy belongings is something I will need to do as well. For now though, I wish to understand how to add items and give the player an inventory. I also need to know how to make items as well though. Bits of this code are also from QuadRaxi's tutorials on RPG as well. In his video though, he seperated container and item into two files. I don't recall why though and I may have missed a step cause my file for container still has the item defined.

2 Comments On This Entry

Page 1 of 1

atraub Icon

19 August 2012 - 01:34 PM
Hey boss, it looks great so far. My one quibble is with the syntax
from module import *
This is typically considered poor form. There are some alternatives that work nicely. Here are some examples
import characters.player

Of course, using this syntax you'll have to preface function/method calls with the module name, such as
player.Player()
. The benefit of this is that you know precisely what module every method call comes from. If you want it to be a bit shorter, you can assign the module an alias. To do that, you would import the module like this
import module as alias
This allows you to have something like this:
import SomeModuleWithAnAnnoyinglyLongName as SMWAALN
. Why is this so important? Well, you might do things you don't mean to otherwise. The classic example is the OS module. if you do
from os import *
everything might seem fine at first... but you'll have inadvertently done something terrible! You see, the os module has a function called "open" for opening files... but there's an open function built into the python standard library. By using the import * syntax, you'll overwrite that open function, thus you'll be using the open function from the os module rather than the standard python one. So, you'll get unexpected results if you try to use it.

I'm really liking what I see, I hope this rpg turns out great :)
1

Akasen Icon

20 August 2012 - 06:24 AM

atraub, on 19 August 2012 - 08:34 PM, said:

Hey boss, it looks great so far. My one quibble is with the syntax
from module import *
This is typically considered poor form. There are some alternatives that work nicely. Here are some examples
import characters.player

Of course, using this syntax you'll have to preface function/method calls with the module name, such as
player.Player()
. The benefit of this is that you know precisely what module every method call comes from. If you want it to be a bit shorter, you can assign the module an alias. To do that, you would import the module like this
import module as alias
This allows you to have something like this:
import SomeModuleWithAnAnnoyinglyLongName as SMWAALN
. Why is this so important? Well, you might do things you don't mean to otherwise. The classic example is the OS module. if you do
from os import *
everything might seem fine at first... but you'll have inadvertently done something terrible! You see, the os module has a function called "open" for opening files... but there's an open function built into the python standard library. By using the import * syntax, you'll overwrite that open function, thus you'll be using the open function from the os module rather than the standard python one. So, you'll get unexpected results if you try to use it.

I'm really liking what I see, I hope this rpg turns out great :)


So would I just put
from character import Character

at the top of the player and enemy modules or what? If I do just
import Character

in say the 'enemy' module I will get this statement

Traceback (most recent call last):
File "C:\Users\Joshua\Documents\Python\Text Adventure\RPGtest\characters\enemy.py", line 1, in <module>
import Character
ImportError: No module named Character
0
Page 1 of 1

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

August 2014

S M T W T F S
     12
3456789
10111213141516
1718192021 22 23
24252627282930
31      

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)