Click the above-left picture of the Breakout applet to start the game full screen.
I think that the first ever video game, Pong is a pretty reasonable one to attempt for a first time Actionscript game writer. If nothing else, it provides an opportunity to create moving paddles and a ball. But it does require two concurrent players and that's not as easy to accomplish on a PC's keyboard. So forget that.
Instead, Breakout, another game that also has a paddle and ball, extends the concept with a field of bricks. Fancier variations have more interesting displays of bricks, multiple balls, and power-up options. This version just goes for the gameplay basics.
The finished product, without bells or whistles, weighs in at a paltry 7,555 bytes. The score is displayed only after the game is finished. Clicking, starts it up again.
Looking to be object oriented, but not over do it in terms of classes, I start with classes for paddle, ball, and the block (brick). Each of these draw a simple static shape. The Ball requires a few variables so is extended from a Sprite rather than a Shape.
This Shape extended class just draws a rectangle based on the parameters passed in to it. Pretty straightforward.
Each Block is a randomly colored rectangle with a thin half-opaqued edge. Hmmm, a few magic numbers that I should have cleaned up. The base color, maxC, is slightly bluish green. The contortions used in choosing the random color: maxC*(Math.random()+3)/4, permit only one quarter of the full range to vary.
The Ball class, extending a Sprite, begins with the drawing of its simple shape, a circle, but also has an update function which nudges its position by dX & dY.
The remainder of the code resides in the Breakout class. Additional logical classes could have been applied here, but this is a pretty simple game, so it hardly overtaxes a single class. Besides importing MovieClp, Event, MouseEvent, and text related classes, the PaddleHorz, Block, and Ball classes are needed, clearly demonstrating my lazy and non-canonical approach to placing my created classes in the project's root directory.
Besides the instances of PaddleHorz, pad, and Ball, b, an array, blocks, contains the blocks. Blip, Bounce, & Bleep, are the three sounds used. remainingBlocks lets us know when a level is finished. Position of blocks is assisted with bw (block width), maxBlocks, and bpr (blocks per row). Shorthand notations of sw, and sh provide values for the often referenced width and height of the stage; maxX the maximum horizontal paddle position. A textfield is created for my blurred touchspin.com logo, along with a multitasked TextFormat, tff.
All Those Resetting Functions
The Breakout.as constructor displays the game via resetGame and then puts it into a gameOver hold awaiting the player's starting click. This probably seems like an unnecessary number of similarly named functions: doResetGame, resetGame, newGame, resetBlocks -- but there is a bit of logic to it.
The event listener requires that it's handler receive an event parameter, but a similar call from the constructor does not. Also, the event handler, doResetGame, needs to remove the listener. The actual code for resetting the game appears in newGame because clearing the level of bricks during gameplay shouldn't reset the score as an actual reset does. Finally, resetBlocks removes, by name, all blocks from the displaylist and then regenerates them.
Within resetBlocks, the modulo provides the x coordinates and the floor of the dividend, the y. Embarrassingly, even though I've written this sort of thing several times in the past, it took me a bit to re-invent this wheel. I need to make some sort of mental note on this.
All the movement of ball & paddle, and checking for collisions between ball and paddle, blocks, & walls occurs in the update function. This function handler for the ENTER_FRAME listener is created in newGame.
The original Pong game had players mistakenly believing they could place a spin on the ball by sliding the paddle at the moment it contacted the ball. However, this version of Breakout has variable padDX, effectively the difference between the last and current position, which affects horizontal motion. Since the paddle is repositioned to the mouse's x position it has a much peppier feel than if it just had a fixed velocity.
A few if-s keep the ball bouncing within game board region, making a "bleep" when it bounces off the walls.
Because the root FLA file operates at 33 frames/second, the innaccuracy of the hitTestObject between the rectangular paddle and the round ball is too fast to be seen. When such collision occurs, only one tenth of the padDX delta is added to the dX as "spin".
Finally, a loop iterating through the blocks checks for collisions with the ball. Rather than any fancy explosion, or fading out, a blip sound is produced, and the block is simply removed from the displayList.
It would have been easy enough to have displayed the updated score, but, by this time, I didn't have much screen real estate left and instead opted to only report the score when the game ends.
This was a fun little project, which took me about four hours to create. If I had a complete design prior to starting coding, it would have been faster. If I had elected to add standard game features such as intro screen, help page, top scores, etc., which are standard for most games, the time might possibly have stretched to four days! But, the ease of creating such a game in such a short time plainly demonstrates Actionscript 3's power to quickly deliver results.
If you modify and create something even more wonderful, please let me know.