How to setup scoring on a shooting game. URGENT
in
Programming Questions
•
4 months ago
Hey, I am making a shooting game and I want to score a point each time I shoot an enemy. It's kind of urgent, Can anybody help me please?
Here's the code:
//import gif
import gifAnimation.*;
Gif myAnimation;
int recX = width/2;
int recY = height/2;
int skip=2;
int j=0;
int playerHealth=100;
int playerWidth = 55;
int playerHeight = 65;
int playerLives=5;
float posX;
boolean playerAlive=true;
boolean createShips=false;
float time=60;
int level=1;
int wallGap = 50;
Ship[] ships;
Laser[] lasers;
PFont font; //import font
PImage space;//import background
PImage enemy;//import enemy
PImage player;//import spaceship
PImage explosion;//import explosion gif
void setup()
{
size(800, 600);//background
noStroke();//images without stroke
font = loadFont("AddElectricCity-48.vlw");//load font
//load images
player=loadImage("space.png");
enemy=loadImage("enemy.png");
space = loadImage("space.jpg");
Gif Animation = new Gif(this, "explosion1.gif");
myAnimation=Animation;
myAnimation.noLoop();
createEnemies(1);//refresh ships
lasers=new Laser[5000];//refresh lasers
}
void draw()
{
image(space,0,0,width,height);
textFont(font, 18);
fill(255);
text("Level "+level+" ", 10, 30);
text("Lives:"+playerLives, width-120, 30);
if(playerLives<=0)
{
textFont(font, 60);
fill(255,0,0);
text("GAME OVER", width/2-200, height/2);
}
if(level==5)
{
textFont(font, 30);
fill(0, 150, 0);
text("Congratulations !!", 200, height/2);
text("You WIN !", width/2-100, height/2+50);
}
if(createShips) { createEnemies(level);createShips=false;}
if(playerAlive) Player();
else explode();
for (int i=0; i<ships.length; i+=skip)//refresh ships
{
ships[i].refresh();
}
if(playerLives>0&&level<=4)// refresh lasers
for(int i=0;i<j;i++)
lasers[i].refresh();
if(level<=4)
{
createShips=true;
for (int i=0; i<ships.length; i+=skip)
{
if(ships[i].isAlive==true) createShips=false;
}
if(createShips) {level++;
j=0;
}
}
}
void createEnemies(int level)
{
int numberOfShips=0;
int ShipsPerRow=0;
int ShipWidth=0;
int ShipHeight=0;
int yShips=0;
int Min=0;
int Max=0;
switch(level)
{
case 1: numberOfShips = 20;
ShipsPerRow = 20;
ShipWidth = width/ShipsPerRow;
ShipHeight = 40;
yShips = 50;
Min=50;
Max=200;
case 2: numberOfShips = 20;
ShipsPerRow = 20;
ShipWidth = width/ShipsPerRow;
ShipHeight = 40;
yShips = 50;
//quantity of lasers
Min=100;
Max=150;
break;//to switch the ships
case 3: numberOfShips = 40;
ShipsPerRow = 20;
ShipWidth = width/ShipsPerRow;
ShipHeight = 40;
yShips = 50;
//quantity of lasers
Min=40;
Max=150;
break;//to switch the ships
case 4: numberOfShips = 60;
ShipsPerRow = 20;
ShipWidth = width/ShipsPerRow;
ShipHeight = 40;
yShips = 50;
//quantity of lasers
Min=60;
Max=150;
break;//to switch the ships
}
ships = new Ship[numberOfShips];
for (int i=0; i<numberOfShips; i++)
{
int rowNum = i/ShipsPerRow;
// coords
int x = ShipWidth*i;
x -= rowNum*ShipsPerRow*ShipWidth;
int y = yShips+i/ShipsPerRow*ShipHeight;
// create Ship
ships[i] = new Ship(x, y, ShipWidth, ShipHeight ,Min,Max);
}
}
void Player()
{
float playerX = constrain(mouseX, playerWidth/3, width-(playerWidth/2));
posX=playerX;
image(player,playerX-(playerWidth/2),height-wallGap,playerWidth,playerHeight);
}
void explode()
{
time-=1;
if(time>0) {myAnimation.play();
image(myAnimation, posX-(playerWidth/2),height-wallGap,playerWidth,playerHeight);
}
else {myAnimation.stop();
if(playerLives>0) {playerAlive=true;time=60;}
}
}
void mouseReleased()
{
if(playerAlive)
{
color laserColor=#74c044;// green color
lasers[j]= new Laser(posX,height-wallGap-5,-1,laserColor);
j++;
}
}
//edit enemies spaceships
public class Enemy
{
int width;
int height;
int x1;
int y1;
Enemy(int W, int H)
{
width = W;
height = H;
}
//position
void setPosition(int X, int Y)
{
x1 = X;
y1 = Y;
}
//load enemies image
void drawEnemy(){
image(enemy, x1, y1, width, height);
}
}
//edit lasers
public class Laser
{
float x;
float LaserY;
int dir;
boolean Alive;
int LaserSize=5;
color Color;
Laser(float X,float Y, int Dir,color laserColor)
{
x=X;
LaserY=Y;
dir=Dir;
Alive=true;
Color=laserColor;
}
void refresh()
{
if(Alive)
{ LaserY+=dir*5;
if(LaserY>height) Alive=false;
fill(Color);
rect(x, LaserY, LaserSize, LaserSize+2);
if(Color==#74c044)//enemies are shoot by the player
for (int i=0; i<ships.length; i+=skip)//laser is touching enemies
{
if(ships[i].isAlive)
{
int py=ships[i].y;
int py2=ships[i].y+ships[i].h;
if(LaserY+LaserSize >= py && LaserY+LaserSize<=py2 && ( x+LaserSize >= ships[i].x && x+LaserSize <= ships[i].x+ships[i].w))
{
ships[i].die();
Alive=false;
break;
}
}
}
else // the laser is shoot by enemies
{
float playerY,playerY2,playerX,playerX2;
playerY=height-wallGap-7;
playerY2=height-wallGap;
playerX=posX-(playerWidth/2);
playerX2=posX-(playerWidth/2)+playerWidth;
if(LaserY+LaserSize>=playerY && LaserY+LaserSize<=playerY2 && (x+LaserSize >=playerX && x+LaserSize <=playerX2))
{
playerAlive=false;
playerLives--;
Alive=false;
}
}
}
}
}
//edit player spaceship
public class Ship
{
Enemy enemy;//new enemy position
boolean isAlive;//player alive
boolean explode;//player explode
int x;
int y;
int w;
int h;
int timer=20;
float time_to_attack;
int MIN,MAX;
int dir;
Ship(int X, int Y, int W, int H,int min,int max)
{
enemy = new Enemy(W, H);
enemy.setPosition(X, Y);
isAlive = true;
explode=false;
MIN=min;
MAX=max;
time_to_attack=random(0,MAX);
x=X;
y=Y;
w=W;
h=H;
if(time_to_attack<75) dir=1;
else dir=-1;
}
void refresh()
{
if (isAlive)
{
if(level==2||level==3||level==4)
{
x+=dir*2;
if((x>width-w)||(x<0)) dir*=-1;
enemy.setPosition(x,y);
}
enemy.drawEnemy();
time_to_attack-=1;
if(time_to_attack<0)
{ color red=#ff0000;
lasers[j]= new Laser(x+(w/2),y+h,1,red);
j++;
time_to_attack=random(MIN,MAX);
}
}
else if(explode)
{
timer-=1;
if(timer>0) {myAnimation.play();
image(myAnimation, x-(w/2),y-(h/2),w*2,h*2);
}
else {myAnimation.stop();explode=false;}
}
}
void die()
{
explode=true;
isAlive = false;
}
}
1