-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGame.m
170 lines (128 loc) · 4.48 KB
/
Game.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//
// Game.m
// The Flappy Saga
//
// Created by Yuxiao Ma on 2015-09-09.
// Copyright (c) 2015 Yuxiao Ma. All rights reserved.
//
#import "Game.h"
@interface Game ()
@end
@implementation Game
-(IBAction)Start:(id)sender{
Start.hidden = YES;
TopObstacle.hidden = NO;
BottomObstacle.hidden = NO;
SagaMovement = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(SagaMoving) userInfo:nil repeats:YES];
[self PlaceObstacle];
ObstacleMovement = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(ObstacleMoving) userInfo:nil repeats:YES];
}
-(void)Score{
ScoreNumber += 1;
ScoreLabel.text =[NSString stringWithFormat:@"Score: %i", ScoreNumber];
}
-(void)GameOver{
if (ScoreNumber > HighScoreNum){
[[NSUserDefaults standardUserDefaults] setInteger:ScoreNumber forKey:@"HighScoreSaved"];
}
[ObstacleMovement invalidate];
[SagaMovement invalidate];
TryAgain.hidden = NO;
Exit.hidden = NO;
TopObstacle.hidden = YES;
BottomObstacle.hidden = YES;
Saga.hidden = YES;
Background.hidden = YES;
Dead.hidden = NO;
GameOver.hidden = NO;
}
//Adjust the edge of RectBox
- (UIImageView *)RectBox:(UIImageView *)box
{
NSData *archive = [NSKeyedArchiver archivedDataWithRootObject:box];
UIImageView *copy = [NSKeyedUnarchiver unarchiveObjectWithData:archive];
copy.frame = CGRectMake(copy.frame.origin.x,
copy.frame.origin.y + 12,
copy.frame.size.width - 32,
copy.frame.size.height - 23);
return copy;
}
//Moving and regenerate the obstacles
-(void)ObstacleMoving{
TopObstacle.center = CGPointMake(TopObstacle.center.x - 1, TopObstacle.center.y);
BottomObstacle.center = CGPointMake(BottomObstacle.center.x - 1, BottomObstacle.center.y);
if (TopObstacle.center.x < -35){
[self PlaceObstacle];
}
if (TopObstacle.center.x == 60){
[self Score];
}
if (CGRectIntersectsRect([self RectBox:Saga].frame, TopObstacle.frame)){
[self GameOver];
}
if (CGRectIntersectsRect([self RectBox:Saga].frame, BottomObstacle.frame)){
[self GameOver];
}
}
//Place the top and bottom obstacle in random position off the screen
-(void)PlaceObstacle{
//Set top obstacle to random y position between lowerBound and upperBound
int lowerBound = -100;
int upperBound = 80;
RandomTopPos = lowerBound + arc4random() % (upperBound - lowerBound);
RandomBotPos = RandomTopPos + 330;
TopObstacle.center = CGPointMake(570, RandomTopPos);
//TopObstacle.frame = CGRectMake(, 0, <#CGFloat width#>, <#CGFloat height#>)
BottomObstacle.center =CGPointMake(570, RandomBotPos);
}
-(void)SagaMoving{
Saga.center = CGPointMake(Saga.center.x, Saga.center.y - SagaFlight);
//Saga will move down in a constant rate.
SagaFlight = SagaFlight - 2.5;
if (SagaFlight < -15) {
SagaFlight = -15;
}
//Saga is moving upward, show upward saga image (useless for now)
if (SagaFlight > 0) {
Saga.image = [UIImage imageNamed:@"wukong.gif"];
}
//Saga is moving upward, show updown saga image
if (SagaFlight < 0) {
Saga.image = [UIImage imageNamed:@"wukong2.gif"];
}
if (Saga.center.y > 350){
[self GameOver];
}
if (Saga.center.y < 5){
Saga.center = CGPointMake(Saga.center.x, 5);
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
SagaFlight = 12;
}
- (void)viewDidLoad {
TopObstacle.hidden = YES;
BottomObstacle.hidden = YES;
Dead.hidden = YES;
GameOver.hidden = YES;
TryAgain.hidden = YES;
Exit.hidden = YES;
ScoreNumber = 0;
HighScoreNum = [[NSUserDefaults standardUserDefaults] integerForKey:@"HighScoreSaved"];
Saga.translatesAutoresizingMaskIntoConstraints = YES;
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end