看不见的障碍物/预先绘制的障碍物

问题描述 投票:0回答:1

地图片段

我正在用Processing Java制作一个Pokemon游戏作为学校作业,我需要让木桩成为运动系统的障碍。

既然这些是预先绘制到背景中的,我怎样才能做到这一点?

我能想到的唯一方法是制作一些难以看到的矩形,或者制作一个角色可以在其中移动的网格,并将这些矩形从网格中排除。

但是,我不确定如何执行这些操作,或者它们是否有效。

java processing game-development
1个回答
0
投票

我给你做了一个简短的例子,这样你就可以尝试一下。这并不完全是我的做法,但我怀疑你正在努力学习,我不想用比你现在可能使用的更先进的技术来淹没你。

对于这个例子,我决定使用 10x10 的图块地图,其中每个图块的宽度为 60 像素。

我使用几个全局变量来跟踪坐标:

int tileSize = 60;
float[][] obstacles;
int pcX, pcY;

现在在

setup()
方法中,我填写其中一些坐标:玩家和障碍物的起始坐标:

void setup() {
  size(600, 600);
  
   // initializing the obstacles array
  obstacles = new float[10][2]; // 10 entries with 2 numbers each
  // the numbers are xy coordinates
  // first obstacle is at 0,0
  // second is at 0, 1, etc
  obstacles[0][0] = 0;
  obstacles[0][1] = 0;
  obstacles[1][0] = 0;
  obstacles[1][1] = 1;
  obstacles[2][0] = 0;
  obstacles[2][1] = 2;
  obstacles[3][0] = 0;
  obstacles[3][1] = 3;
  obstacles[4][0] = 4;
  obstacles[4][1] = 4;
  obstacles[5][0] = 4;
  obstacles[5][1] = 5;
  obstacles[6][0] = 4;
  obstacles[6][1] = 6;
  obstacles[7][0] = 4;
  obstacles[7][1] = 7;
  obstacles[8][0] = 0;
  obstacles[8][1] = 8;
  obstacles[9][0] = 0;
  obstacles[9][1] = 9;
  // etc. for every obstacle there is on your map
  // i did move some around just to demonstrate the principle, but yours are all in the same column
  
  // here I set up starting coordinates for the player character
  pcX = 6;
  pcY = 4;
}

我确保用键盘捕捉玩家的输入(我使用 WASD 因为它很简单):

void keyPressed() {
  println(pcY);
  if (key == 'd') { move(1,0); }
  if (key == 'a') { move(-1,0); }
  if (key == 's') { move(0,1); }
  if (key == 'w') { move(0,-1); }
}

move
方法将在我更新玩家坐标之前验证玩家是否有权移动:

void move(int xx, int yy) {
  // if the player character cannot do that move, this method will return before the pc's coordinates are updated
  
  // validate if the player character is going to go out of the map (I made the map 10 tiles x 10 tiles)
  if (pcX + xx < 0 || pcX + xx > 9) { return; } 
  if (pcY + yy < 0 || pcY + yy > 9) { return; }
  
  // validate if the player character is going to walk over an obstacle
  int i;
  for (i=0; i<obstacles.length; i++) {
    if (pcX + xx == obstacles[i][0] && pcY + yy == obstacles[i][1]) { return; } 
  }
  
  // as there was nothing forbidding the move, the pc's coordinates can now be updated
  pcX += xx;
  pcY += yy;
}

最后我可以制作一个游戏循环来让你测试这段代码:

void draw() {
  background(255);
  drawBackground();
  drawPC();
}

由于我不想在示例中使用图像,因此所有内容都将绘制为矩形!:

void drawBackground() {
  int i;
  fill(200, 0, 0);
  
  // here I draw red squares on tiles that are obstacles. If your background isn't already drawn you don't have to do that part
  for (i=0; i<obstacles.length; i++) {
    rect(tileSize*obstacles[i][0], tileSize*obstacles[i][1], tileSize, tileSize);
  }
}

void drawPC() {
  fill(0, 200, 0);
  rect(tileSize*pcX, tileSize*pcY, tileSize, tileSize);
}

给你,这是完整的代码,以获得更轻松的复制和粘贴体验:

int tileSize = 60;
float[][] obstacles;
int pcX, pcY;

void setup() {
  size(600, 600);
  
   // initializing the obstacles array
  obstacles = new float[10][2]; // 10 entries with 2 numbers each
  // the numbers are xy coordinates
  // first obstacle is at 0,0
  // second is at 0, 1, etc
  obstacles[0][0] = 0;
  obstacles[0][1] = 0;
  obstacles[1][0] = 0;
  obstacles[1][1] = 1;
  obstacles[2][0] = 0;
  obstacles[2][1] = 2;
  obstacles[3][0] = 0;
  obstacles[3][1] = 3;
  obstacles[4][0] = 4;
  obstacles[4][1] = 4;
  obstacles[5][0] = 4;
  obstacles[5][1] = 5;
  obstacles[6][0] = 4;
  obstacles[6][1] = 6;
  obstacles[7][0] = 4;
  obstacles[7][1] = 7;
  obstacles[8][0] = 0;
  obstacles[8][1] = 8;
  obstacles[9][0] = 0;
  obstacles[9][1] = 9;
  // etc. for every obstacle there is on your map
  // i did move some around just to demonstrate the principle, but yours are all in the same column
  
  // here I set up starting coordinates for the player character
  pcX = 6;
  pcY = 4;
}

void draw() {
  background(255);
  drawBackground();
  drawPC();
}

void drawBackground() {
  int i;
  fill(200, 0, 0);
  
  // here I draw red squares on tiles that are obstacles. If your background isn't already drawn you don't have to do that part
  for (i=0; i<obstacles.length; i++) {
    rect(tileSize*obstacles[i][0], tileSize*obstacles[i][1], tileSize, tileSize);
  }
}

void move(int xx, int yy) {
  // if the player character cannot do that move, this method will return before the pc's coordinates are updated
  
  // validate if the player character is going to go out of the map (I made the map 10 tiles x 10 tiles)
  if (pcX + xx < 0 || pcX + xx > 9) { return; } 
  if (pcY + yy < 0 || pcY + yy > 9) { return; }
  
  // validate if the player character is going to walk over an obstacle
  int i;
  for (i=0; i<obstacles.length; i++) {
    if (pcX + xx == obstacles[i][0] && pcY + yy == obstacles[i][1]) { return; } 
  }
  
  // as there was nothing forbidding the move, the pc's coordinates can now be updated
  pcX += xx;
  pcY += yy;
}

void drawPC() {
  fill(0, 200, 0);
  rect(tileSize*pcX, tileSize*pcY, tileSize, tileSize);
}

void keyPressed() {
  println(pcY);
  if (key == 'd') { move(1,0); }
  if (key == 'a') { move(-1,0); }
  if (key == 's') { move(0,1); }
  if (key == 'w') { move(0,-1); }
}

玩得开心!

© www.soinside.com 2019 - 2024. All rights reserved.