I'm programming on: link
The exercise: whenever the user presses the 'A' key a random number between 1 and 6 is assigned as well as if the 'B' key is pressed. I need to make a comparison between these two numbers in order to figure out which is the largest and assign 1 point to the user who pressed the 'A' or 'B' key respectively.
What happens to me is that when I press the key, the points part increases the score infinitely, even before the second user has pressed 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);
}