Halloween-themed Game

I know it’s late, but I made a Halloween-themed game for Exercise 36. I’m sure I went the long route for plenty of things, but it was a fun way to force me to learn new aspects of the program.

Please feel free to tear my code apart and let me know what I could improve on.

Special thanks to Zed (of course), @jimbot, and @ricarod for helping me with specific questions in the forums.

Here it is!

2 Likes

Hi bhedberg
I sent you an pull request on github. I corrected some missing parenthesis.
But there are some more, more complicated errors you have to fix before we can run your script:

P.S. I run your script with Python 3.6

One of this errors is on line 27:

    Traceback (most recent call last):
      File "halloween.py", line 520, in <module>
        living_room()
      File "halloween.py", line 27, in living_room
        choice = raw_input('\n> ').lower()
    NameError: name 'raw_input' is not defined

I had a look at this line. I think there is some logical error with your input inside your while loop.

Kind regards, keep coding.
DidierCH

Hi bhedberg
I think you coded your game in Python2 I tested it in Python 3 because I’m learning Learn Python3 the Hard Way and didn’t thought about it. The corrections I send you on Github will nevertheless run with my changes. So you can merge it in.

I hope I can help.
Kind regards.
DidierCH

1 Like

Thank you for taking a look at it. I suppose I should have specified that I created the game based on the LP2THW book. I started with that book before I saw that v3 had come out. I merged your pull requests, so it should (hopefully) be good to go. Thanks again!

Interesting I have not had the chance to play it but I am indeed running P3 only so know I now it won’t run for me.

Sure thing, any time @bhedberg

1 Like

Hi @bhedberg I run your newly merged game now on python2.7 and it works. But then I’m in the living room and I type in “look window” the programm will crash. Can you have a look if you get the same error?

You are in the living room. There is a kitchen is to the west.
There is a bedroom to the east. The front door is to the north.
There is a window to the south.

> look window
Traceback (most recent call last):
  File "halloween.py", line 520, in <module>
    living_room()
  File "halloween.py", line 43, in living_room
    elif ('look' in choice and 'window' in choice) and (not f_door and not
NameError: global name 'f_door' is not defined

My apologies, @DidierCH. It was a small typo, but it is now adjusted. I tested the updated file from github and it is currently working for me. Please let me know if you come across anymore bugs. I appreciate you taking the time to go through this for me!

Hi @bhedberg I forked your code again and will have a look. First try worked pretty well.

1 Like

Hi @bhedberg I had a look on your code and I have noticed that you have written some very complicated and (not if-statements.
I tried to simplify the first of them and it run the same way.

Instead of:

if ('search' in choice and 'room' in choice) or ('look around' in choice
        ) and (not k_room and not b_room and not f_door):

I have written:

if ('search' in choice and 'room' in choice) or ('look around' in choice):

It’s much shorter and less error prone and it does the same.
I think you can rewrite a lot of your other if-statements like this and remove the and (not checks.

If you do not explicitly need to exclude something you don’t have to name it, it’s true anyway.

1 Like

Hey that’s a really advanced implementation there. Good job. About the only things I’d improve are:

  1. Don’t use single letter and number variables. Use descriptive names.
  2. You can improve this a bit by taking the state variables you have at the top and reference with global and simply move them into a .py file that you import. So, put them in say, state.py, then import state. From then on no need to use global, just do:
state.dead = True
  1. Similarily, your logic for different things is complex enough that you could move those tests out into a python file, maybe called logic.py. Then you have a function for each of the tests and you pass in variables for them. You can make this move by simply going through each of your if-statements, take the complex logic of one, move it to the module, then put the function call in the place of the if test.
1 Like

That makes perfect sense. Giving it a shot now. Thanks, Zed!

1 Like