Final Project:
- Steven Gonzalez
- Dec 17, 2019
- 5 min read
for the final project I decided to make a alien invading game I ran into a lot of problems with the arrays and then I decided to add a boss fight if you defeat all the previous aliens but for some reason the shots that the boss shoots doesn't work when it hits the spaceShip we'll its a tank but my variables its called spaceShip otherwise everything works fine. Sometimes in the alien moving they go really close to each other I also made it so that if a certain amount of aliens are killed the others shoot faster same with the boss and his health. Another thing I noticed which I don't know how to fix is the game itself is really slow when most of the aliens are on screen but I thinks thats due to how I drew them.
var spaceShipX;
var y;
var aliensX = [];
var aliensY = [];
var shotsX = [];
var shotsY = [];
var numA;
var stage;
var ySpeed;
var score;
var shotsAX = [];
var shotsAY = [];
var count;
var count2;
//for alien shot
var aYSpeed;
//for alien ship
var aXSSpeed;
var aXSSpeed2;
var shipSpeed;
var lives;
var idx;
var index;
var timer;
var bossShotX=[];
var bossShotY=[];
var bossShotXSpeed=[];
var bossShotYSpeed=[];
var bossX;
var bossY;
var bossLife;
var bossShotSpeed;
var count3;
function setup() {
createCanvas(1000, 900);
ySpeed = 15;
y = 200;
spaceShipX = 500;
//number of aliens in a row, number of alien columns
numA = 5;
score = 0;
count = 0;
lives = 3;
count2 = 60;
idx = 25
index = 0;
for (var i = 0; i < numA; i++) {
for (var j = 0; j < numA; j++) {
aliensX.push(i * 100 + 200);
aliensY.push(j * 50 + 20);
}
}
aYSpeed = 5;
aXSSpeed2 = -2;
aXSSpeed = 2;
stage=1;
shipSpeed = 10;
timer=10;
bossX=400;
bossY=200;
bossShotSpeed=40;
count3=0
bossLife=15;
}
function draw() {
if (lives != 0&&stage==1) {
background('skyblue');
ShipMove();
//update positions
checkShipShot();
checkAlienShot();
alienMove();
//removes shot if past 0
for (var i = 0; i < shotsX.length; i++) {
if (shotsX[i] < 0) {
shotsX.splice(i, 1);
shotsY.splice(i, 1);
}
}
//add a new alien shot every minute, 60 seconds
if (count % count2 == 0) {
shotsAX.push(aliensX[floor(random(0, idx))]);
shotsAY.push(aliensY[floor(random(0, idx))]);
}
//update alien shots
//draw elements
drawShip();
drawShipShot();
drawAlien();
//score
fill(200, 0, 0);
textSize(20);
text("score: " + score, 50, 50);
text("lives: " + lives, 50, 100);
text("To Shoot: Spacebar", 700, 50);
text("To move: Left and right keys", 700, 100);
drawAlienShot();
count++;
if (score == 3) {
count2 = 55;
}
if (score == 6) {
count2 = 50;
}
if (score == 9) {
count2 = 45;
}
if (score == 12) {
count2 = 40;
}
if (score == 15) {
count2 = 35;
}
if (score == 18) {
count2 = 30;
}
if (score == 21) {
count2 = 25;
}
if (score == 24) {
count2 = 20;
}
if (score == 25) {
text("Boss Fight", 300, 300);
timer--;
text("IN",310,320);
text(timer,315,335);
}
if(timer==0){
stage++;
}
if (lives == 0) {
clear();
textSize(100);
text("YOU LOSE", 300, 300)
}
}
if(stage==2){
clear();
background('skyblue')
drawShip();
drawBoss();
drawShipShot();
textSize(20);
text("lives: " + lives, 50, 100);
text("To Shoot: Spacebar", 700, 50);
text("To move: Left and right keys", 700, 100);
text("BossLife:" +bossLife,50,150);
for (var i = 0; i < shotsX.length; i++) {
shotsY[i] -= ySpeed;
}
moveBoss();
ShipMove();
count3++;
if (count3 % bossShotSpeed == 0) {
bossShotX.push(bossX);
bossShotY.push(bossY+50);
bossShotXSpeed.push(random(-10,10));
bossShotYSpeed.push(floor(random(5,10)));
}
drawBossShot();
for(var i=0;i<bossShotX.length;i++){
if(bossShotY[i]>900&&bossShotY[i]<0){
bossShotY.splice(i,1);
bossShotX.splice(i,1);
bossShotXSpeed.splice(i,1);
bossShotYSpeed.splice(i,1);
}
if(bossShotX[i]>1000&&bossShotX<0){
bossShotX.splice(i,1);
bossShotX.splice(i,1);
bossShotXSpeed.splice(i,1);
bossShotYSpeed.splice(i,1);
}
}
checkShipShot2();
checkBossShot();
stroke(1);
if(bossLife==12){
bossShotSpeed=20;
}
if(bossLife==8){
bossShotSpeed=10
}
if(bossLife==5){
bossShotSpeed=7;
}
if(bossLife==2){
bossShotSpeed=2;
}
noStroke();
if(bossLife<=0){
stage=3;
}
}
if(stage==3){
clear();
background('red');
textSize(50);
text("Congratulations",200,200);
text("You defeated the Boss",200,300);
text("You Win",250,400);
}
}
function keyPressed() {
// if (keyCode == LEFT_ARROW) {
// spaceShipX -= shipSpeed;
// }
// if (keyCode == RIGHT_ARROW) {
// spaceShipX += shipSpeed;
// }
if (key === ' ') {
//console.log("adding lasers");
shotsX.push(spaceShipX + 26);
shotsY.push(y + 450);
}
}
function ShipMove() {
if (keyIsDown(LEFT_ARROW)) {
spaceShipX -= shipSpeed;
}
if (keyIsDown(RIGHT_ARROW)) {
spaceShipX += shipSpeed;
}
}
function checkShipShot() {
for (var i = 0; i < shotsX.length; i++) {
shotsY[i] -= ySpeed;
}
//loop through each alien location
for (var i = 0; i < aliensY.length; i++) {
//loop through each shot location
for (var j = 0; j < shotsY.length; j++) {
//if the shot is inside the alien
if ((shotsY[j] < (aliensY[i] + 30)) && (shotsY[j] > (aliensY[i])) &&
(shotsX[j] > (aliensX[i] - 25)) && (shotsX[j] < (aliensX[i] + 25))) {
aliensY.splice(i, 1);
aliensX.splice(i, 1);
shotsY.splice(j, 1);
shotsX.splice(j, 1);
score++;
idx--;
}
}
}
}
function checkAlienShot() {
for (var i = 0; i < shotsAX.length; i++) {
shotsAY[i] += aYSpeed;
}
for (var j = 0; j < shotsAX.length; j++) {
if (((shotsAY[j] < (710)) && (shotsAY[j] > (680))) &&
((shotsAX[j] < (spaceShipX + 50)) && (shotsAX[j] > (spaceShipX - 25)))) {
shotsAY.splice(j, 1);
shotsAX.splice(j, 1);
lives--;
}
}
}
function alienMove() {
for (var i = 0; i < 5; i++) {
for (var j = 0; j < 5; j++) {
if(i==0){
aliensX[i, j * 5] += aXSSpeed;
if (aliensX[i, j * 5] > 1000-20 || aliensX[i, j * 5] < 0) {
aXSSpeed = aXSSpeed*-1;
}
}
if(i==2){
aliensX[i, j * 5+2] += aXSSpeed;
if (aliensX[i, j * 5+2] > 1000-20 || aliensX[i, j * 5+2] < 0) {
aXSSpeed = aXSSpeed*-1;
}
}
if(i==4){
aliensX[i, j * 5+4] += aXSSpeed;
if (aliensX[i, j * 5+4] > 1000-20 || aliensX[i, j * 5+4] < 0) {
aXSSpeed = aXSSpeed*-1;
}
}
if(i==1){
aliensX[i,j*5+1]+=aXSSpeed2;
if(aliensX[i,j*5+1]>1000-20||aliensX[i,j*5+1]<0+20){
aXSSpeed2=-aXSSpeed2;
}
}
if(i==3){
aliensX[i,j*5+3]+=aXSSpeed2;
if(aliensX[i,j*5+3]>1000-20||aliensX[i,j*5+3]<0+20){
aXSSpeed2=-aXSSpeed2;
}
}
}
}
}
function drawShip() {
fill('green');
noStroke();
rect(spaceShipX, 700, 50, 10)
stroke(1);
noFill();
quad(spaceShipX, 710, spaceShipX + 50, 710, spaceShipX + 40, 720, spaceShipX + 10, 720)
fill('lightgray')
ellipse(spaceShipX + 15, 714, 10, 10);
ellipse(spaceShipX + 25, 714, 10, 10);
ellipse(spaceShipX + 35, 714, 10, 10);
fill('green')
noStroke();
rect(spaceShipX + 10, 690, 30, 10);
rect(spaceShipX + 20, 670, 10, 20);
rect(spaceShipX + 17, 660, 15, 10)
noStroke();
fill('lightGreen');
rect(0,720,1000,200);
}
function drawShipShot() {
fill('blue');
for (var i = 0; i < shotsX.length; i++) {
rect(shotsX[i], shotsY[i], 3, 8);
}
}
function drawAlien() {
for (var a = 0; a < aliensY.length; a++) {
for (var i = 0; i < aliensX.length; i++) {
fill('steel');
ellipse(aliensX[a], aliensY[a], 50, 25);
fill('skyblue');
triangle(aliensX[a] - 25, aliensY[a], aliensX[a], aliensY[a] - 10, aliensX[a] - 30, aliensY[a] - 20);
triangle(aliensX[a] + 25, aliensY[a], aliensX[a], aliensY[a] - 10, aliensX[a] + 25, aliensY[a] - 20);
fill('lightblue')
rect(aliensX[a] - 10, aliensY[a] - 29, 20, 25, 20, 20, 0, 0);
fill('blue')
ellipse(aliensX[a] - 20, aliensY[a] + 2, 7, 7);
ellipse(aliensX[a] - 13, aliensY[a] + 7, 7, 7);
ellipse(aliensX[a] - 4, aliensY[a] + 9, 7, 7);
ellipse(aliensX[a] + 5, aliensY[a] + 9, 7, 7);
ellipse(aliensX[a] + 14, aliensY[a] + 7, 7, 7);
ellipse(aliensX[a] + 20, aliensY[a] + 2, 7, 7)
fill('green');
ellipse(aliensX[a], aliensY[a] - 18, 13, 13);
triangle(aliensX[a] - 5, aliensY[a] - 15, aliensX[a] + 6, aliensY[a] - 15, aliensX[a], aliensY[a] - 5);
fill('black')
ellipse(aliensX[a] - 2, aliensY[a] - 15, 3, 7);
ellipse(aliensX[a] + 2, aliensY[a] - 15, 3, 7);
}
}
}
function drawAlienShot() {
for (var i = 0; i < shotsAX.length; i++) {
fill('white')
rect(shotsAX[i], shotsAY[i] + 10, 5, 20, 40, 40, 40, 40);
fill('blue');
rect(shotsAX[i] + 1, shotsAY[i] + 12, 3, 16, 40, 40, 40, 40)
}
}
function drawBoss(){
fill('steel')
ellipse(bossX,bossY,300,150);
fill('skyblue')
triangle(bossX-150,bossY,bossX,bossY-50,bossX-150,bossY-150);
triangle(bossX+150,bossY,bossX,bossY-50,bossX+150,bossY+-150);
fill('lightBlue');
ellipse(bossX,bossY+73,100,5);
rect(bossX-50,bossY-165,100,135,100,100,0,0);
fill('green')
ellipse(bossX,bossY-100,65,65);
triangle(bossX-30,bossY-90,bossX+30,bossY-90,bossX,bossY-28);
fill('black');
ellipse(bossX-10,bossY-100,10,30);
ellipse(bossX+10,bossY-100,10,30);
fill('blue');
ellipse(bossX-130,bossY+10,30,30);
ellipse(bossX-100,bossY+35,30,30);
ellipse(bossX-65,bossY+50,30,30);
ellipse(bossX-30,bossY+56,30,30);
ellipse(bossX+5,bossY+56,30,30);
ellipse(bossX+40,bossY+55,30,30);
ellipse(bossX+75,bossY+47,30,30);
ellipse(bossX+105,bossY+35,30,30);
ellipse(bossX+130,bossY+10,30,30);
}
function moveBoss(){
bossX+=aXSSpeed;
if(bossX>1000-150||bossX<0+150){
aXSSpeed=-aXSSpeed;
}
}
function drawBossShot(){
for(var i=0;i<bossShotX.length;i++){
ellipse(bossShotX[i],bossShotY[i],10,10);
}
}
function checkShipShot2(){
for(var i=0;i<shotsX.length;i++){
if((shotsY[i]<bossY+100&&shotsY[i]>bossY-100)&&(shotsX[i]>bossX-150&&shotsX[i]<bossX+150)){
shotsX.splice(i,1);
shotsY.splice(i,1);
bossLife--;
}
}
}
function checkBossShot(){
for(var i=0;i<bossShotX.length;i++){
bossShotX[i]+=bossShotXSpeed[i];
bossShotY[i]+=bossShotYSpeed[i];
}
for(var i=0;i<bossShotX.length;i++){
if(((bossShotY[i]<710)&&(bossShotY[i]>680))&&((bossShotX[i]<spaceShipX+50)&&(bossShotX>spaceShipX-25))){
bossShotX.splice(i,1);
bossShotY.splice(i,1);
bossShotXSpeed.splice(i,1);
bossShotYSpeed.splice(i,1);
lives--;
}
}
}
Comentarios