Skip to content

Demo: Pong

bgroenks96 edited this page Jan 30, 2013 · 6 revisions

The source for this demo can be found in the demos respository: com/snap2d/demo/pong/

This demo shows a working, simple implementation of Pong built on Snapdragon2D. It uses a lot of the various aspects of the APIs, so it should make a great reference for learning how to work with the engine.

Note that many aspects of this demo are simplistic and fast-written for the purposes of just demonstrating how to build a simple game. For instance, the physics implementation uses real physics collisions rather than the way Pong actually behaves, so as a game it's actually quite boring. However, it does a good job of showing how to use the 2DX-GL Physics API and how it works.

Also note how the ball sometimes passes nearly completely through the paddle before the collision is applied. This is actually because this game, for simplicity, uses very simple collision logic. It simply applies collision logic to the PhysicsNode when the CollisionEvent is received. This means the ball will still update its position normally at the instance of collision. It is entirely possible (and not too difficult) to implement a finer-tuned collision system that will only allow the Entity to reach a certain point if a collision has occurred.

The application itself consists of only three classes: PongGame, Ball, and Paddle. PongGame initializes and maanages the game components, and Ball/Paddle represent the ball and two pong paddles in the game (both subclass Entity).

Hopefully you will find the source for this demo helpful. It is fully compilable with the 2DX/Snapdragon2D libraries. You can click on the Java Web Start deployment link below to try the demo. Enjoy.

Play the Demo (JWS)