I had a topic on here earlier, but I solved the problem on that. I'm a beginner at Processing and read most of a book on it and decided to make my own simple program. I decided to make a game where aliens fall down and whenever you press the mouse, "ammo " comes out of the box and hits the alien, which sends it off the screen, having the effect that you defeated it. The problem is that the ammo is not coming out when the mouse is pressed. Heres my code.
TTab 1
Code:
Ammo ammo;
Timer timer;
Alien[] alien;
int totalAlien = 0;
int score=0;
void setup() {
size(750,750);
alien = new Alien[250];
ammo = new Ammo(20);
timer = new Timer(1000);
timer.start();
}
void draw(){
background (137,112,112);
if(timer.isFinished()) {
alien[totalAlien]= new Alien();
totalAlien++;
if(totalAlien >= alien.length) {
totalAlien = 0;
}
timer.start();
}
for(int i=0; i< totalAlien;i++) {
alien[i].move();
alien[i].display();
if(ammo.intersect(alien[i])) {
alien[i].caught();
}
}
stroke(0);
fill(72,2,3);
rectMode(CENTER);
rect(mouseX,width-20,40,40);
if(ammo.fired) {
ammo.drawAmmo();
ammo.move();
}
text("score:"+score,100,20);
}
void mousePressed() {
// if (!ammo.fired) {
ammo.x = mouseX;
// ellipse(mouseX,width-30,40,40);
ammo.y = height-30;
ammo.fired = true;
//}
}
Tab 2 Alien
Code:
class Alien {
float x,y;
float speed;
color c;
float r;
Alien() {
r = 8;
x = random(width);
y= -r*4;
speed = (2);
c = color(50,100,150);
}
void move(){
y += speed;
}
boolean reachedBottom() {
if (y > height + r*4) {
return true;
} else {
return false;
}
}
void display() {
ellipseMode(CENTER);
rectMode(CENTER);
fill(255);
for(int i=2; i<r; i++)
ellipse(x,y+i*4,40,40);
fill(0);
for(int i=2; i<r; i++){
ellipse(x-19,y+i*4,10,26);
ellipse(x+19,y+i*4,10,26);
}
}
void caught() {
speed = 0;
y = - 10000;
if(ammo.fired){
score++;
}
}
}
Tab 3 Ammo
Code:
class Ammo {
float r;
color col;
float x,y;
float speed;
boolean fired = false;
Ammo (float tempR) {
r = tempR;
col = color(50,10,10,150);
x = 0;
y = 0;
speed = 23;
}
void drawAmmo() {
ellipse(x,y,r,r);
}
void move() {
y-=speed;
}
boolean intersect(Alien d) {
float distance = dist(x,y,d.x,d.y);
if (distance < r + d.r) {
return true;
} else {
return false;
}
}
}
Tab 4 Timer
Code:
class Timer {
int savedTime;
int totalTime;
Timer(int tempTotalTime) {
totalTime = tempTotalTime;
}
void start() {
savedTime = millis();
}
boolean isFinished() {
int passedTime = millis()- savedTime;
if (passedTime > totalTime) {
return true;
} else {
return false;
}
}
}