I'm writing a game for a class and I thought about switching up images so the targets don't look the same. However, I can't seem to figure it out. My instructor suggested me to use the case function, but I can't seem to figure it out.
I was thinking of having an int variable that is selecting a random number between 0-2 and having it swap out with the image name. In this attempt, which is bugtype.
Code:
//required imports for tacTile
import processing.net.*;
import tacTile.net.*;
TouchAPI tacTile;
//boolean to determine which Touch Server connected to.
boolean connectToTacTile = true;
//Names of machines you might use
String localMachine = "insert your IP address here";
String tacTileMachine = "tactile.evl.uic.edu";
//Port for data transferf
int dataPort = 7100;
int msgPort = 7340;
int num = 30; // number of objects and bullets
int[] obj1_x = new int[num];
int[] obj1_y = new int[num];
float[] obj2_x = new float[num];
float[] obj2_y = new float[num];
int [] bugtype = new int[num];
//float obj2_x, obj2_y;
boolean[] isAlive = new boolean [num]; //state of ellipses, true in the beginning
boolean col;
int k = 0;
int[] speedX = new int[num];
int[] speedY = new int[num];
PFont font;
int score; //score variable
PImage bug0,bug1,bug2;
void setup() {
if ( connectToTacTile ){
//ALTERNATIVE: constructor to setup the connection on TacTile
//tacTile = new TouchAPI( this );
//Create connection to Touch Server
tacTile = new TouchAPI( this, dataPort, msgPort, tacTileMachine);
tacTile.log_on(true);
//System.out.println(tacTile.log);
//size of the screen
size( screen.width, screen.height );
}
else {
//ALTERNATIVE: constructor to setup the connection LOCALLY on your machine
//tacTile = new TouchAPI( this, 7000);
//Create connection to Touch Server
tacTile = new TouchAPI( this, dataPort, msgPort, localMachine);
//size of the screen
size( screen.width, screen.height );
}
//tacTile.toggleLogging();
bug0 = loadImage ("bug_1.png");
bug1 = loadImage ("bug_2.png");
bug2 = loadImage ("bug_3.png");
//color of the background
ellipseMode(CENTER);
noStroke();
font = loadFont("BlueHighwayDType-48.vlw");
textFont(font);
for (int i = 0; i < num; i++) { // set initial positions and direction
speedX[i] = int(random(3)+1);
speedY[i] = int(random(3)+1);
obj1_x[i] = int(random(width-20))+10;
obj1_y[i] = int(random(height-20))+10;
isAlive[i] = true; //state of ellipses
bugtype[i] = int(random (0,2)); //bugtype
}
smooth();
}
void draw() {
tacTile.process();
ArrayList newDown = tacTile.getTouchesDown();
for(int j = 0; j < newDown.size(); j++){
obj2_x[j] = -2000;
obj2_y[j] = -2000;
}
background(255);
fill (0);
text ("score: "+score, 10, 20);
fill (0);
//walls
for (int i = 0; i < num; i++) {
obj1_x[i]+=speedX[i];
obj1_y[i]+=speedY[i];
if (obj1_x[i] >= (width-25) || obj1_x[i] <= 25) { //horizontal axis
speedX[i]=-speedX[i];
}
if (obj1_y[i] <= 25 || obj1_y[i] >= (height-25)) { //vertical axis
speedY[i]=-speedY[i];
}
//move away from finger
if (abs(obj1_x[i] - obj2_x[i]) <= 25 && abs(obj1_y[i] - obj2_y[i]) <= 25) { //horizontal axis
speedX[i]=-speedX[i];
speedY[i]=-speedY[i];
}
if (isAlive[i] == true) image(bug2, obj1_x[i], obj1_y[i]); //bug.bugtype[i]
for (int j = 0; j < num; j++) {
if (collision(i,j) == true) {
isAlive [i] = false;
obj1_x[i] = width + 120; //int(random(width-30))+10;
obj1_y[i] = height + 120; //int(random(height-30))+10;
}
}
if( !newDown.isEmpty() ){
for(int j = 0; j < newDown.size(); j++){
Touches touch = ((Touches) newDown.get(j));
println("New Down: " + touch.getFinger() + " at " + touch.getXPos() + ", " + touch.getYPos());
int finger = touch.getFinger();
fill (255,0,0);
obj2_x[j] = 1920 * touch.getXPos();
obj2_y[j] = 1080 - touch.getYPos() * 1080;
ellipse(obj2_x[j], obj2_y[j], 100, 100);
fill (0);
}
}
}
/*
if( !newDown.isEmpty() ){
for(int i = 0; i < newDown.size(); i++){
Touches touch = ((Touches) newDown.get(i));
println("New Down: " + touch.getFinger() + " at " + touch.getXPos() + ", " + touch.getYPos());
int finger = touch.getFinger();
fill (255,0,0);
obj2_x[i] = 1920 * touch.getXPos();
obj2_y[i] = 1080 - touch.getYPos() * 1080;
ellipse(1920 * touch.getXPos(), 1080 - touch.getYPos() * 1080, 20, 20);
}
}
*/
}
boolean collision(int i, int j) {
if (abs(obj1_x[i]-obj2_x[j]) <= 50 && abs(obj1_y[i]-obj2_y[j]) <= 50) {
col = true;
//obj2_y[k] = -5; //bullet reset, firing bullet off screen
k += 1;
if (k >= num) k=0;
score+=1; //score counter
}
else {
col = false;
}
return col;
}
// press any key to reset the background to white.
void keyPressed(){
background(255,255,255);
}