一、 题目描述
1.1游戏介绍
- 打地鼠是一款休闲游戏,游戏的规则也特别的简单,玩家只需要敲打一只只从地洞里冒出头的地鼠即可,游戏要求在限定的时间内,敲打的地鼠越多,分数越高,游戏画面比较清新,地鼠造型也比较滑稽可爱。
- 打地鼠玩起来简单充满乐趣,游戏中地鼠从不同的洞口出现,只需要你迅速的击晕它,那么便能得到一定的分数,地鼠冒出来后点击,冒出的地鼠,变可以去地鼠哭泣并躲到洞中,得到一定分数,
- 游戏功略,需要全身贯注,手眼并用,不仅要速度快,而且要看准,有时候因为心急,容易砸错洞后,并扣分。下面有一个时间进度控制条,从游戏开始,随着时间而慢慢消耗精力,知道精力消耗结束。游戏开始有一定的音乐效果产生,当砸到地鼠的时候,会播放地鼠哭泣的声音,使得游戏更具有可玩性。
1.2相关技术简介
- 打地鼠作为一款常见的传统小游戏,简单有趣,深受人们的喜爱,本项目基于IOS平台运用xcode作为编程编程工具的设计的打地鼠,希望通过开发打地鼠游戏,对其中游戏的思想进行理解,并掌握ios移动端开发语言ojbective-c的使用规则,同时提高编程设计能力。
- Objective-C,通常写作ObjC或OC和较少用的Objective C或Obj-C,是扩充C的面向对象编程语言。它主要使用于Mac OS X和GNUstep这两个使用OpenStep标准的系统,而在NeXTSTEP和OpenStep中它更是基本语言。
- Objective-C是非常实用的语言。它是一个用C写成很小的运行库,令应用程序的尺寸增加很小,和大部分OO系统使用极大的VM执行时间会取代了整个系统的运作相反。Objective-C写成的程序通常不会比其原始码大很多。而其函式库(通常没附在软件发行本)亦和Smalltalk系统要使用极大的内存来开启一个窗口的情况相反。因此,Objective-C它完全兼容标准C语言(C++对C语言的兼容仅在于大部分语法上,而在ABI(Application Binary Interface)上,还需要使用extern “C”这种显式声明来与C函数进行兼容),而在此基础上增加了面向对象编程语言的特性以及Smalltalk消息机制。
- Xcode 是运行在操作系统Mac OS X上的集成开发工具(IDE),由苹果公司开发。Xcode是开发OS X 和 iOS 应用程序的最快捷的方式。Xcode 具有统一的用户界面设计,编码、测试、调试都在一个简单的窗口内完成 。在新游戏页面点击后,即可直接进入游戏。此刻,地鼠会从一个个地洞中不经意的探出一个脑袋,或者一双眼睛,企图躲过游戏者的视线。不用心软,直接敲你的手丫子,拍掌下去,力求一次一个准,来一个砸一个,来二个砸一双。
二、 流程设计
2.1概要设计
玩家开始游戏后,将播放主题音乐,同时地鼠会随机出现在3*3的洞洞中,地鼠每隔一定的时间会随机出现在某一个洞洞中,玩家可以通过鼠标点击地鼠,并积累一定的分数,并为游戏限定时间。
2.2详细设计
游戏开始,初始化游戏场景图片,初始化地鼠,创建时间控制进度条,初始化背景音效,开始游戏;
- 初始化游戏场景图片:由于背景图,是由四张图片组成,目的是为了达到每个地鼠从洞洞里面出现的效果,将所需要的图片文件统一放入到plist文件中,将获取的路径放入到字符串NSString *filePath中,用数组NSArray *array存放每一组图片信息。运用for循环(NSDictionary *dict in array)从信息数组中一一取出每组图片的所占据布局的位置信息(imageView.frame/imageView.tag),并显示到屏幕上(addSubView.imageView)。
- 初始化地鼠:初始化存放所有地鼠的数组_mouseArray,运用两层for循环,绘制3*3九个地鼠,对正确位置的地鼠进行初始化,并放到合适的位置上。并一一将每次创建好的地鼠加入到数组中[_mouseArray addObject:mouse]。
- 初始化进度条:初始化标签,设置好适合的坐标,使得进度条刚好摆放到图片透明的位置上,并设置Label颜色,并设置Tag即可以认为是进度条的生命值是100。
- 初始化背景音乐:从资源文件中获取背景音乐gophermusic.mp3存放到字符串NSString *file,将资源文件转换成正确格式,从而初始化音乐,设置播放为一直循环,_player.numberOfLoops=-1。
- 开始游戏:为游戏设置时间重复计时器,所需要完成的功能有:选择地鼠,运用随机数产生下标0-8的数字(arc4random()%9)然后添加到地鼠数组中,然后让地鼠能可动setMouseMove,循环的从能动的地鼠数组中,一一让地鼠出洞outToHole,并设置时间定时器timerlose,让时间一秒一秒的减少。
- 实例化地鼠: 将每一个地鼠看作一个Button,每次创建的地鼠都拥有能移动且能出动的功能,当在鼠标点击到地鼠后,即触发监听事件,开始播放地鼠击中哭泣的声音,且图片变为哭泣状,地鼠冒出来多长部分,击中地鼠的那一刻,地鼠往洞里下降。
三、 系统实现
- 游戏视图控制器GameViewController.h
- 游戏视图控制器GameViewController.m
- 实例化的地鼠Mouse.h
- 实例化的地鼠Mouse.m
- 游戏视图控制器GameViewController.h
#import <UIKit/UIKit.h>
@interface GameViewController : UIViewController
@end
- 游戏视图控制器GameViewController.m
//GameViewController.m
#import "GameViewController.h"
#import "Mouse.h"
#import <AVFoundation/AVFoundation.h>
@interface GameViewController ()
@property (nonatomic,retain) NSMutableArray *mouseArray;
@property (nonatomic,retain) NSTimer *timer;
//@property BOOL isWin;
@property NSInteger mscore;
@property AVAudioPlayer *player;
@end
@implementation GameViewController
-(void)dealloc
{
self.mouseArray=nil;
self.timer=nil;
self.player=nil;
[super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self creatGameBackGround];//初始化背景图
[self creatMouse];//初始化地鼠
[self createProgreeBar];//初始化时间进度条
[self creatBackmusic];//初始化背景音乐
[self startGame];//开始游戏
}
-(void)createScore
{
UILabel *sredlabel=[[UILabel alloc]initWithFrame:CGRectMake(140, 80, 50, 50)];
sredlabel.backgroundColor=[UIColor yellowColor];
[self.view insertSubview:sredlabel aboveSubview:[self.view viewWithTag:1]];
sredlabel.tag=50;
// _mscore.=score;
sredlabel.text = @"100$";
sredlabel.textAlignment = UITextAlignmentCenter;
[sredlabel release];
}
-(void)createProgreeBar
{
UILabel *redlabel=[[UILabel alloc]initWithFrame:CGRectMake(90, 420, 205, 22)];
redlabel.backgroundColor=[UIColor redColor];
[self.view insertSubview:redlabel belowSubview:[self.view viewWithTag:4]];
redlabel.tag=100;
[redlabel release];
}
-(void)gameover
{ // self.timer=nil;
[ _timer setFireDate:[NSDate distantFuture]];
}
-(void)creatBackmusic
{
NSString *file=[[NSBundle mainBundle]pathForResource:@"gophermusic" ofType:@"mp3"];
NSURL *url=[NSURL fileURLWithPath:file];
_player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
_player.numberOfLoops=-1;
[_player play];
}
-(void)startGame
{
//时间控制器
_timer=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(action) userInfo:nil repeats:YES];
}
-(void)chooseMouse
{
static int i=0;
if (i%5==0) {
NSInteger index=arc4random()%9;
Mouse *mouse=_mouseArray[index];
[mouse setMouseMove];
}
i++;
}
-(void)action
{
[self chooseMouse];
for (Mouse *mouse in _mouseArray) {
[mouse outToHole];
}
[self timerLose];//时间进度条控制
}
-(void)timerLose
{
UILabel *label=(UILabel*)[self.view viewWithTag:100];
CGRect frame=label.frame;
frame.origin.x-=1;
label.frame=frame;
if(frame.origin.x ==-110)
{
[self gameover];
}
}
-(void)creatMouse
{
_mouseArray=[[NSMutableArray alloc]init];
for (int i=0; i<3; i++) {
for (int j=0; j<3; j++) {
Mouse *mouse=[[Mouse alloc]initWithFrame:CGRectMake(30+j*103, 198+i*87, 56, 79)];
// [self.view addSubview:mouse];
[self.view insertSubview:mouse belowSubview:[self.view viewWithTag:i+2]];
[_mouseArray addObject:mouse];
[mouse release];
}
}
}
-(void)creatGameBackGround
{
NSString *filePath=[[NSBundle mainBundle]pathForResource:@"BackImage" ofType:@"plist"];
NSArray *array=[NSArray arrayWithContentsOfFile:filePath];
for (NSDictionary * dict in array) {
UIImageView *imangeView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:[dict objectForKey:@"name"]]];
imangeView.frame=CGRectFromString([dict objectForKey:@"rect"]);
imangeView.tag=[[dict objectForKey:@"order"]integerValue];
[self.view addSubview:imangeView];
[imangeView release];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
3: 实例化的地鼠Mouse.h
//Mouse.h
#import <UIKit/UIKit.h>
@interface Mouse : UIButton
-(void)outToHole;
- (void)setMouseMove;
@end
4: 实例化的地鼠Mouse.m
// Mouse.m
#import "Mouse.h"
#import <AVFoundation/AVFoundation.h>
#define MOUSE_SPEED 5
@interface Mouse ()
@property NSInteger high;
@property NSInteger score;
@property BOOL isStartMove;
@property (nonatomic,retain) AVAudioPlayer *player ;
@end
@implementation Mouse
-(void)dealloc
{
self.player=nil;
[super dealloc];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setImage:[UIImage imageNamed:@"Mole01"] forState:UIControlStateNormal];
[self setImage:[UIImage imageNamed:@"Mole04"] forState:UIControlStateDisabled];
[self addTarget:self action:@selector(beaten) forControlEvents:UIControlEventTouchUpInside];
self.enabled=NO;
NSString *filePath=[[NSBundle mainBundle] pathForResource:@"Sound15" ofType:@"wav"];
//NSLog(@"filePath=%@",filePath);
NSURL *url=[NSURL fileURLWithPath:filePath];
_player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
[_player prepareToPlay];
}
return self;
}
-(void)beaten
{
if (_high<65) {
_high=130-_high;
}
self.enabled=NO;
_score+=1;
[_player play];
}
-(void)setMouseMove
{
if (!_isStartMove) {
_isStartMove=YES;
self.enabled=YES;
}
}
-(void)outToHole
{
if (_isStartMove==NO) {
return;
}
CGPoint center = self.center;
if (_high < 65) {
center.y -= MOUSE_SPEED;
} else {
center.y += MOUSE_SPEED;
}
self.center = center;
_high += MOUSE_SPEED;
if (_high >= 130) {
_high = 0;
_isStartMove = NO;
self.enabled = NO;
}
}
@end
四、 系统演示
4.1游戏开始
4.2地鼠出洞
4.3击中地鼠