Get ready to learn some awesome iPhone programming this week!
I'm Scrabble and I will be your fearless tour guide, but this website is here for you to refer back to at any time!
Finally, you no longer have to bug your friends when you get your weekly RPS urge...now you can play against your phone!
Using our knowledge of random numbers, if statements, and the iPhone's amazing and lovable Buttons/IBActions and Labels/IBOutlets, we're going to make a Rock Paper Scissors app.
It's not easy, but you should know everything that you need to make it by now. Try to do as much as you can before asking me for help teeny tiny hints...it'll make it THAT much better!
Here are the minimum requirements: (feel free to have more, obviously!)
Until then, try to only use your memory, your past projects, your neighbor, or the internet for research/help. I promise you'll thank me later when you all have a full working game and you can say you did it yourself!
imageComp.image = [UIImage imageNamed:@"hand-rock.png"];
#import <UIKit/UIKit.h> @interface ViewController : UIViewController { int compChoice; // 0 = Rock, 1 = Paper, 2 = Scissors IBOutlet UIImageView *imageYou; IBOutlet UIImageView *imageComp; IBOutlet UILabel *labelYou; IBOutlet UILabel *labelComp; IBOutlet UILabel *labelWinner; } - (IBAction)pressRock:(id)sender; - (IBAction)pressPaper:(id)sender; - (IBAction)pressScissors:(id)sender; @end
- (IBAction)pressRock:(id)sender { // user chose rock, let's see what the computer chooses... compChoice = (arc4random() % 3); // generates number from 0 to 2, including 0 and 2 (in other words: 0, 1, or 2) labelYou.text = @"Rock"; imageYou.image = [UIImage imageNamed:@"hand-rock.png"]; if(compChoice == 0) { labelComp.text = @"Rock"; imageComp.image = [UIImage imageNamed:@"hand-rock.png"]; labelWinner.text = @"It's a tie!"; } else if(compChoice == 1) { labelComp.text = @"Paper"; imageComp.image = [UIImage imageNamed:@"hand-paper.png"]; labelWinner.text = @"Computer wins!"; } else if(compChoice == 2) { labelComp.text = @"Scissors"; imageComp.image = [UIImage imageNamed:@"hand-scissors.png"]; labelWinner.text = @"You win!!!"; } else { NSLog(@"Oops, something went terribly wrong with the computer's choice!"); } }
We have one more sample project before we get into making our first "REAL" game!
This app will give your user a random number on their screen (in a Label) when they press a button, so we can learn how to change a label's property (and any other item's property) through the code with something called delegates (which is also a cool noun to use when you go home...look it up in the dictionary!).
#import <UIKit/UIKit.h> @interface ViewController : UIViewController { int myNumber; // Tell our phone to expect us to set the "myNumber" // variable equal to some integer. // Math class refresher... ints = FULL numbers // (NO DECIMALS!) IBOutlet UILabel *myLabel; // Tell our phone to expect us to control the // LABEL on our ViewController.xib which will // display our random number variable! } - (IBAction)pressGenerate:(id)sender; // Tell our phone to expect a method that we'll // link to our button's "touch up inside" event. // This should look pretty familiar! @end
- (IBAction)pressGenerate:(id)sender { myNumber = (arc4random() % 100 - 1 + 1) + 1; // Generates a random number from 1 to 100 // (including 1 and 100). // The formula is ... (arc4random() % (high - low + 1)) + low // So if you wanted random numbers between 18 and 53, you // would do: number = (arc4random() % (53 - 18 + 1)) + 18 // ...Easy, right? :) We'll talk about this more today. // We told the phone to expect us to change the "myLabel" label on our view controller. // Now we're doing it! myLabel.text = [NSString stringWithFormat:@"%i", myNumber]; // IMPORTANT NOTE: We can't just set myLabel.text equal to myNumber, our integer, because // labels always need a STRING for their text! // The second half of that line converts myNumber into a string that myLabel can understand. }
Let's fill the halls of iD Princeton with our beautiful music! Right click and "save" here to download the image ...and here for the best 8-second MP3 of your life.
#import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> @interface ViewController : UIViewController { // We keep track of the audio player here on the class AVAudioPlayer *audioPlayer; } // Our UIButton clicked methods - (IBAction)playClicked:(id)sender; - (IBAction)stopClicked:(id)sender;
- (IBAction) playClicked:(id)sender { // Open a music file that's been added to the project, called lady.mp3 NSString * mp3Path = [NSString stringWithFormat:@"%@/lady.mp3", [[NSBundle mainBundle] resourcePath]]; NSURL *url = [NSURL fileURLWithPath:mp3Path]; // create an AudioPlayer with the file used above NSError *error; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; if (audioPlayer == nil) NSLog(@"Error playing sound %@", [error description]); else [audioPlayer play]; } - (IBAction)stopClicked:(id)sender { [audioPlayer stop]; }
Our first button...the start of a beautiful relationship. Click to open the image.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Welcome!" message:@"Enjoy the app!" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Button1", @"Button2", nil]; [alert show];
This app requires NO CODE! Thanks, Xcode!
I'll start putting these up right after the first person finishes their Rock Paper Scissors game above. It's not a race, though...we have all week for these!
If the week has ended and you're bored in a town that's not called Princeton right now (booooo), these are some AMAAAAAAAAZING sites to keep your momentum going throughout the summer and beyond!
Or, if I'm really busy helping other people in class and you need something new for your project, I guarantee there's at least a dozen things here that you never thought of!
Scrabble also doesn't know everything...crazy, right?!?! I won't lie to you and tell you that I know something, but I will ABSOLUTELY help you learn how to understand these sites and the people who write them.
iOS apps are powered by the same programming languages used to create Mac apps since the first version of OS X in the 90's.
It's called Objective-C and it looks a little scary at first...but really, it's not too different from the languages you already know.
Once you know one programming language, the hard part of learning your next programming language is already out of the way.
So while the code might look different and scary, don't be afraid to try and relate everything I say to things you already know, and please remind me if I forget to do that. Objective-C is still pretty new to me (and most iPhone developers) and I still love Java and C++, so we'll be talking a lot about those too, even if we're not coding in them!
We'll be using a shiny Apple app called Xcode!
Even though it's already in its fourth version, it definitely still has bugs.
As beautiful as it is, you'll love to hate it, and at some point you won't believe me when I tell you it was made by Apple.
Fortunately, Xcode saves all your code for you every time you run your app. So if you haven't tested in a while...
No, no, NO!
We'll be using iOS 5.1.x for ALL of our apps.
Trust me — I just put iOS 6 on my iPhone and it's definitely not ready yet. And there's no going back.
I'll let you play with mine all week, but if you want your iPhone to still make phone calls or your iPod to still play music, wait for the suckers like me to finish dealing with all the bugs first!