-
Notifications
You must be signed in to change notification settings - Fork 63
/
assign6.pde
177 lines (166 loc) · 4.04 KB
/
assign6.pde
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
171
172
173
174
175
176
177
class GameState
{
static final int START = 0;
static final int PLAYING = 1;
static final int END = 2;
}
class Direction
{
static final int LEFT = 0;
static final int RIGHT = 1;
static final int UP = 2;
static final int DOWN = 3;
}
class EnemysShowingType
{
static final int STRAIGHT = 0;
static final int SLOPE = 1;
static final int DIAMOND = 2;
static final int STRONGLINE = 3;
}
class FlightType
{
static final int FIGHTER = 0;
static final int ENEMY = 1;
static final int ENEMYSTRONG = 2;
}
int state = GameState.START;
int currentType = EnemysShowingType.STRAIGHT;
int enemyCount = 8;
int shooting = 0;
Enemy[] enemys = new Enemy[enemyCount];
Fighter fighter;
Background bg;
FlameMgr flameMgr;
Treasure treasure;
HPDisplay hpDisplay;
Bullet[]bullets = new Bullet[5];
boolean isMovingUp;
boolean isMovingDown;
boolean isMovingLeft;
boolean isMovingRight;
int time;
int wait = 4000;
void setup () {
size(640, 480);
flameMgr = new FlameMgr();
bg = new Background();
treasure = new Treasure();
hpDisplay = new HPDisplay();
fighter = new Fighter(20);
for (int i = 0; i < 5; ++i) {
bullets[i]=null;
}
}
void draw()
{
if (state == GameState.START) {
bg.draw();
}
else if (state == GameState.PLAYING) {
bg.draw();
treasure.draw();
flameMgr.draw();
fighter.draw();
//enemys
if(millis() - time >= wait){
addEnemy(currentType++);
currentType = currentType%4;
}
for (int i = 0; i < enemyCount; ++i) {
if (enemys[i]!= null) {
enemys[i].move();
enemys[i].draw();
if (currentType == EnemysShowingType.STRAIGHT){
if (enemys[i].isCollideWithFighter()) {
fighter.hpValueChange(-50);
flameMgr.addFlame(enemys[i].x, enemys[i].y);
enemys[i]=null;
}
}else if (currentType != EnemysShowingType.STRAIGHT){
if (enemys[i].isCollideWithFighter()) {
fighter.hpValueChange(-20);
flameMgr.addFlame(enemys[i].x, enemys[i].y);
enemys[i]=null;
}
}
else if (enemys[i].isOutOfBorder()) {
enemys[i]=null;
}
}
}
hpDisplay.updateWithFighterHP(fighter.hp);
//bullet
for (int i = 0; i < 5; i++) {
for (int j = 0; j < enemyCount; ++j) {
if(bullets[i] != null){
bullets[i].draw();
bullets[i].move();
if(bullets[i] != null && enemys[j] != null && isHit(bullets[i].x, bullets[i].y, bullets[i].bulletImg.width, bullets[i].bulletImg.height, enemys[j].x, enemys[j].y, enemys[j].enemyImg.width, enemys[j].enemyImg.height)){
enemys[j].life--;
bullets[i] = null;
if(enemys[j].life == 0){
flameMgr.addFlame(enemys[j].x, enemys[j].y);
enemys[j]=null;
bullets[i]=null;
}
}
}
}
}
}
else if (state == GameState.END) {
bg.draw();
currentType = EnemysShowingType.STRAIGHT;
for (int i = 0; i < 5; ++i) {
bullets[i]=null;
}
}
}
boolean isHit(int ax, int ay, int aw, int ah, int bx, int by, int bw, int bh)
{
// Collision x-axis?
boolean collisionX = (ax + aw >= bx) && (bx + bw >= ax);
// Collision y-axis?
boolean collisionY = (ay + ah >= by) && (by + bh >= ay);
return collisionX && collisionY;
}
void keyPressed(){
switch(keyCode){
case UP : isMovingUp = true ;break ;
case DOWN : isMovingDown = true ; break ;
case LEFT : isMovingLeft = true ; break ;
case RIGHT : isMovingRight = true ; break ;
default :break ;
}
}
void keyReleased(){
switch(keyCode){
case UP : isMovingUp = false ;break ;
case DOWN : isMovingDown = false ; break ;
case LEFT : isMovingLeft = false ; break ;
case RIGHT : isMovingRight = false ; break ;
default :break ;
}
if (key == ' ') {
if (state == GameState.PLAYING) {
if (bullets[shooting] == null){
fighter.shoot(shooting);
shooting++;
shooting%=5;
}
}
}
if (key == ENTER) {
switch(state) {
case GameState.START:
case GameState.END:
state = GameState.PLAYING;
enemys = new Enemy[enemyCount];
flameMgr = new FlameMgr();
treasure = new Treasure();
fighter = new Fighter(20);
default : break ;
}
}
}