首页 > 解决方案 > 如何使功能在另一个处理之后的步骤中工作

问题描述

我想制作一个方形玩家游戏,但是当尝试使用函数进行方形移动时,它只会做一个动作。我想要它,以便我可以通过一系列语句来进行方形移动我还想稍后添加障碍。

oben = 向上

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);
}  

标签: processing

解决方案


我做了一些更改,允许您使用 WASD 键移动方块。通过创建一个二维数组来保存电路板的状态,可以大大简化整个代码。

不过,我会坚持原来的实现,只添加让你继续前进所需的部分。重要的一点是添加keyPressed()方法(参见文档)并替换行

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;
  }
}

推荐阅读