Nama anggota:
A'an
Hendy Nurfrianto
Muhammad eko darwanto
Disini saya kan menjelaskan secara singkatnya bagaimana kodingan ini akan bekerja dalam game tersebut :
///////////////////////////
|
// Question
class//
|
///////////////////////////
|
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(){
|
}
|
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, 2));
|
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 =
mouseCoordsOnClick[0];
|
float y =
mouseCoordsOnClick[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){
|
print("Correct
answer\n");
|
cars.speedUp();
|
}
|
else if(answers[0].isCorrect()==false){
|
print("Wrong
answer\n");
|
cars.slowDown();
|
}
|
// Set the flag to start a new
question
|
newQuestion = true;
|
}
|
}
|
Untuk question
atau pertanyaan dalam game disini memakai clas private mula mula deklarasikan
private int num1;
|
private int num2;
|
private int operator;
|
private int answer;
|
private String questionString;
|
Kemudian
buat operator disini untuk pertanyaan hanya menggunakan operator
private final String[] operators =
{"+", "-", "*", "/"};
|
Untuk pertanyaannya
akan dirandom berdasarkan angka dan hanya bisa menjawab 1 pertanyaan 1 klik
button sedangkan untuk menjawanya menggunakan kodingan:
//////////////////////////////
|
// Answer
class //
|
//////////////////////////////
|
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;
|
}
|
}
|
Di sini
dalam menjawab pertanyaannya menggunakan kodingan tipe Boolean dengan priotas
public dan jika jawaban benar maka car akan menambahkan jarak jika tidak atau
salah diakan menurunkan jaraknya
link video:
http://www.youtube.com/watch?v=hESaNfg8bmc&feature=youtu.be
Tidak ada komentar:
Posting Komentar