Can anyone help me to fix my code?????(about array of objects in simple game)
in
Programming Questions
•
2 years ago
Hi, I'm triying to make a simple game that user controls right, and left arrow keys to avoid randomly falling objects from the top of the screen. As for object's random falling, I tried both Arraylist and just simple array that has preseted size, but it's not working and totally have no idea what to do.
Here's my code below, and just need help..T-T
//algorithm: avoide falling germs controlling hand's move using left, right arrow keys
Germ []germ;
int totalGerms=0;
int cnt;
Hand hand;
Hand hand1;
Page page;
void setup(){
size(500,700);
smooth();
noStroke();
page = new Page();
germ=new Germ[1000];
hand = new Hand("h1.png", width/3, height-75);
if(totalGerms<germ.length){
germ[totalGerms]=new Germ();
totalGerms++;
}
// hand1 = new Hand("h1 germ.png",hand.posX,hand.posY);
}
void draw(){
page.firstPage();
if(page.game){
int t=(7000-millis())/1000; //if 5 secs passed then germs fall
if(t>0){
text(t, width/2, height/2);
}
page.displayPage();
if((t==0) || (t<0)){
for(int i=0; i<totalGerms; i++){
if(!germ[i].finished){
germ[i].fall();
if(germ[i].reachBottom()){
germ[i].finished();
}
}
}
hand.display();
hand.move();
}
}
}
// if (hand.meetGerm()){ //if hand intersects with germ
// gs.germInfect(hand); //hand turns in purple(actually, i've got a purple hand img)
// }
/*
if(hand.infect(germ[i])){
println("dead");
hand = new Hand("h1 germ.png",hand.posX,hand.posY);
}
*/
//}
class Germ{
float xPos, yPos;
float gravity;
float speed;
PImage img;
boolean finished = false;
Germ(){
xPos = random(0,width-40);//initially located at random x within the boundary
yPos = -100; //hide germ before the game starts -100
gravity=0.07;
speed=random(-2,2);
img = loadImage("t5.gif");//136 *109
}
void fall(){
speed += gravity;
yPos += speed;
image(img,xPos,yPos);
}
boolean reachBottom(){
if(yPos > height){
return true;
}
else
return false;
}
void finished(){
finished=true;
}
}
class Hand{
float posX;
float posY;//position of hand
int lives;
PImage hImage;
Hand(String filename, float x, float y){
posX= x;
posY=y;
lives=3;
hImage = loadImage(filename);
}
void display(){
posX= constrain(posX,0,width-75);
translate(posX,posY);
image(hImage,0,0,80,80);
}
/*
boolean meetGerm(){
float area = dist(posX, posY, xPos , yPos); //the range of intersection
if (area <40){
lives--;
return true;
}
else{
return false;
}
}
*/
void move(){ //function of left arrow and right arrow
if (keyPressed && (key == CODED)) //using coded key
{
if(keyCode == LEFT){
hand.posX-=4;
}
else if (keyCode == RIGHT){
hand.posX+=4;
}
}
}
}
class Page{
boolean game;
int score;
int stage;
int lives;
PFont font;
PImage button;
PImage img;
Hand hand;
Page(){
game=false;
score=0;
stage=1;
lives=3;
font = loadFont("ARCARTER-48.vlw");
img=loadImage("germ.jpg");
}
void firstPage(){
background(255);
image(img,width*3/4,height*2/3);
fill(0,0,255);//font color
textFont(font,50);
textAlign(CENTER);
text("First page!!", width/2,height/2);
textFont(font,30);
text("Press Enter to start game",width/2,height/2-80);
if (keyPressed && (key == ENTER)){
game=true;
}
}
/*
if(mousePressed){
if(mouseButton==LEFT)
{
}
}
*/
void displayPage(){
background(0); //black bg color
int t=(7000-millis())/1000; //if 5 secs passed then germs fall
if(t>0){
text(t, width/2, height/2);
}
fill(255,255,255);
textFont(font,23);
textAlign(LEFT);
text("Stage: ", width/20,height/15);
text("Score: ", width/20,height*2/15);
text(int(stage),width/20+60, height/15);
text(int(score), width/20+60, height*2/15);
strokeWeight(2);
stroke(0,255,0);
line(0,9*height/10+20,width,9*height/10+20);
fill(255,0,0);
text("Lives: ", width-100,height/15);
text(int(lives), width-100+50, height/15);
}
void show(){
// if(hand.infect()){
fill(255,0,30);
textAlign(CENTER);
text("GERM INFECTED!", width/2, height/2-50);
}
void gameOverPage(){
background(255);
textFont(font,50);
textAlign(CENTER);
text("Game Over", width/2,height/2);
}
}
1