-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAryan_Kotwal_A4.pde
368 lines (322 loc) · 7.93 KB
/
Aryan_Kotwal_A4.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*********************
*Aryan Kotwal*
* Space Invaders *
**************************/
//these are the variables declared at the start of the game
PImage Aliens;
PImage Plane;
PImage Bullet;
int numberOfRows = 5;
int numberOfCols = 11;
int numberOfAliens = numberOfRows * numberOfCols;
int timeOfLastMovement;
int speedUpBullet;
int alienDirectionX = 1; // 1 = right, -1 = left
int alienDirectionY = 25;
boolean[] alienIsHit = new boolean[numberOfAliens];
boolean alienRight;
boolean clickedStart = false, clickedHTP = false;
int[] alienX = new int[numberOfAliens];
int [] alienY = new int[numberOfAliens];
int movementRate = 1;
int TankX = 500;
boolean tankRight, tankLeft;
boolean bulletIsFiring;
float gameState = 0 ;
int bulletY = 750;
int bulletX = TankX;
int score = 0;
int alienNum = numberOfAliens;
int multiplier = 10;
void setup() {
textAlign(CENTER, CENTER);
size(1000, 800);
imageMode(CENTER);
Plane = loadImage ("Plane1.png");
Aliens = loadImage ("invader2.png");
Bullet = loadImage ("bullet.png");
//size of plane
Plane.resize(60, 70);
Aliens.resize(50, 50);
Bullet.resize (30, 35);
rectMode(CENTER);
makeAlienStartingPositions();
timeOfLastMovement = millis();
SetVariables ();
}
//Alien start positions
void makeAlienStartingPositions() {
for (int i = 0; i < numberOfAliens; i++) {
alienX[i] = 50+50*(i%numberOfCols);
alienY[i] = 100 + 50 * (i/numberOfCols);
alienIsHit[i] = false;
}
}
void draw() {
println(gameState);
background(#000000);
if (gameState == 0) {
Menu();
}
if (gameState == 0.2) {
howToPlayText();
}
if (gameState == 1) {
drawScore();
drawAliens();
bullet();
drawTank();
moveTank ();
moveAliens();
for (int i = 0; i < 55; i++) {
destroyAlien(i);
checkLose(i);
}
checkWin();
}
if (gameState == 2) {
winState();
drawScore();
clickReplayButton();
for (int i = 0; i < numberOfAliens; i++) {
alienX[i] = 50+50*(i%numberOfCols);
alienY[i] = 100 + 50 * (i/numberOfCols);
}
}
if (gameState==3) {
loseState();
drawScore();
clickReplayButton();
for (int i = 0; i < numberOfAliens; i++) {
alienX[i] = 50+50*(i%numberOfCols);
alienY[i] = 100 + 50 * (i/numberOfCols);
}
}
}
void Menu() {
textSize(64);
fill(#FFFFFF);
text ("Aryan's Space Invaders", width/2, 70);
fill(#000000);
stroke(#FFFFFF);
rect (width/2, 205, 100, 30);
rect (width/2, 250, 170, 35);
textSize(32);
fill(#FFFFFF);
text("START", 500, 200);
text ("How To Play", width/2, 245);
text ("Creator : Aryan Kotwal \n", width/2, 600);
textSize(15);
text ("All Rights Reserved®", width/2, 700);
}
void SetVariables () {
numberOfRows = 5;
numberOfCols = 11;
numberOfAliens = numberOfRows * numberOfCols;
timeOfLastMovement = 0;
speedUpBullet = 0;
alienDirectionX = 1; // 1 = right, -1 = left
alienDirectionY = 25;
movementRate = 1;
TankX = 500;
bulletY = 750;
bulletX = TankX;
score = 0;
alienNum = numberOfAliens;
multiplier = 10;
makeAlienStartingPositions();
timeOfLastMovement = millis();
moveAliens();
moveTank ();
shootBullet();
}
void checkMenuButtons() {
if (mouseX >400 && mouseX < 600 && mouseY > 190 && mouseY < 220) {
gameState = 1;
SetVariables ();
} else if (mouseX > 230 && mouseX <670 && mouseY > 215 && mouseY < 285) {
gameState = 0.2;
}
/**/
}
void clickHTPButtons() {
if (mouseX >230 && mouseX < 570 && mouseY > 480 && mouseY < 520) {
gameState = 0;
}
}
void howToPlayText() {
textSize(40);
text ("Left arrow = Move Plane Left \n Right arrow = Move Plane Right \n Space bar = Shoot Bullet", width/2, 300);
stroke(#FFFFFF);
noFill();
rect (width/2, 500, 150, 30);
//textSize(32);
fill(#FFFFFF);
text("Return", width/2, 495);
if (mouseX >400 && mouseX < 600 && mouseY > 470 && mouseY < 530 && mousePressed == true) {
gameState = 0;
}
}
//draw Player score
void drawScore() {
textSize(40);
text(" Points: " + score, 500, 50);
}
//This is the winners command
void winState() {
textSize(50);
text("You win!!", width/2, 400);
}
void loseState() {
textSize(50);
text("You lose!", width/2, 400);
}
void checkWin() {
if (alienNum < 1) {
gameState = 2;
}
}
void clickReplayButton() {
stroke(#FFFFFF);
noFill();
rect (width/2, 500, 150, 35);
textSize(32);
fill(#FFFFFF);
text("Play Again", width/2, 495);
if (mouseX >400 && mouseX < 600 && mouseY > 470 && mouseY < 530 && mousePressed == true) {
gameState = 0;
//SetVariables ();
}
}
void checkLose(int i) {
if (alienY[i] > 780) {
gameState = 3;
}
}
//move aliens down after bounce
void moveAliensDown() {
for (int i = 0; i < numberOfAliens; i++) {
alienY[i] = alienY[i] + alienDirectionY;
}
timeOfLastMovement = millis();
multiplier += 10;
}
boolean someoneTouchedTheEdge = false;
//bounce aliens off the edges
void checkAliensHitEdge(int thisAlien) {
if ((alienX[thisAlien] > 930 && !alienIsHit[thisAlien]) || (alienX[thisAlien] < 75 && !alienIsHit[thisAlien])) {
someoneTouchedTheEdge = true;
}
}
//draw aliens
void drawAliens() {
for (int i = 0; i < numberOfAliens; i++) {
if (!alienIsHit[i]) {
image(Aliens, alienX[i], alienY[i]);
}
}
}
//move the aliens
void moveAliens() {
movementRate = 400 + (alienNum)*30;
if (millis() - timeOfLastMovement > movementRate) {
for (int i = 0; i < numberOfAliens; i++) {
alienX[i] += alienDirectionX*70;
checkAliensHitEdge(i);
}
if (someoneTouchedTheEdge) {
moveAliensDown();
alienDirectionX *= -1;
someoneTouchedTheEdge = false;
}
timeOfLastMovement = millis();
}
}
//draw the tank
void drawTank () {
image(Plane, TankX, 750);
//rect(TankX, 750, 50, 30);
}
//move the tank
void moveTank () {
int tankRightSide = TankX + 25;
int tankLeftSide = TankX - 25;
if (tankRight && tankRightSide < width) {
TankX = TankX + 10;
}
if (tankLeft && tankLeftSide > 0)
TankX=TankX - 10;
}
//draw the bullet and regulate firing
void bullet () {
if (bulletIsFiring) {
//rect(bulletX, bulletY, 10, 15);
image(Bullet, bulletX, bulletY);
shootBullet();
}
}
//move the bullet firing
void shootBullet() {
bulletY -= 10;
if (bulletY<0) {
bulletIsFiring=false;
}
if (millis() - speedUpBullet > 5000) {
for (int i = 0; i < 5; i++) {
bulletY -= 10 + i;
}
speedUpBullet = millis();
}
}
//this is the destroy alien code
void destroyAlien (int i) {
int AlienR = alienX[i] + 15;
int AlienL = alienX[i] - 15;
int AlienT = alienY[i] -15;
int AlienD = alienY[i] + 15;
int bulletR = bulletX + 5;
int bulletL = bulletX - 5;
int bulletT = bulletY - 7;
int bulletB = bulletY + 7;
if (!alienIsHit[i] && bulletT < AlienD && bulletR > AlienL && bulletL < AlienR && bulletB > AlienT) {
alienIsHit[i] = true;
bulletIsFiring= false;
bulletX = TankX;
bulletY = 700;
score += 100 + multiplier;
alienNum-=1;
}
}
//what happens when key is pressed
void keyPressed() {
if (keyCode == 32 && !bulletIsFiring) {
bulletIsFiring= true;
bulletX = TankX;
bulletY = 700;
}
if ( keyCode == 39) {
tankRight=true;
}
if ( keyCode == 37) {
tankLeft=true;
}
}
//what happens when keys are released
void keyReleased() {
if (keyCode == 39) {
tankRight = false;
}
if (keyCode == 37) {
tankLeft = false;
}
}
//mouse pressed code (what happens when clicked)
void mousePressed() {
if (gameState == 0) {
checkMenuButtons();
} else if (gameState == 0.2) {
clickHTPButtons();
} else if (gameState == 2 || gameState == 3) {
clickReplayButton();
}
}