r/roguelikedev • Robinson • Aug 01 '17

RoguelikeDev Does The Complete Python Tutorial - Week 7 - Part 10: Main Menu and Saving

This week we will cover part 10 of the Complete Roguelike Tutorial.

Part 10: Main menu and saving

No bonus sections this week


FAQ Friday posts that relate to this week's material:

#20: Saving

Feel free to work out any problems, brainstorm ideas, share progress and and as usual enjoy tangential chatting. If you're looking for last week's post The entire series is archived on the wiki. :)

30 Upvotes

36 comments sorted by

View all comments

Show parent comment

1

u/Daealis Aug 13 '17 edited Aug 13 '17

Oh boy I'm late with this one. I had some initial issues and then just kinda felt out of the groove trying to work out the kinks. But I'll be damned if I give up this close to finishing my first RL.

Now I already typed in a plea for help because I had two issues, but as I typed them in I also tried to google the relevant stackoverflows or documents, and found answers to both my problems. So I got some catching up to do, but first commenting on this part.

Still alive and kicking along with Python 2.7 and Libtcod.

So, the first issue I ran into with this is that 2.7 obviously doesn't have FileNotFoundError. Going around that is pretty straightforward, I just switched it to a ValueError and catch that instead. Cool. Don't know if this will cause me issues later on, but works for now.

The thing that got me really stuck and kinda dampened my spirits was an issue with Shelve. Both load and save complain about the same thing and I couldn't figure out why. Saving even worked somewhat, it created a savegame file with 24kb of content. It still crashed with the same error message as load:

AttributeError: DbfilenameShelf instance has no attribute '__exit__'

Okay, there's no **exit ** attribute for some reason in 2.7 versions. Luckily there's a workaround.

import contextlib
...
with contextlib.closing(shelve.open('savegame', 'n')) as data_file:

And there we go, saving and loading are working. With that I'm done with this week, still going against my better judgement with 2.7. It does help in that aspect that I have to actually do some work of my own, instead of straight up copy and pasting with minimal changes. Have to dig for answers and understand some things about a language that I've never really written with before.

Thank you again u/AetherGrey for the excellent tutorial and I'm off to do the next part.

//Edit: One thing that does seem like an error: The error popups if there's no savegame and you try to load it. The text is off center and blends into the menu.

1

u/AetherGrey Aug 13 '17

It does help in that aspect that I have to actually do some work of my own, instead of straight up copy and pasting with minimal changes. Have to dig for answers and understand some things about a language that I've never really written with before.

This is the best way to learn! Out of my own curiosity, is there a reason you're using 2.7?

Regarding the error popup: yeah, it doesn't look that great. I think it's the way the original tutorial had it, so that's why I did it that way. I might go back in the future and try and make that work better. The more obvious solution would be to gray out the "load" option and simply do nothing when it's selected.