5 Replies - 634 Views - Last Post: 09 June 2016 - 01:15 PM Rate Topic: -----

#1 jadon.smith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-April 16

Simple Racing Game timer problem

Posted 09 June 2016 - 11:47 AM

Hi,

I am a working on a simple racing game in processing (very similar to java) and im encountering a small problem. I have made a timer class in my code so that i can calculate the time elapsed once the game ends. However, for some reason the timer class is not calculating the time. Could you please check the code or suggest an alternate way.

Here is the code:

private Game game = new Game();
private Question question = new Question();
private Answer[] answers = new Answer[3];
private int wrongIncrement=0;
private int correctCount = 0;
// Coordinates for where to draw the buttons
private static final int[] buttonLocation = {40, 240, 440};

// Limits for generating the random numbers in the questions
private static final int[] limits = {1, 301};

// To store the coordinates of the mouse when clicked
private float[] mouseCoords = new float[2];

// Flag for whether we need a new question for the user
private boolean newQuestion = true;

// Flag for whether the game has been started
private boolean gameStarted = false;

// Flag for winning or losing the game
private int gameWon = 0;

// Create new object for the class Cars
private Cars cars = new Cars();

float x1 = 10;
float x2 = 10;
float speedIncrement = 0.4;

StopWatchTimer timer = new StopWatchTimer();
int start;
int ellapsed;

void setup()
{
frameRate(30);
size(600, 600);
smooth();

// Initialise the Answer objects
for(int i=0; i<3; i++){
answers[i] = new Answer();

// Set each answer box's x-coordinate
answers[i].setXCoord(buttonLocation[i]);
}
}

void draw()
{

// Draw background
fill(255);
rect(0, 0, width, 250);

// If the button has been pressed to start the game
if((gameStarted == true))
{
//timer.start();
// Game lost
if(gameWon==1)
{
  
timer.stop();
  
background(255);
fill(0);
textSize(35);
text("YOU LOST!", 215, 200);
textSize(10);
text("Number of Correct Answers: "+ correctCount + "Number of Wrong Answers: "+ wrongIncrement, 215, 400);
text(timer.getElapsedTime() + " "+ timer.second(), 215, 500);

}
else if(gameWon==2)
{
  
timer.stop();

background(255);
fill(0);
textSize(35);
text("YOU WON!", 215, 200);
textSize(10);
text("Number of Correct Answers: "+ correctCount + "Number of Wrong Answers: "+ wrongIncrement, 215, 400);
text(timer.getElapsedTime() + " "+ timer.second(), 215, 500);

}
if(gameWon==0)
{
// If a new question is needed (user answered)
if(newQuestion == true)
{
// No new questions until the user has answered
newQuestion = false;

// Draw background
fill(255);
rect(0, 250, width, 350);

// New question!
game.newQuestion();
}
cars.moveCars();
cars.drawCars();
cars.checkWin();
}
}
else
{
  
fill(0);
rect(20, 400, 150, 100);
fill(255);
textSize(25);
text("Easy", 70, 465);
  
fill(0);
rect(210, 400, 150, 100);
fill(255);
textSize(25);
text("Medium", 240, 465);
  
fill(0);
rect(400, 400, 150, 100);
fill(255);
textSize(25);
text("Hard", 450, 465);
  
  
}
}


void mouseClicked()
{
// Store the mouse's current coordinates
// for use in another method
if(gameStarted==true)
{
mouseCoords[0] = mouseX;
mouseCoords[1] = mouseY;
question.checkAnswer();

}
else
{

if (mouseX>0 && mouseX<175 && mouseY> 350 && mouseY< 600)
{
gameStarted = true;
speedIncrement = 0.9;

}
  
else if (mouseX>204 && mouseX<363 && mouseY> 350 && mouseY< 600)
{
gameStarted = true;
speedIncrement = 1.5;

}
  
else if (mouseX>393 && mouseX<550 && mouseY> 350 && mouseY< 600)
{
gameStarted = true;
speedIncrement = 3.0;

}
}
}

class Answer
{
// X-coordinate of the box to draw
private int xCoord;
// Value of the answer to display
private int value = 0;
// Whether this answer box is the 'correct' one
private boolean correct = false;

// Constructor
public void Answer()
{

}

// Draw the box in which the answer text sits
public void drawButton(){
fill(0);
rect(xCoord, 475, 125, 75);
}

// Draw the text onto the box
public void drawText(){
fill(255);
text(value, xCoord+35, 525);
}

// Mutator for the x-coordinate
public void setXCoord(int newXCoord){
xCoord = newXCoord;
}

// Mutator for the answer value
public void setValue(int newValue){
value = newValue;
}

// Accessor for the answer value
public int getValue()
{
return value;
}

// Mutator for the 'correct answer box' boolean
public void setCorrect(boolean isCorrect)
{
correct = isCorrect;
}

// Accessor for the 'correct answer box' boolean
public boolean isCorrect()
{
return correct;
}
}

class Cars
{
  
boolean value;
boolean correct;
boolean win;


public void drawCars()
{
  
fill(0,0,100);
rect(x1,120,35,20);
fill(100);
rect(x1+5,120-3,10,5);
rect(x1+20,120-3,10,5);
rect(x1+20,120+17,10,5);
rect(x1+5,120+17,10,5);
  
fill(0,0,100);
rect(x2,180,35,20);
fill(100);
rect(x2+5,180-3,10,5);
rect(x2+20,180-3,10,5);
rect(x2+20,180+17,10,5);
rect(x2+5,180+17,10,5);
//Draw two cars
//fill(#FF0000);
//rect(x1, 120, 50, 20);
//fill(#03FF04);
//rect(x2, 180, 50, 20);
  
// To indicate that the green car is the player's
fill(0);
textSize(1);
text("Player", x2+10, 195);
}

public void moveCars()
{
x1 = x1 + speedIncrement;
x2 += 0.25;
}

public void speedUp()
{
x2 += 15;
}

public void slowDown()
{
x2 -= 10;
}

public void checkWin()
{
if((x1>=550) || (x2 <=1))
{
gameWon=1;
}
else if(x2>=550)
{
gameWon=2;
}
}
}

class Game
{
// Constructor
public void Game()
{

}

// Sets up and initiates the Question and Answer classes,
// creating a new question and answer selection for the user
public void newQuestion()
{
int rand=0;
// Draw a new question to the screen
question.drawQuestion();

// Reset the 'correct' answer box
for(int i=0; i<3; i++)
{
answers[i].setCorrect(false);
}

// Randomly designate an answer box as the 'correct' one
answers[int(random(0, 3))].setCorrect(true);

for(int i=0; i<3; i++)
{
if(answers[i].isCorrect()==true)
{
// Give the 'correct' answer box the value of the answer that was worked out earlier
answers[i].setValue(question.getAnswer());
}
else
{
// Give the two other answer boxes random numbers in the possible range
do
{
rand = int(random(-149, 301));
} while(rand==question.getAnswer());
answers[i].setValue(int(random(-149, 301)));
}

// Draw the box that the answer sits in
answers[i].drawButton();

// Draw the answer text in the box
answers[i].drawText();
}
}
}

class Question
{
private int num1;
private int num2 ;
private int operator;
private int answer;
private String questionString;

// For use in building questionString
private final String[] operators = {"+", "-", "*", "/"};

// Constructor
public void Question()
{

}

// Used by drawQuestion to build a question to display to the
// user and set fields we'll need later such as the answer
private void createQuestion(){
// Create the two random numbers
num1 = int(random(limits[0], limits[1]));
num2 = int(random(limits[0], limits[1]));

// Select a random operator (multiplication and division aren't used)
int opSelect = int(random(0, 4));

switch(opSelect)
{
case 0:
answer = num1 + num2;
break;
case 1:
answer = num1 - num2;
break;
case 2: // Unused
answer = num1 * num2;
break;
case 3: // Unused
answer = num1/ num2;
break;
}
// Build the question string we want to display to the user
questionString = num1 + " " + operators[opSelect] + " " + num2 + " = ?";
}

// Create and display the question string to the user
public void drawQuestion()
{
createQuestion();
fill(0);
textSize(30);
text(questionString, 175, 300);
}

// Used for returning the correct answer to the random question
public int getAnswer(){
return answer;
}

// Use the mouse coordinates stored by the mouseclicked event handler
// and the known coordinates of the answer boxes to find which answer
// the user actually clicked on, returns -1 if no box was clicked.
public int getAnswerChoice()
{
// For simplicity
float x = mouseCoords[0];
float y = mouseCoords[1];

if((y>=475) && (y<=550))
{ // Check if mouse is on the right level (y-coordinates)
if((x>=40) && (x<=165))
{ // Leftmost box
//print("First answer\n");
return 0;
}
else if((x>=240) && (x<=365)){ // Middle box
//print("Second answer\n");
return 1;
}
else if((x>=440) && (x<=565))
{ // Rightmost box
//print("Third answer\n");
return 2;
}
else
{
return -1;
}
}
else
{
return -1;
}
}

// Check if the user's answer pick was the correct one
public void checkAnswer()
{
int choice = getAnswerChoice();
// A return value of -1 indicates that the user
// did not click within any of the answer boxes
if(choice==-1)
{
print("Not a button\n");
return;
}
else if(answers[choice].isCorrect()==true)
{
cars.speedUp();
print("Correct answer\n");
correctCount++;
  
}
else if(answers[0].isCorrect()==false)
{
cars.slowDown();
print("Wrong answer\n");
fill(0);
textSize(35);
wrongIncrement++;
  
}
// Set the flag to start a new question
newQuestion = true;
}
}

class StopWatchTimer
{
int startTime = 0, stopTime = 0;
boolean running = false;
void start() {
startTime = millis();
running = true;
}
void stop() {
stopTime = millis();
running = false;
}
int getElapsedTime()
{
int elapsed;
if (running)
{
elapsed = millis() - startTime;
}
else
{
elapsed = (stopTime - startTime);
}
return elapsed;
}
int second()
{
return (getElapsedTime() / 1000) % 60;
}
int minute() {
return (getElapsedTime() / (1000*60)) % 60;
}
int hour() {
return (getElapsedTime() / (1000*60*60)) % 24;
}
}

This post has been edited by andrewsw: 09 June 2016 - 12:04 PM
Reason for edit:: added [code][/code] tags


Is This A Good Question/Topic? 0
  • +

Replies To: Simple Racing Game timer problem

#2 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6380
  • View blog
  • Posts: 25,775
  • Joined: 12-December 12

Re: Simple Racing Game timer problem

Posted 09 June 2016 - 12:04 PM

Square brackets are not code tags:

Attached Image
Was This Post Helpful? 0
  • +
  • -

#3 jadon.smith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-April 16

Re: Simple Racing Game timer problem

Posted 09 June 2016 - 12:37 PM

View Postandrewsw, on 09 June 2016 - 12:04 PM, said:

Square brackets are not code tags:

Attachment codetags.gif

Could you please help with my problem
Was This Post Helpful? 0
  • +
  • -

#4 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: Simple Racing Game timer problem

Posted 09 June 2016 - 01:04 PM

Quote

the timer class is not calculating the time.

Can you post the program's current output, add some comments saying what is wrong with it and show what the output should be.
Was This Post Helpful? 0
  • +
  • -

#5 jadon.smith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-April 16

Re: Simple Racing Game timer problem

Posted 09 June 2016 - 01:13 PM

View PostNormR, on 09 June 2016 - 01:04 PM, said:

Quote

the timer class is not calculating the time.

Can you post the program's current output, add some comments saying what is wrong with it and show what the output should be.

The timer isn't stopping as the game ends. It keeps going on.

You'll have run the game and understand what I'm talking about

View Postjadon.smith, on 09 June 2016 - 01:11 PM, said:

View PostNormR, on 09 June 2016 - 01:04 PM, said:

Quote

the timer class is not calculating the time.

Can you post the program's current output, add some comments saying what is wrong with it and show what the output should be.

The timer isn't stopping as the game ends. It keeps going on.

You'll have run the game and understand what I'm talking about

Could you look at the StopWatch class and find out why its doing this
Was This Post Helpful? 0
  • +
  • -

#6 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: Simple Racing Game timer problem

Posted 09 June 2016 - 01:15 PM

How can the code be compiled and executed for testing?


Quote

find out why its doing this

How are you trying to debug the code? If you see what the statements are doing and what the values of the variables are, you may understand why there is a problem.

This post has been edited by NormR: 09 June 2016 - 01:16 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1