如何使功能与其他步骤一起工作 Processing
How to make the function work with steps one after the other Processing
我想制作一个方块玩家游戏,但是当尝试使用函数让方块移动时,它一直只做一个动作。我想要它,这样我就可以通过一系列的语句来使方块移动我也想在以后添加障碍。
oben = up
unten = 向下
合法 = 正确
链接 = 左
这是我的代码:
long lastTime = 0;
int[] xpos = new int[1];
int[] ypos = new int[1];
int playerCount = 0;
long[] posCase = new long[50];
int[] playerNo = new int[50];
int delay = 500;
boolean f = true;
int count = 0;
int border = 20; // border on one side is 20 both is 40
int sqsize = 96;
void setup() {
size(1000, 1000);
xpos[playerCount] = border;
ypos[playerCount] = border;
frameRate(5);
}
void draw() {
background(#767C7C);
fill(255, 255, 255);
// for start
for (int w = 0; w < 10; w++) {
int newBorderWidth = border + w*sqsize;
for (int i = 0; i < 10; i++) {
int newBorderLength = border + i*sqsize;
fill(#F6F9EF);
stroke(#BABAB6);
strokeWeight(0.5);
rect(newBorderLength, newBorderWidth, sqsize, sqsize);
}
}
// for end
switch(int(posCase[playerCount])) {
case 1 :
if (xpos[playerCount] < border + 9*sqsize)
xpos[playerCount] = xpos[playerCount] + sqsize;
posCase[playerCount] = 0;
println("case 1");
break;
case 2 :
if (xpos[playerCount] > border)
xpos[playerCount] = xpos[playerCount] - sqsize;
posCase[playerCount] = 0;
println("case 2");
break;
case 3 :
if (ypos[playerCount] > border)
ypos[playerCount] = ypos[playerCount] - sqsize;
posCase[playerCount] = 0;
println("case 4");
break;
case 4 :
if (ypos[playerCount] < border + 9*sqsize)
ypos[playerCount] = ypos[playerCount] + sqsize;
posCase[playerCount] = 0;
println("case 3");
break;
}
fill(255);
rect(xpos[0], ypos[0], sqsize, sqsize);
}
void rechts() {
posCase[playerCount] = 1;
delay(delay);
}
void links() {
posCase[playerCount] = 2;
delay(delay);
}
void oben() {
posCase[playerCount] = 3;
delay(delay);
}
void unten() {
posCase[playerCount] = 4;
delay(delay);
}
我做了一些更改,允许您使用 WASD 键移动方块。通过创建一个二维数组来保存棋盘的状态,可以大大简化整个代码。
虽然我会坚持原来的实现,但只添加让你继续前进所需的部分。重要的一点是添加 keyPressed() 方法(参见 documentation)并替换行
rect(xpos[0], ypos[0], sqsize, sqsize);
和
rect(xpos[playerCount], ypos[playerCount], sqsize, sqsize);
以上代码包括所需的更改:
long lastTime = 0;
int[] xpos = new int[1];
int[] ypos = new int[1];
int playerCount = 0;
long[] posCase = new long[50];
int[] playerNo = new int[50];
int delay = 500;
boolean f = true;
int count = 0;
int border = 20; // border on one side is 20 both is 40
int sqsize = 96;
void setup() {
size(1000, 1000);
xpos[playerCount] = border;
ypos[playerCount] = border;
frameRate(5);
}
void draw() {
background(#767C7C);
fill(255, 255, 255);
// for start
for (int w = 0; w < 10; w++) {
int newBorderWidth = border + w*sqsize;
for (int i = 0; i < 10; i++) {
int newBorderLength = border + i*sqsize;
fill(#F6F9EF);
stroke(#BABAB6);
strokeWeight(0.5);
rect(newBorderLength, newBorderWidth, sqsize, sqsize);
}
}
// for end
switch(int(posCase[playerCount])) {
case 1 :
if (xpos[playerCount] < border + 9*sqsize)
xpos[playerCount] = xpos[playerCount] + sqsize;
posCase[playerCount] = 0;
println("case 1");
break;
case 2 :
if (xpos[playerCount] > border)
xpos[playerCount] = xpos[playerCount] - sqsize;
posCase[playerCount] = 0;
println("case 2");
break;
case 3 :
if (ypos[playerCount] > border)
ypos[playerCount] = ypos[playerCount] - sqsize;
posCase[playerCount] = 0;
println("case 4");
break;
case 4 :
if (ypos[playerCount] < border + 9*sqsize)
ypos[playerCount] = ypos[playerCount] + sqsize;
posCase[playerCount] = 0;
println("case 3");
break;
}
fill(255);
rect(xpos[playerCount], ypos[playerCount], sqsize, sqsize);
}
void rechts() {
posCase[playerCount] = 1;
delay(delay);
}
void links() {
posCase[playerCount] = 2;
delay(delay);
}
void oben() {
posCase[playerCount] = 3;
delay(delay);
}
void unten() {
posCase[playerCount] = 4;
delay(delay);
}
void keyPressed() {
switch(key) {
case 'a':
case 'A':
links();
break;
case 'd':
case 'D':
rechts();
break;
case 'w':
case 'W':
oben();
break;
case 's':
case 'S':
unten();
break;
}
}
我想制作一个方块玩家游戏,但是当尝试使用函数让方块移动时,它一直只做一个动作。我想要它,这样我就可以通过一系列的语句来使方块移动我也想在以后添加障碍。
oben = up
unten = 向下
合法 = 正确
链接 = 左
这是我的代码:
long lastTime = 0;
int[] xpos = new int[1];
int[] ypos = new int[1];
int playerCount = 0;
long[] posCase = new long[50];
int[] playerNo = new int[50];
int delay = 500;
boolean f = true;
int count = 0;
int border = 20; // border on one side is 20 both is 40
int sqsize = 96;
void setup() {
size(1000, 1000);
xpos[playerCount] = border;
ypos[playerCount] = border;
frameRate(5);
}
void draw() {
background(#767C7C);
fill(255, 255, 255);
// for start
for (int w = 0; w < 10; w++) {
int newBorderWidth = border + w*sqsize;
for (int i = 0; i < 10; i++) {
int newBorderLength = border + i*sqsize;
fill(#F6F9EF);
stroke(#BABAB6);
strokeWeight(0.5);
rect(newBorderLength, newBorderWidth, sqsize, sqsize);
}
}
// for end
switch(int(posCase[playerCount])) {
case 1 :
if (xpos[playerCount] < border + 9*sqsize)
xpos[playerCount] = xpos[playerCount] + sqsize;
posCase[playerCount] = 0;
println("case 1");
break;
case 2 :
if (xpos[playerCount] > border)
xpos[playerCount] = xpos[playerCount] - sqsize;
posCase[playerCount] = 0;
println("case 2");
break;
case 3 :
if (ypos[playerCount] > border)
ypos[playerCount] = ypos[playerCount] - sqsize;
posCase[playerCount] = 0;
println("case 4");
break;
case 4 :
if (ypos[playerCount] < border + 9*sqsize)
ypos[playerCount] = ypos[playerCount] + sqsize;
posCase[playerCount] = 0;
println("case 3");
break;
}
fill(255);
rect(xpos[0], ypos[0], sqsize, sqsize);
}
void rechts() {
posCase[playerCount] = 1;
delay(delay);
}
void links() {
posCase[playerCount] = 2;
delay(delay);
}
void oben() {
posCase[playerCount] = 3;
delay(delay);
}
void unten() {
posCase[playerCount] = 4;
delay(delay);
}
我做了一些更改,允许您使用 WASD 键移动方块。通过创建一个二维数组来保存棋盘的状态,可以大大简化整个代码。
虽然我会坚持原来的实现,但只添加让你继续前进所需的部分。重要的一点是添加 keyPressed() 方法(参见 documentation)并替换行
rect(xpos[0], ypos[0], sqsize, sqsize);
和
rect(xpos[playerCount], ypos[playerCount], sqsize, sqsize);
以上代码包括所需的更改:
long lastTime = 0;
int[] xpos = new int[1];
int[] ypos = new int[1];
int playerCount = 0;
long[] posCase = new long[50];
int[] playerNo = new int[50];
int delay = 500;
boolean f = true;
int count = 0;
int border = 20; // border on one side is 20 both is 40
int sqsize = 96;
void setup() {
size(1000, 1000);
xpos[playerCount] = border;
ypos[playerCount] = border;
frameRate(5);
}
void draw() {
background(#767C7C);
fill(255, 255, 255);
// for start
for (int w = 0; w < 10; w++) {
int newBorderWidth = border + w*sqsize;
for (int i = 0; i < 10; i++) {
int newBorderLength = border + i*sqsize;
fill(#F6F9EF);
stroke(#BABAB6);
strokeWeight(0.5);
rect(newBorderLength, newBorderWidth, sqsize, sqsize);
}
}
// for end
switch(int(posCase[playerCount])) {
case 1 :
if (xpos[playerCount] < border + 9*sqsize)
xpos[playerCount] = xpos[playerCount] + sqsize;
posCase[playerCount] = 0;
println("case 1");
break;
case 2 :
if (xpos[playerCount] > border)
xpos[playerCount] = xpos[playerCount] - sqsize;
posCase[playerCount] = 0;
println("case 2");
break;
case 3 :
if (ypos[playerCount] > border)
ypos[playerCount] = ypos[playerCount] - sqsize;
posCase[playerCount] = 0;
println("case 4");
break;
case 4 :
if (ypos[playerCount] < border + 9*sqsize)
ypos[playerCount] = ypos[playerCount] + sqsize;
posCase[playerCount] = 0;
println("case 3");
break;
}
fill(255);
rect(xpos[playerCount], ypos[playerCount], sqsize, sqsize);
}
void rechts() {
posCase[playerCount] = 1;
delay(delay);
}
void links() {
posCase[playerCount] = 2;
delay(delay);
}
void oben() {
posCase[playerCount] = 3;
delay(delay);
}
void unten() {
posCase[playerCount] = 4;
delay(delay);
}
void keyPressed() {
switch(key) {
case 'a':
case 'A':
links();
break;
case 'd':
case 'D':
rechts();
break;
case 'w':
case 'W':
oben();
break;
case 's':
case 'S':
unten();
break;
}
}