Adding pop-up messages and pictures

This one’s on a time crunch for a class, so I’d really appreciate help fast. Issue is, I’m a beginner trying to execute an idea overly ambitious for me, but now I really need to complete it for class. I’d appreciate any help possible. Thank you!

  1. How can I increase the number of yellow coins available?

  2. Also, how do I display a pop-up message randomly from a list of predefined messages? (eg. each time you get a coin, it says any one of the 20 messages from a defined list) Or maybe each time you collect a coin a one from a predefined set of pictures pops up for a few seconds?


int[] h = new int[500];

int[] hw = new int[500];

int[] x = new int[500];

int[] v = new int[500];

int[] vw = new int[500];

int[] y = new int[500];

float[] coinx = new float[4];

float[] coiny = new float[coinx.length];

boolean[] ctd = new boolean[coinx.length];

int points = 0;

int px, py;

boolean up = true, down = true, left = true, right = true;

int set = 50;

void setup() {

size(600, 600);

px = width/2-10;

py = height/2-10;

for (int i = 0; i < v.length; i++) {

v[i] = (int)random(width);

vw[i] = (int)random(set);

x[i] = (int)random(60)*50;

h[i] = (int)random(width);

hw[i] = (int)random(set);

y[i] = (int)random(50)*80;

}

for (int i = 0; i < coinx.length; i++) {

coinx[i] = (int)random(width);

coiny[i] = (int)random(height);

ctd[i] = true;

}

}

void draw() {

background(0);

for (int i = 0; i < coinx.length; i++) {

if (ctd[i]) {

stroke(255, 255, 0);

} else {

stroke(0);

}

strokeWeight(7);

point(coinx[i], coiny[i]);

if (ctd[i] && (dist(px, py, coinx[i], coiny[i]) <= 5)) {

ctd[i] = false;

points++;

}

if (coinx[i] < 0) {

coinx[i] = width;

coiny[i] = (int)random(height);

ctd[i] = true;

}

if (coinx[i] > width) {

coinx[i] = 0;

coiny[i] = (int)random(height);

ctd[i] = true;

}

if (coiny[i] < 0) {

coiny[i] = height;

coinx[i] = (int)random(width);

ctd[i] = true;

}

if (coiny[i] > height) {

coiny[i] = 0;

coinx[i] = (int)random(width);

ctd[i] = true;

}

}

for (int i = 0; i < v.length; i++) {

fill(0, 0, 128);

noStroke();

rect(x[i], v[i], 12, vw[i]);

rect(h[i], y[i], hw[i], 15);

if (x[i] + 15 < 4) {

x[i] = width;

v[i] = (int)random(width);

vw[i] = (int)random(set);

}

if (h[i] + hw[i] < 0) {

h[i] = width;

hw[i] = (int)random(set);

y[i] = (int)random(20)*30;

}

if (x[i] > width) {

x[i] = -15;

v[i] = (int)random(width);

vw[i] = (int)random(set);

}

if (h[i] > width) {

hw[i] = (int)random(set);

y[i] = (int)random(20)*30;

h[i] = -hw[i];

}

if (v[i] + vw[i] < 0) {

v[i] = height;

vw[i] = (int)random(set);

x[i] = (int)random(20)*30;

}

if (y[i] + 15 < 0) {

y[i] = height;

h[i] = (int)random(width);

hw[i] = (int)random(set);

}

if (v[i] > height) {

vw[i] = (int)random(set);

x[i] = (int)random(20)*30;

v[i] = -vw[i];

}

if (y[i] > height) {

y[i] = -15;

h[i] = (int)random(width);

hw[i] = (int)random(set);

}

strokeWeight(3.5);

stroke(255);

point(px, py);

for (int e = 0; e < x.length; e++) {

if (((px == h[e]) && (py <= y[e] + 15) && (py >= y[e])) || ((px == x[e]) && (py <= v[e] + vw[e]) && (py >= v[e]))) {

right = false;

}

if (((px == h[e] + hw[e]) && (py <= y[e] + 15) && (py >= y[e])) || ((px == x[e] + 15) && (py <= v[e] + vw[e]) && (py >= v[e]))) {

left = false;

}

if (((py == v[e]) && (px <= x[e] + 15) && (px >= x[e])) || ((py == y[e]) && (py <= h[e] + hw[e]) && (py >= h[e]))) {

down = false;

}

if (((py == v[e] + vw[e]) && (px <= x[e] + 15) && (px >= x[e])) || ((py == y[e] + 15) && (py <= h[e] + hw[e]) && (py >= h[e]))) {

up = false;

}

}

if (keyPressed && (key == CODED)) {

if (up && (keyCode == UP)) {

y[i] += 1;

v[i] += 1;

for (int e = 0; e < coiny.length; e++) {

coiny[e] += 0.002;

}

}

if (down && (keyCode == DOWN)) {

y[i] -= 1;

v[i] -= 1;

for (int e = 0; e < coiny.length; e++) {

coiny[e] -= 0.002;

}

}

if (left && (keyCode == LEFT)) {

x[i] += 1;

h[i] += 1;

for (int e = 0; e < coinx.length; e++) {

coinx[e] += 0.002;

}

}

if (right && (keyCode == RIGHT)) {

x[i] -= 1;

h[i] -= 1;

for (int e = 0; e < coinx.length; e++) {

coinx[e] -= 0.002;

}

}

}

up = true;

down = true;

left = true;

right = true;

}

fill(0);

strokeWeight(2);

stroke(255);

rect(500, 25, 60, 60);

fill(255);

textSize(30);

text(points, 522, 65);

textSize(40);

text("", 30, 65);

}

Hi srishtiverma,

To increase the number of coins, I would advise you to change the data type of your coins variable. Instead of using a basc array, consider using an arrayList

For the pop-up message, you need to have an array with all the messages possible in it. Then get an index between 0 and the max index of your array using the random() function.

1 Like
String[] msg_messages = {
  "You got a coin!", 
  "Oooo! Shiny thing!", 
  "Cha-ching!", 
  "Gold coins are awesome!", 
  "Eat at Joe's Crab Hut!", 
  "Comsume! Spend money!", 
  "This is a yellow circle, not a coin.", 
  "Oh hey, look at that, you found a thing.", 
  "Make sure your messages fit!", 
  "YOUR MESSAGE HERE", 
  "ERROR: MESSAGE NOT FOUND (704)", 
};
int msg_time = 0;
boolean msg_display = false;
int msg_which = 0;
boolean[] msg_seen = new boolean[msg_messages.length];
int msg_seen_count = 0;

void message() {
  //if( msg_display ){ return; }
  if ( msg_seen_count == msg_messages.length ) {
    return; // All messages seen, no need to display any more.
  }
  msg_which = int(random(msg_messages.length));
  if ( msg_seen[msg_which] ) { // Oops! We've seen this one already.
    message(); // Try again!
    return;
  }
  msg_seen_count++;
  msg_seen[msg_which] = true;
  msg_time = millis() + 3000;
  msg_display = true;
}

void draw_message_maybe() {
  if ( !msg_display ) return;
  if ( millis() > msg_time ) {
    msg_display = false;
  }
  pushStyle();
  textSize(28);
  fill(0);
  stroke(255);
  rect(20, height - 60, width-40, 40);
  fill(255);
  text( msg_messages[msg_which], 30, height - 30);
  popStyle();
}

void setup() {
  size(600, 400);
}

void draw() {
  background(64);
  draw_message_maybe();
}

void mousePressed() {
  message();
}

I wrote you a pop-up message thing. It’s pretty plug-and-play, so credit me if you like or promise to understand it when you have time.

I should have put it in a class for the namespace. Hrm.

2 Likes

Actually, because of the way that he’s tracking objects and re-randomizing them once a coin leaves the visible window, all that you need to change to have more coins is just to increase the size of the array that tracks coin X positions. The rest of the code bases the number of coins off that already. Try editing this line:

float[] coinx = new float[4];

to some larger value:

float[] coinx = new float[40];
1 Like

Thank you so much you two for responding so soon @TfGuy44 @jb4x! It’s a huge help.

@TfGuy44 , I’m going to both credit you and also learn as soon as possible. :smiley: I’m sorry but I’ve got a really silly follow up question, I tried putting this at the end of the code but it says “Error on void” and “unexpected token:void”. How can that be resolved?

Also is there some way I can make the maze itself a few mixed colours rather than just one?

The code for the message system is a stand-alone thing. To put it into your code, remove the setup() and draw() functions, put a call to draw_message_maybe() at the end of your own draw() function, change it to use your own messages, and then call message() whenever you pick up a coin.

Ok, that might be less plug and play than I indicated.


Since each of your maze walls are represented by arrays of variables, you will need to add a new array that records their colors too. Then when you draw each wall, use the color from that array that is in the same index as the wall. Also, when a wall moves, you could give it as new color.

You might also just consider changing a wall’s color based on it’s position. The map() function might be good for this - you can map, say, the X position of a wall to the amount of red and the Y position to the amount of blue in its color.


You should also consider switching to using classes. Had you been using them from the start, it would make doing changes to things (like, say, adding colors to your walls) much easier.

It’s not too late. Start with a Wall and a Coin class

1 Like