Hier finden Sie die Quellcodes und benötigten Grafiken zum Geschicklichkeitsspiel „Marble Maze“ aus dem Buch (Kapitel 3.4), damit Sie den langen Code nicht von Hand abtippen müssen.
Video des Gameplays:
VIDEO
Verwendete Grafiken: Download
Quellcode:
01
#
import
<UIKit/UIKit.h>
02
#
import
<CoreMotion/CoreMotion.h>
03
#
import
<QuartzCore/QuartzCore.h>
05
@interface
marblemazeViewController : UIViewController {
08
UIImageView *ballSchatten;
09
NSMutableArray *walls;
10
NSMutableArray *holes;
12
CMMotionManager *motionManager;
20
@property
(nonatomic,strong) UIImageView *canvas;
21
@property
(nonatomic,strong) UIImageView *ball;
22
@property
(nonatomic,strong) UIImageView *ballSchatten;
23
@property
(nonatomic, strong) NSMutableArray *walls;
24
@property
(nonatomic, strong) NSMutableArray *holes;
25
@property
(nonatomic, strong) CMMotionManager *motionManager;
27
- (
void
)missionFailed: (
int
) holeId;
001
#
import
"marblemazeViewController.h"
002
#include
"leveldesign.h"
004
@implementation
marblemazeViewController
005
@synthesize
canvas, ball, ballSchatten, walls, holes, motionManager;
009
[[UIApplication sharedApplication] setStatusBarHidden:YES];
010
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
013
canvas = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen]
015
[canvas setImage:[UIImage imageNamed:@
"canvas.png"
]];
016
canvas.userInteractionEnabled = YES;
020
holes = [[NSMutableArray alloc] init];
021
for
(
int
i =
0
; i < sizeof(holeX)/sizeof(holeX[
0
]); i++) {
022
UIImageView *tempImgView = [[UIImageView alloc]initWithFrame:
023
CGRectMake(holeX[i]-
15
, holeY[i]-
15
,
30
,
30
)];
024
[tempImgView setImage:[UIImage imageNamed:@
"hole.png"
]];
025
if
(i==
0
)[tempImgView setImage:[UIImage imageNamed:@
"ziel.png"
]];
026
[holes addObject:tempImgView];
027
[canvas addSubview:[holes objectAtIndex:i]];
031
walls = [[NSMutableArray alloc] init];
032
for
(
int
i =
0
; i < sizeof(wallX0)/sizeof(wallX0[
0
]); i++) {
033
UIImageView *tempImgView = [[UIImageView alloc] initWithFrame:
034
CGRectMake(wallX0[i], wallY0[i],
036
wallY1[i]-wallY0[i])];
037
[tempImgView setImage:[UIImage imageNamed:@
"wall.png"
]];
038
[walls addObject:tempImgView];
039
[canvas addSubview:[walls objectAtIndex:i]];
043
ballSchatten = [[UIImageView alloc] initWithFrame:CGRectMake(
40
+
8
,
440
+
8
,
045
[ballSchatten setImage:[UIImage imageNamed:@
"ball_shadow.png"
]];
046
[canvas addSubview:ballSchatten];
049
ball = [[UIImageView alloc] initWithFrame:CGRectMake(
40
,
440
,
25
,
25
)];
050
[ball setImage:[UIImage imageNamed:@
"ball.png"
]];
051
[canvas addSubview:ball];
054
motionManager = [[CMMotionManager alloc] init];
055
motionManager.accelerometerUpdateInterval =
0.060
;
056
[motionManager startAccelerometerUpdates];
063
CADisplayLink *callGameEngine = [CADisplayLink
064
displayLinkWithTarget:self
065
selector:
@selector
(gameEngine)];
066
[callGameEngine setFrameInterval:
2
];
067
[callGameEngine addToRunLoop:[NSRunLoop currentRunLoop]
068
forMode:NSDefaultRunLoopMode];
073
- (
void
)missionFailed: (
int
) holeId {
075
__block
int
x=holeX[holeId];
076
__block
int
y=holeY[holeId];
080
[UIView transitionWithView:self.view
082
options:UIViewAnimationOptionCurveEaseIn
084
ball.center = CGPointMake(x, y);
085
ballSchatten.center = CGPointMake(x, y);
086
ball.transform = CGAffineTransformMakeScale(
0.9
,
0.9
);
087
} completion:^(BOOL finished) {
090
[UIView transitionWithView:self.view
092
options:UIViewAnimationOptionCurveEaseIn
095
ballSchatten.alpha=
0.0
;
096
} completion:^(BOOL finished) {
097
ball.center = CGPointMake(
40
,
440
);
098
ballSchatten.center = CGPointMake(
40
-
4
,
440
-
4
);
100
ballSchatten.alpha=
1.0
;
102
ball.transform = CGAffineTransformMakeScale(
1
,
1
);
111
CMAccelerometerData *acceleration = motionManager.accelerometerData;
112
ballSpeedX+=acceleration.acceleration.x;
113
ballSpeedY-=acceleration.acceleration.y;
116
if
((ball.center.x+ballSpeedX>
320
-
10
) || (ball.center.x+ballSpeedX<
0
+
10
)) {
117
ballSpeedX=-ballSpeedX/
4
;
118
if
(ballSpeedX*ballSpeedX>
0.5
) ballSpeedY=ballSpeedY/
4
;
120
if
((ball.center.y+ballSpeedY>
480
-
10
) || (ball.center.y+ballSpeedY<
0
+
10
)) {
121
if
(ballSpeedY*ballSpeedY>
0.5
) ballSpeedX=ballSpeedX/
4
;
122
ballSpeedY=-ballSpeedY/
4
;
126
for
(
int
i =
0
; i < sizeof(holeX)/sizeof(holeX[
0
]); i++) {
127
UIImageView *hole = [holes objectAtIndex:i];
129
(hole.center.x-ball.center.x)*(hole.center.x-ball.center.x) +
130
(hole.center.y-ball.center.y)*(hole.center.y-ball.center.y) <
15
*
15
132
if
(i==
0
) ball.center=CGPointMake(hole.center.x, hole.center.y);
133
else
[self missionFailed:i];
138
for
(
int
i =
0
; i < sizeof(wallX0)/sizeof(wallX0[
0
]); i++) {
139
UIImageView *wall = [walls objectAtIndex:i];
142
if
(CGRectIntersectsRect(CGRectMake(ball.frame.origin.x+ballSpeedX,
143
ball.frame.origin.y+ballSpeedY,
144
25
,
25
), wall.frame))
147
if
((ballSpeedX>
0
) &&
148
(ball.center.x < wall.center.x) &&
149
(ball.center.y > wallY0[i]) &&
150
(ball.center.y < wallY1[i]) ) {
151
ball.center = CGPointMake(wallX0[i]-
13
, ball.center.y);
152
ballSpeedX=-ballSpeedX/
4
;
153
if
(ballSpeedX*ballSpeedX>
0.5
) ballSpeedY=ballSpeedY/
4
;
156
}
else
if
((ballSpeedX<
0
) &&
157
(ball.center.x > wall.center.x) &&
158
(ball.center.y > wallY0[i]) &&
159
(ball.center.y < wallY1[i]) ) {
160
ball.center = CGPointMake(wallX1[i]+
13
, ball.center.y);
161
ballSpeedX=-ballSpeedX/
4
;
162
if
(ballSpeedX*ballSpeedX>
0.5
) ballSpeedY=ballSpeedY/
4
;
165
}
else
if
((ballSpeedY>
0
) &&
166
(ball.center.y < wall.center.y) &&
167
(ball.center.x > wallX0[i]) &&
168
(ball.center.x < wallX1[i]) ) {
169
ball.center = CGPointMake(ball.center.x, wallY0[i]-
13
);
170
if
(ballSpeedY*ballSpeedY>
0.5
) ballSpeedX=ballSpeedX/
4
;
171
ballSpeedY=-ballSpeedY/
4
;
175
}
else
if
((ballSpeedY<
0
) &&
176
(ball.center.y > wall.center.y) &&
177
(ball.center.x > wallX0[i]) &&
178
(ball.center.x < wallX1[i]) ) {
179
ball.center = CGPointMake(ball.center.x, wallY1[i]+
13
);
180
if
(ballSpeedY*ballSpeedY>
0.5
) ballSpeedX=ballSpeedX/
4
;
181
ballSpeedY=-ballSpeedY/
4
;
187
ball.center = CGPointMake(ball.center.x+ballSpeedX,
188
ball.center.y+ballSpeedY);
189
ballSchatten.center = CGPointMake(ball.center.x+
8
, ball.center.y+
8
);
194
- (
void
)didReceiveMemoryWarning {
196
[
super
didReceiveMemoryWarning];
201
- (
void
)viewDidUnload {
09
@interface
leveldesign : NSObject {}
01
#
import
"leveldesign.h"
03
int
holeX[
15
]={
23
,
183
,
143
,
103
,
183
,
223
,
303
,
23
,
103
,
223
,
263
,
263
,
223
,
23
,
63
};
04
int
holeY[
15
]={
23
,
23
,
63
,
103
,
143
,
223
,
183
,
223
,
263
,
383
,
463
,
303
,
103
,
143
,
343
};
06
int
wallX0[
21
]={
240
,
80
,
240
,
280
,
240
,
120
,
280
,
07
0
,
120
,
80
,
160
,
200
,
240
,
0
,
08
200
,
280
,
80
,
200
,
80
,
240
,
0
};
09
int
wallY0[
21
]={
40
,
0
,
40
,
80
,
120
,
160
,
160
,
10
200
,
160
,
240
,
200
,
200
,
280
,
360
,
11
360
,
360
,
240
,
440
,
320
,
280
,
80
};
12
int
wallX1[
21
]={
280
,
86
,
246
,
320
,
280
,
240
,
320
,
13
126
,
126
,
160
,
166
,
206
,
320
,
86
,
14
280
,
286
,
86
,
206
,
86
,
246
,
40
};
15
int
wallY1[
21
]={
46
,
160
,
240
,
86
,
126
,
166
,
166
,
16
206
,
200
,
246
,
480
,
360
,
286
,
366
,
17
366
,
400
,
280
,
480
,
360
,
320
,
86
};
19
@implementation
leveldesign