Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Have we any Python users here?  (Read 930 times)

itisnotlogical

  • Bay Watcher
  • might be dat boi
    • View Profile
Have we any Python users here?
« on: December 20, 2012, 11:54:55 pm »

So, I've been fascinated with Python for a while. About a week or so ago I got a sudden burst of inspiration and started making a text-based RPG engine, using newfound knowledge from a few excellent tutorials. However, I'm having a bit of a problem with the room handler module that I wrote.

Spoiler: Example (click to show/hide)

As you can see, even though I should return to the first room after typing "go s", I'm staying in the second room. As far as I can tell, my command interpreter works fine and there is no fault in the test setup itself. Thus, the problem is probably in my roomhandler.py module. The module itself is clunky and probably larger than it needs to be, but AFAIK it should work as written. I've tried the debugger, but it provides no usable information. Help?

Spoiler: fulltest.py (click to show/hide)
Spoiler: interpreter.py (click to show/hide)
Spoiler: roomhandler.py (click to show/hide)
Logged
This game is Curtain Fire Shooting Game.
Girls do their best now and are preparing. Please watch warmly until it is ready.

Berserker

  • Bay Watcher
    • View Profile
Re: Have we any Python users here?
« Reply #1 on: December 21, 2012, 02:04:38 pm »

The core of the problem is that when you move to the second room, it replaces the attributes of the first room with the attributes from the second room, which means that the first room doesn't exist anymore, and you have two identical rooms.

One way to fix it would be making a variable that points to the current room, and making the roomhandler return the next room, and replacing the current room with the returned room.

Something like this (I also replaced the recursive start call with a loop):

Spoiler: fulltest.py (click to show/hide)

Spoiler: roomhandler.py (click to show/hide)
Logged

itisnotlogical

  • Bay Watcher
  • might be dat boi
    • View Profile
Re: Have we any Python users here?
« Reply #2 on: December 21, 2012, 04:25:10 pm »

Ah, I see! Thanks very much, sometimes it's the obvious things I need help with :P
Logged
This game is Curtain Fire Shooting Game.
Girls do their best now and are preparing. Please watch warmly until it is ready.

SethCreiyd

  • Bay Watcher
  • [VESPERTINE]
    • View Profile
Re: Have we any Python users here?
« Reply #3 on: December 21, 2012, 05:26:10 pm »

You can use a single attribute to link a value corresponding to a specific room.  The value could be an index in a list of rooms, or a key in a dictionary of all rooms where the key is the name of the room.  That way you could skip a lot of condition checking and just move to the room connected to the desired exit, if it exists.

I'd also recommend a Player class or something like that, for keeping track of things like position, score, rooms-already-traveled-to and what have you.  Here's a rough example, using a list to store rooms:

Spoiler (click to show/hide)
« Last Edit: December 21, 2012, 05:45:24 pm by SethCreiyd »
Logged

itisnotlogical

  • Bay Watcher
  • might be dat boi
    • View Profile
Re: Have we any Python users here?
« Reply #4 on: December 21, 2012, 07:32:06 pm »

Ah, that's a neat idea, sethcreiyd. I have a "player" class that I largely use for combat:

Spoiler: player class (click to show/hide)

... which is based on the Microlite20 rules. None of the non-combat attributes like self.comm (Communication, in-game) and self.subt (Subterfuge) are implemented yet.

Your classes look a lot more compact than mine, definitely. That comes with experience I imagine, since this is my first real-deal game.

The way the game is currently set up, combat is almost completely independent of the room itself, calling a seperate combat module based on a random check every time you move in to a new room.

Spoiler: new fulltest.py (click to show/hide)

I have to reset enemyroll at the end of every if-block because it wouldn't reset when it normally should, for some reason.

I have a complete test including both combat and movement, if anybody would like to see. The full source code is in the "library" folder.
Logged
This game is Curtain Fire Shooting Game.
Girls do their best now and are preparing. Please watch warmly until it is ready.

Berserker

  • Bay Watcher
    • View Profile
Re: Have we any Python users here?
« Reply #5 on: December 22, 2012, 04:58:47 am »

That's pretty good for a first game. I did a similar game when I was learning Python.

Here are some notes about the game, and the source code:

The game crashes when you type a command that doesn't have spaces.
Spoiler: Traceback (click to show/hide)
The game crashed when I won a battle in the second room. The library.zip didn't contain fulltest.py, but I guess current_room somehow became None.
Spoiler: Traceback (click to show/hide)

Some kind of help command would be handy.

I recommend writing class names in CamelCase to distinguish them from variable and function names.

You can clear the screen with os.system("cls").
Logged

itisnotlogical

  • Bay Watcher
  • might be dat boi
    • View Profile
Re: Have we any Python users here?
« Reply #6 on: December 22, 2012, 07:52:16 am »

That's pretty good for a first game. I did a similar game when I was learning Python.

Here are some notes about the game, and the source code:

The game crashes when you type a command that doesn't have spaces.
Spoiler: Traceback (click to show/hide)
The game crashed when I won a battle in the second room. The library.zip didn't contain fulltest.py, but I guess current_room somehow became None.
Spoiler: Traceback (click to show/hide)

Some kind of help command would be handy.

I recommend writing class names in CamelCase to distinguish them from variable and function names.

You can clear the screen with os.system("cls").

The interpreter isn't terribly good at handling incorrect input altogether, I've noticed. I noticed that if I put in random two-word phrases, it still behaves as if I attempted to move to another room. It started a battle when I typed in "blah blah". The interpreter automatically expects two-word commands and isn't prepared to deal with unexpected input.

Sorry fulltest.py wasn't in the library. I'm still getting the hang of cx_freeze, this is only the second or third time I've used it.

Spoiler: fulltest (click to show/hide)

I imagine the crash after the battle had something to do with unexpected input, as it works fine for me until I intentionally enter something wrong. It attempted to change current_room to the connected room, but interpret.interpret(input()) never returned a valid direction, thus the crash immediately after the battle.

Spoiler: runtime (click to show/hide)
Logged
This game is Curtain Fire Shooting Game.
Girls do their best now and are preparing. Please watch warmly until it is ready.

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: Have we any Python users here?
« Reply #7 on: December 24, 2012, 10:48:43 pm »

Can you please post links to the "excellent" tutorials you used? I'm trying to learn Python, but I'm having a hard time grasping it with just the manual and the tutorials I've found.
Logged

itisnotlogical

  • Bay Watcher
  • might be dat boi
    • View Profile
Re: Have we any Python users here?
« Reply #8 on: December 25, 2012, 12:40:01 am »

Can you please post links to the "excellent" tutorials you used? I'm trying to learn Python, but I'm having a hard time grasping it with just the manual and the tutorials I've found.

Invent with Python is probably the best one I've seen. It covers quite a lot, even Pygame if you want to take it that far. It teaches you exactly how much you need to know without getting too in-depth. And this. There's also plenty of tutorials available here, although I can't remember exactly which ones I used.

Practice is really helpful in conjunction with the above tutorials. You don't have to pump out high-quality finished programs every week, but try something new every time you write a program. For every function you learn, you're unlocking a new world of possibility. I couldn't have written that program in the above posts until I knew what class objects were, and I figured those out largely by messing around with them to see what works.
Logged
This game is Curtain Fire Shooting Game.
Girls do their best now and are preparing. Please watch warmly until it is ready.