Need help in changing the counter for player and computer in my pong game from a team selection page

47 Views Asked by At

I have a pong game with computer and player working well was looking if anyone could help in making a team selection view controller so players can choose their side. Also looking to control the ball speed from a settings controller. If any one could help would be awesome thank you.

My code I'm using is :

@implementation Game
-(void)Collision{
if (CGRectIntersectsRect(Ball.frame, Player.frame)) {
    Y = arc4random() %5;
    Y = 0-Y;
}

if (CGRectIntersectsRect(Ball.frame, Computer.frame)) {
    Y = arc4random() %5;
}


}




-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

UITouch *Drag = [[event allTouches] anyObject];
Player.center = [Drag locationInView:self.view];

if (Player.center.y > 620) {
    Player.center = CGPointMake(Player.center.x, 620);
}

if (Player.center.y < 620) {
    Player.center = CGPointMake(Player.center.x, 620);
}

if (Player.center.x < 25) {
    Player.center = CGPointMake(25, Player.center.y);
}

if (Player.center.x > 340) {
    Player.center = CGPointMake(340, Player.center.y);
}
}





-(void)ComputerMovement{

if (Computer.center.x > Ball.center.x) {
    Computer.center = CGPointMake(Computer.center.x - 2, Computer.center.y);
}

if (Computer.center.x < Ball.center.x) {
    Computer.center = CGPointMake(Computer.center.x + 2, Computer.center.y);
}

if (Computer.center.x < 25) {
    Computer.center = CGPointMake(25, Computer.center.y);
}

if (Computer.center.x > 340) {
    Computer.center = CGPointMake(340, Computer.center.y);
}


}


-(IBAction)StartButton:(id)sender{

StartButton.hidden = YES;
Exit.hidden = YES;
WinOrLose.hidden = YES;  

Y = arc4random() %11;
Y = Y- 5;

X = arc4random() %11;
X = X - 5;

if (Y == 0){
    Y =1;
}

if (X==0){
    X = 1;
    }

{
         timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self                                                 selector:@selector(BallMovement) userInfo:nil repeats:YES];
}


}
-(void)BallMovement{

[self ComputerMovement];
[self Collision];

Ball.center= CGPointMake(Ball.center.x + X, Ball.center.y + Y);
if (Ball.center.x <5) {
    X = 0 - X;
}

if (Ball.center.x >340) {
    X = 0 - X;
}

if (Ball.center.y < 0) {
    PlayerScoreNumber = PlayerScoreNumber + 1;
    PlayerScore.text = [ NSString stringWithFormat:@"%i", PlayerScoreNumber];

    [timer invalidate];
    StartButton.hidden = NO;
    WinOrLose.hidden = YES;
    Ball.center = CGPointMake(147, 326);
    Computer.center = CGPointMake(134, 28);

    if (PlayerScoreNumber == 10) {
        StartButton.hidden = YES;
        Exit.hidden = NO;
        WinOrLose. Hidden = NO;
        WinOrLose.text = [NSString stringWithFormat:@"You Win!"];

    }

}

if (Ball.center.y > 640) {
    ComputerScoreNumber = ComputerScoreNumber + 1;
    ComputerScore.text = [NSString stringWithFormat:@"%i", ComputerScoreNumber];

    [timer invalidate];
    StartButton.hidden = NO;
    Ball.center = CGPointMake(147, 3269);
    Computer.center = CGPointMake(134, 28);


    if (ComputerScoreNumber == 10) {
        StartButton.hidden =YES;
        Exit.hidden = NO;
        WinOrLose. Hidden = NO;
        WinOrLose.text = [NSString stringWithFormat:@"You Lose!"];
    }

}

// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before      navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

@end
1

There are 1 best solutions below

2
On

I would start with a Storyboard to create the needed ViewControllers. After that you can connect them with segues and model the transition triggers.

I've written a small tutorial about this some time ago. Maybe this helps: HowTo: Add View Controllers to the game storyboard and use segues to navigate between them

enter image description here