• Create Account

## Movement Game Logic in a Python Text-Based Game

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1mauermcx  Members

Posted 07 June 2013 - 09:21 AM




Hey all,
Thanks to a few guys on the forums I finally got around to starting on my text-based adventure called "a knights quest." It features a knight played by the player that is sent by his king to wipe out a cave full of foul creatures. However I am a bit stuck on how to simulate player movement in Python. I looked around the internet for movement logic specifically in Python, but so far I have had no luck. So I come to you guys, the experts. Does anyone have a good method of simulating player movement? In case anyone is interested I am 51% of the way through the CodeAcademy Python track so I haven't completely learned the basics of Python yet. Should I write a function? Or I heard of something called a class which I have yet to learn about yet. Here's my code below if anyone is interested in giving me tips on what I already have.
Michael

# -*- coding: utf-8 -*-
"""
Created on Sun Jun  2 17:29:40 2013

@author: Michael
"""
epicfailure = "Since you failed to type your own name you lose"
def printresponse®:
if len® > 0:
return True
else:
return False

name = raw_input("What is your name?")
if printresponse(name) == True:
hometown = raw_input("Where are you from?")
else:
print epicfailure
def help(input):
helpint = "typable options: north, south, east, west, hit creature, run away, use torch"
if input == "help":
print helpint

yournameintro = "<Your name is %s , a wearied traveler>" % (name)
yourhometownintro = "<You've been sent to a cave outside of %s by your king>" % (hometown)
yourquestintro = "<Wipe out all of the foul creatures that dwell there>"
caveentrance = "<As you enter the cave, you check your supplies.  You have a sword, and a torch.  You think to yourself 'My king is such a cheapskate'>"
thequestion = "<What do you do?>"
if printresponse(hometown) == True:
print yournameintro
print yourhometownintro
print yourquestintro
else:
print epicfailure
print "======================="
print caveentrance
print "======================="
print thequestion
help("help")
response = raw_input(thequestion)

Edited by mauermcx, 07 June 2013 - 09:23 AM.

### #2dmreichard  Members

Posted 08 June 2013 - 11:25 PM

However I am a bit stuck on how to simulate player movement in Python. I looked around the internet for movement logic specifically in Python, but so far I have had no luck. So I come to you guys, the experts. Does anyone have a good method of simulating player movement? In case anyone is interested I am 51% of the way through the CodeAcademy Python track so I haven't completely learned the basics of Python yet.

From reading this it sounds like you are attempting to tackle a project a bit out of your scope at the moment. Let me clarify something first that there is no "movement logic" specifically in Python. Python is simply a programming language, a tool. To solve the problem of how to simulate player movement would involve thinking about how you are breaking down the representation of your player and a room. What data structures are you going to use to represent these game entities? Will rooms have references to each other by cardinal directions and a list of game objects and/or players?

I think you should read more about Python and experiment with classes, modules etc. before tackling a text based RPG. One way to think about programming is taking a large problem and dividing that into several smaller problems to solve then piecing it all back together. The language itself is simply a tool or means to an end however, if you don't know how to use the tool with some proficiency your problem will be rather hard to solve indeed.

### #3mauermcx  Members

Posted 09 June 2013 - 03:30 PM

Thanks for this advice.  You are right.  I need to focus on learning the language before tackling this big of a project.  I like the idea of dividing it into several smaller projects.  Eventually when I have more experience with Python i will map out my game before I start in on it again.

I think you should read more about Python and experiment with classes, modules etc. before tackling a text based RPG. One way to think about programming is taking a large problem and dividing that into several smaller problems to solve then piecing it all back together. The language itself is simply a tool or means to an end however, if you don't know how to use the tool with some proficiency your problem will be rather hard to solve indeed.

### #4tp9  Members

Posted 10 June 2013 - 01:53 PM

Hmm. It's been a while since I've worked on a text-based RPG but IIRC you can set up the room objects using a graph structure where the rooms are nodes and they are connected by bi-directional edges. As an example, Room1 has exits (S, W, N) and Room2 has exits (N, E, S, W), and Room1 (W) is attached to Room2 (E).

I know this may sound confusing but googling some of these terms may help get you started. Good luck on your game and remember to have fun.

### #5mauermcx  Members

Posted 11 June 2013 - 06:55 AM

Thanks tp9!

### #6MrSkullz  Members

Posted 29 June 2013 - 11:49 AM

Remember that everything in code is a model of something and the more complex (detailed) your model the more complex your code.  If you have an Age class which stores age in years as an int that is a model which restrictions.  You can't have half years because it's an int and you have to put logic in there to handle cases where age will be negative because that is outside the domain of an Age.  If years as an int is not detailed enough you could model Age as a start datetime and an end datetime but then you have to add code to handle conversions, pretty printing, etc.  It depends on what you need the model to do.

Get a pencil and paper and start to sketch out how you will model space.  As tp9 mentioned you could go the classic way of Rooms which have connections to other rooms which would be a great way to start.  You could build on a 2d plane and model the player's location as an x,y point but that is getting very complex.  Define the player commands (attack, move, etc) and then sketch a few possible sequences such as "player moves north twice and then attacks".  Well defined and thought out data structures lend themselves to simple code that works.

You will want to separate your user interface out from the game logic so that you don't have raw_input's next to move_to_room() functions or room data structures.

Personally I think a project like this (if you stick to the room based game) would be an excellent way to learn Python.  I would recommend getting basic functionality going and then looking around to see if there is a library to do it for you.  That will allow you to understand what the library is doing for you rather than treating it like magic.

Dive in, fix the stuff that doesn't work, break stuff trying to do something new.  Good luck!

Posted 29 June 2013 - 12:56 PM

1. Your printresponse function is poorly named because it does not print a response. It looks like it checks the length of a response to see if it is greater than zero, but this is not going to provide much information about the content of the response from the player. Looking into regular expressions in Python will provide you a way of checking player input in a more robust manner.

2. I tell everyone making a text based game that you really want to use a data driven design. Look at how to do File I/O in Python and define your rooms and game data in a file. Read this file into a data structure (once you learn about them) and write code to read this data structure and perform various actions based on the player input. Essentially your configuration file(s) are the content of the game, and your code is what allows the player to interact with the content. Doing things in this way will allow you to add more content without adding a proportional amount of new code. There are lots of options for what format to make such a file including but not limited to XML and JSON. XML is widely used but JSON is very popular right now.

3. Design first and code second. Plan out your game and how it will work. Especially when you have a lack of programming experience you need to really focus on defining your problem and how to break it down into parts that you can research how to do. Design is important no matter what level of experience and it is my view that at least thinking about the design first will make you a better programmer. That is not to say that you should spend months on a design or that you should not do anything but design, but it is that you should at least get a clear picture in your head of what you are trying to build so that you know what direction to go in.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.