Skip to content

Instantly share code, notes, and snippets.

- (void)resetGame
{
score = 0;
lives = 3;
timeLeft = 30;
}
- (CGRect)makeFrameForButton:(UIButton*)button
{
int randomHeightInt = roundf(self.view.frame.size.height - self.hitMeButton.frame.size.height)-50;
int randomWidthInt = roundf(self.view.frame.size.width - self.hitMeButton.frame.size.width) - 20;
int randomHeight = arc4random() %randomHeightInt + 50;
int randomWidth = arc4random() % randomWidthInt + 20;
CGRect frame = CGRectMake(randomWidth,
randomHeight,
button.frame.size.width,
#import "CCViewController.h"
@interface CCViewController ()
{
int score,lives,highScore,timeLeft;
BOOL playing;
}
@property (weak, nonatomic) IBOutlet UIButton *hitMeButton;
@property (weak, nonatomic) IBOutlet UIButton *loseLifeButton;
- (void)viewDidLoad
{
[super viewDidLoad];
// do any additional...
UITapGestureRecognizer *gestureRecogniser =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleTap:)];
gestureRecogniser.numberOfTapsRequired = 1;
- (void)endGame
{
if (playing) {
playing = NO;
[self hideButton];
[self.playPauseButton setTitle:@"Play" forState:UIControlStateNormal];
if (score > highScore) {
highScore = score;
- (void)handleTap:(UIGestureRecognizer*)tap {
if (playing) {
// score--;
[self updateLabels];
if (score < 0) {
[self endGame];
}
}
}
- (IBAction)pressHitMeButton:(UIButton *)sender
{
if (playing) {
score++;
[self updateLabels];
[self hideButton];
}
}
- (IBAction)pressLoseLifeButton:(UIButton *)sender
{
if (playing) {
lives--;
[self updateLabels];
if (lives == 0) {
[self endGame];
}
}
}
- (IBAction)pressAddMoreTimeButton:(UIButton *)sender {
timeLeft += 3;
[self updateLabels];
}
- (IBAction)pressPlayPause:(UIButton *)sender {
if (playing) {
playing = NO;
[self.playPauseButton setTitle:@"Play" forState:UIControlStateNormal];
self.resetButton.hidden = NO;
} else {
[self.playPauseButton setTitle:@"Pause" forState:UIControlStateNormal];
playing = YES;
self.resetButton.hidden = YES;
[self resetGame];