Basketball

My very first sports game! A simple basketball game. Get the ball into the basket. No nonsense, really. The physics for this game was relatively simple. And fun. But then again, physics is always fun.

The key here is to setup a good aiming system. The game builds itself around that. One of the best solutions is to represent exactly half the parabolic path of the ball. The player will still have to judge the path of the ball, but the portion that we show on the screen would give him an idea of the velocity and angle.

Our interest is in calculating the path of the ball, which is determined by two parameters:

  • The velocity
  • The launch angle

Given that the mouse pointer represents the highest point on the parabolic path, both launch velocity and launch angle can be calculated. It’s really simple (should you have taken physics in high school).

Parabolic path of the ball

 

In the above equations R is the range of the projectile and H is the maximum height to which it rises. It’s easy to see why, for a given position of the mouse, R and H are constants that can be calculated. Rearranging and solving these equations will give us the velocity and angle which is what we need to describe the motion. The velocity will then be resolved into components, for convenience.

The game depends on a few libraries like SOIL for texture loading, freeglut for openGL and irrKlang for audio.

Coming up next is a version with highscore tables! Gotta learn a little php for that 😛

Edit: High score table is live but a little buggy.

Available on gumroad:
Basketball

Source

Advertisements

Project Euler – Problem 6

Problem 6 is by far one of the easiest problems yet.

The sum of the squares of the first ten natural numbers is,

12 + 22 + … + 102 = 385

The square of the sum of the first ten natural numbers is,

(1 + 2 + … + 10)2 = 552 = 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

Again, there are at – least two possible methods. One is our dear friend, Mr. Brute Force 🙂 The other is more mathematical.

Brute force description

There’s not much to be said about this one. Create loops to add the sum of numbers from 1 to 100, and another loop to add the squares of numbers from 1 to 100. Square the sum you get in the first loop, and then find it’s difference. It’s simple.

Mathematical Approach

To understand the mathematical approach, you must have knowledge of the two formulas given below:

It’s all high school maths. They are easy to prove as well, especially the first one.
Writing out functions for the above should not be a difficult task, whatever language you choose.

In my version, I’ve written them as Macros (using the # pre processor directive)

Comparison

For a single iteration, both methods take less than 0.001 s or 1 ms which means they can’t be measured. Over 1,000,000 iterations, the brute force method takes 0.723 s while the other approach still clocks in at a nifty 0.001 s.

Solution in C++

Falling Balls listed on games.softpedia.com

Falling Balls, which recently got listed on download.com, is now also listed at softpedia.com! It’s got about 9 downloads so far.
Visit the page or directly download: 

Shoot ‘Em Up

Shoot ‘Em Up is a simple shooting game, where you have to survive for as long as possible. Enemies spawn at random locations and move towards you.

Kill them by firing bullets.

This particular game involved a bit of math, especially making the enemies re – orient themselves when the player moves.

It’s trigonometry time!

Here’s a short of explanation of the math behind that particular feature:

For this game, I’ve written a small 2D Vector class which uses Unit Vector Notation to represent vectors.

Both the player and the Enemies are objects of the particle class. The floating point co – ordinates, and the velocity of a particle are stored as vectors.

The acute angle between the line joining the two particles, and the  x – axis is given by:

Basically, the enemy must move along this line, if it is to reach the player. When this angle is calculated, the velocity of the enemy may be in some other direction. Our goal now, is to “point” the enemy in the direction of the player without changing his speed.

Speed is defined as:

The mod() function of the Vector class gives this result. To re-orient the velocity vector along the new θ, keeping it’s magnitude constant, we calculate the new x and y components as follows:

There is one problem. θ will always be between 0 and π/2. Hence cos θ and sin θ will always be positive. To get around this, we check the sign of Δy and Δx. If Δy is negative, multiply the y component by -1, and if Δx is negative, multiply the x component by -1.

Pointing bullets towards the mouse works in a similar fashion, where the position of the mouse replaces the position of the enemy. Also, the bullet is pointed towards the mouse only at the time of firing.

So there you have it. Application of high school maths and physics in programming. Hope this helped 🙂

Screenshot of the game:

 

Download the game

Download the source

View the help file

Blackjack

Blackjack is the most widely played casino banking game in the world. It involves a dealer and one or more players. Each player is dealt two cards and then has the option to take additional cards to bring the value of his hand to 21 or less.

A game of Blackjack with the hand 21 (Image credit: Wikipedia)
Blackjack has been the target of many high-profile advantage players especially card counters, which prompted casinos to take preventive measures such as the use of 7 to 8 decks and the installation of the “eye in the sky”.

The movie 21 is based on the real life story of MIT students who trained as advantage player and made millions from Vegas casinos.

Unfortunately, when I wrote the game, I wasn’t too familiar with the rules of blackjack. The game play is somewhat lacking in experience. But, it was my first project involving texture loading, which was a big step forward.

Screenshot:

Blackjack Screenshot

Download the game

Download the source

View the help file

Brick Breaker

Another remake of the classic game. Originally called Breakout by Atari, it spawned a new era for video games, and is seen as the icon of the arcade culture.

Atari Breakout (Image Credit: Wikipedia)

Pictured to the right, is the original Atari game. The idea was conceptualized by Nolan Bushel.  Countless remakes of the game have been made. Some such as Arkanoid, DX Ball, DX Ball – 2 have achieved notable fame, rivaling that of the original itself.

Checkout this Wikipedia article for a full list of notable Breakout clones.

While my version may not be as detailed or feature filled  as some of the other “clones”, it’s still fun to play. With unlimited lives, and five levels, it can be looked upon as a Zen Version of the game.

Screenshot of my version:

Download the source

View the help file

Fork on Github

Tic Tac Toe OR (X & O, if you like it that way)

Who hasn’t played Tic Tac Toe ?

Also know as Noughts and Crosses, its one of the best games to fill those idle moments, any-place, any-time!

Having previously written a console version of the same game, I thought it would be cool to rewrite the game with GUI elements, and to update the logic a little bit.

Tic Tac Toe is really easy to program, even for a newbie.  Try your own hand at it, or look through the source code to get some tips.

See the animation below:

It’s not that hard to get the hang of. First person to get 3 X’s or O’s in a row wins!

Download the game

Get the source