Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //PENDING//
- //Add asteroids*
- //2-5hp*
- //random rotation of image - pd3???
- //gameOver splash screen (playing loseBGM instead of BGM)
- //keyPressed('ENTER'); = restart game*
- //loseBGM plays instead of BGM
- //game over splash screen when shipHealth = 0 or earth health = 0*
- //display score*
- //input name and score into a text file
- //Start Screen:
- //display previous scores with names from text file
- //boss shooting back
- //if(bullet colliding with ship || asteriod colliding with ship)
- //shipHealth = shipHealth-1;
- //COMPLETE//
- //-(easy) change alien img *
- //-(hard) when alien hits bottom, add more aliens in top *
- //-(easy) boss at 50 kills *
- //-(medium) health for ship and aliens *
- //-(easy) background music *
- //add kill counter*
- //increase life every 100 kills*
- //if healthTempKills == 100, then add life, then set back to 0*
- //spawn boss every 50 kills, temp spawn boss set back to 0*
- //display kills*
- //explosion on kill*
- //explosion when boss dies*
- //explosion when asteroid dies*
- //earth health*
- //decreases when enemies y>height && x<width && x>0*
- //Parallax scrolling background*
- import ddf.minim.*;
- Minim minim;
- AudioPlayer shootingSound,BGM,loseBGM,explosionSound;
- boolean gameRunning=true;
- String typing = "";
- String saved = "";
- int shipHealth;
- int earthHealth;
- int asteroidHealth = (int)random(2,5);
- int enemyHealth = 3;
- int bossHealth = 20;
- int shipX,shipY;
- int shipWidth,shipHeight;
- int killCount;
- int tempKills;
- int bossSpawn=10;
- int healthTempKills;
- PImage starW_LG;
- PImage starW_SM;
- PImage starR;
- PImage starO;
- PImage starY;
- PImage starB;
- PImage bg;
- PImage shipImg;
- PImage enemyImg;
- PImage bossImg;
- PImage asteroidImg;
- ArrayList<Bullet> bullets;
- ArrayList<bossBullet> bossBullets;
- ArrayList<Enemies> enemies;
- ArrayList<boss> boss;
- ArrayList <Asteroids> asteroids;
- ArrayList<Explosion> explosions;
- ArrayList<Starfield> starfield;
- ArrayList<Starfield2> starfield2;
- PImage[] images;
- int imageCount = 70;
- PrintWriter scores;
- void setup(){
- size(800,800);
- shipHealth = 10;
- earthHealth = 100;
- killCount = 0;
- tempKills = 0;
- healthTempKills =0;
- shipX = width/2;
- shipWidth = 60;
- shipHeight = 60;
- shipY = height - shipHeight - 20;
- bullets = new ArrayList <Bullet> ();
- bossBullets = new ArrayList <bossBullet> ();
- enemies = new ArrayList <Enemies> ();
- boss = new ArrayList <boss> ();
- asteroids = new ArrayList <Asteroids> ();
- explosions = new ArrayList <Explosion> ();
- starfield = new ArrayList <Starfield> ();
- starfield2 = new ArrayList <Starfield2> ();
- images = new PImage[imageCount];
- for (int i = 1; i <= imageCount; i++) {
- // Use nf() to number format 'i' into four digits
- String filename = "frame" + nf(i, 2) + ".png";
- images[i-1] = loadImage(filename);
- }
- //generate enemie
- for(int i=0;i<random(2,5);i++){
- Enemies e = new Enemies((int) random(width),(int) random(-height/2),enemyHealth);
- enemies.add(e);
- }
- //generate asteroids
- for(int i=0;i<2;i++){
- Asteroids a = new Asteroids((int) random(width),(int) random(-height/2),asteroidHealth);
- asteroids.add(a);
- }
- //generate starfield 2
- for(int i=0;i<200;i++){
- Starfield2 s2 = new Starfield2((int) random(width),(int) random(height));
- starfield2.add(s2);
- }
- //generate starfield
- for(int i=0;i<100;i++){
- Starfield s = new Starfield((int) random(width),(int) random(height));
- starfield.add(s);
- }
- minim = new Minim(this);
- // this loads mysong.wav from the data folder
- shootingSound = minim.loadFile("shootingSound.mp3");
- BGM= minim.loadFile("BGM.mp3");
- loseBGM=minim.loadFile("loseBGM.mp3");
- explosionSound = minim.loadFile("explosion.mp3");
- //Background Image
- bg = loadImage("backgroundImage.png");
- bg.resize(width,height);
- //Stars Images
- starW_LG = loadImage("whiteStarLG.png");
- starW_SM = loadImage("whiteStarSM.png");
- starR = loadImage("redStar.png");
- starO = loadImage("orangeStar.png");
- starY = loadImage("yellowStar.png");
- starB = loadImage("blueStar.png");
- //Character Images
- shipImg = loadImage("ship.png");
- shipImg.resize(shipWidth,shipHeight);
- enemyImg = loadImage("alien.png");
- enemyImg.resize(50,50);
- bossImg = loadImage("boss.png");
- bossImg.resize(132,132);
- asteroidImg = loadImage("asteroid.png");
- asteroidImg.resize(50,50);
- // Create a new file in the sketch directory
- BGM.loop();
- }
- void draw(){
- background(0);
- if(earthHealth>100){
- earthHealth = 100;
- }
- if(earthHealth<=0 || shipHealth<= 0){
- gameRunning=false;
- gameOver();
- }
- else{
- gameRunning = true;
- drawStars();
- moveStars();
- drawStars2();
- moveStars2();
- drawShip();
- drawBullets();
- moveBullets();
- drawEnemies();
- moveEnemies();
- drawAsteriod();
- moveAsteroids();
- drawBoss();
- moveBoss();
- drawExplosions();
- moveExplosion();
- drawScoreHealth();
- checkForCollisions();
- }
- //println(frameRate);
- }
- void drawScoreHealth(){
- //Box
- fill(0);
- rect(0,0,width,75);
- //Font
- PFont font;
- font = createFont("spaceFont.ttf",32);
- textFont(font, 32);
- //Score and Health
- fill(255,10,0);
- text("Invaders Killed: "+killCount,10,50);
- fill(255,255,10);
- text("Ship Health: "+shipHealth, 500,30);
- text("Earth Health: "+earthHealth,500,60);
- }
- void drawStars(){
- for(int i=0;i<starfield.size();i++){
- starfield.get(i).display();
- }
- }
- void drawStars2(){
- for(int i=0;i<starfield2.size();i++){
- starfield2.get(i).display();
- }
- }
- void drawShip(){
- fill(0,100,255);
- image(shipImg,shipX-shipWidth/2,shipY);
- }
- void drawBullets(){
- for(int i=0;i<bullets.size();i++){
- bullets.get(i).display();
- }
- }
- void drawEnemies(){
- for(int i=0; i <enemies.size();i++){
- enemies.get(i).display();
- }
- }
- void drawBossBullet(){
- for(int i=0;i<bossBullets.size();i++){
- bossBullets.get(i).display();
- //create Bullet object
- bossBullet b = new bossBullet(boss.get(i).getX(),boss.get(i).getY());
- bossBullets.add(b);
- }
- }
- void drawBoss(){
- for(int i=0;i<boss.size();i++){
- boss.get(i).display();
- }
- }
- void drawExplosions() {
- for(int i=0;i<explosions.size();i++){
- if(explosions.get(i).display()) {
- explosions.remove(i);
- }
- }
- }
- void drawAsteriod(){
- for(int i=0;i<asteroids.size();i++){
- //imageMode(CENTER);
- asteroids.get(i).display();
- }
- }
- void moveStars(){
- for(int i=0;i<starfield.size();i++){
- boolean onScreen = starfield.get(i).move();
- if(onScreen == false){
- starfield.remove(i);
- Starfield s = new Starfield((int) random(width),(int) random(100) - 100);
- starfield.add(s);
- }
- }
- }
- void moveStars2(){
- for(int i=0;i<starfield2.size();i++){
- boolean onScreen = starfield2.get(i).move();
- if(onScreen == false){
- starfield2.remove(i);
- Starfield2 s2 = new Starfield2((int) random(width),(int) random(100) - 100);
- starfield2.add(s2);
- }
- }
- }
- void moveBullets(){
- for(int i=0;i<bullets.size();i++){
- boolean onScreen = bullets.get(i).move();
- if(onScreen == false){
- bullets.remove(i);
- }
- }
- }
- void moveEnemies(){
- for(int i=0;i<enemies.size();i++){
- boolean onScreen = enemies.get(i).move();
- if(onScreen == false){
- enemies.remove(i);
- Enemies e = new Enemies((int) random(width),(int) random(100) - 100,enemyHealth);
- enemies.add(e);
- }
- }
- }
- void moveBoss(){
- for(int i=0;i<boss.size();i++){
- boolean onScreen = boss.get(i).move();
- if(onScreen == false){
- boss.remove(i);
- }
- }
- }
- void moveBossBullets(){
- for(int i=0;i<bossBullets.size();i++){
- boolean onScreen = bossBullets.get(i).move();
- if(onScreen == false){
- bossBullets.remove(i);
- }
- }
- }
- void moveAsteroids(){
- for(int i=0;i<asteroids.size();i++){
- boolean onScreen = asteroids.get(i).move();
- if(onScreen == false){
- asteroids.remove(i);
- Asteroids a = new Asteroids((int) random(width),(int) random(100) - 100,asteroidHealth);
- asteroids.add(a);
- }
- }
- }
- void moveExplosion(){
- for(int i=0;i<explosions.size();i++){
- boolean onScreen = explosions.get(i).move();
- if(onScreen == true){
- }
- }
- }
- void checkForCollisions(){
- for(int i=0;i<enemies.size();i++){
- for(int j=0;j<bullets.size();j++){
- //if i-th enemey is colliding with j-th bullet, remove the enemy, remove
- //the bullet, adjust score
- if(enemies.get(i).collidingWith(bullets.get(j))){
- //remove bullet
- bullets.remove(j);
- if(enemies.get(i).enemyHealth<=0){
- //explosion animation
- explosions.add(new Explosion(enemies.get(i).getX(),enemies.get(i).getY()));
- moveExplosion();
- explosionSound.play();
- explosionSound.rewind();
- //remove the enemy
- enemies.remove(i);
- //spawn new enemy
- Enemies e = new Enemies((int) random(width),(int) random(100) - 100,enemyHealth);
- enemies.add(e);
- //increase kill count
- killCount++;
- //increase kills for extra life
- healthTempKills++;
- /*if(healthTempKills>100){
- shipHealth++;
- healthTempKills =0;
- }*/
- //increase kills for boss spawn
- tempKills++;
- //spawn boss after x kills
- if(tempKills>=bossSpawn){
- boss b = new boss((int) random(width),(int) random(-height/2),bossHealth);
- boss.add(b);
- tempKills = 0;
- }
- break;
- }
- }
- }
- }
- for(int i=0;i<boss.size();i++){
- for(int j=0;j<bullets.size();j++){
- //if i-th enemey is colliding with j-th bullet
- if(boss.get(i).collidingWith(bullets.get(j))){
- //remove bullet
- bullets.remove(j);
- if(boss.get(i).bossHealth<=0){
- //explosion animation
- explosions.add(new Explosion(boss.get(i).getX(),boss.get(i).getY()));
- moveExplosion();
- explosionSound.play();
- explosionSound.rewind();
- //remove the enemy
- boss.remove(i);
- //increase kill count
- killCount++;
- healthTempKills++;
- earthHealth+=15;
- break;
- }
- }
- }
- }
- for(int i=0;i<asteroids.size();i++){
- for(int j=0;j<bullets.size();j++){
- //if i-th enemey is colliding with j-th bullet
- if(asteroids.get(i).collidingWith(bullets.get(j))){
- //remove bullet
- bullets.remove(j);
- if(asteroids.get(i).asteroidHealth<=0){
- //explosion animation
- explosions.add(new Explosion(asteroids.get(i).getX(),asteroids.get(i).getY()));
- moveExplosion();
- explosionSound.play();
- explosionSound.rewind();
- //remove the enemy
- asteroids.remove(i);
- //spawn new enemy
- Asteroids a = new Asteroids((int) random(width),(int) random(100) - 100,asteroidHealth);
- asteroids.add(a);
- break;
- }
- }
- }
- }
- if(healthTempKills>100){
- shipHealth++;
- healthTempKills =0;
- }
- //println(tempKills);
- //println(healthTempKills);
- }
- void mouseMoved(){
- shipX = mouseX;
- }
- void keyPressed(){
- // If the return key is pressed, save the String and clear it
- // Otherwise, concatenate the String
- // Each character typed by the user is added to the end of the String variable.
- typing = typing + key;
- if(key == ENTER){
- //save typing and score to file
- String nameScore = (typing + ": " + killCount);
- scores = createWriter("scores.txt");
- scores.append(nameScore + "\n");
- scores.flush(); // Writes the remaining data to the file
- scores.close(); // Finishes the file
- earthHealth = 100;
- gameRunning = true;
- tempKills = 0;
- healthTempKills =0;
- shipHealth = 10;
- killCount = 0;
- }
- }
- void keyReleased(){
- if(key == ' ' && gameRunning == true){
- //create Bullet object
- Bullet b = new Bullet(shipX,shipY);
- bullets.add(b);
- shootingSound.play();
- shootingSound.rewind();
- }
- }
- void gameOver(){
- background(0);
- PFont font;
- font = createFont("spaceFont.ttf",32);
- textFont(font, 100);
- //You Lose
- fill(255,0,0);
- text("A LOSER IS YOU",width/11,height/4);
- //Score / Name
- fill(255);
- text("Score: "+killCount,width/13,height/2);
- text("Name: "+typing,width/8,height/2+100);
- //Restart
- fill(190,160,0);
- text("Restart: Enter",width/18,height/2+250);
- }
- void BGM(){
- if(gameRunning == true){
- BGM.loop();
- }
- else if(gameRunning == false){
- loseBGM.loop();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement