I implemented the classic Snake game (a.k.a “matopeli” in Finnish) using Java and Swing in retro style:
Why Java and Swing?
Originally, the idea was to implement something to demonstrate to students in our GUI design & implementation course. Namely, how to have one frame (window; JFrame
) and be able to easily switch between different content views (JPanel
s) within that window, by using Swing CardLayout
. And since Java and Swing are the default tools to use in this course (they can pick almost anything else they like, though), that’s why.
The game settings panel, hall of fame panel and the actual game view panel are all included in the same frame using the CardLayout
. The demo also shows how to use keyboard shortcuts and buttons to switch between the three cards.
As usual for me, things got out of hand and I ended up implementing the whole game with all the features (I came up with). Unnecessary for the original purpose of the demo, but more fun than other more boring responsibilities, like various macrodata refinement types of tasks.
Since this app is “too complete” and could be used to copy-paste solutions in the course, the plan is not to publish this in source code. But parts of this can be included in (future) tutorials on how to:
- use the
CardLayout
, - play the game using keyboard,
- control the game using a
Timer
andTimerTask
, - draw graphics on the screen,
- store and restore settings using
Properties
, and - store and restore the hall of fame stats so that users cannot (at least easily) cheat by editing the hall of fame file manually.
The Hall of Fame file is scrambled using Base64 encoding and the Cesar cipher, so not actually rocket science. The scrambled Hall of Fame contents you can see in the video looks something like this, when scrambled:
OknoKOie4VrfFNtlmHHQ}gHQ7oHQ~YHR7InG4YnoKOie4VrfFNtlm...
Not something you can edit just by looking at it.
Anyone could try out implement the same scrambling algorithms (since I have now revealed all the important secrets about it!) and attempt to “decrypt” the file and then manually put themselves at the top, then “encrypt” it back into the file. They can then be proud no one ever reaches the same level of gameplay again… Damn those clever cheaters! Luckily one can always delete the file and start from scratch.
Thanks for reading and have a nice rest of your day!