We are about to switch to a new forum software. Until then we have removed the registration on this forum.
As an assignement for school I have to create a simply game with dissapearing circles. When a circle's size equals 0, the game displays a gameover screen (loaded from void gameOver). The function void gameOver() consists of a reset of the score and an svg that's being loaded on the center of the screen.
However I made 5 different svgs I would want to include in the gameOver screen. I want to display only one of course but each time gameOver happens it should be a randomly chosen svg instead of just using shape(filename,etc).
I hope it's clear what I want. I've tried playing around with random(); but it didn't really help. I hope someone can help me.
Full paste of the code here:
PImage bg;
PShape beachball;
PShape folder;
PShape crash;
PShape crash1;
PShape crash2;
PShape crash3;
PShape crash4;
float angle;
void setup() {
size(1024, 768);
bg = loadImage("bg.jpg");
beachball = loadShape("beachball.svg");
folder = loadShape("folder.svg");
crash = loadShape("crash.svg");
crash1 = loadShape("crash1.svg");
crash2 = loadShape("crash2.svg");
crash3 = loadShape("crash3.svg");
crash4 = loadShape("crash4.svg");
}
float size = 100;
float wx = random(size, width-size);
float wy = random(size, height-size);
float afstand = dist(mouseX, mouseY, wx, wy);
int score = 0;
void draw() {
println(score);
if (size > 0) {
imageMode(CORNER);
image(bg, 0, 0);
shapeMode(CENTER);
pushMatrix();
translate(wx, wy);
rotate(angle);
shape(beachball, 0, 0, size, size);
angle += 0.1;
popMatrix();
size--;
}
if (size == 0) {
gameover();
}
scoreCounter();
}
void keyPressed() {
switch(key) {
case 'p':
noLoop();
break;
case 'r':
loop();
break;
}
}
void mouseClicked() {
float afstand = dist(mouseX, mouseY, wx, wy);
if (afstand<=size/2) {
circleGen();
score++;
}
}
void scoreCounter() {
switch(score) {
case 2:
shape(folder, 895, 95);
break;
case 4:
shape(folder, 895, 223);
break;
case 6:
shape(folder, 895, 350);
break;
case 8:
shape(folder, 895, 478);
break;
case 10:
shape(folder, 895, 606);
break;
}
}
void circleGen() {
size = 120;
wx = random(size, width-size);
wy = random(size, height-size);
}
void gameover() {
background(255);
score = 0;
shapeMode(CENTER);
pushMatrix();
translate(width/2, height/2);
scale(2.0);
shape(crash4, 0, 0);
popMatrix();
if(keyPressed){
key = ' ';
size = 100;
}
}
If you also want to see the 5 different svg's which has to be chosen of, I've uploaded them to Behance: behance.net/gallery/Beachball-Bch-Game/12723209
Answers
use an array rather than crash, crash1, crash2 etc
this lets you reference the items using an index - crash[0], crash[1] etc - one thing, many indexes, easier to handle than many things.
then you can use
crash[int(random(crash.length))]; // random crash
So instead of
I should use:
And then instead of
shape(crash1,0,0);
use:
crash[int(random(crash.length))];
?
The teacher only just yesterday learned us about arrays, I get the basic idea of what it does, but he only showed examples with int's.
all untested 8)
(if you'd named your images better you can use a for loop to load them. but given you've only 5 images it's probably not worth it
)
Ok so using that code snippet it worked but now when gameOver() appears the images keep scrolling. I need to get only 1 image when gameOver(), then when the game is restarted and again gameOver(), i need to get a different one.
But thanks for helping me, so grateful :)
ah, ok, it's still looping despite being gameover and it's choosing a different image every time it's drawn.
probably easiest is setting noLoop(); in gameover(). but then how to restart?
or add a global variable at the top, shapeIndex, say, set it to
int(random(crash.length))
once per game and then in gameover() useshape(crash[shapeIndex], 0, 0);