Advertisement
Guest User

Alex's Game

a guest
Dec 14th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.78 KB | None | 0 0
  1. //PENDING//
  2.  
  3. //Add asteroids*
  4. //2-5hp*
  5. //random rotation of image - pd3???
  6.  
  7. //gameOver splash screen (playing loseBGM instead of BGM)
  8. //keyPressed('ENTER'); = restart game*
  9. //loseBGM plays instead of BGM
  10. //game over splash screen when shipHealth = 0 or earth health = 0*
  11. //display score*
  12. //input name and score into a text file
  13. //Start Screen:
  14. //display previous scores with names from text file
  15.  
  16. //boss shooting back
  17.  
  18. //if(bullet colliding with ship || asteriod colliding with ship)
  19. //shipHealth = shipHealth-1;
  20.  
  21. //COMPLETE//
  22.  
  23. //-(easy) change alien img *
  24. //-(hard) when alien hits bottom, add more aliens in top *
  25. //-(easy) boss at 50 kills *
  26. //-(medium) health for ship and aliens *
  27. //-(easy) background music *
  28.  
  29. //add kill counter*
  30. //increase life every 100 kills*
  31. //if healthTempKills == 100, then add life, then set back to 0*
  32. //spawn boss every 50 kills, temp spawn boss set back to 0*
  33. //display kills*
  34.  
  35. //explosion on kill*
  36. //explosion when boss dies*
  37. //explosion when asteroid dies*
  38.  
  39. //earth health*
  40. //decreases when enemies y>height && x<width && x>0*
  41.  
  42. //Parallax scrolling background*
  43.  
  44. import ddf.minim.*;
  45.  
  46. Minim minim;
  47. AudioPlayer shootingSound,BGM,loseBGM,explosionSound;
  48.  
  49. boolean gameRunning=true;
  50.  
  51. String typing = "";
  52. String saved = "";
  53.  
  54. int shipHealth;
  55. int earthHealth;
  56. int asteroidHealth = (int)random(2,5);
  57. int enemyHealth = 3;
  58. int bossHealth = 20;
  59. int shipX,shipY;
  60. int shipWidth,shipHeight;
  61. int killCount;
  62. int tempKills;
  63. int bossSpawn=10;
  64. int healthTempKills;
  65.  
  66. PImage starW_LG;
  67. PImage starW_SM;
  68. PImage starR;
  69. PImage starO;
  70. PImage starY;
  71. PImage starB;
  72. PImage bg;
  73. PImage shipImg;
  74. PImage enemyImg;
  75. PImage bossImg;
  76. PImage asteroidImg;
  77.  
  78. ArrayList<Bullet> bullets;
  79. ArrayList<bossBullet> bossBullets;
  80. ArrayList<Enemies> enemies;
  81. ArrayList<boss> boss;
  82. ArrayList <Asteroids> asteroids;
  83. ArrayList<Explosion> explosions;
  84. ArrayList<Starfield> starfield;
  85. ArrayList<Starfield2> starfield2;
  86.  
  87. PImage[] images;
  88. int imageCount = 70;
  89.  
  90. PrintWriter scores;
  91.  
  92. void setup(){
  93. size(800,800);
  94. shipHealth = 10;
  95. earthHealth = 100;
  96. killCount = 0;
  97. tempKills = 0;
  98. healthTempKills =0;
  99.  
  100. shipX = width/2;
  101. shipWidth = 60;
  102. shipHeight = 60;
  103. shipY = height - shipHeight - 20;
  104.  
  105. bullets = new ArrayList <Bullet> ();
  106. bossBullets = new ArrayList <bossBullet> ();
  107. enemies = new ArrayList <Enemies> ();
  108. boss = new ArrayList <boss> ();
  109. asteroids = new ArrayList <Asteroids> ();
  110. explosions = new ArrayList <Explosion> ();
  111. starfield = new ArrayList <Starfield> ();
  112. starfield2 = new ArrayList <Starfield2> ();
  113.  
  114. images = new PImage[imageCount];
  115. for (int i = 1; i <= imageCount; i++) {
  116. // Use nf() to number format 'i' into four digits
  117. String filename = "frame" + nf(i, 2) + ".png";
  118. images[i-1] = loadImage(filename);
  119. }
  120. //generate enemie
  121. for(int i=0;i<random(2,5);i++){
  122. Enemies e = new Enemies((int) random(width),(int) random(-height/2),enemyHealth);
  123. enemies.add(e);
  124. }
  125.  
  126. //generate asteroids
  127. for(int i=0;i<2;i++){
  128. Asteroids a = new Asteroids((int) random(width),(int) random(-height/2),asteroidHealth);
  129. asteroids.add(a);
  130. }
  131.  
  132. //generate starfield 2
  133. for(int i=0;i<200;i++){
  134. Starfield2 s2 = new Starfield2((int) random(width),(int) random(height));
  135. starfield2.add(s2);
  136. }
  137. //generate starfield
  138. for(int i=0;i<100;i++){
  139. Starfield s = new Starfield((int) random(width),(int) random(height));
  140. starfield.add(s);
  141. }
  142.  
  143. minim = new Minim(this);
  144.  
  145. // this loads mysong.wav from the data folder
  146. shootingSound = minim.loadFile("shootingSound.mp3");
  147. BGM= minim.loadFile("BGM.mp3");
  148. loseBGM=minim.loadFile("loseBGM.mp3");
  149. explosionSound = minim.loadFile("explosion.mp3");
  150.  
  151. //Background Image
  152. bg = loadImage("backgroundImage.png");
  153. bg.resize(width,height);
  154.  
  155. //Stars Images
  156. starW_LG = loadImage("whiteStarLG.png");
  157. starW_SM = loadImage("whiteStarSM.png");
  158. starR = loadImage("redStar.png");
  159. starO = loadImage("orangeStar.png");
  160. starY = loadImage("yellowStar.png");
  161. starB = loadImage("blueStar.png");
  162.  
  163. //Character Images
  164. shipImg = loadImage("ship.png");
  165. shipImg.resize(shipWidth,shipHeight);
  166. enemyImg = loadImage("alien.png");
  167. enemyImg.resize(50,50);
  168. bossImg = loadImage("boss.png");
  169. bossImg.resize(132,132);
  170. asteroidImg = loadImage("asteroid.png");
  171. asteroidImg.resize(50,50);
  172. // Create a new file in the sketch directory
  173.  
  174. BGM.loop();
  175. }
  176.  
  177. void draw(){
  178. background(0);
  179. if(earthHealth>100){
  180. earthHealth = 100;
  181. }
  182. if(earthHealth<=0 || shipHealth<= 0){
  183. gameRunning=false;
  184. gameOver();
  185. }
  186. else{
  187. gameRunning = true;
  188. drawStars();
  189. moveStars();
  190. drawStars2();
  191. moveStars2();
  192. drawShip();
  193. drawBullets();
  194. moveBullets();
  195. drawEnemies();
  196. moveEnemies();
  197. drawAsteriod();
  198. moveAsteroids();
  199. drawBoss();
  200. moveBoss();
  201. drawExplosions();
  202. moveExplosion();
  203. drawScoreHealth();
  204. checkForCollisions();
  205. }
  206. //println(frameRate);
  207. }
  208.  
  209. void drawScoreHealth(){
  210.  
  211. //Box
  212. fill(0);
  213. rect(0,0,width,75);
  214.  
  215. //Font
  216. PFont font;
  217. font = createFont("spaceFont.ttf",32);
  218. textFont(font, 32);
  219.  
  220. //Score and Health
  221. fill(255,10,0);
  222. text("Invaders Killed: "+killCount,10,50);
  223. fill(255,255,10);
  224. text("Ship Health: "+shipHealth, 500,30);
  225. text("Earth Health: "+earthHealth,500,60);
  226. }
  227.  
  228. void drawStars(){
  229. for(int i=0;i<starfield.size();i++){
  230. starfield.get(i).display();
  231. }
  232. }
  233.  
  234. void drawStars2(){
  235. for(int i=0;i<starfield2.size();i++){
  236. starfield2.get(i).display();
  237. }
  238. }
  239.  
  240. void drawShip(){
  241. fill(0,100,255);
  242. image(shipImg,shipX-shipWidth/2,shipY);
  243. }
  244.  
  245. void drawBullets(){
  246. for(int i=0;i<bullets.size();i++){
  247. bullets.get(i).display();
  248. }
  249. }
  250.  
  251. void drawEnemies(){
  252. for(int i=0; i <enemies.size();i++){
  253. enemies.get(i).display();
  254. }
  255. }
  256.  
  257. void drawBossBullet(){
  258. for(int i=0;i<bossBullets.size();i++){
  259. bossBullets.get(i).display();
  260. //create Bullet object
  261. bossBullet b = new bossBullet(boss.get(i).getX(),boss.get(i).getY());
  262. bossBullets.add(b);
  263. }
  264. }
  265.  
  266. void drawBoss(){
  267. for(int i=0;i<boss.size();i++){
  268. boss.get(i).display();
  269. }
  270. }
  271.  
  272. void drawExplosions() {
  273. for(int i=0;i<explosions.size();i++){
  274. if(explosions.get(i).display()) {
  275. explosions.remove(i);
  276. }
  277. }
  278. }
  279.  
  280. void drawAsteriod(){
  281. for(int i=0;i<asteroids.size();i++){
  282. //imageMode(CENTER);
  283. asteroids.get(i).display();
  284. }
  285. }
  286.  
  287. void moveStars(){
  288. for(int i=0;i<starfield.size();i++){
  289. boolean onScreen = starfield.get(i).move();
  290. if(onScreen == false){
  291. starfield.remove(i);
  292. Starfield s = new Starfield((int) random(width),(int) random(100) - 100);
  293. starfield.add(s);
  294. }
  295. }
  296. }
  297.  
  298. void moveStars2(){
  299. for(int i=0;i<starfield2.size();i++){
  300. boolean onScreen = starfield2.get(i).move();
  301. if(onScreen == false){
  302. starfield2.remove(i);
  303. Starfield2 s2 = new Starfield2((int) random(width),(int) random(100) - 100);
  304. starfield2.add(s2);
  305. }
  306. }
  307. }
  308.  
  309. void moveBullets(){
  310. for(int i=0;i<bullets.size();i++){
  311. boolean onScreen = bullets.get(i).move();
  312. if(onScreen == false){
  313. bullets.remove(i);
  314. }
  315. }
  316. }
  317.  
  318.  
  319. void moveEnemies(){
  320. for(int i=0;i<enemies.size();i++){
  321. boolean onScreen = enemies.get(i).move();
  322. if(onScreen == false){
  323. enemies.remove(i);
  324. Enemies e = new Enemies((int) random(width),(int) random(100) - 100,enemyHealth);
  325. enemies.add(e);
  326. }
  327. }
  328. }
  329.  
  330. void moveBoss(){
  331. for(int i=0;i<boss.size();i++){
  332. boolean onScreen = boss.get(i).move();
  333. if(onScreen == false){
  334. boss.remove(i);
  335. }
  336. }
  337. }
  338.  
  339. void moveBossBullets(){
  340. for(int i=0;i<bossBullets.size();i++){
  341. boolean onScreen = bossBullets.get(i).move();
  342. if(onScreen == false){
  343. bossBullets.remove(i);
  344. }
  345. }
  346. }
  347.  
  348. void moveAsteroids(){
  349. for(int i=0;i<asteroids.size();i++){
  350. boolean onScreen = asteroids.get(i).move();
  351. if(onScreen == false){
  352. asteroids.remove(i);
  353. Asteroids a = new Asteroids((int) random(width),(int) random(100) - 100,asteroidHealth);
  354. asteroids.add(a);
  355. }
  356. }
  357. }
  358.  
  359. void moveExplosion(){
  360. for(int i=0;i<explosions.size();i++){
  361. boolean onScreen = explosions.get(i).move();
  362. if(onScreen == true){
  363.  
  364. }
  365. }
  366. }
  367.  
  368. void checkForCollisions(){
  369. for(int i=0;i<enemies.size();i++){
  370. for(int j=0;j<bullets.size();j++){
  371. //if i-th enemey is colliding with j-th bullet, remove the enemy, remove
  372. //the bullet, adjust score
  373. if(enemies.get(i).collidingWith(bullets.get(j))){
  374. //remove bullet
  375. bullets.remove(j);
  376. if(enemies.get(i).enemyHealth<=0){
  377. //explosion animation
  378. explosions.add(new Explosion(enemies.get(i).getX(),enemies.get(i).getY()));
  379. moveExplosion();
  380. explosionSound.play();
  381. explosionSound.rewind();
  382. //remove the enemy
  383. enemies.remove(i);
  384. //spawn new enemy
  385. Enemies e = new Enemies((int) random(width),(int) random(100) - 100,enemyHealth);
  386. enemies.add(e);
  387. //increase kill count
  388. killCount++;
  389. //increase kills for extra life
  390. healthTempKills++;
  391. /*if(healthTempKills>100){
  392. shipHealth++;
  393. healthTempKills =0;
  394. }*/
  395. //increase kills for boss spawn
  396. tempKills++;
  397. //spawn boss after x kills
  398. if(tempKills>=bossSpawn){
  399. boss b = new boss((int) random(width),(int) random(-height/2),bossHealth);
  400. boss.add(b);
  401. tempKills = 0;
  402. }
  403. break;
  404. }
  405. }
  406. }
  407. }
  408. for(int i=0;i<boss.size();i++){
  409. for(int j=0;j<bullets.size();j++){
  410. //if i-th enemey is colliding with j-th bullet
  411. if(boss.get(i).collidingWith(bullets.get(j))){
  412. //remove bullet
  413. bullets.remove(j);
  414. if(boss.get(i).bossHealth<=0){
  415. //explosion animation
  416. explosions.add(new Explosion(boss.get(i).getX(),boss.get(i).getY()));
  417. moveExplosion();
  418. explosionSound.play();
  419. explosionSound.rewind();
  420. //remove the enemy
  421. boss.remove(i);
  422. //increase kill count
  423. killCount++;
  424. healthTempKills++;
  425. earthHealth+=15;
  426. break;
  427. }
  428. }
  429. }
  430. }
  431. for(int i=0;i<asteroids.size();i++){
  432. for(int j=0;j<bullets.size();j++){
  433. //if i-th enemey is colliding with j-th bullet
  434. if(asteroids.get(i).collidingWith(bullets.get(j))){
  435. //remove bullet
  436. bullets.remove(j);
  437. if(asteroids.get(i).asteroidHealth<=0){
  438. //explosion animation
  439. explosions.add(new Explosion(asteroids.get(i).getX(),asteroids.get(i).getY()));
  440. moveExplosion();
  441. explosionSound.play();
  442. explosionSound.rewind();
  443. //remove the enemy
  444. asteroids.remove(i);
  445. //spawn new enemy
  446. Asteroids a = new Asteroids((int) random(width),(int) random(100) - 100,asteroidHealth);
  447. asteroids.add(a);
  448. break;
  449. }
  450. }
  451. }
  452. }
  453. if(healthTempKills>100){
  454. shipHealth++;
  455. healthTempKills =0;
  456. }
  457. //println(tempKills);
  458. //println(healthTempKills);
  459. }
  460. void mouseMoved(){
  461. shipX = mouseX;
  462.  
  463. }
  464.  
  465. void keyPressed(){
  466.  
  467. // If the return key is pressed, save the String and clear it
  468. // Otherwise, concatenate the String
  469. // Each character typed by the user is added to the end of the String variable.
  470. typing = typing + key;
  471. if(key == ENTER){
  472.  
  473. //save typing and score to file
  474. String nameScore = (typing + ": " + killCount);
  475. scores = createWriter("scores.txt");
  476. scores.append(nameScore + "\n");
  477.  
  478. scores.flush(); // Writes the remaining data to the file
  479. scores.close(); // Finishes the file
  480.  
  481.  
  482. earthHealth = 100;
  483. gameRunning = true;
  484. tempKills = 0;
  485. healthTempKills =0;
  486. shipHealth = 10;
  487. killCount = 0;
  488.  
  489. }
  490. }
  491.  
  492. void keyReleased(){
  493.  
  494. if(key == ' ' && gameRunning == true){
  495. //create Bullet object
  496. Bullet b = new Bullet(shipX,shipY);
  497. bullets.add(b);
  498. shootingSound.play();
  499. shootingSound.rewind();
  500. }
  501. }
  502.  
  503. void gameOver(){
  504. background(0);
  505. PFont font;
  506. font = createFont("spaceFont.ttf",32);
  507. textFont(font, 100);
  508.  
  509. //You Lose
  510. fill(255,0,0);
  511. text("A LOSER IS YOU",width/11,height/4);
  512.  
  513. //Score / Name
  514. fill(255);
  515. text("Score: "+killCount,width/13,height/2);
  516. text("Name: "+typing,width/8,height/2+100);
  517.  
  518. //Restart
  519. fill(190,160,0);
  520. text("Restart: Enter",width/18,height/2+250);
  521. }
  522.  
  523. void BGM(){
  524. if(gameRunning == true){
  525. BGM.loop();
  526. }
  527. else if(gameRunning == false){
  528. loseBGM.loop();
  529. }
  530. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement