Hi,
So I’m trying to create a game and I have an Arraylist of enemies. I want upon collision to reset it, in order if chosen by the player to play again from the “lose” screen to take him back and totally restart the game.
Any solutions for that?
This is a sample of my code:
String gameState;
float posX;
//Level 1 (100 secs mode)
ArrayList<Enemies> enemiesRowOne = new ArrayList<Enemies>();
//player Array
ArrayList<Player> player = new ArrayList<Player>();
Player p;
Timer t;
Enemies e;
public void settings() {
size(699, 700);
}
void setup() {
//Classes
e= new Enemies();
p= new Player();
t= new Timer(103);
gameState = "START";
//Player Array List
player= new ArrayList<Player>();
for (int i=0; i < 1; i++) {
player.add(new Player());
}
//Level 1 (100secs mode)
enemiesRowOne= new ArrayList<Enemies>();
for (int i=0; i < 30; i++) {
enemiesRowOne.add(new Enemies());
}
}
void draw() {
if (gameState== "START") {
startGame();
} else if(gameState == "CREDITS"){
creditsPage();
} else if (gameState == "INSTRUCTIONS"){
instructionsPage();
}else if (gameState == "CHOOSE") {
choosePlayer();
} else if (gameState == "PLAY1") {
choosePlayer1();
} else if (gameState == "PLAY2") {
choosePlayer2();
} else if (gameState == "PLAY3") {
choosePlayer3();
} else if (gameState == "WIN") {
winGame();
} else if (gameState == "LOSE") {
loseGame();
}
}
void startGame() {
background(#212121);
rect(150, 100, 350, 225);
noStroke();
fill(#20C4F4);
rect(210, 405, 225, 50);
fill(255);
text("START GAME", 227, 440);
noStroke();
fill(#20C4F4);
rect(210, 480, 225, 50);
fill(255);
text("INSTRUCTIONS", 223, 517);
noStroke();
fill(#20C4F4);
rect(210, 555, 225, 50);
fill(255);
text("CREDITS", 264, 590);
if (mousePressed == true) {
if (mouseX > 210 && mouseX< 210+225 && mouseY> 405 && mouseY< 405+50) {
gameState="CHOOSE";
}
}
}
void choosePlayer() {
background(#212121);
fill(255);
text("CHOOSE YOURMOTORCYCLE'S COLOUR", 75, 250);
stroke(255);
strokeWeight(3);
fill(0, 204, 255);
rect(100, 300, 100, 100);
stroke(255);
strokeWeight(3);
fill(255, 102, 204);
rect(300, 300, 100, 100);
stroke(255);
strokeWeight(3);
fill(102, 255, 51);
rect(500, 300, 100, 100);
if (mousePressed == true) {
if (mouseX > 100 && mouseX< 100+100 && mouseY> 300 && mouseY< 300+100) {
gameState = "PLAY1";
}
}
if (mousePressed == true) {
if (mouseX > 300 && mouseX< 300+100 && mouseY> 300 && mouseY< 300+100) {
gameState = "PLAY2";
}
}
if (mousePressed == true) {
if (mouseX > 500 && mouseX< 500+100 && mouseY> 300 && mouseY< 300+100) {
gameState = "PLAY3";
}
}
}
void choosePlayer1() {
if (t.getTime()< 0 ) {
gameState= "WIN";
}
background(#212121);
t.countDown();
fill(255);
text(t.getTime(), 615, 50);
//Level 1 for 100 seconds mode
if (t.getTime()< 102 &&t.getTime()> 80 ) {
for ( Enemies e : enemiesRowOne) {
e.displayRowOne();
e.move();
}
//for collision player & obstacles Lvl1
for ( Enemies e : enemiesRowOne) {
for ( Player p : player) if (e.crashesRowOne(p)) {
gameState = "LOSE";
}
}
}
for ( Player p : player) {
p.displayGreen();
}
}
class Timer{
float Time;
Timer(float set){
Time=set;
}
float getTime(){
return(Time);
}
void setTime(float set){
Time = set;
}
void countDown(){
Time-=1/frameRate;
}
}
class Enemies {
int eW;
int eH;
int eMW;
int eMH;
float speedLvl1;
float speedLvl2;
float speedLvl3;
float speedLvl4;
float speedLvl5;
float life=1;
//row1 Enemies
float rowOneX= 40;
float rowOneY= random(-15000, 0);
Enemies() {
eW=50;
eH=70;
speedLvl1=2.8;
speedLvl2=3.4;
speedLvl3=4;
speedLvl4=4.6;
speedLvl5=5.2;
eMW=30;
eMH=70;
}
void move() {
//enemies move for Lvl1 for 100 seconds mode
rowOneY=rowOneY+speedLvl1;
}
//Enemy objects display for 100secs
void displayRowOne() {
noStroke();
fill(255);
rect(rowOneX, rowOneY, eW, eH);
}
//booleans used for collision of player and obstacles
boolean crashesRowOne(Player p) {
if(50<rowOneX+eW && 50+p.playerW>rowOneX && p.playerY<rowOneY+eH && p.playerY+p.playerH>rowOneY){
return true;
}else{
return false;
}
}
}
class Player{
float playerX;
float playerY;
float playerW;
float playerH;
int lives=3;
Player(){
playerW=30;
playerH=70;
playerY=550;
}
void displayPink(){
fill(255,102,204);
rect(50, playerY, playerW,playerH);
}
void displayGreen(){
fill(102,255,51);
rect(50, playerY, playerW,playerH);
}
void displayBlue(){
fill(0,204,255);
rect(50, playerY, playerW,playerH);
}
}
void instructionsPage() {
}
void creditsPage() {
}
void winGame() {
background(#212121);
fill(255);
text("YOU WIN", 250, 300);
}
void loseGame() {
background(#212121);
text("YOU LOST!", 250, 235);
noStroke();
fill(#20C4F4);
rect(215, 335, 245, 50);
fill(255);
text("PLAY AGAIN", 248, 370);
noStroke();
fill(#20C4F4);
rect(215, 400, 245, 50);
fill(255);
text("BACK TO MENU", 226, 437);
if (mousePressed == true) {
if (mouseX > 215 && mouseX< 215+245 && mouseY> 335 && mouseY< 335+50) {
gameState = "CHOOSE";
}
}
}
void choosePlayer2() {
if (t.getTime()< 0 ) {
gameState= "WIN";
}
background(#212121);
t.countDown();
fill(255);
text(t.getTime(), 615, 50);
//Level 1 for 100 seconds mode
if (t.getTime()< 102 &&t.getTime()> 80 ) {
for ( Enemies e : enemiesRowOne) {
e.displayRowOne();
e.move();
}
//for collision player & obstacles Lvl1
for ( Enemies e : enemiesRowOne) {
for ( Player p : player) if (e.crashesRowOne(p)) {
gameState = "LOSE";
}
}
}
for ( Player p : player) {
p.displayGreen();
}
}
void choosePlayer3() {
if (t.getTime()< 0 ) {
gameState= "WIN";
}
background(#212121);
t.countDown();
fill(255);
text(t.getTime(), 615, 50);
//Level 1 for 100 seconds mode
if (t.getTime()< 102 &&t.getTime()> 80 ) {
for ( Enemies e : enemiesRowOne) {
e.displayRowOne();
e.move();
}
//for collision player & obstacles Lvl1
for ( Enemies e : enemiesRowOne) {
for ( Player p : player) if (e.crashesRowOne(p)) {
gameState = "LOSE";
}
}
for ( Player p : player) {
p.displayGreen();
}
}
}