#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# Como poner una imagen en un lugar aleatorio

edited February 2017

|| Hola, soy un estudiante que esta intentando hacer una "Primitiva" en la que hay 50 casillas numeradas y se tiene que poner 5 "X" (Imagen) de forma aleatoria en 5 casillas. He estado indagando pero no he llegado a comprender como hacerlo. Si me pudiese alguien ayudar sería GENIAL. Muchas gracias por prestar atención a esto y gracias por la ayuda.

|| Hello, I am a student who is trying to make a "Primitiva" in which there are 50 numbered boxes and you have to put 5 "X" (Image) randomly in 5 boxes. I have been inquiring but I have not come to understand how. If anyone could help me, it would be GREAT. Thank you very much for paying attention to this and thanks for the help.

Tagged:

• Please post your code as in copy and pate. No screenshots of your code. Hard to read, we cannot reproduce your output and ppl in the forum won't be able to find it using the search engine.

Kf

• edited February 2017

IT WILL BE HARD TO UNDERSTAND BECAUSE IT IS ALL TOGETHER

int ANCHO = 55; int ALTO = 45; color FONDO = color(195, 252, 184); color RECTANGULO = color(255, 255, 255); color LETRA = color(0, 0, 0); float POSX; float POSY; int MI_NUMERO; PImage cruz;

int np[] = new int[6]; void load_np(){ np [0] = 8; np [1] = 15; np [2] = 22; np [3] = 30; np [4] = 47; np [5] = 46;

} }

void setup(){ size (400, 720); POSX = 0.0; POSY = 0.0; MI_NUMERO = 1; cruz = loadImage ("imagen_x.png"); cruz.resize (ANCHO-10, ALTO-10); // cargamos los numero premiados load_np(); } void draw(){ if (MI_NUMERO<50){ background (FONDO); for(int FILAS = 0; FILAS<10; FILAS++){ for(int COLUMNAS = 0; COLUMNAS<5; COLUMNAS++){ //calcula la psicion x e y para rectangulo y letra POSX = 20+(COLUMNAS70); POSY = 30+(FILAS70); //pinto rectangulo fill (RECTANGULO); rect(POSX,POSY,ANCHO,ALTO,7); //pinto la letra textSize(30); fill (LETRA); text(MI_NUMERO, POSX + ANCHO/3, POSY + ALTO/1.5); if (esPremio(MI_NUMERO)){ image (cruz, POSX + ANCHO/8, POSY + ALTO/8); } //INCREMENTO LA VARIABLE NUMERO A MI ANTOJO MI_NUMERO++; } } } if (tecla == 0) { tecla = 255; } else { tecla = 0; }
}

// boolean es aleatorio boolean esPremio (int numero) { for (int i = 0; i < np.length; i++){ if (np[i] == numero) { return true; } } return false ; }

• Format your code. Press the gear icon to edit your post, then select your code and press ctrl+o. Leave a line above and below your code.

Kf

• Check the curling brackets for load_np(). You have an extra one there.

Kf

Check the changes and the added code.

Kf

``````int ANCHO = 55;
int ALTO = 45;
color FONDO = color(195, 252, 184);
color RECTANGULO = color(255, 255, 255);
color LETRA = color(0, 0, 0);
float POSX;
float POSY;
PImage cruz;

int[] np;

np = new int[6];
np [0] = 8;
np [1] = 15;
np [2] = 22;
np [3] = 30;
np [4] = 47;
np [5] = 46;
}

void setup() {
size (400, 720);
POSX = 0.0;
POSY = 0.0;

cruz = loadImage ("imagen_x.png");
cruz.resize (ANCHO-10, ALTO-10); // cargamos los numero premiados
}

void draw() {

int MI_NUMERO=1;

if (MI_NUMERO<50) {
background (FONDO);
for (int FILAS = 0; FILAS<10; FILAS++) {
for (int COLUMNAS = 0; COLUMNAS<5; COLUMNAS++) {
//calcula la psicion x e y para rectangulo y letra
POSX = 20+(COLUMNAS*70);
POSY = 30+(FILAS*70); //pinto rectangulo
fill (RECTANGULO);
rect(POSX, POSY, ANCHO, ALTO, 7); //pinto la letra
textSize(30);
fill (LETRA);
text(MI_NUMERO, POSX + ANCHO/3, POSY + ALTO/1.5);
if (esPremio(MI_NUMERO)) {
image (cruz, POSX + ANCHO/8, POSY + ALTO/8);
} //INCREMENTO LA VARIABLE NUMERO A MI ANTOJO
MI_NUMERO++;
}
}
}
}

void mousePressed() {
}

for (int p=0; p<np.length; p++)
np[p]=int(random(50));
}

// boolean es aleatorio
boolean esPremio (int numero) {
for (int i = 0; i < np.length; i++) {
if (np[i] == numero) {
return true;
}
}
return false ;
}
``````
• Thank you very much!!!!