I've found a post about it:
http://forum.processing.org/topic/how-do-i-make-an-object-disappear-upon-collision
Car Game.pde:
Sprite.pde:
http://forum.processing.org/topic/how-do-i-make-an-object-disappear-upon-collision
Car Game.pde:
/** * Car Game {Alternative} (v1.0) * by Michael (mtwalworth) * modders Chrisir & GoToLoop * * http://forum.processing.org/topic * /how-do-i-make-an-object-disappear-upon-collision * * http://www.openprocessing.org/sketch/103128 */ final static int CAR_W = 70, CAR_H = 40, CAR_S = 10; final static int GRAPE_W = 10, GRAPE_H = 10, GRAPE_S = 0; final static int BANANA_W = 10, BANANA_H = 20, BANANA_S = 0; final static color CAR_C = #0000FF; final static color GRAPE_C = #F080F0, BANANA_C = #FFFF00; final static float FPS = 50, BOLD = 2; final static color BG = -1, FG = 0; Sprite car, grape, banana; void setup() { size(800, 600); frameRate(FPS); smooth(); stroke(FG); strokeWeight(BOLD); rectMode(CENTER); ellipseMode(CENTER); car = createCar(); grape = createGrape(); banana = createBanana(); } void draw() { background(BG); car.script(); grape.script(); banana.script(); if (car.checkImmersion(grape)) grape.resetSprite((int) random(GRAPE_W, width-GRAPE_W), (int) random(GRAPE_H, height-GRAPE_H)); if (car.checkImmersion(banana)) banana.resetSprite((int) random(BANANA_W, width-BANANA_W), (int) random(BANANA_H, height-BANANA_H)); } void keyPressed() { final int k = keyCode; if (k == ' ' | k == ENTER) car.setAcceleration( 1); else if (k == SHIFT | k == CONTROL) car.setAcceleration(-1); else if (k == 'W' | k == UP) car.setVertical(-1); else if (k == 'S' | k == DOWN) car.setVertical( 1); else if (k == 'A' | k == LEFT) car.setHorizontal(-1); else if (k == 'D' | k == RIGHT) car.setHorizontal( 1); } void keyReleased() { final int k = keyCode; if (k == ' ' | k == ENTER && car.a > 0 || k == SHIFT | k == CONTROL && car.a < 0) car.setAcceleration(0); else if (k == 'W' | k == UP) car.north = false; else if (k == 'S' | k == DOWN) car.south = false; else if (k == 'A' | k == LEFT) car.west = false; else if (k == 'D' | k == RIGHT) car.east = false; } Sprite createCar() { return new Sprite(width >> 1, height >> 1, CAR_W, CAR_H, CAR_S, CAR_C, false); } Sprite createGrape() { return new Sprite((int) random(GRAPE_W, width - GRAPE_W), (int) random(GRAPE_H, height - GRAPE_H), GRAPE_W, GRAPE_H, GRAPE_S, GRAPE_C, true); } Sprite createBanana() { return new Sprite((int) random(BANANA_W, width - BANANA_W), (int) random(BANANA_H, height - BANANA_H), BANANA_W, BANANA_H, BANANA_S, BANANA_C, false); }
Sprite.pde:
class Sprite { // requires rectMode(CENTER); \\ final static byte ACCEL_FREQ = 20; final static byte SPD_MIN = 0, SPD_MAX = 50; int x, y; byte w, h, rx, ry, s, a; color c; boolean isRounded; int top, bottom, left, right; boolean north, south, west, east; Sprite(int xx, int yy, int ww, int hh, int ss, color cc, boolean r) { // invokes initialization method: setSprite(xx, yy, ww, hh, ss, cc, r); } void setSprite(int xx, int yy, int ww, int hh, int ss, color cc, boolean r) { w = (byte) ww; // diameter x \\ h = (byte) hh; // diameter y \\ rx = (byte) (ww >> 1); // radius x \\ ry = (byte) (hh >> 1); // radius y \\ s = (byte) ss; // speed \\ c = cc; // color \\ isRounded = r; // ellipse(true) or rect(false) \\ resetSprite(xx, yy); // completes initialization \\ } void resetSprite(int xx, int yy) { x = xx; // location x \\ y = yy; // location y \\ setHorizontal(0); setVertical(0); } void setHorizontal(int horizontal) { if (horizontal < 0) west = true; else if (horizontal > 0) east = true; else west = east = false; } void setVertical(int vertical) { if (vertical < 0) north = true; else if (vertical > 0) south = true; else north = south = false; } void setAcceleration(int accel) { a = (byte) (accel == 0? 0 : accel >> 31 | 1); } void updateSpeed() { if (frameCount % ACCEL_FREQ != 0) return; if ((s += a) == SPD_MIN) s = SPD_MIN + 1; else if (s == SPD_MAX) s = SPD_MAX - 1; } void moveSprite() { if (west) x -= s; if (east) x += s; if (north) y -= s; if (south) y += s; } void updateBorders() { left = x - rx; right = x + rx; top = y - ry; bottom = y + ry; } void confineToScreen() { // invoke updateBorders() 1st! \\ if (left < 0) x = rx; else if (right >= width) x = width - rx - 1; if (top < 0) y = ry; else if (bottom >= height) y = height - ry - 1; } void displaySprite() { fill(c); if (isRounded) ellipse(x, y, w, h); else rect(x, y, w, h); } boolean checkImmersion(Sprite other) { return abs(x - other.x) < abs(rx - other.rx) & abs(y - other.y) < abs(ry - other.ry); } void script() { if (s != 0) { updateSpeed(); moveSprite(); updateBorders(); confineToScreen(); } displaySprite(); } }