Trying to get the game engine in exercise 43

I’m trying to make up my own game in exercise 43.

On Zed’s advise I’ve been drawing out a game on paper, put down some some classes and now trying to get the game engine to to work where I print and use the pdb-tool every 2 lines.

But I’ve again hit a bit of a block. My shell says I can’t return a string, which im not totally sure whay that should be a problem.

class Dateprofile(object):
    #This is where a date will be generated
    pass


class Gameengine(object):
    def __init__(self, map_scene):
        self.map_scene = map_scene

    def play(self):
        currentscene = self.map_scene.start()

    


class Datespots(object):

    Datesequence = {
    'start' : Dateprofile()

    }

    def __init__(self, start_scene):
        self.start = start_scene

    def start(self):
        print('inside start')
        return 'next scene'



map = Datespots('start')

firstgame = Gameengine(map)
firstgame.play()
print(firstgame.currentscene)

Shell return:

Traceback (most recent call last):
File “swipeleft3.py”, line 61, in
firstgame.play()
File “swipeleft3.py”, line 37, in play
currentscene = self.map_scene.start()
TypeError: ‘str’ object is not callable

Anyone that can explain me why I shouldn’t be able to use the ‘next scene’ as a return?

As the error says, it’s a string. What are you expecting this string to ‘trigger’? Is it a dictionary key, a class name, a variable?

Hello @ktrager

I would like to suggest that you follow Zeds game a little bit more closely.
Especially Engine and Map classes.
Make another two perhaps three classes.
Try to make it as small as possible.
Strip it down until it stops working. One thing at the time. When it breaks. Put the last thing back.
Do changes. Same thing:
One thing at the time. Until it stops working. When it breaks. Put the last thing back.
Scrap the file and make a new one. Make it work. strip down. change it.
Boring as hell. But after about ten times you will know much more how this works.

1 Like

Hi @ulfen69 think I’m again trying to get ahead of myself. I’m following your advice. I guess it just comes down to continuous trial and error.

I did exactly the same mistake.

Now I think I have rewritten(revise) the same code (game) about 15 times.
Starting to see something in the end of the tunnel.
:sweat_smile:

1 Like

Not expecting it to trigger anything. It was just a test, and wondering why I can’t return a string.

It is returning the string but the string is then getting called (like a mothod) which throws a TypeError as its not allowed.

1 Like

You’re close on this, but I think you should write out in English what you’re really trying to do. Your thinking is muddied here:

currentscene = self.map_scene.start()

What is that supposed to do, in English. One of the reasons beginners have a hard time coding is they haven’t become accostomed to exacting explicit thought. The computer knows NOTHING, but when you talk to yourself or other people you have a ton of shorthand and ambiguities. When I read the above line I see someone who thought:

“Ok now start the first scene.”

Really? I look at what you’ve written and I see a whole lot of missing steps:

  1. Get the map_scene from Gameengine.
  2. Get the scene from the Datesposts.Datesequence map.
  3. Call start on it, even though you don’t have start defined yet.

In between that is a lot of missing steps. So, here’s my suggestion:

  1. Remove your Engine/Map code and use mine.
  2. Make your own Rooms and get them to run in the Engine, but do it IN TINY CHUNKS ONE ROOM AT A TIME AND RUN IT CONSTANTLY. I WANT A 10 RUNS TO 2 LINES OF CODE RATIO.
  3. Slowly modify my engine to study how it works and make it your own.
  4. Rewrite my engine BUT KEEP ALL OF YOUR ROOMS WORKING.
  5. Lastly, you aren’t following the procedure I laid out and you’re trying to just blast code out of your brain and hope it works. Follow the coding process I laid out:
    a. Write in plain English what you want the code to do.
    b. Write psuedo-code under the English that’s kind of a python.
    c. Write the python under the pseudo-code and run it, run it, run it, until it does what you said it should do.
    d. Delete the a and b comments when it’s all working and move on to the next thing to write.

Follow this process religiously. I use it all the time when I have no idea what I need to do, so you should too. It totally works and will help you get this all working.

Keep trying. You’re actually learning a ton of stuff about programming just attempting these little projects so don’t take anything like this as a failure, just that you learned more and need to try again.

1 Like

I’ll keep trucking…

Yep, and also feel free to take a break from this, keep doing the lessons, then come back and try again. It may sound weird but as you keep banging your head on code you eventually just wake up from the matrix one day and go “Whoa! I Know Python”.