Block Breaker is a Breakout clone written with the Swing 2D graphics API. Block Breaker also has a simple software synthesizer for sound effects.
I wrote this while learning some Java 9/10 features and IntelliJ IDEA.
The following shows the debug overlay, with stuff like hitboxes and marks showing the ball's previous collisions.
Block Breaker is released under an open-source MIT license.
Copyright (c) 2018 David Staver
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Aside from the regular gameplay controls (which are shown in-game), the following debug controls exist:
Ctrl + Xtoggles a cross overlay.
Ctrl + Gtoggles a grid overlay.
Ctrl + Ftoggles a frame counter overlay.
Ctrl + Vtoggles a velocity vector display for the ball.
Ctrl + Ttoggles an ability to throw the ball with the mouse (described below).
Ctrl + Wtoggles ball collisions with the bottom wall (instead of being lost).
Ctrl + Ctoggles marks that display where the ball has collided.
Ctrl + Htoggles game object hit box displays.
If the program is run with the argument
debug, then the game is started
with all of the above-mentioned debug options enabled rather than disabled.
Throwing the ball with the mouse works like this:
Press the left mouse button down to mark where the ball should start from.
Drag to position the ball velocity vector.
Release the left button to throw the ball.
If the game is in "launch mode", then the ball won't move from its fixed position above the paddle, but you can still click and drag to change its velocity vector.
The controls for this game were written using a
KeyEventDispatcher, because I don't
like the Swing key bindings API for games. The main reasons for this are as follows:
One of the main reasons to use the key bindings API in the first place is that it's well-integrated with the focus system, but we don't care about that for this kind of game.
The key bindings API makes assumptions about certain keys (such as
TAB, which is generally used for focus traversal, or modifiers such as
CTRL) which don't apply to games. For a game you generally want to be able to bind any key to any action, which the key bindings API does not do.
For a game, sometimes you actually want to be able to poll the control state on every frame, rather than use a command pattern.
KeyEventDispatcher would be more complicated if it was necessary to have
any sort of proper menu screen with Swing components like buttons and text boxes.
The following files probably contain the most interesting code:
Simple audio synthesis in
Sound playback and utilities in
Controls using a