user input to generate random numbers
in
Programming Questions
•
2 years ago
Hi Processing World! I'm new to the Processing language, and currently I'm programming a VERY simple video game. It's a basic math game, that has two random numbers pop-up and the user types in numbers that equal the sum of those two questions- resulting in a score increase or decrease. My problem is that I once the user has pressed ENTER and the question was properly answered, a new set of numbers would automatically pop-up to be answered. I understand where the coding needs to be placed, but I don't know if I've been staring at the screen for too long but I simply have over complicated such a minor detail and I'm in search for some help!!! Below is just the coding for the math question, which eventually will be place with the rest of the game. I'm hoping someone can help me out please!!!!!
PFont helvetica;
// question 1
int r1= int(random(100));
int r2= int(random(100));
int answer = int(r1+r2);
String numbers = "";
void setup() {
size(600, 200);
loop();
helvetica = loadFont("HelveticaNeue-UltraLight-48.vlw");
textFont(helvetica);
}
void draw() {
background(255);
stroke(8);
noFill();
rect(50, 30, 475, 100);
textFont(helvetica);
fill(0);
text(r1, 100, 100);
text(r2, 250, 100);
text('+', 185, 100);
text('=', 350, 100);
text(numbers, 400, 100);
}
void keyPressed() {
numbers= numbers.toLowerCase();
println(numbers);
if((key>='0')&&(key<='9')) {
numbers+=key;
println(numbers);
}
if(key== BACKSPACE) {
if(numbers.length() > 0) {
numbers= numbers.substring(0, numbers.length()-1);
}
}
else if (textWidth(numbers+key) < width) {
}
if(key == ENTER) {
println("user has entered answer");
if(answer == int(numbers)) {
println("CORRECT");
}
else {
println("wrong");
}
numbers = "";
}
}
PFont helvetica;
// question 1
int r1= int(random(100));
int r2= int(random(100));
int answer = int(r1+r2);
String numbers = "";
void setup() {
size(600, 200);
loop();
helvetica = loadFont("HelveticaNeue-UltraLight-48.vlw");
textFont(helvetica);
}
void draw() {
background(255);
stroke(8);
noFill();
rect(50, 30, 475, 100);
textFont(helvetica);
fill(0);
text(r1, 100, 100);
text(r2, 250, 100);
text('+', 185, 100);
text('=', 350, 100);
text(numbers, 400, 100);
}
void keyPressed() {
numbers= numbers.toLowerCase();
println(numbers);
if((key>='0')&&(key<='9')) {
numbers+=key;
println(numbers);
}
if(key== BACKSPACE) {
if(numbers.length() > 0) {
numbers= numbers.substring(0, numbers.length()-1);
}
}
else if (textWidth(numbers+key) < width) {
}
if(key == ENTER) {
println("user has entered answer");
if(answer == int(numbers)) {
println("CORRECT");
}
else {
println("wrong");
}
numbers = "";
}
}
1