help with input of classes and arry list
in
Programming Questions
•
11 months ago
I have been working on a game for uni over the last week and have pieced together a mouse dodging game where the objective is to survive for as long as possible. The game is working fine but i want to clear up the code with classes and array lists. Ive looked at some tutorials on classes and arrays and understand their function but am having trouble incorporating it into the code. need classes for dropping objects and an array list for the amount on screen at once. Any help would be most appreciated thanks :)
code is as follows,
PImage enemy1; // Images for game
PImage enemy2;
PImage salmon;
PImage cover;
boolean startscreen = true; // functions for game starting and ending
boolean startgame = false;
boolean endgame = false;
int c; // variables for survival time
int jj = 0;
int rr;
int sec;
float velocity1; // dropping speed variables
float velocity2;
float velocity3;
float velocity4;
float velocity5;
float velocity6;
float positionx1;
float positiony1;
float positionx2;
float positiony2;
float positionx3;
float positiony3;
float positionx4;
float positiony4;
float positionx5;
float positiony5;
float positionx6;
float positiony6;
float positionx7;
float positiony7;
float positionx8;
float positiony8;
float positionx9;
float positiony9;
float positionx10;
float positiony10;
float positionx11;
float positiony11;
float positionx12;
float positiony12;
float positionx13;
float positiony13;
float positionx14;
float positiony14;
float positionx15;
float positiony15;
void setup(){
size(1350, 700);
smooth();
enemy1 = loadImage("bear.png"); // image loading
salmon = loadImage("salmon.png");
cover = loadImage("cover.png");
enemy2 = loadImage("caveBear.png");
velocity1 = 15; //dropping speed
velocity2 = 10;
velocity3 = 6;
//enemys moving from left
positionx1 = -100;
positiony1 = random(0, 1390);
positionx2 = -200;
positiony2 = random(0, 1390);
positionx3 = -300;
positiony3 = random(0, 1390);
positionx4 = -400;
positiony4 = random(0, 1390);
positionx5 = -50;
positiony5 = random(0, 1390);
// enemys moving from top
positionx6 = random(0, 1390);
positiony6 = -500;
positionx7 = random(0, 1390);
positiony7 = -600;
positionx8 = random(0, 1390);
positiony8 = -700;
positionx9 = random(0, 1390);
positiony9 = -800;
positionx10 = random(0, 1390);
positiony10 = -110;
positionx11 = -120;
positiony11 = random(0, 1390);
//positionx12 =
// positiony12 =
// positionx13 =
// positionx14 =
// positiony14 =
// positionx15 =
// positiony15 =
}
void draw(){
background(255); // will be edited once i edit the enemys and remove the white around them
if(startscreen == true){ // start screen for game
image(cover, 0, 0);
}
if(mousePressed == true){ // clicking the mouse with begin the game
startscreen = false;
startgame = true;
}
if(startgame == true) {
image(enemy1, positionx1, positiony1); // positions of enemys
image(enemy1, positionx2, positiony2);
image(enemy1, positionx3, positiony3);
image(enemy1, positionx4, positiony4);
image(enemy1, positionx5, positiony5);
image(enemy1, positionx6, positiony6);
image(enemy1, positionx7, positiony7);
image(enemy1, positionx8, positiony8);
image(enemy1, positionx9, positiony9);
image(enemy1, positionx10, positiony10);
image(enemy2, positionx11, positiony11);
} else {
startgame = false;
}
positionx1 = positionx1 + velocity1; // adding velocity to tge enemys
positionx2 = positionx2 + velocity3;
positionx3 = positionx3 + velocity2;
positiony4 = positiony4 + velocity1;
positiony5 = positiony5 + velocity1;
positiony6 = positiony6 + velocity3;
positiony7 = positiony7 + velocity1;
positiony8 = positiony8 + velocity1;
positiony9 = positiony9 + velocity3;
positiony10 = positiony10 + velocity2;
positionx11 = positionx11 + velocity3;
if(startgame == true){
rr = rr;
}
c = millis()-rr+jj; // timer
sec = (c/(1000));
textSize(100);
text(sec, 100, 100);
fill(0);
if (startgame == false){ // when game started make timer 0
rr =0+millis();
}
if (positionx1 >= 1390) { // if the position of x is beyond 1390, it gives it a new random y position and x is set to -50
positiony1 = random(0, 1390);
positionx1 = -50;
}
if (positionx2 >= 1390) {
positiony2 = random(0, 1390);
positionx2 = -50;
}
if (positionx3 >= 1390) {
positiony3 = random(0, 1390);
positionx3 = -50;
}
if (positiony4 >= 1390) {
positiony4 = random(0, 1390);
positionx4 = -50;
}
if (positiony5 >= 1390) {
positiony5 = random(0, 1390);
positionx5 = -50;
}
if (positionx11 >= 1390) {
positiony11 = random(0, 1390);
positionx11 = -50;
}
if (positiony6 >= 900) { // if the position if y is beyond 900, it gives it a new random x position and x is set to -50
positiony6 = -50;
positionx6 = random(0, 1390);
}
if (positiony7 >= 900) {
positiony7 = -50;
positionx7 = random(0, 1390);
}
if (positiony8 >= 900) {
positiony8 = -50;
positionx8 = random(0, 1390);
}
if (positiony9 >= 900) {
positiony9 = -50;
positionx9 = random(0, 1390);
}
if (positiony10 >= 900) {
positiony10 = -50;
positionx10 = random(0, 1390);
}
image(salmon, mouseX, mouseY, 40, 40); // salmon miages for the mouse
if(dist(mouseX, mouseY, positionx1, positiony1) <= 100){ // collision detection for the enemys with mouse
startscreen = true;
startgame = false;
}
if(dist(mouseX, mouseY, positionx2, positiony2) <= 100){
startscreen = true;
startgame = false;
}
if(dist(mouseX, mouseY, positionx3, positiony3) <= 100){
startscreen = true;
startgame = false;
}
if(dist(mouseX, mouseY, positionx4, positiony4) <= 100){
startscreen = true;
startgame = false;
}
if(dist(mouseX, mouseY, positionx5, positiony5) <= 100){
startscreen = true;
startgame = false;
}
if(dist(mouseX, mouseY, positionx6, positiony6) <= 100){
startscreen = true;
startgame = false;
}
if(dist(mouseX, mouseY, positionx7, positiony7) <= 100){
startscreen = true;
startgame = false;
}
if(dist(mouseX, mouseY, positionx8, positiony8) <= 100){
startscreen = true;
startgame = false;
}
if(dist(mouseX, mouseY, positionx9, positiony9) <= 100){
startscreen = true;
startgame = false;
}
if(dist(mouseX, mouseY, positionx10, positiony10) <= 100){
startscreen = true;
startgame = false;
}
if(dist(mouseX, mouseY, positionx11, positiony11) <= 170){
startscreen = true;
startgame = false;
}
}
code is as follows,
PImage enemy1; // Images for game
PImage enemy2;
PImage salmon;
PImage cover;
boolean startscreen = true; // functions for game starting and ending
boolean startgame = false;
boolean endgame = false;
int c; // variables for survival time
int jj = 0;
int rr;
int sec;
float velocity1; // dropping speed variables
float velocity2;
float velocity3;
float velocity4;
float velocity5;
float velocity6;
float positionx1;
float positiony1;
float positionx2;
float positiony2;
float positionx3;
float positiony3;
float positionx4;
float positiony4;
float positionx5;
float positiony5;
float positionx6;
float positiony6;
float positionx7;
float positiony7;
float positionx8;
float positiony8;
float positionx9;
float positiony9;
float positionx10;
float positiony10;
float positionx11;
float positiony11;
float positionx12;
float positiony12;
float positionx13;
float positiony13;
float positionx14;
float positiony14;
float positionx15;
float positiony15;
void setup(){
size(1350, 700);
smooth();
enemy1 = loadImage("bear.png"); // image loading
salmon = loadImage("salmon.png");
cover = loadImage("cover.png");
enemy2 = loadImage("caveBear.png");
velocity1 = 15; //dropping speed
velocity2 = 10;
velocity3 = 6;
//enemys moving from left
positionx1 = -100;
positiony1 = random(0, 1390);
positionx2 = -200;
positiony2 = random(0, 1390);
positionx3 = -300;
positiony3 = random(0, 1390);
positionx4 = -400;
positiony4 = random(0, 1390);
positionx5 = -50;
positiony5 = random(0, 1390);
// enemys moving from top
positionx6 = random(0, 1390);
positiony6 = -500;
positionx7 = random(0, 1390);
positiony7 = -600;
positionx8 = random(0, 1390);
positiony8 = -700;
positionx9 = random(0, 1390);
positiony9 = -800;
positionx10 = random(0, 1390);
positiony10 = -110;
positionx11 = -120;
positiony11 = random(0, 1390);
//positionx12 =
// positiony12 =
// positionx13 =
// positionx14 =
// positiony14 =
// positionx15 =
// positiony15 =
}
void draw(){
background(255); // will be edited once i edit the enemys and remove the white around them
if(startscreen == true){ // start screen for game
image(cover, 0, 0);
}
if(mousePressed == true){ // clicking the mouse with begin the game
startscreen = false;
startgame = true;
}
if(startgame == true) {
image(enemy1, positionx1, positiony1); // positions of enemys
image(enemy1, positionx2, positiony2);
image(enemy1, positionx3, positiony3);
image(enemy1, positionx4, positiony4);
image(enemy1, positionx5, positiony5);
image(enemy1, positionx6, positiony6);
image(enemy1, positionx7, positiony7);
image(enemy1, positionx8, positiony8);
image(enemy1, positionx9, positiony9);
image(enemy1, positionx10, positiony10);
image(enemy2, positionx11, positiony11);
} else {
startgame = false;
}
positionx1 = positionx1 + velocity1; // adding velocity to tge enemys
positionx2 = positionx2 + velocity3;
positionx3 = positionx3 + velocity2;
positiony4 = positiony4 + velocity1;
positiony5 = positiony5 + velocity1;
positiony6 = positiony6 + velocity3;
positiony7 = positiony7 + velocity1;
positiony8 = positiony8 + velocity1;
positiony9 = positiony9 + velocity3;
positiony10 = positiony10 + velocity2;
positionx11 = positionx11 + velocity3;
if(startgame == true){
rr = rr;
}
c = millis()-rr+jj; // timer
sec = (c/(1000));
textSize(100);
text(sec, 100, 100);
fill(0);
if (startgame == false){ // when game started make timer 0
rr =0+millis();
}
if (positionx1 >= 1390) { // if the position of x is beyond 1390, it gives it a new random y position and x is set to -50
positiony1 = random(0, 1390);
positionx1 = -50;
}
if (positionx2 >= 1390) {
positiony2 = random(0, 1390);
positionx2 = -50;
}
if (positionx3 >= 1390) {
positiony3 = random(0, 1390);
positionx3 = -50;
}
if (positiony4 >= 1390) {
positiony4 = random(0, 1390);
positionx4 = -50;
}
if (positiony5 >= 1390) {
positiony5 = random(0, 1390);
positionx5 = -50;
}
if (positionx11 >= 1390) {
positiony11 = random(0, 1390);
positionx11 = -50;
}
if (positiony6 >= 900) { // if the position if y is beyond 900, it gives it a new random x position and x is set to -50
positiony6 = -50;
positionx6 = random(0, 1390);
}
if (positiony7 >= 900) {
positiony7 = -50;
positionx7 = random(0, 1390);
}
if (positiony8 >= 900) {
positiony8 = -50;
positionx8 = random(0, 1390);
}
if (positiony9 >= 900) {
positiony9 = -50;
positionx9 = random(0, 1390);
}
if (positiony10 >= 900) {
positiony10 = -50;
positionx10 = random(0, 1390);
}
image(salmon, mouseX, mouseY, 40, 40); // salmon miages for the mouse
if(dist(mouseX, mouseY, positionx1, positiony1) <= 100){ // collision detection for the enemys with mouse
startscreen = true;
startgame = false;
}
if(dist(mouseX, mouseY, positionx2, positiony2) <= 100){
startscreen = true;
startgame = false;
}
if(dist(mouseX, mouseY, positionx3, positiony3) <= 100){
startscreen = true;
startgame = false;
}
if(dist(mouseX, mouseY, positionx4, positiony4) <= 100){
startscreen = true;
startgame = false;
}
if(dist(mouseX, mouseY, positionx5, positiony5) <= 100){
startscreen = true;
startgame = false;
}
if(dist(mouseX, mouseY, positionx6, positiony6) <= 100){
startscreen = true;
startgame = false;
}
if(dist(mouseX, mouseY, positionx7, positiony7) <= 100){
startscreen = true;
startgame = false;
}
if(dist(mouseX, mouseY, positionx8, positiony8) <= 100){
startscreen = true;
startgame = false;
}
if(dist(mouseX, mouseY, positionx9, positiony9) <= 100){
startscreen = true;
startgame = false;
}
if(dist(mouseX, mouseY, positionx10, positiony10) <= 100){
startscreen = true;
startgame = false;
}
if(dist(mouseX, mouseY, positionx11, positiony11) <= 170){
startscreen = true;
startgame = false;
}
}
1