我还在学习,当我输入一个新的句子时,我试图把一个句子一个字一个字地拆分,并试图使它在处理过程中位于画布的中心。我试图使用“for”循环来实现这一点,但我无法确定如何将X和Y坐标定位到循环中,然后循环将句子一个字一个字地拆分,并使其位于画布的中心。我可以手动将句子一个字一个字地拆分,但当我尝试更新成一个新句子时,它会搞砸。到目前为止,这是我所做的。
import javax.swing.JOptionPane;
String Answer; // FOR GET AN INPUT
String Secret = "Sponge"; // CORRECT ANSWER
int textposX, textposY; //X AND Y CO-ORDINATES OF THE TEXT
final int ansBoxHeight = 50; // HEIGHT OF THE ANSWER BOX
final int ansBoxWidth = 200; // WIDTH OF THE ANSWER BOX
int boxPosX, boxPosY; // ANSWER BOX X AND Y CO-ORDINATES
String riddle = "WHAT IS: FULL OF HOLES BUT STILL HOLDS WATER, WHAT AM I?";
void setup() {
size(500, 500);
textposX = width/2;
textposY = height/2;
}
void draw() {
answerBox();
riddle();
answerComparison();
}
void riddle() {
fill(0);
textSize(50);
for (int textposX = 0; textposX<=450; textposX = textposX+120) {
for (int textposY = 0; textposY>=450; textposY = textposY+100) {
text(riddle, textposX, textposY);
}
}
//text("WHAT IS:", textposX-100, textposY-200);
//text("FULL OF HOLES", textposX-150, textposY-90);
//text("BUT", textposX-50, textposY-50);
//text("STILL HOLDS", textposX-130, textposY-10);
//text("WATER?", textposX-70, textposY+30);
//text("WHAT AM I;", textposX-110, textposY+120);
}
void answerBox() {
noFill();
rect(boxPosX+150, boxPosY+440, ansBoxWidth, ansBoxHeight);
if (mouseX>boxPosX+150 && mouseX<boxPosX+150+ansBoxWidth+ansBoxHeight && mouseY>boxPosY+440 && mouseY<boxPosY+440+ansBoxWidth+ansBoxHeight && mousePressed) {
noLoop();
Answer = JOptionPane.showInputDialog("Correct Answer Is;");
rect(boxPosX+150, boxPosY+440, ansBoxWidth, ansBoxHeight);
text(Answer, textposX-70, textposY+230);
}
}
void answerComparison() {
if (Secret.equals(Answer) == true) {
fill(0);
background(0);
}
}
1条答案
按热度按时间k3bvogb11#
假设你有一个很好的小函数,它为你返回一个适当宽度的字符串数组列表:
现在,您可以将您的谜语字符串作为 splitText 方法的参数传递,并获得短字符串的ArrayList:
您的rider()现在应该只调用一个splitText()
最后的代码是: