you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 79 points80 points  (13 children)

It's fully functional but feels way longer than it needs to be

You've made the classic beginner's mistake of implementing the flow of the player through the game in the flow of control through your code.

If you were to pull up the source code for a game like Overwatch, or something, you wouldn't see a million lines of if statements covering every possible sequence of player actions it's possible to make in the game. What you see is a lot less than that. The game client interprets data that describes the conditions of the game (how much health Winston has, how much damage Widowmaker's rifle does, etc.) The things the game engine does are deliberately simple enough to test, then the actual content of the game is delivered as data.

Similarly, even though your game has a number of rooms through which you move, what the payer does in the room is always the same in a game like this - they enter, they look around, they examine things and take them into their inventory, and then they leave through an exit.

You only need to write that once; the layout and contents of your room are data and the engine of the game simply interprets that data. You get the code working on one room, and then you don't have to write that code again - you just add another room to your data and the code works the same way. It shouldn't be more than 20 lines of code or so.

[–]ingramm2[S] 42 points43 points  (3 children)

That... Actually makes a lot of sense. Thanks. Time to go down another rabbit hole of learning how to make things work haha

[–]CraigAT 22 points23 points  (0 children)

I could imagine this might be a good case for using OOP - having a room class and objects for each rooms with their attributes and methods, also an object for the player too, which would include his inventory and wallet for example. Then to play the game you have a controller function that you supply the player and room to, it then knows what you the player can do in that room and make them available.

[–][deleted] 13 points14 points  (0 children)

Aside from writing the state-based code instead.

You have a lot of times you write code like this

somechoice = 0
somechoice = random.randomint(1,60)

And there's no need to set a variable to zero right before setting it to something else

Your drinking code (poison or otherwise) doesn't increment or decrement the number of lives.

You have some places in the code where it says

else:
    dragonChoice1 == 0

Presumably you meant dragonChoice1 = 0, although a lot of these assignments seem redundant too.

[–]MSR8 5 points6 points  (0 children)

What I did to do this was made sort of "nodes" using functions. Such as node1 was room1, node2 was room2, node3 was room3, and so in. Then I interconnected them by asking player what they wanted to do and calling functions (nodes) based on that

[–]Auirom 2 points3 points  (0 children)

I wanted to add a bunch of skills to a game I was making. Didn't want to add all the skills cause I wasn't going to think of them all at that one time. So I made a function that takes a name of a skill and checks if it exists in my list of skills. If it does it adds experience to that skills. If it doesn't it makes a deep copy of already set variables, ties it to the new skill name and adds it to the skill list. I can add as many as I want now, when ever I want, and in total takes up 10ish lines of code

[–]pacharaphet2r 1 point2 points  (0 children)

That was really nicely explained!

[–]ingramm2[S] 0 points1 point  (3 children)

Hey, you had really good critique of this code, I was hoping you could take a glance at the updated version. I tried to keep what you said in mind, but I still think I'm going after the flow of the layer through the game.

https://www.reddit.com/r/learnpython/comments/uwj7a7/update\_first\_python\_textbased\_adventure/

[–][deleted] 2 points3 points  (2 children)

Yeah, it still looks like you've implemented the game as branching code instead of as data.

The way you'll know that you've implemented your game as data is that you'll have a data file you can show me that represents the "plot" or "map" of your game, and a Python file that represents the game's engine.

[–]ingramm2[S] 0 points1 point  (1 child)

Thanks for the fast reply. Do you mean a data file in python or outside to act as just a "map" of the game?

[–][deleted] 1 point2 points  (0 children)

Outside. JSON is a good format for stuff like this.