利用iOS基础知识实现剪刀石头布小游戏

本文介绍了一个简单的iOS应用中游戏逻辑的实现方式,包括通过随机数判断游戏胜负、更改视图颜色及透明度来表现游戏结果,并使用UIAlertView进行提示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

其中运用到得都是iOS中的基础知识,并非很难。

一些基本的button设置,label设置不展示了,只是写一下方法:


- (void)PshearView:(ImageView *)PshearView

{

    ImageView *image2 = (ImageView *)[self.view viewWithTag:4];

    NSInteger num = arc4random()%(3 - 1 + 1) + 1;

    if (num == 1) {

        NSLog(@"%ld", num);

        ImageView *image = (ImageView *)[self.view viewWithTag:1];

        image2.backgroundColor = [UIColor orangeColor];

        image.backgroundColor = [UIColor orangeColor];

        image2.alpha = 0.7;

        image.alpha = 0.7;

        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示!" message:@"平局!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"继续", nil];

        [alertView show];

        [alertView release];

    }

    else if (num == 2)

    {

        ImageView *image = (ImageView *)[self.view viewWithTag:2];

        NSLog(@"%ld",num);

        image.backgroundColor = [UIColor greenColor];

        image2.backgroundColor = [UIColor redColor];

        image2.alpha = 0.7;

        image.alpha = 0.7;

        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示!" message:@"你输了!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"继续", nil];

        [alertView show];

        [alertView release];

    }

    else if (num == 3)

    {

        ImageView *image = (ImageView *)[self.view viewWithTag:3];

        NSLog(@"%ld", num);

        image.backgroundColor = [UIColor redColor];

        image2.backgroundColor = [UIColor greenColor];

        image2.alpha = 0.7;

        image.alpha = 0.7;

        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示!" message:@"你赢了!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"继续", nil];

        [alertView show];

        [alertView release];

    

    }

}


- (void)Pstone:(ImageView *)Pstgone

{

    ImageView *image2 = (ImageView *)[self.view viewWithTag:5];

    NSInteger num = arc4random()%(3 - 1 + 1) + 1;

    if (num == 1) {

        NSLog(@"%ld", num);

        ImageView *image = (ImageView *)[self.view viewWithTag:1];

        image2.backgroundColor = [UIColor greenColor];

        image.backgroundColor = [UIColor redColor];

        image2.alpha = 0.7;

        image.alpha = 0.7;

        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示!" message:@"你赢了!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"继续", nil];

        [alertView show];

        [alertView release];

    }

    else if (num == 2)

    {

        NSLog(@"%ld",num);

        ImageView *image = (ImageView *)[self.view viewWithTag:2];

        image.backgroundColor = [UIColor orangeColor];

        image2.backgroundColor = [UIColor orangeColor];

        image2.alpha = 0.7;

        image.alpha = 0.7;

        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示!" message:@"平局!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"继续", nil];

        [alertView show];

        [alertView release];

    }

    else if (num == 3)

    {

        NSLog(@"%ld", num);

        ImageView *image = (ImageView *)[self.view viewWithTag:3];

        image.backgroundColor = [UIColor greenColor];

        image2.backgroundColor = [UIColor redColor];

        image2.alpha = 0.7;

        image.alpha = 0.7;

        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示!" message:@"你输了!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"继续", nil];

        [alertView show];

        [alertView release];

        

    }

    

}


- (void)Pcloth:(ImageView *)Pcloth

{

    ImageView *image2 = (ImageView *)[self.view viewWithTag:6];

    NSInteger num = arc4random()%(3 - 1 + 1) + 1;

    if (num == 1) {

        NSLog(@"%ld", num);

        ImageView *image = (ImageView *)[self.view viewWithTag:1];

        image2.backgroundColor = [UIColor redColor];

        image.backgroundColor = [UIColor greenColor];

        image2.alpha = 0.7;

        image.alpha = 0.7;

        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示!" message:@"你输了!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"继续", nil];

        [alertView show];

        [alertView release];

    }

    else if (num == 2)

    {

        NSLog(@"%ld",num);

        ImageView *image = (ImageView *)[self.view viewWithTag:2];

        image.backgroundColor = [UIColor redColor];

        image2.backgroundColor = [UIColor greenColor];

        image2.alpha = 0.7;

        image.alpha = 0.7;

        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示!" message:@"你赢了!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"继续", nil];

        [alertView show];

        [alertView release];

    }

    else if (num == 3)

    {

        NSLog(@"%ld", num);

        ImageView *image = (ImageView *)[self.view viewWithTag:3];

        image.backgroundColor = [UIColor orangeColor];

        image2.backgroundColor = [UIColor orangeColor];

        image2.alpha = 0.7;

        image.alpha = 0.7;

        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示!" message:@"平局!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"继续", nil];

        [alertView show];

        [alertView release];

        

    }

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

    ImageView *image1 = (ImageView *)[self.view viewWithTag:1];

    ImageView *image2 = (ImageView *)[self.view viewWithTag:2];

    ImageView *image3 = (ImageView *)[self.view viewWithTag:3];

    ImageView *image4 = (ImageView *)[self.view viewWithTag:4];

    ImageView *image5 = (ImageView *)[self.view viewWithTag:5];

    ImageView *image6 = (ImageView *)[self.view viewWithTag:6];

    switch (buttonIndex) {

        case 1:

            image1.backgroundColor = [UIColor clearColor];

            image2.backgroundColor = [UIColor clearColor];

            image3.backgroundColor = [UIColor clearColor];

            image4.backgroundColor = [UIColor clearColor];

            image5.backgroundColor = [UIColor clearColor];

            image6.backgroundColor = [UIColor clearColor];

            break;

        case 0:

            image1.backgroundColor = [UIColor clearColor];

            image2.backgroundColor = [UIColor clearColor];

            image3.backgroundColor = [UIColor clearColor];

            image4.backgroundColor = [UIColor clearColor];

            image5.backgroundColor = [UIColor clearColor];

            image6.backgroundColor = [UIColor clearColor];

            break;

        default:

            break;

    }

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值