我正在使用称为处理的软件对在屏幕上向下移动的正方形进行编码。但是,我希望我的方块每次按空格键都停止,并在再次按空格键时移动。另外,我如何使我的方块以8个相等的步幅移动?
这是我的代码:
int x = 0;
int y = 0;
int dy = 1;
void setup() {
size(1280, 720);
surface.setResizable(true);
}
void draw() {
background(240, 240, 240);
fill(255, 147, 79);
rect(x, y, 90, 90);
y += dy;
if(y + 90 > height || y < 0) {
dy *= -1;
}
}
您将需要实现一个KeyListener。
https://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyListener.html
我不确定您对第二部分的意思...