We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Random Array of Images
Page Index Toggle Pages: 1
Random Array of Images (Read 868 times)
Random Array of Images
Feb 3rd, 2010, 10:14am
 
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);
}







Re: Random Array of Images
Reply #1 - Feb 3rd, 2010, 10:15am
 
This is when I tried using a case function

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,myBug;


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("AgencyFB-Reg-16.vlw");
 textFont(font);
 for (int i = 0; i < num; i++) { // set initial positions and direction
   speedX[i] = int(random(5)+1);
   speedY[i] = int(random(5)+1);
   obj1_x[i] = int(random(width-30))+10;
   obj1_y[i] = int(random(height-30))+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 (50,200,50);
 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) {
   println (bugtype[i]);
switch(bugtype[i]) {
case 1:
 myBug = bug0;
 break;
case 2:
 myBug = bug1;
 break;
case 3:
 myBug = bug2;
 break;    
}
image(myBug, obj1_x[i], obj1_y[i]); //bug.bugtype[i]
   }
   //moveGun(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+=2;  //score counter
 }
 else {
   col = false;
 }
 return col;
}

// press any key to reset the background to white.
void keyPressed(){
 background(255,255,255);
}


Thanks in advance.
Re: Random Array of Images
Reply #2 - Feb 3rd, 2010, 10:25am
 
We can't really test your code (no tacTile, etc.) or spend time analyzing in detail your code.
At a quick glance, the switch version seems OK.
If you explained where you are stuck (got errors? got bad behavior?), we could help you more.
Re: Random Array of Images
Reply #3 - Feb 3rd, 2010, 11:06am
 
Well, it'll still run without tactile, I just can't get the the 3 separate images to load randomly.
Re: Random Array of Images
Reply #4 - Feb 4th, 2010, 1:21am
 
Actually, you don't even need a switch, just put the images in an array:
Code:
// PImage bug0,bug1,bug2,myBug;
PImage[] bugs = new PImage[3];

// bug0 = loadImage ("bug_1.png");
// bug1 = loadImage ("bug_2.png");
// bug2 = loadImage ("bug_3.png");
for (int i = 0; i < bugs.length; i++) {
bugs[i] = loadImage("bug_" + (i + 1) + ".png");
}

if (isAlive[i] == true) {
println (bugtype[i]);
/*
switch(bugtype[i]) {
case 1:
myBug = bug0;
break;
case 2:
myBug = bug1;
break;
case 3:
myBug = bug2;
break;
}
image(myBug, obj1_x[i], obj1_y[i]); //bug.bugtype[i]
*/
image(bugs[bugtype[i]], obj1_x[i], obj1_y[i]);
}
(untested)
Not sure why your version doesn't work.
Ah, yes, you test bugtype[i] in the 1..3 range while it is in the 0..1 range!
So, additional fix:
Code:
//bugtype[i] = int(random (0,2));
bugtype[i] = int(random(3));
Page Index Toggle Pages: 1