Play a game for a predefined time
in
Programming Questions
•
3 months ago
Hello everybody,
I have made a (simple) game in processing and I want it to be played for a predefined time, let's say 1 minute. So people have to click on highlighted arrows and I want the game to automatically stop after 1 minute (and preferably have a "play again" button, but that is for the next step).
I am really bad at programming and the code I have now only exist with help from a colleague and adding and adjusting existing code from examples.
Thank you very much for your help!
The code I have now is as follows:
----------------------------------------------------------
import ddf.minim.*;
Minim minim;
AudioPlayer au_player;
int start_time;
int mouseClicks;
boolean arrow_shown;
// Arrow values
int[] arrows_x = new int[6];
int[] arrows_y = new int[6];
boolean[] arrows_on = new boolean[6];
int[] arrows_orientation = new int[6];
// Drawing arrow defaults
int arrow_height;
int arrow_width;
// Variables for showing text
boolean showText;
boolean showHit;
void setup(){
size(1000, 600);
background(255);
start_time = millis();
arrow_shown = false;
// Set arrow defaults
arrow_height = 80;
arrow_width = 60;
// Set default arrows
resetArrows();
// Set text defaults
showText = false;
showHit = false;
// Load sound file
minim = new Minim(this) ;
au_player = minim.loadFile("hitCan.mp3") ;
}
void draw(){
// Clear canvas
background(255);
fill(0);
ellipse(810,470,100,100);
fill(0,255,0);
text(mouseClicks+"", 800,450,width,height);
// Calculate the amount of milliseconds that have passed
int past_milliseconds = millis()-start_time;
// Once 2 seconds have passed, print the random arrow number and reset the start time
if(past_milliseconds >= 2000) {
// Is an arrow shown?
if(arrow_shown == false){
// Choose the index of the random arrow, which can be between 0 and 5
int random_arrow = int(random(5));
arrows_on[random_arrow]=true;
arrow_shown=true;
}
}
// Once 5 seconds have passed
if(past_milliseconds >= 5000){
// Reset arrows to default
resetArrows();
// Start time to current time
start_time = millis();
// Arrow is not shown anymore
arrow_shown = false;
// Text is not shown anymore
showText = false;
showHit = false;
}
// Draw arrows
for(int index=0; index<6; index++){
drawArrow(arrows_x[index],arrows_y[index],arrows_on[index],arrows_orientation[index]);
}
// Show a message if a person hit something
showHitMiss();
}
void resetArrows(){
// Adjust X and Y here
int[] tmp_arrows_x = {250,400,500,700,500+arrow_width,400+arrow_width};
int[] tmp_arrows_y = {325,125,125,345-arrow_height,475,475};
boolean[] tmp_arrows_on = {false,false,false,false,false,false};
int[] tmp_arrows_orientation = {270,0,0,90,180,180};
for(int index=0; index<6; index++){
arrows_x[index]=tmp_arrows_x[index];
arrows_y[index]=tmp_arrows_y[index];
arrows_on[index]=tmp_arrows_on[index];
arrows_orientation[index]=tmp_arrows_orientation[index];
}
}
void mousePressed() {
/*Once the mouse has been pressed, check if it falls within an arrow*/
// Hard code, no need to look at this part until the IF with mouseX and mouseY
for(int index=0; index<6; index++) {
int start_x = arrows_x[index]+arrow_width/2;
int start_y = arrows_y[index];
int rotation_x = arrows_x[index]+arrow_width/2;
int rotation_y = arrows_y[index]+arrow_height/2;
start_x = start_x - rotation_x;
start_y = start_y - rotation_y;
int sin_angle = int(sin(radians(arrows_orientation[index])));
int cos_angle = int(cos(radians(arrows_orientation[index])));
int rot_x = cos_angle*start_x - (sin_angle*start_y);
int rot_y = sin_angle*start_x + cos_angle*start_y;
int new_x = rot_x + rotation_x;
int new_y = rot_y + rotation_y;
int[] bounding_x = new int[2];
int[] bounding_y = new int[2];
float tmp_span = arrow_width*0.25;
int span = int(tmp_span);
if(arrows_orientation[index]== 0){
bounding_x[0] = new_x - span;
bounding_x[1] = new_x + span;
bounding_y[0] = new_y;
bounding_y[1] = new_y + arrow_height;
}
else if(arrows_orientation[index]== 90){
bounding_x[0] = new_x - (2*arrow_height);
bounding_x[1] = new_x - arrow_height;
bounding_y[0] = new_y - span;
bounding_y[1] = new_y + span;
}
else if(arrows_orientation[index]== 180){
bounding_x[0] = new_x - arrow_width - span;
bounding_x[1] = new_x - arrow_width + span;
bounding_y[0] = new_y - (2*arrow_height);
bounding_y[1] = new_y - arrow_height;
}
else if(arrows_orientation[index]== 270){
bounding_x[0] = new_x;
bounding_x[1] = new_x + arrow_height;
bounding_y[0] = new_y - arrow_width - span;
bounding_y[1] = new_y - arrow_width + span;
}
// As soon as we know where we are allowed to click, do this!
if(mouseX >= bounding_x[0] && mouseX <= bounding_x[1] && mouseY >= bounding_y[0] && mouseY <= bounding_y[1]){
showText = true;
if(arrows_on[index] == true){
// println("HIT THE ARROW!! :D");
showHit = true;
showHitMiss();
arrows_on[index]=false;
// count how many times clicked at the right arrow
mouseClicks++;
}
else {
showHit = false;
// println("Wrong arrow :(");
}
}
}
}
void showHitMiss() {
if(showText)
{
au_player.play() ;
au_player.rewind( );
//minim.loadFile ("Raak.WAV");
textSize(32);
if(showHit) {
fill(50,200,100);
text("RAAK", 435,310);
}
else {
fill(255,0,50);
text("MIS", 450,310);
}
}
}
void drawArrow(int x, int y, boolean on, int orientation) {
/* Code to draw an arrow using the variables given */
pushMatrix();
// Translation
translate(x,y);
// Rotation
rotate(radians(orientation));
smooth();
noStroke();
// If should be shown, draw yellow arrow
if(on){
fill(255, 170, 0);
beginShape();
vertex(arrow_width/2, 0); // Top
vertex(arrow_width, arrow_height/2); // Right outer corner
vertex(3*arrow_width/4, arrow_height/2); // Right top of base
vertex(3*arrow_width/4, arrow_height); // Right bottom of base
vertex(arrow_width/4, arrow_height); // Left bottom of base
vertex(arrow_width/4, arrow_height/2); // Left top of base
vertex(0, arrow_height/2); // Left outer corner
endShape(CLOSE);
}
// Draw black arrow
else {
fill(0);
beginShape();
vertex(arrow_width/2, 0); // Top
vertex(arrow_width, arrow_height/2); // Right outer corner
vertex(3*arrow_width/4, arrow_height/2); // Right top of base
vertex(3*arrow_width/4, arrow_height); // Right bottom of base
vertex(arrow_width/4, arrow_height); // Left bottom of base
vertex(arrow_width/4, arrow_height/2); // Left top of base
vertex(0, arrow_height/2); // Left outer corner
endShape(CLOSE);
}
popMatrix();
}
1