用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;
}
}
相关文章推荐
- 我是运营,我没有假期
- 每个 Linux 游戏玩家都绝不想要的恼人体验
- 在 Fedora 上使用 Steam play 和 Proton 来玩 Windows 游戏
- Steam 让我们在 Linux 上玩 Windows 的游戏更加容易
- 如何使用 Steam Play 在 Linux 上玩仅限 Windows 的游戏
- 新一代iPad适配应用之游戏篇
- VB实现的《QQ美女找茬游戏》作弊器实例
- C#实现洗牌游戏实例
- C#实现的算24点游戏算法实例分析
- C#实现简单的井字游戏实例
- C++编写简单的打靶游戏
- C++实现基于控制台界面的吃豆子游戏
- winform异型不规则界面设计的实现方法
- 纯javascript实现的小游戏《Flappy Pig》实例
- JavaScript实现俄罗斯方块游戏过程分析及源码分享
- JS小游戏之仙剑翻牌源码详解
- JS小游戏之宇宙战机源码详解
- Android基本游戏循环实例分析
- JavaScript游戏之优化篇
- js实现俄罗斯方块小游戏分享