0
How to make my code cleaner and less verbose?
I wrote a simple game recently and I'm not that happy because the code is too verbose. Can somebody give me advice or point me to some kind of technique to improve the code so it will be much cleaner while maintain readability? https://code.sololearn.com/cM5X6274y3TT/?ref=app
8 Réponses
+ 5
Nice job! 👏
Here are a few resources to help you 'beautify' your code:
https://code.visualstudio.com/docs/JUMP_LINK__&&__python__&&__JUMP_LINK/formatting (check out the autopep8 and black formatter extensions)
https://pypi.org/project/autopep8/
PEP8 Style Guide:
https://peps.python.org/pep-0008/
https://geeksforgeeks.org/pep-8-coding-style-guide-python/
https://realpython.com/python-pep8/
You may also want to consider breaking up the code into multiple files...2 or 3.
Hope that helps 👍
+ 2
Keith
I split most of the string into list so it can adapt the playground environment while keeping the code easy to read and still able passes to textwrap wrapper or other formatting. Current coding style is the solution after asking question in other post.
I read about PEP8 and tried to limiting line length to 79, but I extended it to 100 to cover most of the lines for this project, yet there a few lines exceed the limit.
I just don't comfortable about the opening and closing brace practice, which makes the code like Java or C family with lots of closing brace on their own line.
And yes, Lothar did suggest putting the string into another file. Yet again it won't fit the playground. However I will try this method on my computer locally.
+ 2
Wong Hei Ming yeah, I was following along on the other post that you're talking about. Overall, you did quite good!
+ 2
Wong Hei Ming
I went over your code and you mentioned implementing a web version.
I found it interesting and made a quick mockup. All that is needed now is to port your game logic to javascript...
https://code.sololearn.com/W9z06hWK83hK/?ref=app
+ 2
Bob_Li
Good work!
But ultimately you need a die or similar method to determine how much ap the winner get.
It is 2 states. The logic is deeply hidden in line 855-857, it keep re-rolling if there is a tie.
+ 1
Bob_Li
The interface is interesting, and I imagine there will be two ships showing up in the main area during battle sequence.
I have an idea to draw the ship in svg format because it support event handler from my research. The color of turrets and shield will change accordingly to the ship status.
The health is an undefined yet calculated "attribute", use to determine which ending we will get and is hidden from the player.
The "health" concept was introduced after writing the endings and main function but before the game engine. I thought the game has different ending would be fun but ignored how to pick the ending. When I realized the problem I modified the main function and add the health property at __init__ with a sum of other 3 properties. It turns out it won't update on the fly so I removed it from __init__ and defined it as a function.
Now to think of it I could use a setter to update the property after the battle ended.
+ 1
Wong Hei Ming
The interface is just for fancy ui. random choice is actually not hard to implement. Making the animated interface actually made it more complicated than it needed to be.
I think there's no point in dice roll if it's only 2 state. Maybe a random choice is better, like random.choice(player,ai)? or random.randint(1,2)
another random.choice(1,n) could be used for the ap value. But at least no cycle is wasted for tie-breaking.
Something like this:
https://code.sololearn.com/cvXeWCGh6o76/?ref=app
0
Wong Hei Ming
since it's just a prototype, I'm tyring to keep the interface minimal and easy to modify. Its still missing a random turn selector and I was thinking instead of a dice roll, maybe something different, like a random choice machine.
Is your game two or three state? dice rolls and rock paper scissors are 3 state: win, lose, tie. 2 state is a binary condtion like a coin flip, win or lose.
I made a crazy css/js 2 state widget. I think it's possible to modify it to multistate. Right now it is button-controlled, but I think it could be modified to stop at random time ranges.
https://code.sololearn.com/W4sQrDDJcwT3/?ref=app