float laserSpeed = 10; int health = 100; class PEW { float x, y, sx, sy; PEW (float x, float y, float x2, float y2) { this.x = x; this.y = y; float deltax = x2 - x; float deltay = y2 - y; float hyp = dist (x2, y2, x, y); this.sx = laserSpeed * deltax / hyp; this.sy = laserSpeed * deltay / hyp; } void process() { this.x += this.sx; this.y += this.sy; this.sy += 0.2; stroke(0, 255, 0); line(this.x, this.y, this.x + this.sx, this.y + this.sy); } } ArrayList lasers = new ArrayList(); void setup() { size(600, 600); } void draw() { fill(0); rect(0, 0, 600, 600); for (int i=0; i < lasers.size(); i++) lasers.get(i).process(); for (int i=0; i < lasers.size(); i++) { if (lasers.get(i).x < 0 || lasers.get(i).x > 600 || lasers.get(i).y < 0 || lasers.get(i).y > 600) { //kill the laser lasers.remove(i); i--; } else if (dist(lasers.get(i).x, lasers.get(i).y, mouseX, mouseY) < 50) { //kill the laser, hit the player lasers.remove(i); i--; health--; } } if (random(10) > 8) { for (int i=0; i < 5; i++) lasers.add(new PEW(100 + i * 100, 500, mouseX, mouseY)); } fill(255); text("Health : " + health, 100,100); } void mousePressed() { lasers.add(new PEW(300, 500, mouseX, mouseY)); }