2
I’m programming in the: http://alpha.editor.p5js.org
The exercise: whenever the user presses the 'A' key a random number is assigned between 1 and 6 as well as pressing the 'B' key. I need to compare these two numbers in order to figure out which one is bigger and assign 1 point to the user who primed the 'A' or 'B' key respectively.
What happens to me is that when I press the key, in the points part it increases the score infinitely, even before the second user has primed his key and the computer has made the comparison.
var s = "Pontos A";
var r = "Pontos B";
var d1 = 0;
var d2 = 0;
var pontosA = 0;
var pontosB = 0;
function setup() {
createCanvas(400, 400);
}
function keyPressed() {
if (keyCode === 65) {
d1 = 1 + int((6 - 1 + 1) * random());
} else if (keyCode === 66) {
d2 = 1 + int((6 - 1 + 1) * random());
}
return false;
}
function draw() {
if (d1 > d2) {
pontosA = pontosA + 1;
}
if (d2 > d1) {
pontosB = pontosB + 1;
}
background(220);
text(s, 50, 10, 70, 80);
text(r, 300, 10, 80, 80);
text(pontosA, 60, 50);
text(pontosB, 320, 50);
text(d1, 60, 100);
text(d2, 320, 100);
}
It worked, thanks for the fixes and improvements in the code!
– pedrocxb