1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
//  iTennisViewController.m
//  iTennis
//
//  Created by Brandon Trebitowski on 1/14/09.
//  Copyright __MyCompanyName__ 2009. All rights reserved.
//

#import "iTennisViewController.h"

#define kGameStateRunning 1
#define kGameStatePaused  2

#define kBallSpeedX 3
#define kBallSpeedY 4

#define kCompMoveSpeed 3
#define kScoreToWin 5

@implementation iTennisViewController
@synthesize ball,racquet_yellow,racquet_green,player_score,computer_score,gameState,ballVelocity,tapToBegin;

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  if(gameState == kGameStatePaused) {
    tapToBegin.hidden = YES;
    gameState = kGameStateRunning;
  } else if(gameState == kGameStateRunning) {
    [self touchesMoved:touches withEvent:event];
  }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  UITouch *touch = [[event allTouches] anyObject];
  CGPoint location = [touch locationInView:touch.view];
  CGPoint xLocation = CGPointMake(location.x,racquet_yellow.center.y);
  racquet_yellow.center = xLocation;
}

-(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;
    }

    if(CGRectIntersectsRect(ball.frame,racquet_yellow.frame)) {
      if(ball.center.y < racquet_yellow.center.y) {
        ballVelocity.y = -ballVelocity.y;
        NSLog(@"%f %f",ball.center,racquet_green.center);
      }
    }

    if(CGRectIntersectsRect(ball.frame,racquet_green.frame)) {
      if(ball.center.y > racquet_green.center.y) {
        ballVelocity.y = -ballVelocity.y;
      }
    }


    // Begin Simple AI
    if(ball.center.y <= self.view.center.y) {
      if(ball.center.x < racquet_green.center.x) {
        CGPoint compLocation = CGPointMake(racquet_green.center.x - kCompMoveSpeed, racquet_green.center.y);
        racquet_green.center = compLocation;
      }

      if(ball.center.x > racquet_green.center.x) {
        CGPoint compLocation = CGPointMake(racquet_green.center.x + kCompMoveSpeed, racquet_green.center.y);
        racquet_green.center = compLocation;
      }
    }
    // Begin Scoring Game Logic
    if(ball.center.y <= 0) {
      player_score_value++;
      [self reset:(player_score_value >= kScoreToWin)];
    }

    if(ball.center.y > self.view.bounds.size.height) {
      computer_score_value++;
      [self reset:(computer_score_value >= kScoreToWin)];
    }

  } else {
    if(tapToBegin.hidden) {
      tapToBegin.hidden = NO;
    }
  }
}

-(void)reset:(BOOL) newGame {
  self.gameState = kGameStatePaused;
  ball.center = self.view.center;
  if(newGame) {
    if(computer_score_value > player_score_value) {
      tapToBegin.text = @"Computer Wins!";
    } else {
      tapToBegin.text = @"Player Wins!";
    }

    computer_score_value = 0;
    player_score_value = 0;
  } else {
    tapToBegin.text = @"Tap To Begin";
  }

  player_score.text = [NSString stringWithFormat:@"%d",player_score_value];
  computer_score.text = [NSString stringWithFormat:@"%d",computer_score_value];
}

// 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 scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

- (void)dealloc {
    [super dealloc];
  [ball release];
  [racquet_green release];
  [racquet_yellow release];
  [player_score release];
  [computer_score release];
  [tapToBegin release];
}

@end