我试图重新创建井字游戏,以再次使用Processing。我记得在将函数放入keyPressed()ifs之前,该程序运行良好,每次用户在控制台中输入数字时,都会在控制台中输入文本行。现在,每次我按下按键时,都不会显示任何内容,甚至不会显示我分配的错误消息,以防用户决定在数字之外键入任何内容。现在我不确定哪里出了问题。由于我并非完全擅长编写Processing,因此我不确定如何解决此错误。任何帮助,将不胜感激。谢谢。
代码:
String pos = "";
boolean isx = true;
boolean onepressed = false;
boolean twopressed = false;
boolean threepressed = false;
boolean fourpressed = false;
boolean fivepressed = false;
boolean sixpressed = false;
boolean sevenpressed = false;
boolean eightpressed = false;
boolean ninepressed = false;
void setup() {
size(1000,1000);
frameRate(60);
background(255,255,255);
smooth();
noStroke();
}
void keyPressedUpdate() {
if (isx == true) {
print("An x has been placed in the ");
print(pos);
print(" of the board");
print("Currently x's turn");
isx = false;
} else if (isx == false) {
print("An o has been placed in the ");
print(pos);
print(" of the board");
print("Currently o's turn");
isx = true;
}
}
void keyPressed() {
if (key == '1' && onepressed == false) {
pos = "top-left corner";
keyPressedUpdate();
onepressed = true;
} else if (key == '2' && twopressed == false) {
pos = "top-center square";
keyPressedUpdate();
twopressed = true;
} else if (key == '3' && threepressed == false) {
pos = "top-right corner";
keyPressedUpdate();
threepressed = true;
} else if (key == '4' && fourpressed == false) {
pos = "center-left square";
keyPressedUpdate();
fourpressed = true;
} else if (key == '5' && fivepressed == false) {
pos = "center square";
keyPressedUpdate();
fivepressed = true;
} else if (key == '6' && sixpressed == false) {
pos = "center-right square";
keyPressedUpdate();
sixpressed = true;
} else if (key == '7' && sevenpressed == false) {
pos = "bottom-left corner";
keyPressedUpdate();
sevenpressed = true;
} else if (key == '8' && eightpressed == false) {
pos = "center-bottom square";
keyPressedUpdate();
eightpressed = true;
} else if (key == '9' && ninepressed == false) {
pos = "bottom-right corner";
keyPressedUpdate();
ninepressed = true;
} else {
print("Please enter a number, not a letter or symbol.");
}
}
答案 0 :(得分:0)
如果您正在使用keyPressed()
之类的事件函数,则即使没有实际绘制任何内容,也需要具有draw()
函数。
换句话说,添加一个draw()
函数:
void draw(){}
现在,您将在控制台中看到打印语句。