Dec-06-2019, 02:49 AM
There's a lot going on in that code lol. If it works, that's cool, but I'd like to offer some notes for the next project:
1) try to stick to a single event loop. The snake game has 6. https://gameprogrammingpatterns.com/game-loop.html
2) I'd recommend looking into State Machines to help control what the game should be doing at any given point in time. A Menu-State, Game-State, PauseScreen-State, etc. That will help with having just one event loop, and will also help with keeping the call stack down (what I mean by that is, you don't currently use while loops for repeating events... if you PlayAgain 20 times, you've added 20 things to the callstack when really that should have been 0). I don't think this would actually help with performance, but it'd help make the code much cleaner to look at, which then makes it significantly easier to add features or fix bugs. https://gameprogrammingpatterns.com/state.html
3) Personally, I hate the fact that there's a
4) I also personally hate global variables. But a lot of that might just clean itself up if you switch the control flow to use a state machine.
1) try to stick to a single event loop. The snake game has 6. https://gameprogrammingpatterns.com/game-loop.html
2) I'd recommend looking into State Machines to help control what the game should be doing at any given point in time. A Menu-State, Game-State, PauseScreen-State, etc. That will help with having just one event loop, and will also help with keeping the call stack down (what I mean by that is, you don't currently use while loops for repeating events... if you PlayAgain 20 times, you've added 20 things to the callstack when really that should have been 0). I don't think this would actually help with performance, but it'd help make the code much cleaner to look at, which then makes it significantly easier to add features or fix bugs. https://gameprogrammingpatterns.com/state.html
3) Personally, I hate the fact that there's a
quit()
function available in python. The program ends just fine when it reaches the end of the file, and things are easier to understand without sudden exits like that. You could just as easily replace all the quit() calls with a return statement, which has the added benefit of syntax highlighting in editors, so it sticks out a bit more. Plus, it helps with debugging when you can unwind what's happening, instead of the program just killing itself lol.4) I also personally hate global variables. But a lot of that might just clean itself up if you switch the control flow to use a state machine.