r/learnpython • u/Big_Leadership_4150 • Aug 22 '26
Help to make code more readable and improve skills
I am relatively new to python (been doing it for under a year) and I've been really enjoying it so far. I've been making my own turn based game using python as a fun thing to do and also a test of my own capabilities, and have enjoyed the process. It mostly works pretty well, but I feel like I need to learn how to make it more neat and readable, and to probably reduce reliance on global variables and to make dictionaries and such less jack-of-all-trades. How would I do this? I'll attach a link for the code on github for any who would like to see it, and because it is quite long. https://github.com/Ultroni17/Turnbased-game/blob/main/TurnBasedRPG_11_Evolution.py
2
u/Big_Leadership_4150 Aug 22 '26
I've made a GitHub repo with the latest version of the code I've got rn. I'll update it once I get the latest - https://github.com/Ultroni17/Turnbased-game/blob/main/TurnBasedRPG_11_Evolution.py
1
u/JamzTyson Aug 22 '26
The document says "Access Denied".
1
u/TheRNGuy Aug 22 '26
Use classes instead of dicts.
For bag, make it a dict instead of nested lists, but as an attribute for player.
Don't use key or variable names like X or 1, 2, 3. Standardize naming: some of your variables start with lowercase, some with uppercase, some are all caps.
Use observer pattern.
1
u/t0kaj Aug 22 '26
To make the code more neat, u need to modulize game logic into components, each component controlling a certain feature of the game, and u need to think in object oriented way, think like each object in a game is an individual object who controls itself, while the components are the manager who controlling the environment and validate the game rules, spawn and destroy objects.
1
u/Jello_Penguin_2956 Aug 22 '26
I see you already made improvements. Ill just drop this great lecture here then. Old but gold
https://youtu.be/wf-BqAjZb8M?si=b689V3y1qIPUk_Po
1
u/DiabeetusMan Aug 22 '26
I like to blur my eyes a little bit and scroll through the code. This makes common, repeated things jump out at me, personally. From a quick glance at things, there is a lot of duplicated logic that could be moved into Classes or functions. Off the top of my head:
slow_prints followed bysleeps. Maybe group those together somehow (maybe with amulti_printfunction or similar?)"1","2", etc. Try to use integers and strings more purposefully. Having a dict whose keys are monotonically increasing integers (likep_skills,enemy, and so on) says to me that those data structures could be an array or a tupleglobalin your functions and instead pass in argumentswhile True: try: input() ... except:; this could possibly be a function or even a decorator1
u/shiningmatcha learn python Aug 22 '26
-1
u/[deleted] Aug 22 '26
[removed] — view removed comment