Hi
Firstly, I’m french, so my english is not so good and I have trouble using the forum
I programmed a kind of Space Invaders, it’s a project for the school. I must have finished him for monday but I have a problem : we must shoot to update the number of lives
concerned party :
void game(){
for(Bullet Bullet : bullets){
Bullet.display();
Bullet.move();
}
for(Enemy Enemy : enemies){
Enemy.display();
Enemy.move();
}
if(millis()-iTime > random(1000, 2000)){
enemies.add(new Enemy(random(4, 1030), -16, random(25, 50), random(1, 6)));
iTime = millis();
}
for(int i=0; i < bullets.size(); i++){
for(int j=0; j < enemies.size(); j++){
float distance = dist(bullets.get(i).x, bullets.get(i).y, enemies.get(j).x+enemies.get(j).w/2, enemies.get(j).y+enemies.get(j).w/2);
if(distance < enemies.get(j).w/2+9){
bullets.remove(i);
enemies.remove(j);
score += 1;
break;
}
else if(bullets.get(i).y+5 < 50){
bullets.remove(i);
break;
}
else if(enemies.get(j).y+enemies.get(j).w > 688){
enemies.remove(j);
lives -= 1;
break;
}
}
}
program :
int clr;
int iTime;
int lives = 3;
int score = 0;
int xplayer=500;
ArrayList<Bullet> bullets = new ArrayList<Bullet>();
ArrayList<Enemy> enemies = new ArrayList<Enemy>();
boolean keyAPressed=false, keyZPressed=false, keySPressed=false;
void setup(){
size(1080, 680);
frameRate(60);
iTime = millis();
}
void draw(){
smooth();
noStroke();
colorMode(HSB, 75);
clr++;
if(clr>75) clr=0;
clean();
if(keySPressed){
if(lives > 0){
game();
player();
}
else{
gameover();
for(int i=enemies.size()-1; i >= 0; i--){
enemies.remove(i);
}
/* for(int i=bullets.size()-1; i >= 0; i--){
bullets.remove(i);
}*/
if(keyPressed && key==ENTER){
game();
lives = 3;
score = 0;
}
}
}
else{
startgame();
}
}
void clean(){
background(0);
}
void startgame(){
fill(0, 0, 100);
textSize(30);
text("Press 'ENTER' to start game", 330, 350);
}
void player(){
fill(0, 0, 100);
rect(xplayer, 660, 80, 20);
rect(xplayer+20, 650, 40, 10);
if(-20 < xplayer) if(keyAPressed) xplayer -= 10;
if(xplayer < 1020) if(keyZPressed) xplayer += 10;
}
void game(){
for(Bullet Bullet : bullets){
Bullet.display();
Bullet.move();
}
for(Enemy Enemy : enemies){
Enemy.display();
Enemy.move();
}
if(millis()-iTime > random(1000, 2000)){
enemies.add(new Enemy(random(4, 1030), -16, random(25, 50), random(1, 6)));
iTime = millis();
}
for(int i=0; i < bullets.size(); i++){
for(int j=0; j < enemies.size(); j++){
float distance = dist(bullets.get(i).x, bullets.get(i).y, enemies.get(j).x+enemies.get(j).w/2, enemies.get(j).y+enemies.get(j).w/2);
if(distance < enemies.get(j).w/2+9){
bullets.remove(i);
enemies.remove(j);
score += 1;
break;
}
else if(bullets.get(i).y+5 < 50){
bullets.remove(i);
break;
}
else if(enemies.get(j).y+enemies.get(j).w > 688){
enemies.remove(j);
lives -= 1;
break;
}
}
}
textSize(20);
fill(0, 0, 100);
text("Lives : "+lives, 5, 670);
text("Score : "+score, 955, 670);
}
void gameover(){
fill(0, 0, 100);
textSize(150);
text("GAME OVER", 100, 300);
textSize(30);
text("Press 'ENTER' to restart game", 330, 450);
text("Your score : "+score, 430, 380);
xplayer=500;
}
void keyPressed(){
if(key=='a') keyAPressed=true;
if(key=='z') keyZPressed=true;
if(key==' ') bullets.add(new Bullet(xplayer+40, 650));
if(key==ENTER) keySPressed=true;
}
void keyReleased(){
if(key=='a') keyAPressed = false;
if(key=='z') keyZPressed = false;
}
class Bullet{
int x, y;
Bullet(int x, int y){
this.x=x;
this.y=y;
}
void display(){
fill(clr, 100, 100);
ellipse(x, y, 10, 10);
}
void move(){
y -= 15;
}
}
class Enemy{
float x, y, w, speed;
Enemy(float x, int y, float w, float speed){
this.x = x;
this.y = y;
this.w = w;
this.speed = speed;
}
void display(){
fill(clr, 100, 100);
rect(x, y, w, w);
}
void move(){
y += speed;
}
}
thanks