您的位置:首页 > 编程语言 > C语言/C++

用OC语言实现贪吃蛇小游戏

2015-12-18 19:05 537 查看
// 首先进行界面布局,初始化颜色
_headColor = [UIColor blueColor];
_backColor = [UIColor cyanColor];
_snakeColor = [UIColor colorWithRed:0.899 green:0.409 blue:0.184 alpha:1.000];
_foodColor = [UIColor colorWithRed:0.962 green:0.441 blue:1.000 alpha:1.000];
for (int i = 0; i < 37; i++) {
for (int j = 0; j < 22; j++) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20 * j - 13, 20 * i, 20, 20)];
if (j == 0 || j == 21 || i == 36 || i == 0) {
view.backgroundColor = [UIColor redColor];
}
else {
view.layer.borderWidth = 0.5;
view.layer.borderColor = [UIColor blackColor].CGColor;
view.backgroundColor = _backColor;
view.tag = 100 * i + j; // 用tag值标记View,前两位代表行数,后两位代表列数
view.userInteractionEnabled = NO;
}
[self addSubview:view];
}
}
int a[200]; // 全局变量 用来存储贪吃蛇的tag值
<pre name="code" class="objc">    _direction = 1; // 代表方向,下一个View与当前View的tag值之差
    // 为贪吃蛇赋初始值
[self.rv viewWithTag:1510].backgroundColor = self.rv.headColor;
[self.rv viewWithTag:1510].layer.cornerRadius = 10;
[self.rv viewWithTag:1509].backgroundColor = self.rv.snakeColor;
[self.rv viewWithTag:1509].layer.cornerRadius = 5;
a[0] = 1510;
a[1] = 1509;
_length = 2; // 代表贪吃蛇的长度
#define flag (arc4random() % 35 + 1) * 100 + (arc4random() % 21 + 1)
 _flag = flag; // 随机生成 事物所在的tag值
if ([[self.rv viewWithTag:a[0] + _direction].backgroundColor isEqual:self.rv.backColor]) { // 判断食物出现位置是否合理
[self.rv viewWithTag:_flag].backgroundColor = self.rv.foodColor;
[self.rv viewWithTag:_flag].layer.cornerRadius = 20;
}
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:@selector(move) userInfo:nil repeats:YES];


- (void)move {

    if ([[self.rv viewWithTag:a[0] + _direction].backgroundColor isEqual:self.rv.backColor]) { // 单纯的移动

        [self.rv viewWithTag:a[0] + _direction].backgroundColor = self.rv.headColor; // 将下一个View变为蛇头

        [self.rv viewWithTag:a[0] + _direction].layer.cornerRadius = 10;

        

        [self.rv viewWithTag:a[0]].backgroundColor = self.rv.snakeColor; // 将原本的蛇头改为蛇身

        [self.rv viewWithTag:a[0]].layer.cornerRadius = 5;

        

        [self.rv viewWithTag:a[_length - 1]].backgroundColor = self.rv.backColor; // 将蛇尾变为背景

        [self.rv viewWithTag:a[_length - 1]].layer.cornerRadius = 0;

        

        for (int i = _length - 1; i > 0; i--) { // 更新数组中存放的tag值

            a[i] = a[i - 1];

        }

        a[0] += _direction;

    }

    else if ([[self.rv viewWithTag:a[0] + _direction].backgroundColor isEqual:self.rv.foodColor]) { // 吃到食物的情况

        [self.rv viewWithTag:a[0] + _direction].backgroundColor = self.rv.headColor;

        [self.rv viewWithTag:a[0] + _direction].layer.cornerRadius = 10;

        

        [self.rv viewWithTag:a[0]].backgroundColor = self.rv.snakeColor;

        [self.rv viewWithTag:a[0]].layer.cornerRadius = 5;

        

        _length++; // 长度增加

        for (int i = _length - 1; i > 0; i--) {

            a[i] = a[i - 1];

        }

        a[0] += _direction;

        

        _flag = flag;

        while (![[self.rv viewWithTag:_flag].backgroundColor isEqual:self.rv.backColor]) {

            _flag = flag;

        }

        [self.rv viewWithTag:_flag].backgroundColor = self.rv.foodColor;

        [self.rv viewWithTag:_flag].layer.cornerRadius = 20;

    }

}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息