Little problem with my early tower defence
in
Programming Questions
•
6 months ago
Hello I'm doing a tower defence for a school project So far i've done a few good things and the basics are almost in place
but my problem is the following : I want the tower to attack only one "solider" ( here an ellipse) but after several fails I just can't figure out how to do it. right now if 40 balls will be in range 40 projectile will be fired... Well you got the idea.
So here is my code fully commented :
If you can help me with it I would be very pleased.
If you want to run it with its original background that i've commented here it is :
but my problem is the following : I want the tower to attack only one "solider" ( here an ellipse) but after several fails I just can't figure out how to do it. right now if 40 balls will be in range 40 projectile will be fired... Well you got the idea.
So here is my code fully commented :
- //x and y of the balls
int[] x = new int[251];
int[] y = new int [251];
// pos of the towers
int[] x_t = new int[251];
int[] y_t = new int[251];
// pos of the projectiles of the tower
int[] t_x = new int[2501];
int[] t_y = new int[2501];
int[] vie = new int[251]; // vie = life = life of the balls
float d = 0;
int[] v = new int[2501]; // speed of the projectiles
int[] zone = new int [251]; // define if the ball is or not in the range of a tower
int n = 0; // define the number of soliders
int i =0; // for the while who make turn the arrays
//PImage background_img; img of the background, no need
int rayon = 15; // dim/2 of the balls
// speed of the balls in x and y
int[] spX =new int[251];
int[] spY =new int[251];
int[] q= new int[251];
void setup() {
rectMode(CENTER); // for a better placement
// background_img = loadImage("back.png"); no need
for (int u = 0; u<=250; u++) { // fill the array
x[u] = 1500;
y[u] = 1500;
spX[u] = 3;
spY[u] = 3;
vie[u] = 20;
x_t[u] = -1500;
x_t[u] = -1500;
q[u]=0;
}
for (int u = 0; u<=2500; u++) { // fill the other array
v[u] = 0;
t_x[u] = -1500 ;
t_y[u] = -1500 ;
}
// pos of the towers
x_t[0] = 235;
y_t[0] = 75;
x_t[1] = 379;
y_t[1] = 417;
x_t[2] = 490;
y_t[2] = 75;
size(800, 600);
}
void draw() {
//background(background_img);
size(800, 600);
while ( i < 251) {
// func to print the towers, all the tower have by default -1500;1-1500 as coordinate so it print all the towers of the array until it reach a tower with default pos
if ( (x_t[i] != -1500)&&(y_t[i] != -1500)) {
rect(x_t[i], y_t[i], 20, 20);
noFill();
stroke(255);
ellipse(x_t[i], y_t[i], 200, 200);
stroke(0);
fill(255);
}
if ( (x[i] != 1500)&&(y[i] != 1500)) {// same thing with towers, balls have 1500;1500 as default
mouvement(); // mouvement = move, permit to move following a specific path
ellipse(x[i], y[i], 30, 30);
}
for (int k=0; k <= 2; k++) { // in order to calculate if the ball is in the range of the 3 towers we use the func 3 time for 3 towers( 50 if 50 towers for example)
tour_zone(k); // func to calculate if the tower is in range and if yes it fire
}
i++;
}
i=0;
}
void mousePressed() { // func to add a ball
if (mouseButton != CENTER) {
x[n] = rayon+5 ;
y[n] = 50 ;
n += 1;
}
}
void mouvement() { // func to move the ball following a specific path, nothing very interesting
if ( ( (x[i]>=0)&&(x[i] <180 ))&&(y[i]==50) ) {
x[i] +=spX[i];
}
if ( ( (x[i] >= 180)&&(x[i]<200 ))&&( (y[i] >= 50)&&(y[i]<470 )) ) {
y[i] +=spY[i];
}
if ( ( (x[i] >= 180)&&(x[i]<550 ))&&( (y[i] >= 470)&&(y[i]<=470 )) ) {
x[i] +=spX[i];
}
if ( ( (x[i] >= 545)&&(x[i]<=551 ))&&( (y[i] > 50)&&(y[i]<=470 )) ) {
y[i] -=spY[i];
}
if ( ( (x[i] >= 545)&&(x[i]<800 ))&&( (y[i]<=50 )) ) {
x[i] +=spX[i];
}
}
void tour_zone(int k) { // detect if ball is in range
float d = sqrt(sq(x[i]-x_t[k])+ sq(y[i]-y_t[k])); // math so check if the ball is in the area
if (d-(rayon) < 100) { // if yes
if (q[i]==0){ // to initialize the fireing process only one time, if not the projectile will stay too close to the turred because of
t_x[i] = x_t[k]; // this
t_y[i] = y_t[k]; // and this
q[i]=1;
} if(q[i]==1){
tour_tir(k);
}
} if (d-(rayon) <= 100+spY[i] && d-(rayon)>=100-spY[i]){ // if out of range don't fire
q[i]=0;
}
}
void tour_tir(int k) {//algorithm to direct the projectile torwards the ball and dissapear when it touche it
v[i] = 10;
if (t_x[i] < x[i] ) {
t_x[i] += v[i];
}
if (t_x[i]>x[i]) {
t_x[i] -= v[i];
}
if (t_y[i] < y[i] ) {
t_y[i] += v[i];
}
if (t_y[i]>y[i]) {
t_y[i] -= v[i];
}
if (t_x[i] <= x[i]+v[i] &&t_x[i] >= x[i] -v[i] && t_y[i] <= y[i]+v[i] && t_y[i]>= y[i]-v[i]) { // collision with the ball
q[i]=0;
vie[i] -= 1;
println(vie[i]);
if (vie[i] <= 0 ){
x[i] = 8000;
y[i] = 8000;
}
}
ellipse(t_x[i], t_y[i], 10, 10);// the projectile
}
If you can help me with it I would be very pleased.
If you want to run it with its original background that i've commented here it is :
1