Hello for the ones that follow my posts a bit I'm making a tower defence and i'm doing very well i've almost finished all the algorithm
but I need to apply some texture to my rect and ellipses because right now it's pretty ugly
and I can't find what function I need to use to apply an img to a shape
do I need to use the fonction image and make it to appear at the same time as the ellipse so it will cover it and make like a texture but I find it a bit hard and bad so ^^
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 :
//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 ; }
// 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
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 :
Hello ! I've got 2 classes and one is using a variable of the other so I got a "The field component is not visible." I know very well why this is hapenning so i'm looking for a way to make a global variable.
here is the code is you want to read it.
import java.util.List; // actually, it is already imported, but not "activated"! // Constant field variables of the top class, the sketch itself! ;-) // an ArrayList which only stores instances of class Balle, and initial size = 128: final static List<Balle> balles = new ArrayList(0200); final static List<Tour> tours = new ArrayList(0200);
void setup() { size(600, 400); }
void draw() { background(0); size(600, 400); // this is called enhanced or for-each or even for-in loop // iterates all of the elements stored within variable balles // since balles only contains instances of class Balle // iterator b is declared as of type Balle to receive an element from it: for (Balle b: balles) b.script(); for (Tour t: tours) t.tour_script();
}
void mousePressed() { // if it's NOT a middle-cli ck, add a new instance of Balle: switch(mouseButton) {
case CENTER : if (balles.size() != 0) balles.remove(0); break; case LEFT : balles.add( new Balle() ); break; case RIGHT : tours.add( new Tour() ); } }
// classe balle ==================================================== public final class Balle { // fields of inner class Balle (instantiatable): public int x, y; byte spX, spY; int pdv; // static constant fields of inner class Balle (non-instantiatable): final static byte taille = 20, rayon = taille/2; // Balle() { // constructor of inner class Balle x = (short) mouseX; y = (short) mouseY; pdv = 30; // ternary operator ?: which demands 3 operands // if (random(2) < 1) multiplies by -1, otherwise by 1: spX = 3; spY = 3; }
void bump() { // multiplies spX/spY by -1 in case x/y is (off canvas - size/2 of Balle) // afterwards, adds current result of spX/spY into x/y:
if ( x < rayon) { spX *= -1; }
if (x> width-rayon) { spX *= -1; } if ( y <rayon) { spY *= -1; }
if (y > height-rayon){
spY *= -1; }
x += spX; y += spY;
}
void show() { ellipse(x, y,taille,taille); }
void script() { bump(); show(); } }
final class Tour{ int a,b; // pos tour int aire; //cercle de zone int m,n; //pos tir int v;//vitesse du tir int zone; //variable dite "soldat est dans la zone" int dmg; // dommages de la tour
Tour() {
dmg = 5; a = mouseX; b= mouseY; v=0; m=a; n=b; zone = 0; aire = 100; }
void tour_tir(){//v la vitesse de tir if (zone == 1){ v = 10; } if (zone == 0){ v = 0;
m = m + v; } if (m > x){ m = m - v; } if (n < y ){ n = n + v; } if (n>y){ n = n - v; } if ( m <= x+v && m >= x -v && n <= y+v && n>= y-v){ m = a; n = b; pdv -= dmg; } }
void soldat_mort(){ if (pdv < 0){ if (balles.size() != 0) balles.remove(0); } }
void draw() { nettoyer(); for (int i = 0; i < balles.size()-1; i++ ) { // here is when it's highlighted balle balle = (balle) balles.get(i); ball.rebond(); ball.show(); }