Latest Tweet:
  • Loading...

I just completed my first XNA project, the very "Hello World" of game programming, a PONG clone! But as Tino Didriksen points out in his post "But Can You Make Pong?", PONG is an excellent project for any incipient game programmer. A simple game like pong touches all the major aspects of any computer game, such as:

  • Game Logic: Managing different game states, such as menu, playing, pause, end screen etc.
  • Graphics: Drawing simple 2D graphics, scores, menus etc.
  • Sound: What is PONG without the *doink* when you hit walls and paddles?
  • Controller Input: Your game should work with different input mechanisms like game pad, keyboard, mouse etc.
  • Physics: Velocity in 2 dimensions, collision detection, calculating reflection angles based on what part of the paddle that hit the ball.
  • User Interface: Any game needs menus, scores, a title screen and a end screen.
  • Computer AI: Well, PONG isn't AI in the traditional meaning of the word, but making the computer fun to play isn't as easy as you might think. It shouldn't be to hard or to easy to beat the computer.
  • Two player gaming: Gaming is always more fun when you play with friends.

Tino dives a little deeper into all the aspects of PONG in his blog, and discusses why PONG an excellent game for beginner, so I won't spend more time on it in this post. I strongly suggest checking out his post.

So as I mentioned in the beginning I just completed my implementation of PONG written using the XNA framework. I wanted my PONG game to be more "complete" than some of the XNA PONG samples available online. There are some nice tutorials you might check out. One of them is a video tutorial over at LearnXNA that walks you through an implementation of Pong. Two other implementations worth checking out are Rob Loachs' XNA Pong and Tiny Tennis up on Coding4Fun.

Some of the things you might learn from my implementation of PONG is:

  • How to do simple 2D graphics in XNA
  • How to handle different game states
  • How to implement a menu system
  • How to manage different controller mechanisms, such as game pad, keyboard and Wiiremote
  • How to do both one and two player gaming

I'm not going to include too much code in this blog post, but the code is well documented and should be a usable sample for XNA beginners. One of the "cooler" things in the sample is how I handle different input mechanisms. I've abstracted away the different controllers by creating an interface that the different classes handling the different controllers implement. So when the game starts I create an instance of the InputPC class, which tries to create an instance of all available controllers, and adds them to a prioritized list. I then iterates over the list and assign the best available controller to player1 and player2.

To handle cross platform support I've simply created a Xbox 360 project and linked in most of the files. There are only three classes that differ between the 360 and the Windows version of the game.

Another funny feature of the game is the "Pong Clock". When in the main menu the computer will play in the background, and the score will show the time. The left side loose once a minute, the right side will loose once an hour.

  

WiiMote
Robs post about his flock of butterflies flying around in XNA controlled with the WiiMote inspired me to pick up a WiiMote and play with it my self. As Rob mentions in the blog the hard part is not programming against the device, but getting it connected to the PC. The remote is quite picky about Bluetooth devices and stacks, and I spent quite a few hours to get this working. I tried using both my internal Bluetooth chip (IBM ThinkPad T43p) and an external Billionton Dongle. I also tried all kinds of Bluetooth stacks, including the Vista default one, the IBM drivers, the latest version of the Widcomm stack. I finally got it working using the Billionton external dongle and the Toshiba Bluetooth stack. Rob also had to use the Toshiba stack, so looks like they have a decent Vista implementation, so if you have problems connecting the WiiMote on Vista they might be worth checking out… For more information about the WiiMote and how to get it connected check out the WiiBrew Wiki.

The actual integration of the WiiMote in the game was fairly simple. To communicate with the WiiMote I used a managed API implemented posted up on Coding4Fun on MSDN. I created a new class that implemented the IPongInput interface, and use the buttons to navigate the game UI. To handle the actual game play I read the value for the Y axis from the accelerometer. The WiiMote spits out values from -1 to 1, and I simply check if the value is more than 0.05 or less than -0.05 to detect if the player is moving the paddle up or down. I also create a "effect" parameter to calculate the Y-speed of the paddle.

So if you hold the WiiMote at a steep angle you'll get more speed on the paddle. After some tweaking it turned out really nice!

Downloading and playing the game
I've included the compiled Windows version of the game, a XNA Creators Club compiled package of the 360 version, and the full source code of both versions. To play the game on your Windows computer you need the XNA runtime. The game is written using the recently released XNA refresh, so if you haven't downloaded it you need to do that first. To play the 360 version of the game you need a "XNA Creators Club Subscription" and XNA installed on your computer. To deploy the game to the 360 simply double click the "package" to start the deployment process.

The game is licensed under the BSD license, so you're free to do what ever you want with the code. Have fun go mad!

<September 2010>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789