Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to develop Game application for iPhone devices

This article will teach on the iPhone Game Development from the scratch that includes game development basics and iPhone programming fundamentals to guidelines for manage with special graphics.

Let us know how we can create an iPhone game from the land up. We all are aware that about the types of Steep Learning Curves such as OpenGL and Quartz. Let us make things much easier as far as the development process is concerned and avoid using such technologies. The graphics that we will be using will utilizUIImageViews. The game that we are going to create will comprise of graphics, sounds, game mechanics, and even some simple computer AI.

Let us create a game called ITennis. It is basically stink with a tennis thesis. It follows all of the same rules and mechanics of stink. This game article won’t require much of an animation stuff thereby selecting this game to design the development for. The below figure represents the game screenshot:

Represents the screenshot of the game being developed

Figure 1: Represents the screenshot of the game being developed

The following image will be required for this project.

Represents the image required for the project

Figure 2: Represents the image required for the project

Now, create a View Based Application and Open up XCode file. The name of the application is iTennis. This template provides a starting point for an application that uses a single view. It provides a view controller to manage the view, and a nib file that contains the view. Here, is the new project:

After you unzip all of the images, drag them into the project Resources folder. After you have done this, the resources folder should look like this.

Now we are ready to start coding. We need to first establish our IBOutlet

Representing the template to be selected for the new Project

Figure 3: Representing the template to be selected for the new Project

Here is how the resource folder looks where the images required to build a game have been moved in.

Now we are prepared to start coding.

Representing the Resource folder structure

Figure 4: Representing the Resource folder structure

First, we need to launch our IBOutlet connections so that we can easily relate with our images in code. We will also insert a good number of game variables that we will be using in this article. The following code needs to be added in iTennisViewController.h:

# import   <UIKit/UIKit.h>
@interface  iTennisViewController   :   UIViewController   {
        IBOut let  UI ImageView  *ball;
        IBOut let  UI ImageView  *racquet_yellow;
        IBOut let  UI ImageView   *racquet_green;
        IBOut let  UI Label             *taptoBegin;

        IBOut let  UI Label  *player_score;
        IBOut let  UI Label  *computer_score;

        CGPoint  ballVelocity;

        NSInteger  gameState;
}

@property(nonatomic,retain) IBOut let  UI ImageView  *ball ;
@property(nonatomic,retain) IBOut let  UI ImageView   *racquet_green;
@property(nonatomic,retain) IBOut let  UI ImageView   *racquet_yellow;
@property(nonatomic,retain) IBOut let  UI Label             *tapToBegin;

@property(nonatomic,retain) IBOut let  UI Label             *player_score;
@property(nonatomic,retain) IBOut let  UI Label             *compuer_score;

@property(nonatomic) CGPoint  ballVelocity;
@property(nonatomic) NSInteger   gameState;

@end

It can be seen that we have a good number of IBOutlets. This is because; we can easily interact with the many objects. For the time being, let us ignore the ballVelocity and gameState variables. Now, we are prepared to construct our interface.

We need close Interface Build and return to XCode once the connections have been made. Some constants are required to set up the code. These values can be hardcoded however coming with a constants is actually simpler to alter when the mechanics of your game is tweaked. The following code needs to be added in iTennisViewController.m:

 #import  “iTennisViewController.h”

 #define   kGameStateRunning  1
 #define   kGameStatePaused     2

 #define   kBallSpeedX  10
 #Define  kBallSpeedY  15

@implementation  iTennisViewController
@synthesize  ball,racquet_yellow,racquet_green,player_score,computer_score,gamestate,ballvelocity,tapToBegin;

Let us try to see the idea behind the constants. The first 2 are game states. We use these to determine what needs to be displayed on the screen. We would need to see if the game is paused or is it running and is it at the title screen?

The next 2 variables are nothing but the speeds for the ball to journey. For the reason that game is in 2D, the ball can actually have an X and Y speed. This is required in order to make up its rapidity vector. The getter and setter are the two known methods which are used to synthesize the variable in the next line automatically.

The below is a snippet of viewDidLoad method. The subsequent coding needs to be added:

// Implement  viewDidLoad to do additional setup after loading the view, typically from a nib.
	(void)viewDidLoad  {
[super viewDidLoad];
self.gamestate = kGameStatePaused;
ballVelocity = CGPointMake(kBallSpeedX,KBallSpeedY);
[NSTimer  scheduledWithTimeInterval:0.05 target:self  selector:@selector(gameloop)  userInfo:nil  repeats:YES];
}

As can be seen above, the game state is set to a paused game position. The game doesn’t start at the moment when the view loads. On the next step, we have to create a vector for the ball’s velocity. Adjust all the constants that we distinct before for a faster or slower ball speed. Finally, NSTimer instance needs to be created.

The time step is the first parameter of the ball’s velocity. The time step has been set to 0.05 seconds. The speed of the entire game will change after adjusting the time step.

The next step would be to set the target. This is telling the timer the location on the call-back. The call-back is the next parameter of the ball’s velocity. On the whole, we are telling the timer to call self.gameLoop after every 0.05 seconds of interval.

Let’s generate the game loop once the game is initialized with the help of below code:

(void) gameLoop {
    if(gameState == kGameStateRunning) {
  
    Ball.center = CGPointMake(ball.center.x + ballVelocity.x ,          ball.center.y + ballVelocity.y);

    if(ball.center.x > self.view.bounds.size.width || ball.center.x<0){
         ballVelocity.x = -ballVelocity.x;
    }

    if(ball.center.y >self.view.bounds.size.height || ball.center.y<0){
         ballVelocity.y = -ballVelocity.y;
    }
  }else{
      if(tapToBegin.hidden){
          tapToBegin.hidden = NO;
    }
  }
}

Here we are verifying to see the mode of the game whether it is in a running state since we don’t want to move the ball in a paused state. The tapToBegin label will be displayed if the game is not running else we start to move the ball according to its velocity vector.

The following coding will describe about the process of bounds checking. We would like to reverse its velocity in order to make “bounce” at the time when the ball hits the side of the screen.

(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   If(gameState == kGameStatePaused) {
       taptoBegin.hidden = YES;
       gameState = kGameStateRunning;
}
   }

What we need to check first when the game is in the paused state. Here is the condition; we set the tapToBegin label in a hide mode and the game in a running state. Now the game will begin to run. Finally, we will write the code to organize the player racket in this purpose.

The following code needs to be added to the dealloc method.

	(void)deal loc {
  [super deal loc];
  [ball release];
  [racquet_green release];
  [racquet_yellow release];
  [player_score release];
  [computer¬_score release];
  [tapToBegin release];
   }

Conclusion

So there you have a complete overview of iPhone game development. It’s tough to develop a powerful game and the rewards may not be what you’re expecting. You’ll have both good as well as bad times. Don’t be afraid to experiment, you often get unexpected results and then you succeed. Get involved with the iPhone development.



Software Developer from India. I hold Master in Computer Applications Degree and is well versed with programming languages such as Java, .Net, C and C++ and possess good working knowledge on Mobile Platforms as well.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login