patternpythonMinor
Randomly generated world for text-based RPG
Viewed 0 times
generatedtextrandomlyworldforbasedrpg
Problem
I am developing a text-based RPG with a randomly generated world space. I have built a light engine to produce a world for me and fill it with 'world objects' like trees, mountains, villages, and the like. The world can be generated either through a fully automatic method, or a "manual" method, that allows for more specific placement of world objects.
My code might be a bit sloppy, since I'm rather new to Python programming and the world of OOP. That being said, the following code provides the complete
I believe all of this code to be in working condition. My apologies if any of you get errors.
Player class:
```
class Player(object):
def __init__(self, x, y):
self.x = x
self.y = y
self.baseStats = {'health': 1, 'stamina': 0, 'mana': 0, 'equip load': 0,
'level': 1}
self.descStats = {'name': None, 'race': None, 'class': None,
'description': None}
self.baseSkills = {'strength': 0, 'intelligence': 0, 'defense': 0,
'speech': 0, 'endurance': 0, 'faith': 0, 'luck': 0}
self.inventory = {'weapons': [], 'armor sets': [], 'rings': [],
'miracles': [], 'spells': [], 'gold': None, 'keys': None}
self.equipment = {'weapon': None, 'armor set': None, 'rings': []}
def moveNorth(self):
self.x -= 1
def moveSouth(self):
self.x += 1
def moveEast(self):
self.y += 1
def moveWest(self):
self.y -= 1
def movement(self, world):
movement_inp = raw_input("""
W. | Move North
S. | Move South
D. | Move East
A. | Move West
> """).lower()
if movement_inp == 'w':
self.moveNorth()
elif movement_inp == 's':
self.moveSouth()
elif movement_inp == 'd':
My code might be a bit sloppy, since I'm rather new to Python programming and the world of OOP. That being said, the following code provides the complete
WorldSpace class, but a rather bare-bones version on the Player class -- a version that just specifies some basic variables to show how I will be structuring the real Player class.I believe all of this code to be in working condition. My apologies if any of you get errors.
from random import randint, choice, randrangePlayer class:
```
class Player(object):
def __init__(self, x, y):
self.x = x
self.y = y
self.baseStats = {'health': 1, 'stamina': 0, 'mana': 0, 'equip load': 0,
'level': 1}
self.descStats = {'name': None, 'race': None, 'class': None,
'description': None}
self.baseSkills = {'strength': 0, 'intelligence': 0, 'defense': 0,
'speech': 0, 'endurance': 0, 'faith': 0, 'luck': 0}
self.inventory = {'weapons': [], 'armor sets': [], 'rings': [],
'miracles': [], 'spells': [], 'gold': None, 'keys': None}
self.equipment = {'weapon': None, 'armor set': None, 'rings': []}
def moveNorth(self):
self.x -= 1
def moveSouth(self):
self.x += 1
def moveEast(self):
self.y += 1
def moveWest(self):
self.y -= 1
def movement(self, world):
movement_inp = raw_input("""
W. | Move North
S. | Move South
D. | Move East
A. | Move West
> """).lower()
if movement_inp == 'w':
self.moveNorth()
elif movement_inp == 's':
self.moveSouth()
elif movement_inp == 'd':
Solution
Small suggestion, instead of:
Use:
Or better:
Which gets completely rid of the (very similar)
I also see no point in having
While it is nice to abstract the skills part away a bit into different categories, it might be more advantageous to have them in the namespace directly. This way you could do
if movement_inp == 'w':
self.moveNorth()
elif movement_inp == 's':
self.moveSouth()
elif movement_inp == 'd':
self.moveEast()
elif movement_inp == 'a':
self.moveWest()
else:
print 'Not a valid command'
self.movement(world)Use:
movement = dict(w=self.moveNorth, s=self.moveSouth, d=self.moveEast, a=self.moveWest)
....
try:
movement[movement_inp]()
except KeyError:
print 'Not a valid command'
self.movement()Or better:
def __init__(self, x=0, y=0):
self.directions = dict(w=(0,1), s=(0,-1), d=(1,0), a=(-1,0))
...
def movement(self):
while True:
movement_inp = raw_input("""
W. | Move North
S. | Move South
D. | Move East
A. | Move West
> """).lower()
try:
direction = self.directions[movement_inp]
except KeyError:
print 'Not a valid command'
else:
self.x, self.y = self.x + direction[0], self.y + direction[1]
breakWhich gets completely rid of the (very similar)
moveX functions.I also see no point in having
world as a parameter when you don't do any checks with it.While it is nice to abstract the skills part away a bit into different categories, it might be more advantageous to have them in the namespace directly. This way you could do
print "Strength: {.strength}".format(player) or player.strength += 5 on level up.Code Snippets
if movement_inp == 'w':
self.moveNorth()
elif movement_inp == 's':
self.moveSouth()
elif movement_inp == 'd':
self.moveEast()
elif movement_inp == 'a':
self.moveWest()
else:
print 'Not a valid command'
self.movement(world)movement = dict(w=self.moveNorth, s=self.moveSouth, d=self.moveEast, a=self.moveWest)
....
try:
movement[movement_inp]()
except KeyError:
print 'Not a valid command'
self.movement()def __init__(self, x=0, y=0):
self.directions = dict(w=(0,1), s=(0,-1), d=(1,0), a=(-1,0))
...
def movement(self):
while True:
movement_inp = raw_input("""
W. | Move North
S. | Move South
D. | Move East
A. | Move West
> """).lower()
try:
direction = self.directions[movement_inp]
except KeyError:
print 'Not a valid command'
else:
self.x, self.y = self.x + direction[0], self.y + direction[1]
breakContext
StackExchange Code Review Q#138594, answer score: 3
Revisions (0)
No revisions yet.