r/learnpython • u/magestooge • Jun 10 '20
I made a silly game to practice using classes
I have been learning python for a few months, albeit slowly, because I can only do it in my free time and profession is something else. So one day I randomly decided to try making a small and silly text-based game which can be played inside Jupyter Notebook. My primary intention was to understand how to use classes. So I created a character class, a monster class, and a potion class. Then combined them into a game based on a lot of random numbers and some planned numbers.
In the game, you face a monster. You have three options, fight, run, and try befriending. If you fight, each one takes turn to strike until one is dead. The damage and health attributes are displayed on screen. Damage done is proportional to the remaining health. If you run, you lose endurance and must have higher endurance than the monster else they'll catch you. If you befriend, there's a 10% likelihood the monster will be friendly.
When you get a potion, you can take it or leave it. If you take it, there is a 50% chance it will turn out to be a trap. But damage of trap potions is lower than bonuses of actual potions.
All probabilities are based on how lucky you are. You start at 50/50 and get luckier through potions.
The game can be accessed here: https://colab.research.google.com/drive/1WcRTeaPwg3oRXzHH1m76r4SAaDJJkqSV
or here: https://github.com/gouravkr/notebooks
It's not something anyone would actually enjoy playing. But any feedback on the code will be highly appreciated.
Edit: after receiving some feedback, I changed the images to point to public URLs and reduced the number of cells to make it easier to run.
21
Jun 10 '20
Name error. Start_game() won't work.
15
u/magestooge Jun 10 '20
If you're running it in colab, you have to run all the other cells first
7
Jun 10 '20
If you're running it in colab, you have to run all the other cells first
This should be in a single block, so that you can just run it. Users are easily bored and inconvenienced. Users want challenges to come from the gameplay, they don't want any difficulties in getting the game to run.
10
u/magestooge Jun 10 '20
Noted and agreed.
Colab has a menu item to run all cells though.
6
Jun 10 '20
Noted and agreed.
Colab has a menu item to run all cells though.
Doesn't work.
6
u/beje_ro Jun 10 '20
I don't get why you are down voted... Try to correct it...
4
Jun 10 '20
I don't get why you are down voted... Try to correct it...
I voted for it, but please continue anyway.
2
u/magestooge Jun 11 '20
I hadn't downvoted it, I agreed.
I have now reduced the number of cells and added a comment on top to make it clear.
1
Jun 11 '20
I hadn't downvoted it, I agreed.
I have now reduced the number of cells and added a comment on top to make it clear.
It must be my inexperience in Google colab, I can't make it run.
2
u/magestooge Jun 11 '20
My file only has viewing permission, you'll need to create a copy to run it.
After that you can just keep pressing shift+enter to run it.
→ More replies (0)2
u/beje_ro Jun 11 '20
What I meant is that I have tried to correct it and upvoted :-)
1
Jun 11 '20
What I meant is that I have tried to correct it and upvoted :-)
It still doesn't really work.
0
u/beje_ro Jun 11 '20
I tried to correct the injustice here on reddit not the code...
→ More replies (0)
4
Jun 11 '20
If anyone wants to get it work on colab you need to remove any mentions of anything image related (or share the image folder, I couldn't find it). Then run the start_game cell after everything else runs.
3
u/magestooge Jun 11 '20
Oops. I didn't realize the images would not accessible. Thanks for pointing it out.
I'll try to point it to public folder.
1
Jun 11 '20
Looks good otherwise! If you fix the folder let me know. :)
2
2
u/Shootace Jun 11 '20
Something similar to what you created. Highly recommend this. Do them in the order below:
https://py.checkio.org/en/mission/the-warriors/
https://py.checkio.org/en/mission/army-battles/
https://py.checkio.org/en/mission/the-defenders/
https://py.checkio.org/en/mission/the-vampires/
https://py.checkio.org/en/mission/the-lancers/
https://py.checkio.org/en/mission/the-healers/
https://py.checkio.org/en/mission/straight-fight/
1
1
19
u/Ashen2b Jun 10 '20
Pretty much well done :) I think you did a great job as for a beginner in a spare time! I'm trying to learn how to code as well.
5
u/pmac1687 Jun 11 '20
This is exactly how I started programming. After I text adventure I used pyqt to add some images but transitioning to pygame after the pyqt version was completed. Looking back now I would have skipped the pyqt and went straight to pygame. Pygame was huge for me cause you could use functions in pygame but it’s infinitely easier to use classes, and OOP and objects in general can be a tough concept to grasp but pygame is visual and made the connection easier for me understand I highly recommend. Skip the pyqt tho.
1
u/healerf18 Jun 11 '20
Total noob question - how do I run the program from Github? Do I just copy/paste all the code into an IDE?
2
u/magestooge Jun 11 '20
You can do that. But in this case, since it is a Jupyter Notebook and has some images files, it's better you just download the zip file and run it on a local installation of Jupyter Notebook.
Of course, the right way to do it is to fork the repo and clone it to your local system and run it from there.
1
1
u/SmoothDaikon Jun 11 '20
how do you pla y?
1
u/magestooge Jun 11 '20
Open the colab notebook and press shift+enter to run the cells till you reach the bottom. It will start in the last cell.
1
u/karly21 Jun 11 '20
Thanks for taking the time to do this - I have marked it! I wanted to ask, I am on the same boat as you and only making small progress during my free time and full time working/house/husband not kids and hence do have some time lol
It is frustrating because a lot of the activities I am currently doing I KNOW would be done faster with Python, but I am taking ages to learn -- can you share how long it took to learn and if you had some approach to make the learning more efficient?
2
u/magestooge Jun 11 '20
My journey is very different from any regular developer. As I said, I'm professionally in a different field and work on these things only in my free time. So I have phases where I'll be doing a lot of coding, then phases where I'll not touch code for 3 months.
As far as Python is concerned, I started working with it around last year. My progress has been quite slow. But then, I'm in no hurry to prove myself either. But before Python, I had worked quite a bit on R and SQL, for data analytics, have reasonable experience with Excel and VBA, and did some Javascript during my school days around 12-15 years ago. So all that helped a lot.
This game, or whatever you want to call it, was just something I did because I needed a break from my other project, but I didn't want to leave coding and risk going into my no-coding phase.
I've only started working on actual projects 2 months ago. Before this it was all just tutorials and practicing data manipulation with random datasets and all that.
My bigger project is based on Django and Vuejs. I started both of them from scratch in April. I've made some progress, but I was badly stuck on login and authentication. So decided to take a break and do this.
I'm not a pro, so would refrain from giving long term learning advice. The only thing I would want to say is, if you've gotten then basics down, rather than learning theoretically, just get down to building the apps that you want to use. You might have to discard the first two versions and start from scratch, but it will teach you a lot. Also, when working on a problem, don't approach the solution from the perspective of the code. Think of the solution first. That is essential for learning coding.
As I tell my friends trying to learn excel, the formulas don't solve the problem, you solve the problem, the formula and functions only implement that solution.
1
u/karly21 Jun 11 '20
Thanks for taking the time to answer me - and thanks for your advice. I am not a developer either, but have always liked coding in a way - but neither my current nor previous positoins have anythign to do with it. I took interest in VBA (mainly in Excel) but the self teaching process there has been quite painful - and since I don't use it a lot, I start forgetting things, so the cycle begins again. Anyway, I am in a job which is not teaching me a lot, so am trying to get my coding skills top notch to be able to move up or move out. At the same time I think I can have a good legacy by automating a lot of things my area does and saving hours for my colleagues - so I guess that will be my little project! :)
2
u/magestooge Jun 11 '20
Yeah, don't waste time on VBA. If you understand coding, you'll mostly be able to do what you want to do with the help of almighty Google. I never formally learnt VBA and on my own, I can only do basic stuff. But with the Google, I can manage whatever I want. Besides, MS itself is moving to Typescript, they've already released it in early beta. So further development on VBA will stop in a year or two.
Python is quite useful, it is at the peak of its popularity and will easily stay relevant for another 10-12 years, as there are no major competitors yet for general purpose programming.
1
u/karly21 Jun 11 '20
Wow thanks again, I didn't realise VBA was being phased out, but actually makes sense. And as you say, googling it is quite useful, there is already some code for the quite basic things I need to do, and I know how to change it to customize it.
I di think Python has good prospects, people seem to love it. I read about an emerging language called "Julia" but I really am not an ahead of the curve and of course even if it replaces Python that won't happen for a while,so yes will stick to Python!
2
u/magestooge Jun 11 '20
Julia, Go, and Rust are all touted as the languages most likely to overtake Python in future. But they're relatively new and the available resources and guides are not even fraction that of Python. So learning them is difficult.
Good thing is, if you learn python, it will anyway make learning other languages much easier. So nothing lost.
1
u/111NK111_ Jun 11 '20
is that python? (parantheses in return and prints()/inputs() rather than print()/input() confused me)
1
u/magestooge Jun 11 '20
Parenthesis in return is optional. I'm used to using it.
Print has needed parenthesis since Python 3. Not sure about input.
1
u/111NK111_ Jun 11 '20 edited Jun 11 '20
no no i mean printS()/inputS()
rather than
print()/input()
the extra S letter
is that valid in python3?
-----------------------EDIT----------------------------------
I didnt see you defined them. Sry
2
u/magestooge Jun 11 '20
No, no. Those are custom functions defined in the notebook itself.
prints() just adds a waiting time of 1 second after every print statement.
inputs() handles a few special cases, allowing you too get help, see the rules, or quit the game at any point. I use input in 3 places, if not for the custom function, I'd have had to handle the quit, help, and info inputs separately in each place. So I created an overriding function. For all other cases, it just acts as regular input function.
0
Jun 12 '20
This is a cool concept but its amateurish at best, I went and tried it myself and it is SUCH a mess to even get started, its just stupid. I should be able to copy/paste a code, into say VSCode and run it(while making sure i do have the libraries which i did) and there is a long list of errors and its not even functional. Try actually coding in VScode instead of trash "jupyter notebook" next time, u could do better, ESP learn how to use fstrings so u dont have to concat strings making more of a mess. Hopefully u improve it so its at least functional
2
u/magestooge Jun 12 '20
Well, the post clearly says that it's a game which can be played "inside Jupyter Notebook", so no, can't be used with VS Code. It uses iPython features to some extent.
As for errors, I'm aware of a couple of them, but normally, it works.
And as dor being amateurish, I guess my post makes it clear that I'm no pro.
Apart from that, there's no specific comment from you on the code itself, nor as to what error you got.
So thanks for the unhelpful comment.
13
u/murdoc1024 Jun 10 '20
Im learning too. Im actually trying to understand object oriented programing and learning python as Well. Do you know any tutorial that explain classes but in ELI5 language?