Hi, I made a simple game, yet I couldn’t add a scoring mechanism and an opening scene. When the spaceship hits one of the balls, it loses score, when it explodes the balls using mouse function, it gains scores. It would be so appreciated if you would help!
Here’s the code:
Ball b;
Ball [] balls;
int start;
float x = 50;
float y = 325;
float spaceShipX = 0;
float xspeed = 3;
float yspeed = 3;
PImage img;
float [] xstars = new float [100] ;
float []ystars = new float [100];
float[] speedS = new float [100] ;
import processing.sound.*;
SoundFile file;
void setup() {
size(700, 700);
noStroke();
fill(255, 100, 100);
img = loadImage("spaceShip.png");
start = millis();
b=new Ball();
balls = new Ball[20];
for (int i = 0; i < balls.length; i++) {
balls[i] = new Ball();
}
stroke(255);
strokeWeight(2);
int i = 0;
while (i <100) {
xstars [i] = random(0, width);
ystars [i]= random(0, height);
speedS [i]= random(1, 5);
i = i + 1;
}
file = new SoundFile(this, "game-intro-space.wav");
file.play();
}
void draw() {
background(0);
int timer = millis()-start;
textSize(25);
text("Time:", 25, 35);
text(timer, 100, 35);
text("Score:", 425, 35);
text(timer, 100, 35);
if (timer > 20000) {
fill(255);
stroke(255);
println("GAME OVER ");
textMode(CENTER);
strokeWeight(3);
textSize(25);
text("TIME IS UP!", 300, height/2);
text("GAME OVER!", 285, height/2+200);
noFill();
stroke(#466F45);
rectMode(CENTER);
rect(width/2, 450, 300, 75);
fill(255);
stroke(255);
textMode(CENTER);
text("PLAY AGAIN", 280, 455);
}
b.update();
b.display();
for (int i = 0; i <balls.length; i++) {
balls[i].update();
balls[i].display();
}
PImage spaceShip = loadImage("spaceShip.png");
spaceShip.resize(0, 50);
image(spaceShip, x, y);
int i = 0;
while (i < 100) {
point(xstars[i], ystars[i]);
xstars[i] = xstars[i] - speedS[i];
if (xstars[i] < 0) {
xstars[i] = width;
}
i = i + 1;
}
//SPACESHIP MOVES//
if (x >width) {
x = 0;
}
if (x <0) {
x = width;
}
if (y >height) {
y = 0;
}
if (y <0) {
y = height;
}
}
//SPACESHIP MOVES//
//SPACESHIP MOVES OPTION 2//
/* if (x>650) {
x=x-25;
xspeed=xspeed*-1;
}
if (x<0) {
x=x+25;
xspeed=xspeed*-1;
}
if (y>650) {
y=y-25;
yspeed=yspeed*-1;
}
if (y<0) {
y=y+25;
yspeed=yspeed*-1;
} */
//SPACESHIP MOVES OPTION 2//
// LEFT HAND CONTROL//
void keyPressed() {
if (key == 'a') {
x -=20;
}
if (key == 'd') {
x +=20;
}
if (key == 'w') {
y -=20;
}
if (key == 's') {
y +=20;
}
// RIGHT HAND CONTROL//
if (key == CODED) {
if (keyCode == UP) {
y -=20;
} else if (keyCode == DOWN) {
y +=20;
} else if (keyCode == LEFT) {
x -=20;
} else if (keyCode == RIGHT) {
x +=20;
}
}
}
void mousePressed () {
int i = 0;
while (i < 500) {
ellipse(x+100+i, y+25, 5, 5);
i = i + 50;
}
}
class Ball {
float a, b, w, h;
float speedX, speedY;
color c;
Ball() {
a = random(250, width-100);
b = random(250, height-100);
w = random(20, 60);
h = w;
c = color(random(255), random(255), random(255), random(100));
speedX = random(-5, 5);
speedY = random(-5, 5);
}
void update() {
checkBounds();
a+=speedX;
b+=speedY;
}
void display() {
fill(c);
ellipse(a, b, w, h);
}
void checkBounds() {
if (a<0+w/2 || a>width-w/2) {
speedX*=-1;
}
if (b<0+h/2 || y>height-h/2) {
speedY*=-1;
}
}
}