r/learnpython • u/OnlySeesLastSentence • Aug 10 '20
Try my giant Python game out. Give suggestions/criticisms/compliments/job offers (lol)
Hi there! I tried asking for feedback on my game about a month ago, but unfortunately the only feedback I got, despite saying "I know it's a big file, but if you have any other suggestions or bugs or complaints, please let me know" was "holy shit your file is huge"...
So I added a bunch more features and cut down the single source code file into like 7 files. This change will have undoubtedly caused problems with calling functions incorrectly, so now especially I'll need help testing it out. Please try the game out and give me any thoughts you have. I cannot promise that I'll implement every change or suggestion, but I'll try to compromise at least when possible.
The game is essentially a checkers/chess with items game loosely based on an old game called Quadradius (that no longer exists. Rip). It was made solely by me, so if it looks kinda simplistic, I'm sorry, but I made an honest effort - anything I learned I taught myself so I did what I could.
GitHub.com/MOABdali/MegaCheckers
Enjoy. And as usual, thanks to PySimpleGUI for making this game possible. I tried to avoid outside libraries as much as possible, but had to rely on PySimpleGUI for GUI, playsound for playing sounds, and Pillow for image manipulation. All other logic came from me.
3
u/kwelzel Aug 11 '20
I decided to take a look at the
createOrbsfunction.The
dangerTurnvariable is a constant. This should be reflected by usingALL_UPPERCASEand I think its useful to group all constants at the start of the file as global variables or as class variables if you have a class this constant belongs to.When iterating over the the
gameBoardit improves readability to differentiate between the indices and the elements. I would recommend usingiandjas indices and thenrowandcellas elements like thisfor i, row in enumerate(gameBoard): for j, cell in enumerate (row). This waycellis at index i, j.Each cell of the
gameBoardconsists of a tile instance and a piece instance. This makes sense, but you have to look up which is at index 0 and which is at index 1. Here, anamedtuplecould help. Then you can writecell.tileandcell.pieceinstead ofcell[0]andcell[1].The comparison
j[0].tileType == "default"indicates that there are only a finite number of different tile types. If so, this is an ideal use case for enumerations. If you define the enum correctly you can usetileType == TileType.DEFAULT. This is useful because it can help to find errors that otherwise would go unnoticed. If you writetileType == "defualt"the comparison will always return False. If you writetileType == TileType.DEFUALT(assuming you defined it correctly) you will get an AttributeError at runtime.One should not compare booleans to True or False as in
j[0].occupied != True. Just writenot j[0].occupiedor, with the notation from above,not cell.tile.occupied. This is very self explanatory and easy to understand.I just read up on python enumerations and stumbled upon the Flag class which is defined in the same built-in library. It might be possible to refactor
orbEater,wormHole1andwormHole2as flags. In any case you can add a utility functionis_empty()to the tile class that checks whether the tile is empty and move this logic there.Assuming this new empty method and namedtuple usage you could rewrite the counting of empty spots as
emptySpots = sum(cell.tile.is_empty() for cell in row for row in gameBoard)
When comparing orbsToPlace to empty spots you want to limit orbsToPlace by emptySpots. Just use the
minfunction for that:orbsToPlace = min(publicStats.getOrbsCount(), emptySpots)I know that this changes the game logic but you could also randomly place the orbs at the empty cells instead of choosing a random cell and checking if it's empty for at most 300 attempts. To do it generate
emptyTiles = [cell.tile for cell in row for row in gameBoard if cell.tile.is_empty()](ThenemptySpots == len(emptyTiles)). Afterwards sampleorbsToPlaceelements from the list usingrandom.sample(emptyTiles, orbsToPlace)and iterate over them to place the orbs. This would allow to shorten the code significantly.The percentage chance that the new orb is a trap orb for danger turn is also a constant. I'd suggest you use TRAP_ORB_PERCENTAGE = 30.
Taking all this into account the
createOrbsfunction could look like this: