I need help coding a catch game!
in
Programming Questions
•
1 year ago
I've tried getting help from other examples online but I still couldn't get it working properly, and now it seems to freeze after running for only a second or two! I have three classes as well as my main. Can anyone help?
MAIN
alien myalien;
humans[] myhumans;
int totalHumans = 0;
Timer mytimer;
boolean gameOver = false;
int score = 0;
int level = 1;
int lives = 10;
int levelCounter = 0;
PFont f;
PImage img;
void setup() {
size(800, 600);
smooth();
img = loadImage("space_cat.jpg");
background(img);
myalien = new alien();
myhumans = new humans[50];
mytimer = new Timer(3000);
mytimer.start();
f = createFont("Arial",12,true);
}
void draw() {
if (gameOver) {
textFont(f,48);
textAlign(CENTER);
fill(0);
text("GAME OVER",WIDTH/2,HEIGHT/2);
}
else {
myalien.display();
myalien.move();
if (mytimer.isFinished()) {
if (totalHumans < myhumans.length) {
myhumans[totalHumans] = new humans();
totalHumans++;
}
mytimer.start();
}
for (int i = 0; i < totalHumans; i++) {
if (finished = true) {
myhumans[i].move();
myhumans[i].drawBody();
if (myhumans[i].reachedBottom()) {
levelCounter++;
myhumans[i].finished();
lives--;
if (lives <= 0) {
gameOver = true;
}
}
if (myalien.intersect(myhumans[i])) {
myhumans[i].finished();
levelCounter++;
score++;
}
}
}
if (levelCounter >= myhumans.length) {
level++;
levelCounter = 0;
lives = 10;
mytimer.setTime(constrain(300-level*25,0,300));
totalHumans = 0;
}
textFont(f,14);
fill(0);
text("Lives left: "+ lives,10,20);
rect(10,24,lives*10,10);
text("Level: "+ level,300,20);
text("Score: "+ score, 300,40);
}
}
-------------------------------
class Timer {
int savedTime;
int totalTime;
Timer(int tempTotalTime) {
totalTime = tempTotalTime;
}
void setTime(int t) {
totalTime = t;
}
void start() {
savedTime = millis();
}
boolean isFinished() {
int passedTime = millis() - savedTime;
if (passedTime > totalTime) {
return true;
} else {
return false;
}
}
}
-----------------------------
class alien {
float bodyXLoc;
float bodyYLoc;
int bodyWidth;
int bodyHeight;
int headWidth;
float headHeight;
float headXLoc;
float headYLoc;
int eyeWidth;
int eyeHeight;
int speed;
int direction;
alien () {
bodyXLoc = 450;
bodyYLoc = 300;
bodyWidth = 50; //changes width of the body
bodyHeight = 100; //changes height of the body
headHeight = 100;
eyeWidth = 30; //changes the width of the eyes
eyeHeight = 30; //changes the height of the eyes
speed = 5; //changes how fast the alien moves
direction = 1;
}
// void setLocation(float tempX, float tempY) {
// bodyXLoc = tempX;
// bodyYLoc = tempY;
// }
void display() {
//Draw body
stroke(0,0,0);
strokeWeight(1);
fill(137,242,204); //defines the colour for the alien's body
rect(bodyXLoc, bodyYLoc, bodyWidth, bodyHeight); //draws the aliens body
stroke(255,255,255); //defines the colour of the alien's arms
strokeWeight(8); //defines the thickness of the alien's arms
line(bodyXLoc+51, bodyYLoc+30, bodyXLoc+100, bodyYLoc+70); //draws the arms
line(bodyXLoc+51, bodyYLoc+50, bodyXLoc+100, bodyYLoc+90);
line(bodyXLoc+51, bodyYLoc+70, bodyXLoc+100, bodyYLoc+110);
line(bodyXLoc, bodyYLoc+30, bodyXLoc-50, bodyYLoc+70);
line(bodyXLoc, bodyYLoc+50, bodyXLoc-50, bodyYLoc+90);
line(bodyXLoc, bodyYLoc+70, bodyXLoc-50, bodyYLoc+110);
line(bodyXLoc+10, bodyYLoc+100, bodyXLoc+10, bodyYLoc+150);
line(bodyXLoc+25, bodyYLoc+100, bodyXLoc+25, bodyYLoc+150);
line(bodyXLoc+40, bodyYLoc+100, bodyXLoc+40, bodyYLoc+150);
//Draw head
fill(137,242,204); //defines the colour of the alien's head
stroke(0); //defines the colour of the outline of the alien's head
strokeWeight(1); //thins the line around the head
headWidth = bodyWidth*2; //defines the width of the head
headXLoc = bodyXLoc+25; //defines the location where the head is drawn
headYLoc = bodyYLoc+5; // ""
ellipse(headXLoc, headYLoc, headWidth, headHeight); //draws the head
fill(255,255,255); //defines the colour of the eyes
ellipse(headXLoc-10, headYLoc-15, eyeWidth+10, eyeHeight+10); //draws the first eye
ellipse(headXLoc+25, headYLoc-5, eyeWidth-10, eyeHeight-10); //draws the second eye
fill(0,0,0); //defines the colour of the pupils
ellipse(headXLoc-10, headYLoc-15, eyeWidth/2+10, eyeHeight/2+10); //draws the first pupil
ellipse(headXLoc+25, headYLoc-5, eyeWidth/2-10, eyeHeight/2-10); //draws the second pupil
ellipse(headXLoc+10, headYLoc+20, eyeWidth/2, eyeHeight/2);
}
void move() {
if(direction==1){
bodyXLoc = bodyXLoc + speed;
}
if(direction==2){
bodyYLoc=bodyYLoc-speed;
}
if(direction==3){
bodyXLoc=bodyXLoc-speed;
}
if(direction==4){
bodyYLoc=bodyYLoc+speed;
}
if(bodyXLoc<0||bodyXLoc>690||bodyYLoc<0||Y>690){
speed=0;
}
}
void keyPressed(){
if(keyCode==UP){direction=2;}
if(keyCode==LEFT){direction=3;}
if(keyCode== DOWN){direction=4;}
if(keyCode== RIGHT){ direction=1;}
}
boolean intersect (humans h) {
float distance = dist(bodyXLoc, bodyYLoc, h.bodyXLoc, h.bodyYLoc);
if (distance < bodyWidth + h.bodyWidth) {
return true;
} else {
return false;
}
}
}
--------------------------------
class humans {
int bodyXLoc;
int bodyYLoc;
int bodyWidth;
int bodyHeight;
int headWidth;
float headHeight;
int headXLoc;
int headYLoc;
float headheight;
int eyeWidth;
int eyeHeight;
int speed;
humans () {
bodyXLoc = int(random(1,width)); //randomly generates the start position of the humans
bodyYLoc = 1; //ensures the humans are generated at the top
bodyWidth = 10; //changes width of the body
bodyHeight = 20; //changes height of the body
speed = int(random(1,2)); //changes how fast the humans move
}
void move() {
bodyYLoc += speed; //makes the humans move downwards
}
boolean reachedBottom() {
if (bodyYLoc > height + bodyHeight) {
return true;
} else {
return false;
}
}
void drawBody() {
fill(255,0,0); //defines the colour for the human's body
rect(bodyXLoc, bodyYLoc, bodyWidth, bodyHeight); //draws the body
stroke(255,0,0);
strokeWeight(2);
line(bodyXLoc+10, bodyYLoc+5, bodyXLoc+20, bodyYLoc+10);
line(bodyXLoc, bodyYLoc+5, bodyXLoc-10, bodyYLoc+10);
line(bodyXLoc+10, bodyYLoc+20, bodyXLoc+15, bodyYLoc+30);
line(bodyXLoc, bodyYLoc+20, bodyXLoc-5, bodyYLoc+30);
ellipse(bodyXLoc+5, bodyYLoc-5, bodyWidth, bodyWidth);
}
void finished() {
finished = true;
}
}
MAIN
alien myalien;
humans[] myhumans;
int totalHumans = 0;
Timer mytimer;
boolean gameOver = false;
int score = 0;
int level = 1;
int lives = 10;
int levelCounter = 0;
PFont f;
PImage img;
void setup() {
size(800, 600);
smooth();
img = loadImage("space_cat.jpg");
background(img);
myalien = new alien();
myhumans = new humans[50];
mytimer = new Timer(3000);
mytimer.start();
f = createFont("Arial",12,true);
}
void draw() {
if (gameOver) {
textFont(f,48);
textAlign(CENTER);
fill(0);
text("GAME OVER",WIDTH/2,HEIGHT/2);
}
else {
myalien.display();
myalien.move();
if (mytimer.isFinished()) {
if (totalHumans < myhumans.length) {
myhumans[totalHumans] = new humans();
totalHumans++;
}
mytimer.start();
}
for (int i = 0; i < totalHumans; i++) {
if (finished = true) {
myhumans[i].move();
myhumans[i].drawBody();
if (myhumans[i].reachedBottom()) {
levelCounter++;
myhumans[i].finished();
lives--;
if (lives <= 0) {
gameOver = true;
}
}
if (myalien.intersect(myhumans[i])) {
myhumans[i].finished();
levelCounter++;
score++;
}
}
}
if (levelCounter >= myhumans.length) {
level++;
levelCounter = 0;
lives = 10;
mytimer.setTime(constrain(300-level*25,0,300));
totalHumans = 0;
}
textFont(f,14);
fill(0);
text("Lives left: "+ lives,10,20);
rect(10,24,lives*10,10);
text("Level: "+ level,300,20);
text("Score: "+ score, 300,40);
}
}
-------------------------------
class Timer {
int savedTime;
int totalTime;
Timer(int tempTotalTime) {
totalTime = tempTotalTime;
}
void setTime(int t) {
totalTime = t;
}
void start() {
savedTime = millis();
}
boolean isFinished() {
int passedTime = millis() - savedTime;
if (passedTime > totalTime) {
return true;
} else {
return false;
}
}
}
-----------------------------
class alien {
float bodyXLoc;
float bodyYLoc;
int bodyWidth;
int bodyHeight;
int headWidth;
float headHeight;
float headXLoc;
float headYLoc;
int eyeWidth;
int eyeHeight;
int speed;
int direction;
alien () {
bodyXLoc = 450;
bodyYLoc = 300;
bodyWidth = 50; //changes width of the body
bodyHeight = 100; //changes height of the body
headHeight = 100;
eyeWidth = 30; //changes the width of the eyes
eyeHeight = 30; //changes the height of the eyes
speed = 5; //changes how fast the alien moves
direction = 1;
}
// void setLocation(float tempX, float tempY) {
// bodyXLoc = tempX;
// bodyYLoc = tempY;
// }
void display() {
//Draw body
stroke(0,0,0);
strokeWeight(1);
fill(137,242,204); //defines the colour for the alien's body
rect(bodyXLoc, bodyYLoc, bodyWidth, bodyHeight); //draws the aliens body
stroke(255,255,255); //defines the colour of the alien's arms
strokeWeight(8); //defines the thickness of the alien's arms
line(bodyXLoc+51, bodyYLoc+30, bodyXLoc+100, bodyYLoc+70); //draws the arms
line(bodyXLoc+51, bodyYLoc+50, bodyXLoc+100, bodyYLoc+90);
line(bodyXLoc+51, bodyYLoc+70, bodyXLoc+100, bodyYLoc+110);
line(bodyXLoc, bodyYLoc+30, bodyXLoc-50, bodyYLoc+70);
line(bodyXLoc, bodyYLoc+50, bodyXLoc-50, bodyYLoc+90);
line(bodyXLoc, bodyYLoc+70, bodyXLoc-50, bodyYLoc+110);
line(bodyXLoc+10, bodyYLoc+100, bodyXLoc+10, bodyYLoc+150);
line(bodyXLoc+25, bodyYLoc+100, bodyXLoc+25, bodyYLoc+150);
line(bodyXLoc+40, bodyYLoc+100, bodyXLoc+40, bodyYLoc+150);
//Draw head
fill(137,242,204); //defines the colour of the alien's head
stroke(0); //defines the colour of the outline of the alien's head
strokeWeight(1); //thins the line around the head
headWidth = bodyWidth*2; //defines the width of the head
headXLoc = bodyXLoc+25; //defines the location where the head is drawn
headYLoc = bodyYLoc+5; // ""
ellipse(headXLoc, headYLoc, headWidth, headHeight); //draws the head
fill(255,255,255); //defines the colour of the eyes
ellipse(headXLoc-10, headYLoc-15, eyeWidth+10, eyeHeight+10); //draws the first eye
ellipse(headXLoc+25, headYLoc-5, eyeWidth-10, eyeHeight-10); //draws the second eye
fill(0,0,0); //defines the colour of the pupils
ellipse(headXLoc-10, headYLoc-15, eyeWidth/2+10, eyeHeight/2+10); //draws the first pupil
ellipse(headXLoc+25, headYLoc-5, eyeWidth/2-10, eyeHeight/2-10); //draws the second pupil
ellipse(headXLoc+10, headYLoc+20, eyeWidth/2, eyeHeight/2);
}
void move() {
if(direction==1){
bodyXLoc = bodyXLoc + speed;
}
if(direction==2){
bodyYLoc=bodyYLoc-speed;
}
if(direction==3){
bodyXLoc=bodyXLoc-speed;
}
if(direction==4){
bodyYLoc=bodyYLoc+speed;
}
if(bodyXLoc<0||bodyXLoc>690||bodyYLoc<0||Y>690){
speed=0;
}
}
void keyPressed(){
if(keyCode==UP){direction=2;}
if(keyCode==LEFT){direction=3;}
if(keyCode== DOWN){direction=4;}
if(keyCode== RIGHT){ direction=1;}
}
boolean intersect (humans h) {
float distance = dist(bodyXLoc, bodyYLoc, h.bodyXLoc, h.bodyYLoc);
if (distance < bodyWidth + h.bodyWidth) {
return true;
} else {
return false;
}
}
}
--------------------------------
class humans {
int bodyXLoc;
int bodyYLoc;
int bodyWidth;
int bodyHeight;
int headWidth;
float headHeight;
int headXLoc;
int headYLoc;
float headheight;
int eyeWidth;
int eyeHeight;
int speed;
humans () {
bodyXLoc = int(random(1,width)); //randomly generates the start position of the humans
bodyYLoc = 1; //ensures the humans are generated at the top
bodyWidth = 10; //changes width of the body
bodyHeight = 20; //changes height of the body
speed = int(random(1,2)); //changes how fast the humans move
}
void move() {
bodyYLoc += speed; //makes the humans move downwards
}
boolean reachedBottom() {
if (bodyYLoc > height + bodyHeight) {
return true;
} else {
return false;
}
}
void drawBody() {
fill(255,0,0); //defines the colour for the human's body
rect(bodyXLoc, bodyYLoc, bodyWidth, bodyHeight); //draws the body
stroke(255,0,0);
strokeWeight(2);
line(bodyXLoc+10, bodyYLoc+5, bodyXLoc+20, bodyYLoc+10);
line(bodyXLoc, bodyYLoc+5, bodyXLoc-10, bodyYLoc+10);
line(bodyXLoc+10, bodyYLoc+20, bodyXLoc+15, bodyYLoc+30);
line(bodyXLoc, bodyYLoc+20, bodyXLoc-5, bodyYLoc+30);
ellipse(bodyXLoc+5, bodyYLoc-5, bodyWidth, bodyWidth);
}
void finished() {
finished = true;
}
}
1