2015-11-06 1 views
0

Я пытаюсь создать игру, в которой мой персонаж (Гомер Симпсон) должен собирать падающие предметы (пончики) и избегать попадания токсичных танков. Сейчас мой счет увеличивается каждый раз, когда он получает пончик, но я хочу, чтобы пончик исчез после этого. Может ли кто-нибудь помочь мне в этом?Как заставить мой объект исчезать, когда он попадает на моего персонажа в моей игре?

Это мой код до сих пор:

PImage bg2; //background image 

PImage homer; //image of homer 

homer homer1; 

int numberOfToxic = 3; //number of falling toxic tanks 

int numberOfDonut = 3; //number of falling donuts 

int score; //the score 

Toxic[] toxic; //array for the toxic that will fall 
PImage t; 

Donut[] donut; //array for the donuts that will fall 
PImage d; 



void setup() { 
    size(600,500); //size of the window 
    bg2 = loadImage("bg2.jpg"); //uploading the background image 
    homer = loadImage("homer.gif"); //uploading the character image 
    t = loadImage("toxictank.png"); 
    d = loadImage("donut.png"); 
    PFont louiseFont; 
    louiseFont = loadFont("chalk.vlw"); //loading the font I've chosen 
    textFont(louiseFont); //the current font being used in the game 




    toxic = new Toxic[numberOfToxic]; 
    for (int i = 0; i < numberOfToxic; i++) { 
    toxic[i] = new Toxic(); 
} 

    donut = new Donut[numberOfDonut]; 
    for(int i = 0; i < numberOfDonut; i++) { 
    donut[i] = new Donut(); 
} 

score = 0; //the score starts at 0 

} 

void draw() { 
    background(bg2); 
    homer1 = new homer(mouseX-70, 350, 140, 150, homer); //mouseX makes the hero  move on the x axis and 350 defines where it is on the y axis. -70 center the mouse  on the image/hero 
    homer1.drawHomer(); //call the function homer(hero) 

//Making the taxictanks fall 
    for(int i = 0; i < toxic.length; i++) { 
    toxic[i].update(); 
    toxic[i].drawToxic(); 
    if(toxic[i].position.y > 500) { 
     toxic[i].reset(); 
    } 
    } 
//Making the donuts fall 
    for(int i = 0; i < donut.length; i++) { 
    donut[i].update2(); 
    donut[i].drawDonut(); 
    if(donut[i].position2.y > 500) { 
     donut[i].reset2(); 
    } 

//Collecting points if Homer eats donuts 
    if(abs(donut[i].position2.y - homer1.y) <= 2 && abs(donut[i].position2.x-40 - homer1.x)<=40) { 
    score ++; 
    println("ok"); 

    } 
} 

    fill(#0D128B); //color of text 
    textSize(20); //size of text 
    text("SCORE: " + nf(score, 1), 20, 40); //score points - tells how to use  the text in the game 
} 

//class with height, width x, y positions and the hero image 
class homer { 
    int x; 
    int y; 
    int hWidth; 
    int hHeight; 
    PImage homer; 

homer(int x, int y, int hWidth, int hHeight, PImage homer) { 
    this.x = x; 
    this.y = y; 
    this.hWidth = hWidth; 
    this.hHeight = hHeight; 
    this.homer = homer; 
} 

void drawHomer() { 
    image(this.homer, this.x, this.y, this.hWidth, this.hHeight); 
} 
} 



class Toxic { //all the variables for toxic 
    PImage t; 
    PVector position; 
    float speed; 
    float size; 

    //constructor of toxic 
    Toxic() { 
    t = loadImage("toxictank.png"); 
    position = new PVector(random(width), random(height)); 
    speed = 4; 
    size = 20; 
    } 

    void update() { 
    position.y += speed; 

    } 

    void drawToxic() { 
    for(int i = 1; i < 3; i++) { 
     image(t, position.x, position.y); 

    } 
    } 

    void reset() { 
    position.x = random(width); 
    position.y = 0 - 50; 
    speed = 4; 
    size = 20; 
} 
} 




class Donut { //all variables for donut 
    PImage d; 
    PVector position2; 
    float speed2; 
    float size2; 
    boolean falling; 
    int timeToDisplay; 
    int fallingSpeed; 

    Donut() { //constructor of donut 
    d = loadImage("donut.png"); 
    position2 = new PVector(random(width), random(height)); 
    speed2 = 4; 
    size2 = 40; 
    falling = false; 
    timeToDisplay = (int)random(2.60); 
    fallingSpeed = (int)random(2.5); 
    } 

    void update2() { 
    position2.y += speed2; 
    } 

    void drawDonut() { 
    for(int i = 1; i < 3; i++) { 
     image(d, position2.x, position2.y); 
    } 
    } 

    void reset2() { 
    position2.x = random(width); 
    position2.y = 0 - 50; 
    speed2 = 4; 
    size2 = 20; 
    } 

} 

ответ

0

Просто установите переменные в экземпляре Donut так, что она восходит к верхней части экрана, или не тянет вообще. У вас уже есть функция reset2(), которая делает это. Что-то вроде этого:

if(abs(donut[i].position2.y - homer1.y) <= 2 && abs(donut[i].position2.x-40 - homer1.x)<=40) { 
    score ++; 
    donut[i].reset2(); 
    println("ok"); 
}