This is the first in a multipart series where I will be teaching you to create an iPhone game from the ground up. As many of you know, OpenGL and Quartz have quite a steep learning curve. This tutorial will simplify the development process and not use either one of those technologies. For our graphics, we will simply use UIImageViews.
In this tutorial series, I will teach you about every aspect of developing an iPhone game. The game will include graphics, sounds, game mechanics, and even some simple computer AI.
The game we will be creating is called iTennis. It is essentially Pong with a tennis theme. It will follow all of the same rules and mechanics of Pong. I chose this as a first game tutorial as iTennis does not require any special animation. If there is significant interest, I will move on to a more complex game in a later series. Here is a screenshot of the game we will be making:
Let’s get started… Here are the images you will need for the project. (I know my Photoshop skillz are weak, so be easy on me). Click the link below to download the zipped images.
-
Open up XCode and create a View Based Application. Name this application iTennis.
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 connections so that we can interact with our images in code. We will also add some of the game variables that we will be using in this tutorial. Open up iTennisViewController.h and add the following code:As you can see, we have quite a few IBOutlets. This is because there are many objects we need to interact with. For now, ignore the ballVelocity and gameState variables. Now, we are ready to build our interface. Double click on the file iTennishViewController.xib to open it up inside of Interface Builder. Here is a video of me setting up the interface.[qt:http://icodeblog.com/wp-content/uploads/2009/01/Game_1.mov 640 416]
Now that you have made the connections, close Interface Builder and return to XCode. We need to set up some constants. You can hardcode these values in somewhere, but making them constants makes them easier to change when you are tweaking the mechanics of your game. Open up iTennisViewController.m and add the following code.Let me explain the purpose of the constants. The first 2 are game states. We use these to determine what is to be shown on screen. Is the game paused, is it running, is it at the title screen? We will add more states later on in the game. The next 2 variables are speeds for the ball to travel. Since the game is 2D, the ball can have an X and Y speed to make up its velocity vector. The next line synthesizes the variables we will use to create their getter and setter methods automatically.Now let’s write the viewDidLoad method. Add the following code:First, we are setting our game state to a paused game state. This is so the game doesn’t start right away when the view loads. Next, we create a vector for the ball’s velocity. Adjust the constants that we defined earlier for a faster or slower ball speed. Finally, we create an NSTimer instance. The first parameter is the timestep. We have set the time step to 0.05 seconds. Adjusting this will change the speed of the entire game. Next we set the target. This is telling the timer where the callback is located. The callback is the next parameter. Basically, we are telling the timer to call self.gameLoop every 0.05 seconds. We don’t need to worry about the userInfo param. The last parameter just tells the timer to repeat.Now that we have initialized our game, let’s create the game loop. Add the following method:First, we are checking to see if the game is in a running state (we don’t want to move the ball in a paused state). If the game is not running, we will show the tapToBegin label. This is just the label defined earlier that tells the user to tap the screen to begin.If the game is running, we start by moving the ball according to its velocity vector. The next few lines do some bounds checking. If the ball hits the side of the screen, we want to reverse its velocity so that it “bounces”. Without this code, the ball would fly off the screen.The last bit of code we will need to write is the touchesBegan method. We will use this method to start the game when the game is in a paused state. Add the following code:What this does is first checks to see if we are in a paused state. If so, we hide the tapToBegin label and set the game in a running state. This will get the game running. Eventually, we will write the code to control the player paddle in this function.Finally, being good iPhone developers, we need to clean up the resources that we allocated. Add the following code to the dealloc method.That concludes part 1 of our game development tutorial series. If you have any questions or comments, feel free to leave them in the comments section of this post. You can download the source code here. Happy iCoding!
-
frosty
-
Mike
-
David
-
Ben
-
Andre
-
Andre
-
http://blog.wheelerstreet.com Paul Rangel
-
Chintan
-
Tylor
-
techy
-
i-n00b
-
Aaron
-
Gp
-
brandon
-
http://www.speer.me kuyote
-
Tylor
-
narender
-
narender.mudgal
-
iphonenew09
-
naren
-
Andre
-
DN
-
http://www.freshapps.com Brandon
-
Imran
-
ck
-
DN
-
Joe
-
masterjcl
-
http://freshapps.com Brandon
-
http://freshapps.com Brandon
-
http://www.lotofwallpapers.com Chintan
-
Tylor
-
SquirrelKing
-
rey
-
Naren
-
narender
-
http://www.lotofwallpapers.com Chintan
-
naren
-
naren
-
naren
-
Naren
-
thomas
-
rey
-
http://www.lotofwallpapers.com Chintan
-
Clad
-
Kevin
-
Robert
-
Robert
-
mayhem_nz
-
Naren
-
Robert
-
CLAD
-
CLAD
-
techy
-
Naren
-
techy
-
Jhsw77
-
Mark
-
josh
-
Jo
-
http://www.feroc.de Feroc
-
Naren
-
http://www.bwsciencelabs.com Brennon
-
Bruce
-
BdR
-
Ricky
-
Kyle
-
Kyle
-
Ricky
-
Ricky
-
Kyle
-
Kyle
-
Ricky
-
Kyle
-
Jo
-
Naren
-
Naren
-
Naren
-
Naren
-
Naren
-
Naren
-
Naren
-
Naren
-
Ricky
-
miah
-
miah
-
miah
-
http://www.codecurry.com Sameer Gupta
-
chris
-
http://www.haungo.com Hau
-
http://lhunath.lyndir.com Maarten Billemont
-
mingxin
-
http://bob3@hotmail.com bob
-
http://www.haungo.com Hau
-
http://www.haungo.com Hau
-
Mat
-
Owen
-
Steve
-
Frank
-
Ravi , japan
-
Doug
-
Amit
-
http://mdreeling.wordpress.com/2009/03/11/iphone-dev-gamesapps-top-bookmarks/ iPhone Dev – Games/Apps – Top Bookmarks « Java Developer / Fledgling iPod Touch Developer
-
Nick
-
http://afruj.wordpress.com/2009/03/24/iphone-tutorials/ iPhone development Tutorials « The Brook Song – ঝর্ণার গান
-
http://werchris.com Chris Anderson
-
http://www.servingmed.com/CV/Roland Roland Combes
-
http://www.iphonefootprint.com/2009/04/iphone-game-development-guide-on-the-go-tutorial/ iPhone game development guide: On the go tutorial | iPhone Footprint
-
iNewbie
-
iNewbie
-
Francisco
-
iNewBie
-
The Kellen
-
Francisco
-
alex
-
Francisco
-
Lee
-
Jonathan
-
Mario
-
Wolter
-
rich
-
mkppk
-
Jonathan
-
Dcybele
-
http://www.idevgames.com Carlos
-
http://joehenry.name Henry
-
Joe
-
Al
-
Al
-
Al
-
http://greatstuff Anthony
-
Graham
-
iBots
-
http://iphoner.org.ua/blogs/akrakovetsky/archive/2009/07/05/100-iphone.aspx 100 ресурсов для iPhone-разработчиков – Краковецкий Александр: заметки программиста – iPhone Community в Украине
-
http://KRAPPS.com/2009/07/09/itennis-iphone-app/ Developer Steals iPhone App Code, Submits As Own | KRAPPS | a different and funny iPhone app review site
-
http://www.applebloog.com/?p=18760 Apple Bloog » Blog Archive » Lifted tutorial code winds up in $0.99 App Store tennis game
-
http://www.iphonefirmware.com/2009/07/09/lifted-tutorial-code-winds-up-in-099-app-store-tennis-game/ Lifted tutorial code winds up in $0.99 App Store tennis game | iPhone Firmware
-
http://infofork.com/lifted-tutorial-code-winds-up-in-0-99-app-store-tennis-game/ Lifted tutorial code winds up in $0.99 App Store tennis game | InfoFork.com
-
http://news.appleusers.org/tuaw/lifted-tutorial-code-winds-up-in-0-99-app-store-tennis-game/ AppleUsers.org News » Lifted tutorial code winds up in $0.99 App Store tennis game
-
http://iwyre.net/?p=1558 iWyre
-
http://appleusersblog.com/lifted-tutorial-code-winds-up-in-099-app-store-tennis-game/ Apple User’s Blog » Lifted tutorial code winds up in $0.99 App Store tennis game
-
http://shaiperednik.com/2009/07/lifted-tutorial-code-winds-up-in-0-99-app-store-tennis-game/ Lifted tutorial code winds up in $0.99 App Store tennis game » Shai Perednik.com
-
http://iphonepulse.com/2009/07/free-tutorial-code-sold-as-99c-app-on-the-appstore/ Free Tutorial code sold as 99c App on the AppStore » iPhonePulse
-
PLi
-
PLi
-
vconrad
-
vconrad
-
http://lesma.net/iphone/?p=1429 Lifted tutorial code winds up in $0.99 App Store tennis game
-
Sam
-
http://www.bubidevs.net/2009/03/11/devtutorial-13-creiamo-il-nostro-primo-gioco-pong-parte-i/ DevTutorial #13 – Creiamo il nostro primo gioco, Pong! (Parte I) – Bubi Devs
-
Vince
-
Vince
-
Vince
-
http://developer.thomasnicholson.com/2009/08/18/quick-post-iphone-game-development-tutorials/ Quick Post: iPhone Game Development Tutorials | Thomas Nicholson iPhone Developer Newbie
-
ThinkMud
-
Gary
-
Lane
-
http://triffort.com afzal
-
http://triffort.com afzal
-
http://www.shuguo.org/2009/09/02/iphone-ipod-touch%e6%b8%b8%e6%88%8f%e5%bc%80%e5%8f%91/ iPhone/iPod touch游戏开发 | SHUGUO
-
http://eiphonestudy.com/2009-09/look-familiar.html Look Familiar? | Iphone Study Blog
-
http://grannycoder.blogspot.com Granny Coder
-
Amy
-
Amy
-
http://www.uright.ca/?p=115 iPhone Game Programming Tutorial Suggested Link « Uright Tech Blog
-
http://ueepa.wordpress.com unt
-
http://www.property-search-cannes.com Annie ( Period Property ) Wagner
-
http://www.myfirstiphoneapplication.com/2009/10/some-great-resources/ Some Great Resources! | My First iPhone Application
-
jonk
-
Ami Schreiber
-
ash
-
Remi
-
Tommy Myers
-
http://cs-me.com/2009/11/21/the-ultimate-toolbox-for-iphone-development/ The Ultimate Toolbox for iPhone Development – Creative Solutions
-
Aussie96
-
http://mundoimd.com/2009/11/30/enlaces-para-desarrollar-en-el-iphone/ Enlaces para desarrollar en el iPhone | El mundo de IMD
-
Simon88
-
Nick
-
http://learningtechie.wordpress.com/2010/01/21/my-first-iphone-game/ My first iphone game! « Learning techie
-
http://www.casdra.com/blog/?p=1641 CasdraBlog » Blog Archive » links for 2010-01-29
-
http://sickmacaddict.wordpress.com/2010/01/29/iphone-game-programming-tutorial-part-1/ iPhone Game Programming Tutorial – Part 1 « Evolving Minds
-
Andy
-
http://fansibo.com/?p=240 The Ultimate Toolbox for iPhone Development | c'est la vie
-
http://fansibo.com/?p=240 The Ultimate Toolbox for iPhone Development | c'est la vie
-
Chimpy
-
http://N/A Mike
-
http://daveelliottonline.com dave
-
Chris
-
Salim
-
Salim
-
http://iphoneapp.us/the-complete-iphone-development-toolbox/ The Complete iPhone Development Toolbox | iPhoneApp Dev Blog
-
cooldaddybeck
-
Sylvain
-
Matt
-
J. Cohen
-
J. Cohen
-
Matt
-
Tom
-
http://www.coachrickthemittologist.com James Ricky Coward
-
Doug Stafford
-
macmandev
-
darius
-
http://CrawfordWorks.ca Andrew Crawford
-
http://CrawfordWorks.ca Andrew Crawford
-
David
-
http://vaclav.vancura.org/weekly-digest-for-may-2nd Weekly Digest for May 2nd — Hello. My name is Václav Vančura.
-
Mark
-
http://www.silenceit.ca/2010/05/31/introduction-to-iphone-applications/ Introduction to iPhone Applications | silenceIT Web Design and Marketing, Rich Internet Applications, e-Business IT |
-
http://site-design.co.nz mazen
-
Chris
-
Michele
-
Chris
-
http://robobros.net Nate
-
http://robobros.net Nate
-
http://robobros.net Nate
-
http://robobros.net Nate
-
http://robobros.net Nate
-
Noe
-
CJ
-
CJ
-
Anonymous
-
Anonymous
-
Camel
-
Camel
-
http://kerempalabiyik.com/2010/02/27/iphone-uygulamalari-gelistirmek-isteyenler-icin-ucretsiz-100-kurs-ve-rehber/ iPhone uygulamaları geliştirmek isteyenler için ücretsiz 100 kurs ve rehber « Kerem PALABIYIK
-
ashok yagnik
-
http://www.iphoneappacademy.com/7/iphone-game-tutorials/itennis-iphone-game-tutorials/ iTennis – iPhone Game Tutorials – iPhone App Academy
-
http://sites.google.com/site/ubomagicland/index Patrick
-
Xander PK
-
http://animaltracker.wordpress.com/2010/07/23/a-period-of-learning-part-2/ A period of learning (Part 2) « Animal Tracker (iPhone Game) Blog
-
http://andygerth.blogspot.com Andy
-
Giovanni Di Russo
-
Gio
-
http://www.theapplelounge.com/tutorial/app-development-per-ios-i-tutorial/ App development per iOS, i tutorial – TheAppleLounge
-
andy
-
andy
-
joe
-
joe
-
http://icodeblog.com/2010/09/09/iphone-game-programming-series-blackjack-part-1-the-deck/ iPhone Game Programming Series: Blackjack – Part 1: The Deck | iCodeBlog
-
http://www.ios4jailbreak.com/2010/09/iphone-game-programming-series-blackjack-%e2%80%93-part-1-the-deck/ iOS4 Jailbreak » iPhone Game Programming Series: Blackjack – Part 1: The Deck
-
http://icode.dreamvision-soft.com/blog/?p=81 iPhone Game Programming Series: Blackjack – Part 1: The Deck | iCode
-
Eddie
-
Dave
-
Dave
-
Neil Cooper
-
diaz
-
Dr. Rolf R. Fink
-
http://blog.roychowdhury.org/2010/10/20/writing-a-professional-looking-bricks-game-for-the-iphone-part-i/ iConverged » Writing a professional looking Bricks game for the iphone (Part I)
-
Unixx
-
http://iphonetuts.com/?p=107086 iPhone Game Programming Series: Blackjack – Part 1: The Deck | Iphone tutorials
-
http://blog.roychowdhury.org/2010/10/25/tutorial-iphone-game-programming-professional-bricks-part-i/ iConverged » Tutorial: iPhone Game Programming- professional Bricks (Part I)
-
http://techwonders.blogspot.com PT
-
Will
-
Johnny6
-
Vishakha
-
Joe
-
Bob
-
http://www.wizard-soft.com Martin
-
http://www.openxcell.com iPhone Application
-
http://brandontreb.com/new-iphone-game-programming-tutorial-post-on-icodeblog/ brandontreb.com » New iPhone Game Programming Tutorial Post On iCodeBlog
-
http://www.cygnismedia.com/ facebook app develop
-
http://www.worldoftrade.com/ Rolex Deep Sea
-
Ollie
-
Ollie
-
Riscar
-
Zach
-
http://hirephpwebdeveloper.com marksteve
-
Kenneth
-
http://vendomiapple.com/2011/01/29/100-cursos-gratuitos-y-tutoriales-para-aspirantes-a-desarrolladores-de-app-para-iphone/ 100 cursos gratuitos y tutoriales para aspirantes a desarrolladores de Apps para iphone « Programacion « VENDO MI APPLE
-
Christian
-
Thales Medeiros
-
Thales Medeiros
-
John Quinn
-
http://www.my-digital-source.ca Iphonerepairnow
-
Mossa