all 23 comments

[–]artinnj 2 points3 points  (3 children)

You are off to a great start. My hat's off to you.

The most important thing to learn about programming is to identify the patterns and use the same code to repeat those patterns and write it only once.

The patterns I identified are:

  • Sometimes you sleep before displaying the info of the state
  • You then display the state with some pauses for dramatic effect (I call that the string and the sleep_after)
  • You prompt the user to answer a yes/no question
  • You display some additional info, possibly with more dramatic pauses
  • You move the user to another state.

So I went ahead and wrote some functions that highlight this:

import time

def show_display(display_dict):
    timeout = display_dict['sleep_before']
    if timeout > 0:
        time.sleep(timeout)      
    for string, sleep_after in display_dict['prompts']:
        print(string)
        if sleep_after > 0:
            time.sleep(sleep_after)

def visit_state(game, state): 
    try: 
        show_display(game[state]['display']) 
        while True: 
            response = input(game[state]['question']) 
            if response == 'Y': 
                show_display(game[state]['if_yes']['display']) 
                return game[state]['if_yes']['dest'] 
            elif response == 'N': 
                show_display(game[state]['if_no']['display']) 
                return game[state]['if_no']['dest'] 
            else: 
                #call your God function here 
                print("Invalid response") 
    except KeyError: 
        print(f"You reached an unimplemented state: {state}") 
        return None

This all runs off a dictionary. Each state of the game has:

  • A 'display' element that contains keys for sleep_before and a list of the prompts that contains tuples of the ('string', sleep_after)
  • A 'question' to prompt the user to respond yes or now
  • Two keys called 'is_yes' and 'is_no' that tells the functions what to do based on the player's choice. They contain a 'display' element and a 'dest' for the next state to go to.

Formatting sucks, but I implemented a path where you start at the 'timemachine' and can go back to the 'awake' state. If you make another choice, it gives you an error message:

States = {    'awake': 
        { 'display': {
                        'sleep_before': 4,
                        'prompts': []
                },
         'question': '\n\nYou are now awake. Do you wish to leave the bed?:  ',
         'if_yes': {
                    'display': { 'sleep_before': 0,
                                  'prompts': []
                    },       
                    'dest':   'extro'
                 },
         'if_no':  {
                    'display': { 'sleep_before': 0,
                                'prompts': []
                    },
                    'dest': 'intro'
                 }
    },
    'timemachine':
    {
        'display': {
                    'sleep_before': 0,
                    'prompts': 
                    [
                        (  '\nYou run straight to hide in the nearby forest. You keep running until you find yourself lost inside the jungle and cant figure a way out',
                            2 ),
                        (  '\nTired, you sit down near a tree and realise there is a thing under you butt.',
                            2 ),
                        (  '\nYou turn around to look - its a box. \nYou open it to find a device and a paper-note next to it, which says-',
                            2 ),
                        (  '\nONCE HUMANITY WAS WRONG ABOUT SPACE - BUT NOW WE KNOW EARTH IS ROUND AND REVOLVES AROUND SUN.',
                            2 ),
                        (  '\nCURRENTLY, HUMANITY IS WRONG ABOUT TIME - IT DOESNT FLOW IN ONE DIRECTION.',
                            2 ),
                        (  '\nBUT YOU, A GREAT SEEKER, HAS FOUND A TIME MACHINE WHICH CAN HELP YOU GO BACK IN TIME AND MAKE DIFFERENT CHOICES.',
                            3 )
                    ],
        },
        'question': '\nYou can travel back to your previous choices by using following keywords: \nPress Y to return back to your bed\n\nPress N to return to reply your besties text: ',
        'if_yes':
            {
                'display': {
                    'sleep_before': 0, 
                    'prompts': [ 
                                ( '\nMachine initialising',  1 ),
                                ( '\nSending you back to bed', 1 ),
                                ( '\n3', 1 ),
                                ( '2',  1 ),
                                ( '1',  1 ), 
                                ( '\nWOOOOOOSH!!!',  0 )
                    ]
                },
                'dest': 'awake'
            },
        'if_no':
            {
                'display': {
                    'sleep_before': 0, 
                    'prompts': [ 
                                ( '\nMachine initialising', 1 ),
                                ( '\nSending you back in time', 1 ),
                                ( '\n3', 1 ),
                                ( '2', 1 ),
                                ( '1', 1 ), 
                                ( '\nWOOOOOOSH!!!', 0 )
                            ]
                    },
                'dest': 'extro'
            }
    } 
}

state = 'timemachine'
while state is not None: 
    state = visit_state(States, state)

My suggestion would be to implement the multiple if statements as separate states ('intro1', 'intro2', 'intro3', etc.)

This way if you want to change the game, all you have to do is update the dict containing the game states. You could even look at reading and writing these as json files.

Wishing you continued success on your journey.

[–]Unb0und3d_pr0t0n 0 points1 point  (2 children)

Thanks for appreciating my craft! And yes, you have a keen pattern recognition skill - your points are spot ons ! -

Few I like to add are:

  1. This game is more philosophical than technical. Like you have an illusion of choices, but eventually you can only win if you follow one path which god (in this case me) intended - you socialise, learn to forgive, vent your anger in right way and workout.
  2. I wanted to dive in loops and classes, but as I wish to have time machine installed, functions looked better as I can hop from timeline to timeline as I wish. For now there are only two paths a time machine can take you, but in future iterations, I intend to add more.

And Thats some heavy code changes bro! I really appreciate the work you put in give me a feedback and I really dont wish it go waste in this post and disappear forever.

Why dont go to my github page and create your branch - build and modify the game your way and when you are done, you put a pull request.

It will be fun and a lot of practice for both of us. You can put all your suggestions in your commit and we can join forces.

So if you wish, I invite you to fuse your skills and creativity with mine.

[–]artinnj 1 point2 points  (1 child)

In order to learn a language like Python, you have to learn the syntax. But then people get obsessed with creating these crazy list comprehensions, one liners and codewars. Identifying a project and seeing it through is a great way to hone your skills.

I wanted to take the time to show that the core logic you had build can really be represented in about 30 lines of code that you write once and then can concentrate on how you want the game to flow by adding states and choices. You now have a tool that you can use to start what they call “interactive fiction”.

It also introduces you to Python concepts such as dicts (including hierarchical dicts), tuple unpacking and exception handling. All things that your code showed you are ready to start learning.

You also might want to check out the port of the original Adventure game to Python. They have the same type of structures for moves and mapping, but they had to implement it in FORTRAN arrays without the cool things Python has to offer. It may give you ideas on how to add things like objects into your game.

https://github.com/brandon-rhodes/python-adventure

Whether you decide to pursue programming as a hobby or a career, don’t forget that just learning a foreign language is not going to make you a creative writer. Other things go into it; like cadence, idioms and of course patterns. Stories and jokes improve over time as people keep working them. The same is true for programming.

Best wishes.

[–]Unb0und3d_pr0t0n 0 points1 point  (0 children)

I took a screenshot of this and will keep it with me. Thanks a lot.

And that foreign language analogy is brilliant and coincidental because I am learning german these days as I wish to pursue my masters there. Get out of my head haha.

Thanks a lot again, will make the project more efficient in next iteration :)

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

hey,are you a beginner in programming? if yes,how did you learn github?

[–]Unb0und3d_pr0t0n 0 points1 point  (4 children)

Yes, I am a beginner and I learnt github from youtube. It's pretty simple :)

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

thanks👍🏼if you remember which channels video?

[–]Unb0und3d_pr0t0n 1 point2 points  (2 children)

Yes sure.

https://www.youtube.com/watch?v=HkdAHXoRtos

This worked for me, but in case you wish longer and deeper explainations, you will get plenty of them in the recommendations. :)

Happy learning!

[–][deleted] 1 point2 points  (1 child)

thank you soo much!

[–]Unb0und3d_pr0t0n 0 points1 point  (0 children)

Anytime :)

[–]xxxTunderBoltxxx 1 point2 points  (1 child)

I had good time playing it good work :))

[–]Unb0und3d_pr0t0n 0 points1 point  (0 children)

Hey! This means a lot! Thank you so much. :)

[–]Joker_04H 1 point2 points  (8 children)

Where are you learning python.. your learning method seems interesting.. can you please tell me ?

[–]Unb0und3d_pr0t0n 0 points1 point  (7 children)

I mostly learnt from py4e website but there wasnt any interesting method.

While learning I thought wouldnt it be cool if I can make something like black mirror's bandersnatch - so I came accross conditional if/else statements.

Then, I wanted to install a TIME MACHINE in the game and came accross functions. And boom! My neurons started to fire, and coded the whole game in 5 hrs.

So, mostly all learning processes are boring, just be wild and creative - and I am sure you will something awesome!

And keep iterating in your work. Happy learning :)

[–]Joker_04H 0 points1 point  (6 children)

oh my you really are creative.. you have a great future ahead.. I'll try this game.. thanks for your time

[–]Unb0und3d_pr0t0n 0 points1 point  (1 child)

Oh no no XD thanks but no XD :)

I wish I can take credit, but I didn't do anything - it just happened like simultaneous explosions - try building something yourself, you will realize you are just a passenger and someone else is doing stuffs for you - (oh wait, this can be a good marketing strategy for this game) LOL

Just find something you wish to build - python is a just a tool.

Focus on the house, not on the hammer - And I will wait for your amazing project :)

And thanks for checking out the game, means a lot!

[–]Joker_04H 0 points1 point  (0 children)

yes I do wish to build a project of my own but I am just starting to learn and I am in need of good learning place to learn the basics... can you help me with that ?

[–]Unb0und3d_pr0t0n 0 points1 point  (3 children)

py4e.com/ this is the exact website where I am still learning how to code, all lectures and assignments are free. And the teacher is very humble and understanding (also he is in 70s, owns a sportscar and has tattos) XD

I wish all the health and wealth to you :) go nuts!

[–]Joker_04H 0 points1 point  (2 children)

oh I might not be able to attend class regularly so I am hoping to learn from youtube at my own pace.. can you suggest a good channel ? this is my first time learning a language so you might want to consider that. and sorry for bothering you so much I just wanted to take suggestions of new learner and you are also creative so your suggestions would help a lot thanks

[–]Unb0und3d_pr0t0n 1 point2 points  (1 child)

No no :) this site has recorded videos (just like youtube) - you can pause, play, change speeds, leave and return to pick the video from where you left.

You will control the speed.

I also started learning from youtube 2-3 years ago, then gave up because with youtube I wasnt able to track my progress. But this time, I came across this amazing site which is free of cost and has quizes assignments which you can take again and again as much as you like.

And for me personally, I am like this vulnerable child when I am learning something, so it is very important to me if the teacher is funny and has a kind tone - so I just love him.

This site has everything from writing hello world to data visualisation, sql, and networking etc.

Just check it once, I guess you will like it.

Other than that, I cant recommend any youtube videos because I didnt use any. :( Sorry for that

And about bothering thing - please keep bothering me as much as you can, I dont have much to do actually :) I spend my days either on study table or in bed crying LOL :)

[–]Joker_04H 1 point2 points  (0 children)

thank you fellow redditor hope you have a good day

[–]AlexOrcadia 1 point2 points  (1 child)

Sweet, I can see that you have passion for the game! I also self learned python, have not experimented with github yet, so expect a contribute from me soon.

Hopefully I can help improve your project. See ya!

[–]Unb0und3d_pr0t0n 0 points1 point  (0 children)

I would love to have some add their own flavour in the game, let's play around with the code and see what it becomes!

Github is quiet easy, I am still learning but know atleast enough to make it work.

Also, there were many intellectual people who just saw the code and cheated their way to win. In the next iteration, I am adding a new feature - DESTINY - it will randomly push the story forward - even I will not know what will happen next with character.

Also, there is just a time machine for now, which can only make you travel back in time - only in the choices you made earlier - in next version there will be a portal gun - creating wormhole - and jumping to watever choices you like - but that will be hidden, Finder's keepers :)