问题描述
在草图上绘制两个形状(例如矩形和圆形).使用 UP、DOWN、LEFT 和 RIGHT 键控制所选形状的移动.通过按1"键或2"键选择形状,以便分别选择形状 1 或形状 2.我想通过按1"键选择形状或2",但它们无法运行.`
Draw two shapes on the sketch (e.g. a rectangle and a circle). Use the UP, DOWN, LEFT and RIGHT keys to control the movement of the selected shape. A shape is selected by either pressing a key ‘1’ or or key ‘2’ so that shape 1 or shape 2 can be picked up respectively.I want to select the shape by pressing the key "1" or "2", but they cannot run.`
int x = 0;
int y = 0;
int ex= 0;
int ey= 0;
void setup(){
size (400, 400);
}
void draw(){
background(80);
rect(x, y, 25,25);
ellipse(50, 50, 50, 50);
}
void keyPresse() {
if ( (key == '1')) {
if (keyCode == UP) {
y -= 10;
} else if (keyCode == DOWN) {
y += 10;
} else if (keyCode == LEFT) {
x -= 10;
} else if (keyCode == RIGHT) {
x += 10;
}
} else if ((key == '2')){
if (keyCode == UP) {
ey -= 10;
} else if (keyCode == DOWN) {
ey += 10;
} else if (keyCode == LEFT) {
ex -= 10;
} else if (keyCode == RIGHT) {
ex += 10;
}
}
}
推荐答案
有一个错字.键盘回调的名称是 keyPressed
.但是,也存在一些逻辑问题.
There is a typo. The name of the key board callback is keyPressed
. However, there are also some logical issues.
为 x
和 y
坐标创建一个数组.还有一个索引变量 (shape_i
):
Crate an array for the x
and y
coordinates. And an index variabel (shape_i
):
int x[] = new int[]{100, 100};
int y[] = new int[]{200, 100};
int shape_i = 0;
在其位置绘制形状.(x[0]
, y[0]
) 是矩形的位置,(x[1]
, y[1]
) 是椭圆的位置:
Draw the shapes on its position. (x[0]
, y[0]
) is the position of the rectangle and (x[1]
, y[1]
) is the position of the ellipse:
void draw(){
background(80);
rect(x[0], y[0], 25, 25);
ellipse(x[1], y[1], 50, 50);
}
当按下 或 时更改索引 (shape_i
).按下箭头键时更改 (x[shape_i]
, y[shape_i]
):
Change the index (shape_i
) when or is pressed. Change (x[shape_i]
, y[shape_i]
) when an arrow key is pressed:
void keyPressed() {
if (key == '1') {
shape_i = 0;
} else if (key == '2') {
shape_i = 1;
} else if (keyCode == UP) {
y[shape_i] -= 10;
} else if (keyCode == DOWN) {
y[shape_i] += 10;
} else if (keyCode == LEFT) {
x[shape_i] -= 10;
} else if (keyCode == RIGHT) {
x[shape_i] += 10;
}
}
完整示例:
Complete example:
int x[] = new int[]{100, 100};
int y[] = new int[]{200, 100};
int shape_i = 0;
void setup(){
size (400, 400);
}
void draw(){
background(80);
rect(x[0], y[0], 25, 25);
ellipse(x[1], y[1], 50, 50);
}
void keyPressed() {
if (key == '1') {
shape_i = 0;
} else if (key == '2') {
shape_i = 1;
} else if (keyCode == UP) {
y[shape_i] -= 10;
} else if (keyCode == DOWN) {
y[shape_i] += 10;
} else if (keyCode == LEFT) {
x[shape_i] -= 10;
} else if (keyCode == RIGHT) {
x[shape_i] += 10;
}
}
这篇关于处理-通过按“1"键或“2"键来选择形状,以便分别拾取形状 1 或形状 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!