PowerBuilder Tips, Tricks, and Techniques

Berndt Hamboeck

Subscribe to Berndt Hamboeck: eMailAlertsEmail Alerts
Get Berndt Hamboeck: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn

Related Topics: Game Developer

Game Developer: Article

XNA or Game Development for Everyone - Part 2

Restructuring the game

In Part 1 of this article, we started to develop a small racing game using XNA Game Studio Express 2.0. We learned about the game loop and how it’s implemented by the XNA (by using the Update and Draw methods) framework. We also created our first track on the screen and four cars started moving on the screen, but, sadly enough, they left the track and weren’t seen again. What does that mean? It means we should take a closer look at collision detection. In addition we should design a menu with different options. You might remember that we want to have a racing game with three computer-driven cars or a network game with one or more other humans playing against us. Sounds easy? Well, it’s not that hard, but we’ll see that a few small changes in the behavior will lead to a few other problems that have to be solved (i.e., a game menu means we have to use so-called game states). Are you ready? Great, let’s go!

Collision Detection
Our cars simply leave the track and disappear from the screen or one car drives through another one, which shouldn’t happen. When the cars leave the track or crash into one another, we need to react within the racing game. This involves two different checks.

Leaving the Track
We definitely need a way to check if our cars are still on track. This can be done easily if we take our track (see Figure 1) and open it with our favorite image processing tool (e.g., .NET Paint or Photoshop). Change the image so it looks like the one in Figure 2. I think you can see the difference. The road – where the cars are allowed to drive – is gray; everything else – where the cars are not allowed to drive – is painted white.

This gives us exactly what we need. We are able to compare the current position of any of our cars against the position in the picture. If the color is gray, everything is fine, and the car is on track. If the current position’s color is white, then the car is leaving the track, which means we will stop the car immediately. What are the black and red lines good for? The black one is the start and finish line so you can count the laps for every single player. The red one determines when the car really finished a lap. You might remember that the XNA framework updates and redraws the scenery several times a second; it wouldn’t be correct if we determined that we are on the black line several times a second and updated the lap counter more than once. The green line is there to prevent cheating. One of the first things we tried in the original game was to cross the start line, turn around, drive back, and turn around again and finally cross the start line again to get another lap counted. This is not supposed to work and is handled by the green line. Only when the green line is crossed can you pass the start line later on and get the lap counted.

More Stories By Berndt Hamboeck

Berndt Hamboeck is a senior consultant for BHITCON (www.bhitcon.net). He's a CSI, SCAPC8, EASAC, SCJP2, and started his Sybase development using PB5. You can reach him under [email protected]

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.