如何随机生成汽车碰撞游戏的障碍

发布于 2025-01-27 18:04:47 字数 1497 浏览 1 评论 0原文

如何为游戏创建随机的汽车障碍?我有这款需要制作的自上而下的汽车游戏。该游戏的目的是使汽车(在Y轴上)上下移动,以避免障碍,而障碍物则朝着汽车(X轴)移动。是否可以在产卵时创建一个随机的障碍物,使游戏变得愉快?现在,我只有一组障碍可以持续12秒钟,然后才能轻松通过它们。我还可以保持障碍的间距吗?这样汽车可以合适。

代码

color green = color(0,195,0);
color red = color(195,0,0);
color grey = color(100,100,100);
color yellow = color(200,200,0);
color white = color(255,255,255);

float roadx = 70, road1y = 130;
float road2y = 230, road3y = 330;
float carY = road2y;
float carX = roadx;
float carUPspeed = 1;
float laneX = 700;



float lanes;

void lane1(float x, float y){
  fill(white);
  rect(x + 100, y,30,100);
  rect(x + 250, y + 100,30,100);
  rect(x + 250, y + 200, 30, 100);
  rect(x + 400, y + 100, 30, 100);
  rect(x + 550, y + 100, 30, 100);
  rect(x + 550, y, 30, 100);
  rect(x + 700, y + 100, 30, 100);
  rect(x + 700, y + 200, 30, 100);
}

void background(){
  background(green);
}
  
void car(){
  fill(red);
  rect(carX, carY, 60, 40);
}
void setup(){
  surface.setTitle("dodge");
  size(900, 500);
}

void draw(){
  background();
  noStroke();
  fill(grey);
  rect(roadx - 400, road1y - 30, 1500, 100);
  stroke(yellow);
  strokeWeight(5);
  rect(roadx - 400, road2y - 30, 1500, 100);
  noStroke();
  rect(roadx - 400, road3y - 27, 1500, 100);
  car();  
  lane1(laneX, 100);
  laneX -= 1;
  
  lanes = 1;

}
    

void keyPressed(KeyEvent event){
  if (key == 'w'){
    if(carY != 130){
      carY = carY - 100;
    }
  }
  if (key == 's'){
    if(carY != 330){
      carY = carY + 100;
    }
  }
}

How do I create a random car barrier for my game? I have this top-down car game that I need to make. This game aims to make the car move up and down (on the y-axis) to avoid the barriers, while the barriers are moving toward the car (on the x-axis). Is it possible to create a random generation of the barriers when they spawn, making the game enjoyable? Right now, I only have a set of barriers that last for 12 seconds before I get easily through them. Can I also keep the spacing of the barriers the same? So the car can fit.

code

color green = color(0,195,0);
color red = color(195,0,0);
color grey = color(100,100,100);
color yellow = color(200,200,0);
color white = color(255,255,255);

float roadx = 70, road1y = 130;
float road2y = 230, road3y = 330;
float carY = road2y;
float carX = roadx;
float carUPspeed = 1;
float laneX = 700;



float lanes;

void lane1(float x, float y){
  fill(white);
  rect(x + 100, y,30,100);
  rect(x + 250, y + 100,30,100);
  rect(x + 250, y + 200, 30, 100);
  rect(x + 400, y + 100, 30, 100);
  rect(x + 550, y + 100, 30, 100);
  rect(x + 550, y, 30, 100);
  rect(x + 700, y + 100, 30, 100);
  rect(x + 700, y + 200, 30, 100);
}

void background(){
  background(green);
}
  
void car(){
  fill(red);
  rect(carX, carY, 60, 40);
}
void setup(){
  surface.setTitle("dodge");
  size(900, 500);
}

void draw(){
  background();
  noStroke();
  fill(grey);
  rect(roadx - 400, road1y - 30, 1500, 100);
  stroke(yellow);
  strokeWeight(5);
  rect(roadx - 400, road2y - 30, 1500, 100);
  noStroke();
  rect(roadx - 400, road3y - 27, 1500, 100);
  car();  
  lane1(laneX, 100);
  laneX -= 1;
  
  lanes = 1;

}
    

void keyPressed(KeyEvent event){
  if (key == 'w'){
    if(carY != 130){
      carY = carY - 100;
    }
  }
  if (key == 's'){
    if(carY != 330){
      carY = carY + 100;
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谜泪 2025-02-03 18:04:47

我已经修改了您的车道渲染,并增加了障碍物的动态创建。每150个像素移动,都会更新屏障(删除了最左侧的屏障,在最右边添加了新的屏障,并且周期开始了)。

障碍物作为0到6之间的整数存储,因为障碍位置有7种可能性:

int   | 0  1  2  3  4  5  6
------+--------------------
lane0 |    #           #  #
lane1 |       #     #  #
lane2 |          #  #     #

希望这会有所帮助。

修改的代码:

import java.util.*;

private static final int SCREEN_X = 900;
private static final int SCREEN_Y = 500;
private static final int LANE_HEIGHT = 100;

private static final int BARRIER_DISTANCE = 150;
private static final int BARRIER_HEIGHT = 100;
private static final int BARRIER_WIDTH = 30;


color green = color(0,195,0);
color red = color(195,0,0);
color grey = color(100,100,100);
color yellow = color(200,200,0);
color white = color(255,255,255);

float roadx = 70, road1y = 130;
float road2y = 230, road3y = 330;
float carY = road2y;
float carX = roadx;
float carUPspeed = 1;
float laneX = 50;

float lanes;

List<Integer> barriers;

void lane1(float x){
  fill(white);
  for (Integer b : barriers)
  {
    switch(b)
    {
      case 0: // ___
        break;
      case 1: // X__
        rect(x, LANE_HEIGHT, BARRIER_WIDTH, BARRIER_HEIGHT);
        break;
      case 2: // _X_
        rect(x, LANE_HEIGHT * 2, BARRIER_WIDTH, BARRIER_HEIGHT);
        break;
      case 3: // __X
        rect(x, LANE_HEIGHT * 3, BARRIER_WIDTH, BARRIER_HEIGHT);
        break;
      case 4: // _XX
        rect(x, LANE_HEIGHT * 2, BARRIER_WIDTH, BARRIER_HEIGHT);
        rect(x, LANE_HEIGHT * 3, BARRIER_WIDTH, BARRIER_HEIGHT);
        break;
      case 5: // XX_
        rect(x, LANE_HEIGHT * 1, BARRIER_WIDTH, BARRIER_HEIGHT);
        rect(x, LANE_HEIGHT * 2, BARRIER_WIDTH, BARRIER_HEIGHT);
        break;
      case 6: // X_X
        rect(x, LANE_HEIGHT * 1, BARRIER_WIDTH, BARRIER_HEIGHT);
        rect(x, LANE_HEIGHT * 3, BARRIER_WIDTH, BARRIER_HEIGHT);
        break;
    }
    x += BARRIER_DISTANCE;
  }
}

void background(){
  background(green);
}
  
void car(){
  fill(red);
  rect(carX, carY, 60, 40);
}
void setup(){
  surface.setTitle("dodge");
  size(900, 500);
  
  // Initially generate 7 barriers (one more than the screen can fit)
  barriers = new ArrayList<Integer>();
  
  for (int i = 0; i <= (SCREEN_X / BARRIER_DISTANCE); i++)
  {
    barriers.add(int(random(0, 6)));
  }
  
  // Set the first to barriers to 'no barrier' to the player initially has some time
  barriers.set(0, 0);
  barriers.set(1, 0);
}

void draw(){
  background();
  noStroke();
  fill(grey);
  rect(roadx - 400, road1y - 30, 1500, LANE_HEIGHT);
  stroke(yellow);
  strokeWeight(5);
  rect(roadx - 400, road2y - 30, 1500, LANE_HEIGHT);
  noStroke();
  rect(roadx - 400, road3y - 27, 1500, LANE_HEIGHT);
  car();
  lane1(laneX);
  laneX -= 1;
  
  // laneX cycles from 50 to -100 (because of draw position)
  // So, every 150 pixels moved
  if (laneX == -100)
  {
    // Reset laneX
    laneX = 50;
    // Remove leftmost barrier
    barriers.remove(0);
    // Add new barriers, incoming from the right
    barriers.add(int(random(0, 6)));
  }
  
  lanes = 1;
}
    

void keyPressed(KeyEvent event){
  if (key == 'w'){
    if(carY != 130){
      carY = carY - 100;
    }
  }
  if (key == 's'){
    if(carY != 330){
      carY = carY + 100;
    }
  }
}

I've modified your lane rendering and added dynamic creation of barriers. Every 150 pixels moved, the barriers are updated (leftmost barriers are removed, new ones are added at the rightmost position and the cycle starts over).

The barriers are stored as an integer between 0 and 6, as there are 7 possibilities for the barrier positions:

int   | 0  1  2  3  4  5  6
------+--------------------
lane0 |    #           #  #
lane1 |       #     #  #
lane2 |          #  #     #

Hope this helps.

Modified code:

import java.util.*;

private static final int SCREEN_X = 900;
private static final int SCREEN_Y = 500;
private static final int LANE_HEIGHT = 100;

private static final int BARRIER_DISTANCE = 150;
private static final int BARRIER_HEIGHT = 100;
private static final int BARRIER_WIDTH = 30;


color green = color(0,195,0);
color red = color(195,0,0);
color grey = color(100,100,100);
color yellow = color(200,200,0);
color white = color(255,255,255);

float roadx = 70, road1y = 130;
float road2y = 230, road3y = 330;
float carY = road2y;
float carX = roadx;
float carUPspeed = 1;
float laneX = 50;

float lanes;

List<Integer> barriers;

void lane1(float x){
  fill(white);
  for (Integer b : barriers)
  {
    switch(b)
    {
      case 0: // ___
        break;
      case 1: // X__
        rect(x, LANE_HEIGHT, BARRIER_WIDTH, BARRIER_HEIGHT);
        break;
      case 2: // _X_
        rect(x, LANE_HEIGHT * 2, BARRIER_WIDTH, BARRIER_HEIGHT);
        break;
      case 3: // __X
        rect(x, LANE_HEIGHT * 3, BARRIER_WIDTH, BARRIER_HEIGHT);
        break;
      case 4: // _XX
        rect(x, LANE_HEIGHT * 2, BARRIER_WIDTH, BARRIER_HEIGHT);
        rect(x, LANE_HEIGHT * 3, BARRIER_WIDTH, BARRIER_HEIGHT);
        break;
      case 5: // XX_
        rect(x, LANE_HEIGHT * 1, BARRIER_WIDTH, BARRIER_HEIGHT);
        rect(x, LANE_HEIGHT * 2, BARRIER_WIDTH, BARRIER_HEIGHT);
        break;
      case 6: // X_X
        rect(x, LANE_HEIGHT * 1, BARRIER_WIDTH, BARRIER_HEIGHT);
        rect(x, LANE_HEIGHT * 3, BARRIER_WIDTH, BARRIER_HEIGHT);
        break;
    }
    x += BARRIER_DISTANCE;
  }
}

void background(){
  background(green);
}
  
void car(){
  fill(red);
  rect(carX, carY, 60, 40);
}
void setup(){
  surface.setTitle("dodge");
  size(900, 500);
  
  // Initially generate 7 barriers (one more than the screen can fit)
  barriers = new ArrayList<Integer>();
  
  for (int i = 0; i <= (SCREEN_X / BARRIER_DISTANCE); i++)
  {
    barriers.add(int(random(0, 6)));
  }
  
  // Set the first to barriers to 'no barrier' to the player initially has some time
  barriers.set(0, 0);
  barriers.set(1, 0);
}

void draw(){
  background();
  noStroke();
  fill(grey);
  rect(roadx - 400, road1y - 30, 1500, LANE_HEIGHT);
  stroke(yellow);
  strokeWeight(5);
  rect(roadx - 400, road2y - 30, 1500, LANE_HEIGHT);
  noStroke();
  rect(roadx - 400, road3y - 27, 1500, LANE_HEIGHT);
  car();
  lane1(laneX);
  laneX -= 1;
  
  // laneX cycles from 50 to -100 (because of draw position)
  // So, every 150 pixels moved
  if (laneX == -100)
  {
    // Reset laneX
    laneX = 50;
    // Remove leftmost barrier
    barriers.remove(0);
    // Add new barriers, incoming from the right
    barriers.add(int(random(0, 6)));
  }
  
  lanes = 1;
}
    

void keyPressed(KeyEvent event){
  if (key == 'w'){
    if(carY != 130){
      carY = carY - 100;
    }
  }
  if (key == 's'){
    if(carY != 330){
      carY = carY + 100;
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文