天天看點

瘋狂ios講義之執行個體:貪吃蛇

輕掃手勢處理器在遊戲中的應用十分廣泛,比如在經典的貪食蛇遊戲中,可以讓使用者通過輕掃手勢處理器來改變蛇的移動方向。

貪食蛇的關鍵資料就是記錄蛇的身體,蛇身包含多個點,多個點的資料用于确定蛇身的位置,由于蛇身的長度會動态改變,是以程式将會使用NSMutableArray來記錄蛇身的每個點。

除此之外,貪食蛇隻有兩個需要處理的邏輯。

蛇的移動:程式使用一個定時器控制蛇身的移動。對于蛇身的移動來說,蛇頭受移動方向的控制,當移動方向為向左時,代表蛇頭的點的X坐标減1;當移動方向為向右時,代表蛇頭的點的X坐标加1;當移動方向為向上時,代表蛇頭的點的Y坐标減1;當移動方向為向下時,代表蛇頭的點的Y坐标加1。除此之外,蛇身的其他點總是去“追逐”前一個點,也就是将第i個點的坐标設定為第i+1個點的坐标。

蛇的方向改變:通過輕掃手勢處理器檢測使用者的輕掃手勢,根據輕掃手勢的方向來改變蛇的移動方向。

建立一個SingleView Application,該應用将會包含一個應用程式委托類和一個視圖控制器類,但程式還需要一個自定義UIView控件類,該控件類負責繪制貪食蛇的遊戲界面。

下面是自定義的FKSnakeView類(該類繼承了UIView)的接口代碼。

程式清單:codes/01/1.3/Snake/Snake/FKSnakeView.h

// 記錄地圖上的寬和高有多少個格子

#define WIDTH 15

#define HEIGHT 22

// 定義每個格子的大小

#define CELL_SIZE 20

typedefenum {

kDown = 0,

kLeft,

kRight,

kUp

} Orient;

@interface FKSnakeView : UIView<UIAlertViewDelegate>

// 定義蛇的移動方向

@property (nonatomic , assign) Orient orient;

@end

上面接口部分定義了一些宏變量,并使用了一個枚舉類型,該枚舉類型用于定義蛇的移動方向。

程式清單:codes/01/1.3/Snake/Snake/FKSnakeView.m

@implementation FKSnakeView

// 記錄蛇的點,最後一個點代表蛇頭

NSMutableArray* snakeData;

// 定義食物所在的點

FKPoint* foodPos;

NSTimer* timer;

UIColor* bgColor;

UIImage* cherryImage;

UIAlertView * overAlert;

// 代表遊戲音效變量

SystemSoundID gu;

SystemSoundID crash;

@synthesize orient;

- (id)initWithFrame:(CGRect)frame

{

self =[super initWithFrame:frame];

if(self) {

// 加載食物圖檔

cherryImage = [UIImage imageNamed:@"cherry.png"];

// 加載遊戲背景圖檔,并将背景圖檔轉換為平鋪形式的顔色

bgColor = [UIColor colorWithPatternImage:

[UIImage imageNamed:@"grass.png"]];

// 擷取兩個音效檔案的URL

NSURL* guUrl = [[NSBundle mainBundle]

URLForResource:@"gu" withExtension:@"mp3"];

NSURL* crashUrl = [[NSBundle mainBundle]

URLForResource:@"crash" withExtension:@"wav"];

// 加載兩個音效檔案

AudioServicesCreateSystemSoundID((__bridge CFURLRef)guUrl , &gu);

AudioServicesCreateSystemSoundID((__bridgeCFURLRef)crashUrl , &crash);

overAlert = [[UIAlertView alloc] initWithTitle:@"遊戲結束"

message:@"您輸了,是否重新再來?" delegate:self

cancelButtonTitle:@"不來了"otherButtonTitles:@"再來一盤!", nil];

[self startGame];

}

returnself;

- (void) startGame

//FKPoint的第1個參數控制位于水準第幾格,第2個參數控制位于垂直第幾格

snakeData = [NSMutableArray arrayWithObjects:

[[FKPoint alloc] initWithX:1 y:0],

[[FKPoint alloc] initWithX:2 y:0],

[[FKPoint alloc] initWithX:3 y:0],

[[FKPoint alloc] initWithX:4 y:0],

[[FKPoint alloc] initWithX:5 y:0],nil];

// 定義蛇的初始移動方向

orient =kRight;

timer = [NSTimerscheduledTimerWithTimeInterval:0.3 target:self

selector:@selector(move) userInfo:nilrepeats:YES]; // ①

- (void) move

// 除了蛇頭受方向控制之外,其他點都是占它的前一個點

// 擷取最後一個點,作為蛇頭

FKPoint*first = [snakeData objectAtIndex: snakeData.count - 1];

FKPoint*head = [[FKPoint alloc] initWithX:first.x y:first.y];

switch(orient)

case kDown: // 代表向下

// 新蛇頭的位置

head.y = head.y + 1;

break;

case kLeft: // 代表向左

head.x = head.x - 1;

case kRight: // 代表向右

head.x = head.x + 1;

case kUp: // 代表向上

head.y = head.y - 1;

// 如果移動後蛇頭超出界面或與蛇身碰撞,則遊戲結束

if(head.x < 0 || head.x > WIDTH - 1

||head.y < 0 || head.y > HEIGHT - 1

||[snakeData containsObject:head])

// 播放碰撞的音效

AudioServicesPlaySystemSound(crash);

[overAlert show];

[timer invalidate];

};

// 表明蛇頭與食物點重合

if([headisEqual:foodPos])

// 播放吃食物的音效

AudioServicesPlaySystemSound(gu);

// 将食物點添加成新的蛇頭

[snakeData addObject:foodPos];

// 食物清空

foodPos = nil;

else

// 從第1個點開始,控制蛇身向前

for (int i = 0 ; i < snakeData.count- 1; i++)

// 将第i個點的坐标設定為第i+1個點的坐标

FKPoint* curPt = [snakeDataobjectAtIndex:i];

FKPoint* nextPt = [snakeDataobjectAtIndex:i + 1];

curPt.x = nextPt.x;

curPt.y = nextPt.y;

// 重新設定蛇頭坐标

[snakeData setObject:headatIndexedSubscript:(snakeData.count - 1)];

if(foodPos == nil)

while(true)

FKPoint* newFoodPos = [[FKPoint alloc]

initWithX:arc4random() % WIDTH

y:arc4random() % HEIGHT];

// 如果新産生的食物點沒有位于蛇身上

if(![snakeData containsObject:newFoodPos])

foodPos = newFoodPos;

break; // 成功生成了食物的位置,跳出循環

[selfsetNeedsDisplay];

// 定義繪制蛇頭的方法

- (void) drawHeadInRect:(CGRect)rectcontext:(CGContextRef)ctx

CGContextBeginPath(ctx);

// 根據蛇頭的方向,決定開口的角度

CGFloatstartAngle;

switch(orient) {

casekUp:

startAngle = M_PI * 7 / 4;

casekDown:

startAngle = M_PI * 3 / 4;

casekLeft:

startAngle = M_PI * 5 / 4;

casekRight:

startAngle = M_PI * 1 / 4;

default:

// 添加一段弧作為路徑

CGContextAddArc(ctx, CGRectGetMidX(rect), CGRectGetMidY(rect)

,CELL_SIZE / 2, startAngle, M_PI * 1.5 + startAngle, 0);

// 将繪制點移動到中心

CGContextAddLineToPoint(ctx, CGRectGetMidX(rect), CGRectGetMidY(rect));

// 關閉路徑

CGContextClosePath(ctx);

CGContextFillPath(ctx);

- (void)drawRect:(CGRect)rect

// 擷取繪圖API

CGContextRef ctx = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(ctx, [bgColor CGColor]);

// 繪制背景

CGContextFillRect(ctx, CGRectMake(0 , 0

,WIDTH * CELL_SIZE , HEIGHT * CELL_SIZE));

// 繪制文字

[@"瘋狂貪食蛇" drawAtPoint:CGPointMake(50,20)

withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:

[UIFont fontWithName:@"Heiti SC" size: 40] , NSFontAttributeName,

[UIColor colorWithRed:1 green:0 blue:1 alpha:.4],

NSForegroundColorAttributeName,nil]];

[@"www.fkjava.org" drawAtPoint:CGPointMake(50 ,60)

[UIFont fontWithName:@"Heiti SC" size: 26] ,NSFontAttributeName,

NSForegroundColorAttributeName, nil]];

// 設定繪制蛇的填充顔色

CGContextSetRGBFillColor(ctx, 1, 1, 0, 1);

// 周遊蛇的資料,繪制蛇的資料

for (int i = 0 ; i < snakeData.count ;i++ )

// 為每個蛇的點(記錄的是在數組中的位置),在螢幕上繪制一個圓點

FKPoint* cp = [snakeDataobjectAtIndex:i];

// 定義将要繪制蛇身點的矩形

CGRect rect = CGRectMake(cp.x * CELL_SIZE, cp.y * CELL_SIZE

, CELL_SIZE , CELL_SIZE);

// 繪制蛇尾巴,讓蛇的尾巴小一些

if(i < 4)

CGFloat inset =(4 - i);

CGContextFillEllipseInRect(ctx,CGRectInset(rect,inset, inset));

// 如果是最後一個元素,代表蛇頭,繪制蛇頭

else if (i == snakeData.count - 1)

[self drawHeadInRect:rectcontext:ctx];

CGContextFillEllipseInRect(ctx,rect);

// 繪制“食物”圖檔

[cherryImage drawAtPoint:CGPointMake(foodPos.x * CELL_SIZE

,foodPos.y * CELL_SIZE)];

-(void)alertView:(UIAlertView *)alertView

clickedButtonAtIndex:(NSInteger)buttonIndex

// 如果使用者單擊了第2個按鈕,則重新開始遊戲

if(buttonIndex== 1)

上面程式中,move方法中第1段粗體字代碼負責根據移動方向控制蛇頭的移動;move方法中第2段粗體字代碼采用循環将第i個點的坐标設定為第i+1個點的坐标。

<a href="http://s3.51cto.com/wyfs02/M02/23/95/wKiom1M7Z9zAEgZ1AAErNWsWh-k617.jpg" target="_blank"></a>

該自定義類還在①号粗體字代碼處啟動了定時器,該定時器會控制程式每隔0.3秒執行一次move方法,move方法負責控制蛇身的移動,并在蛇身移動完成後通知FKSnakeView重繪自身。

接下來隻要在視圖控制器中建立并添加FKSnakeView控件即可,除此之外,還需要在視圖控制器中使用輕掃手勢處理器來改變蛇的移動方向。下面是視圖控制器類的實作代碼(接口部分幾乎是空的)。

程式清單:codes/01/1.3/Snake/Snake/FKViewController.m

@implementation FKViewController

FKSnakeView* snakeView;

- (void)viewDidLoad

[super viewDidLoad];

// 建立FKSnakeView控件

snakeView = [[FKSnakeView alloc] initWithFrame:

CGRectMake(10, 10, WIDTH*CELL_SIZE , HEIGHT * CELL_SIZE)];

// 為snakeView控件設定邊框和圓角

snakeView.layer.borderWidth = 3;

snakeView.layer.borderColor = [[UIColor redColor] CGColor];

snakeView.layer.cornerRadius = 6;

snakeView.layer.masksToBounds = YES;

// 設定self.view控件支援使用者互動

self.view.userInteractionEnabled = YES;

// 設定self.view控件支援多點觸碰

self.view.multipleTouchEnabled = YES;

for(int i = 0 ; i &lt; 4 ; i++)

// 建立手勢處理器,指定使用該控制器的handleSwipe:方法處理輕掃手勢

UISwipeGestureRecognizer*gesture = [[UISwipeGestureRecognizer alloc]

initWithTarget:selfaction:@selector(handleSwipe:)];

// 設定該手勢處理器隻處理i個手指的輕掃手勢

gesture.numberOfTouchesRequired = 1;

// 指定該手勢處理器隻處理1&lt;&lt; i方向的輕掃手勢

gesture.direction = 1 &lt;&lt; i;

// 為self.view控件添加手勢處理器

[self.viewaddGestureRecognizer:gesture];

[self.view addSubview:snakeView];

// 實作手勢處理器的方法,該方法應該聲明一個形參

// 當該方法被激發時,手勢處理器會作為參數傳給該方法的參數

- (void)handleSwipe:(UISwipeGestureRecognizer*)gesture

// 擷取輕掃手勢的方向

NSUInteger direction = gesture.direction;

switch (direction)

caseUISwipeGestureRecognizerDirectionLeft:

if(snakeView.orient!= kRight) // 隻要不是向右,即可改變方向

snakeView.orient = kLeft;

caseUISwipeGestureRecognizerDirectionUp:

if(snakeView.orient!= kDown) // 隻要不是向下,即可改變方向

snakeView.orient = kUp;

caseUISwipeGestureRecognizerDirectionDown:

if(snakeView.orient != kUp) // 隻要不是向上,即可改變方向

snakeView.orient = kDown;

caseUISwipeGestureRecognizerDirectionRight:

if(snakeView.orient!= kLeft) // 隻要不是向左,即可改變方向

snakeView.orient = kRight;

上面程式中第1段粗體字代碼為該控制器的UIView(即整個界面)注冊了4個方向的手勢處理器,第2段粗體字代碼位于手勢處理方法中,這些粗體字代碼會根據輕掃方向改變蛇的移動方向。編譯、運作該程式,即可看到如圖1.7所示的遊戲界面。

<a href="http://s3.51cto.com/wyfs02/M01/23/95/wKiom1M7Z_zyGqJ8AAFwSpJWhwQ465.jpg" target="_blank"></a>

圖1.7 貪食蛇遊戲界面

繼續閱讀