-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGameScreen.m
131 lines (107 loc) · 4.51 KB
/
GameScreen.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
//
// GameScreen.m
// flutterBird
//
// Created by Ash Bhat on 2/9/14.
// Copyright (c) 2014 Ash Bhat. All rights reserved.
//
#import "GameScreen.h"
@interface GameScreen ()
@end
@implementation GameScreen
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
score = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
[score setText:@"0"];
[score setTextColor:[UIColor whiteColor]];
[score setFont:[UIFont fontWithName:@"helvetica" size:40]];
[score setTextAlignment:NSTextAlignmentCenter];
[self.view addSubview:score];
flipperBird = [[UIImageView alloc]initWithFrame:CGRectMake(40, 100, 40, 40)];
[flipperBird setBackgroundColor:[UIColor greenColor]];
flipperBird.layer.cornerRadius = 20;
flipperBird.layer.masksToBounds = YES;
[flipperBird.layer setBorderColor:(__bridge CGColorRef)([UIColor blackColor])];
[flipperBird.layer setBorderWidth:2];
[self.view addSubview:flipperBird];
top = [[UIImageView alloc]initWithFrame:CGRectMake(320, 0, 40, 200)];
bottom = [[UIImageView alloc]initWithFrame:CGRectMake(320, 360, 40, 200)];
[top setBackgroundColor:[UIColor blackColor]];
[bottom setBackgroundColor:[UIColor blackColor]];
[self.view addSubview:top];
[self.view addSubview:bottom];
myTimer = [NSTimer scheduledTimerWithTimeInterval:.3 target: self
selector: @selector(dropTheBird:) userInfo: nil repeats: YES];
[myTimer fire];
// Do any additional setup after loading the view from its nib.
}
-(void)dropTheBird:(id)sender{
[flipperBird setFrame:CGRectMake(flipperBird.frame.origin.x, flipperBird.frame.origin.y+30, flipperBird.frame.size.width, flipperBird.frame.size.height)];
[top setFrame:CGRectMake(top.frame.origin.x - 10, top.frame.origin.y, top.frame.size.width, top.frame.size.height)];
[bottom setFrame:CGRectMake(bottom.frame.origin.x - 10, bottom.frame.origin.y, bottom.frame.size.width, bottom.frame.size.height)];
if (flipperBird.frame.origin.y > 526) {
UIAlertView *GameOVer = [[UIAlertView alloc]initWithTitle:@"oh no!" message:@"you lost" delegate:self cancelButtonTitle:@"retry" otherButtonTitles:nil, nil];
[GameOVer show];
[myTimer invalidate];
}
if (flipperBird.frame.origin.x >= bottom.frame.origin.x && flipperBird.frame.origin.x <=bottom.frame.origin.x+40) {
if (flipperBird.frame.origin.y <= top.frame.size.height || flipperBird.frame.origin.y >= bottom.frame.origin.y) {
UIAlertView *GameOVer = [[UIAlertView alloc]initWithTitle:@"oh no!" message:[NSString stringWithFormat:@"you lost. Your score was %@",score.text] delegate:self cancelButtonTitle:@"okay" otherButtonTitles:nil, nil];
[GameOVer show];
[myTimer invalidate];
}
}
if (bottom.frame.origin.x < -39) {
[self addtoScore];
[top setFrame:CGRectMake(320, top.frame.origin.y, top.frame.size.width, top.frame.size.height)];
[bottom setFrame:CGRectMake(320, bottom.frame.origin.y, bottom.frame.size.width, bottom.frame.size.height)];
}
}
-(void)addtoScore{
scoreNumber++;
[score setText:[NSString stringWithFormat:@"%i",scoreNumber]];
}
-(void)removeScore{
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
myTimer = nil;
[flipperBird removeFromSuperview];
flipperBird = nil;
[top removeFromSuperview];
top = nil;
[bottom removeFromSuperview];
bottom = nil;
[score removeFromSuperview];
score = nil;
[self viewDidLoad];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self makeFallwithView:flipperBird];
}
-(void)makeFallwithView:(UIImageView*)bird{
[bird setFrame:CGRectMake(bird.frame.origin.x, bird.frame.origin.y-30, bird.frame.size.width, bird.frame.size.height)];
if (bird.frame.origin.y < 0) {
UIAlertView *GameOVer = [[UIAlertView alloc]initWithTitle:@"oh no!" message:[NSString stringWithFormat:@"you lost. Your score was %@",score.text] delegate:self cancelButtonTitle:@"okay" otherButtonTitles:nil, nil];
[GameOVer show];
[myTimer invalidate];
}
}
- (BOOL)prefersStatusBarHidden
{
return YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end