击中时如何使对象消失

发布于 2025-02-01 20:47:10 字数 3247 浏览 4 评论 0原文

我当时正在制作砖砌游戏的过程中,并试图为游戏创建碰撞系统。我能够确定球和砖之间何时发生碰撞,但是当砖被球击中时,我无法使砖消失。我该怎么做?由于我是初学者,我也将不胜感激。

谢谢。

color black = color(0,0,0);
color red = color(255,0,0);
color white = color(255,255,255);

float ballx = 412.5, bally = 600, balld = 20, ballr = balld/2, paddleX = 362.5, paddleY = 650;
float paddleW = 100, paddleH = 15;
float ballspdX, ballspdY;

boolean ball_drop = true;

float direction_choice;

float[] brickX = new float[10];

float[] brickY = new float[5];

float brickW = 50, brickH = 25;


void setup(){
  size(825,800);
  surface.setTitle("Brick breaker");
  noCursor();
  smooth();
  
  brickX[0] = 50;
  brickX[1] = 125; 
  brickX[2] = 200; 
  brickX[3] = 275;
  brickX[4] = 350; 
  brickX[5] = 425;
  brickX[6] = 500; 
  brickX[7] = 575;
  brickX[8] = 650; 
  brickX[9] = 725; 
  
  brickY[0] = 50; 
  brickY[1] = 125; 
  brickY[2] = 200; 
  brickY[3] = 275;
  brickY[4] = 350; 
}

void paddle(){
  noStroke();
  fill(white);
  rect(paddleX, paddleY, paddleW, paddleH);
}
void ball(){
  noStroke();
  fill(red);
  circle(ballx, bally, balld);
}


void draw(){
  background(black);
  paddle();
  ball();
  if (bally + ballr == paddleY && ballx > paddleX && ballx < paddleX + (paddleW / 2)){
    ball_drop = false;
    ballspdY = -ballspdY;
    ballspdX = -4;
  }
  
  
  /*
    if (bally + ballr == paddleY && ballx > paddleX && ballx < paddleX + paddleW){
    ball_drop = false;
    ballspdY = -ballspdY;
    direction_choice = int(random(1,3));
    if (direction_choice == 1){
     ballspdX = 4; 
    }
    if (direction_choice == 2){
      ballspdX = -4;
    }
  }
  println(direction_choice);
  
  */
  if (bally + ballr == paddleY && ballx > paddleX + (paddleW /2) && ballx < paddleX + paddleW){
    ball_drop = false;
    ballspdY = -ballspdY;
    ballspdX = 4;
  }
  
  if (ballx + ballr > width || ballx - ballr < 0){
    ballspdX = -ballspdX;
  }
  
  if (bally - ballr < 0){
    ballspdY = -ballspdY;
  }

  if (ball_drop){
    ballspdX = 0;
    ballspdY = 1;
  }
 for (int i = 0; i < brickX.length; i ++){
   for(int j = 0; j < brickY.length; j ++){ 
   fill(red);
   rect(brickX[i], brickY[j], brickW, brickH); 
   
    if (collideLineCircle(brickX[i], brickY[j] + brickH, brickX[i] + brickW, brickY[j] + brickH, ballx, bally, ballr)){
      ballspdY = -ballspdY;
    }
   
  }
 }

  
 if (bally >= 800){
   bally = 600;
   ballx = 412.5;
   ball_drop = true;
 }
 
 
  bally += ballspdY;
  ballx += ballspdX;
  
}
  boolean collideLineCircle(float x1, float y1, float x2, float y2, float cx, float cy, float cr){
    float A = y2 - y1, B = x1 - x2, C = x2*y1 - x1+y2;
    float denom = A+A + B*B;
    if (denom == 0){
    
      return dist(x1,y1, cx, cy) < cr;
    
    }
    
    float Ix = (B*B*cx-A*B*cy - A*C)/denom, Iy = (A*A*cy-A*B*cx - B*C)/denom;
    
    if (Ix >= min(x1,x2) && Ix <= max(x1,x2) & Iy >= min(y1,y2) && Iy <= max(y1,y2)) {
      return abs (A*cx + B*cy + C)/sqrt(denom) < cr;
    }
    
    float d1 = dist(x1,y1,cx,cy), d2 = dist(x2,y2,cx,cy);
    return min(d1,d2) < cr;
    
  }

void mouseMoved(MouseEvent evt){
  paddleX = evt.getX();
  }

I was working on a brick breaker game in proccessing, and was trying to create the collision system for the game. I was able to identify when a collision happens between the ball and the brick, but I was not able to to make the brick dissapear when it gets hit by the ball. How do I do this? I would also appreciate a explanation, as I am a beginner.

Thanks.

color black = color(0,0,0);
color red = color(255,0,0);
color white = color(255,255,255);

float ballx = 412.5, bally = 600, balld = 20, ballr = balld/2, paddleX = 362.5, paddleY = 650;
float paddleW = 100, paddleH = 15;
float ballspdX, ballspdY;

boolean ball_drop = true;

float direction_choice;

float[] brickX = new float[10];

float[] brickY = new float[5];

float brickW = 50, brickH = 25;


void setup(){
  size(825,800);
  surface.setTitle("Brick breaker");
  noCursor();
  smooth();
  
  brickX[0] = 50;
  brickX[1] = 125; 
  brickX[2] = 200; 
  brickX[3] = 275;
  brickX[4] = 350; 
  brickX[5] = 425;
  brickX[6] = 500; 
  brickX[7] = 575;
  brickX[8] = 650; 
  brickX[9] = 725; 
  
  brickY[0] = 50; 
  brickY[1] = 125; 
  brickY[2] = 200; 
  brickY[3] = 275;
  brickY[4] = 350; 
}

void paddle(){
  noStroke();
  fill(white);
  rect(paddleX, paddleY, paddleW, paddleH);
}
void ball(){
  noStroke();
  fill(red);
  circle(ballx, bally, balld);
}


void draw(){
  background(black);
  paddle();
  ball();
  if (bally + ballr == paddleY && ballx > paddleX && ballx < paddleX + (paddleW / 2)){
    ball_drop = false;
    ballspdY = -ballspdY;
    ballspdX = -4;
  }
  
  
  /*
    if (bally + ballr == paddleY && ballx > paddleX && ballx < paddleX + paddleW){
    ball_drop = false;
    ballspdY = -ballspdY;
    direction_choice = int(random(1,3));
    if (direction_choice == 1){
     ballspdX = 4; 
    }
    if (direction_choice == 2){
      ballspdX = -4;
    }
  }
  println(direction_choice);
  
  */
  if (bally + ballr == paddleY && ballx > paddleX + (paddleW /2) && ballx < paddleX + paddleW){
    ball_drop = false;
    ballspdY = -ballspdY;
    ballspdX = 4;
  }
  
  if (ballx + ballr > width || ballx - ballr < 0){
    ballspdX = -ballspdX;
  }
  
  if (bally - ballr < 0){
    ballspdY = -ballspdY;
  }

  if (ball_drop){
    ballspdX = 0;
    ballspdY = 1;
  }
 for (int i = 0; i < brickX.length; i ++){
   for(int j = 0; j < brickY.length; j ++){ 
   fill(red);
   rect(brickX[i], brickY[j], brickW, brickH); 
   
    if (collideLineCircle(brickX[i], brickY[j] + brickH, brickX[i] + brickW, brickY[j] + brickH, ballx, bally, ballr)){
      ballspdY = -ballspdY;
    }
   
  }
 }

  
 if (bally >= 800){
   bally = 600;
   ballx = 412.5;
   ball_drop = true;
 }
 
 
  bally += ballspdY;
  ballx += ballspdX;
  
}
  boolean collideLineCircle(float x1, float y1, float x2, float y2, float cx, float cy, float cr){
    float A = y2 - y1, B = x1 - x2, C = x2*y1 - x1+y2;
    float denom = A+A + B*B;
    if (denom == 0){
    
      return dist(x1,y1, cx, cy) < cr;
    
    }
    
    float Ix = (B*B*cx-A*B*cy - A*C)/denom, Iy = (A*A*cy-A*B*cx - B*C)/denom;
    
    if (Ix >= min(x1,x2) && Ix <= max(x1,x2) & Iy >= min(y1,y2) && Iy <= max(y1,y2)) {
      return abs (A*cx + B*cy + C)/sqrt(denom) < cr;
    }
    
    float d1 = dist(x1,y1,cx,cy), d2 = dist(x2,y2,cx,cy);
    return min(d1,d2) < cr;
    
  }

void mouseMoved(MouseEvent evt){
  paddleX = evt.getX();
  }

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

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

发布评论

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

评论(2

何其悲哀 2025-02-08 20:47:10

有多种方法可以做到。考虑到您已经拥有的数据结构是创建另一个数组,这次是布尔数组,并将其称为Brickisvisible,也许最简单的更改也许是创建另一个数组。

然后,您可以在循环中使用简单的如果语句,例如(brickisvisible [i]),以确定如何为砖块着色。可以使用相同的变量来判断球和砖之间是否进行碰撞测试。

至于着色本身,有几个选择。

我不确定您是如何绘制董事会的。如果您用每个Gameloop框架重新绘制了整个板,那么大概您首先绘制背景(覆盖在砖块上),然后绘制砖块。如果发生了什么事,则如果Brickis -Visible [i]false您可以简单地省略在背景顶部的绘制该砖块。

有时使用的另一种技术是使用透明的颜色。这是通过在颜色定义中添加 alpha通道值来完成的。因此,有四个变量而不是三个变量。它们被称为RGBA,而不是RGB。如果第四变量为0,则颜色将是透明的。如果是255,它将完全可见。如果要创建淡入淡出或淡入淡出,则可以使用序列中间值。

我猜您正在使用java.awt.color。以下是使用四个参数构造函数,第四个是alpha通道。

Color invisiblePurple = new Color(255, 0, 255, 0);

如果颜色是看不见的,则RGB值并不重要。

如今,我主要将Javafx用于图形,因此我可能不会使用AWT/Swing的所有交易技巧。使用Javafx,可以直接设置显示图形的不透明属性,这很方便。我认为在Javafx中可用的部分原因是屏幕图的结构更像是DOM树(例如HTML域对象模型),并且重新绘制是在树的当前状态下的幕后处理的,而不是对于每个对象,即带有awt/swing。

There are a number of ways this can be done. Perhaps the simplest change, given the data structures you already have is to create another array, this time a boolean array, and call it something like brickIsVisible.

Then you can use a simple if statement, e.g., if (brickIsVisible[i]) in your loops to determine how to color the brick. The same variable can be used to tell whether or not to do a collision test between the ball and the brick.

As for the coloring itself, there are a couple options.

I'm not sure how you are drawing your board. If you redraw the entire board with each gameloop frame, then presumably you first draw the background (which covers over the bricks) and then you draw the bricks. If this what is happening, then if brickIsVisible[i] is false you can simply omit drawing that brick on top of the background.

Another technique that is sometimes used is to make use of a transparent color. This is done by adding an alpha channel value to the color definition. Thus there are four variables instead of three. They are referred to as RGBA instead of RGB. If the fourth variable is 0, the color will be transparent. If it is 255 it will be fully visible. Intermediate values in sequence can be used if you want to create a fade in or fade out.

I am guessing you are using java.awt.Color. Following is an example that uses the four argument constructor, with the fourth being the alpha channel.

Color invisiblePurple = new Color(255, 0, 255, 0);

If the color is invisible, the RGB values don't really matter.

These days I mostly use JavaFX for graphics, so I might not be up on all the tricks of the trade with AWT/Swing. With JavaFX, one can directly set an opacity property for a graphic being displayed, which is pretty convenient. I think part of the reason that this is available in JavaFX is that screen graphics are structured more like a DOM tree (like an HTML domain object model), and the redrawing is handled behind the scenes on the current state of the tree, rather than explicitly for each object, as it is with AWT/Swing.

假装不在乎 2025-02-08 20:47:10

下面显示的另一种可能的技术使用了砖一类数组。该类具有与之相关的“表演”布尔值,只有在砖[id]。显示为真时才显示砖。在此演示中,当砖块被选择鼠标[id]时。显示为false,并且未显示该砖。对于您的项目,当砖被球击中时,您必须这样做。

/*
 Creates a grid of rectangles from a Brick class array.
 Syntax: brick[id] = new Brick( x, y, w, h, "title", bkgrndColor, txtColor); 
 ID is taken from position in array.
 */

final int _brickGridX = 40;
final int _brickGridY = 60;
final int _brickW = 120;
final int _brickH = 60;

color BLUE = color(64, 124, 188);
color LTGRAY = color(185, 180, 180);
color YELLOW = color(245, 250, 13);
color RED = color(255, 0, 0);
color BLACK = color(0, 0, 0);
color WHITE = color(255, 255, 255);

Brick[] brick;

class Brick {
  float x, y, w, h;
  String title;
  color bkgrnd;
  color txtColor;
  boolean show;

  // Constructor
  Brick(int xpos, int ypos, float wt, float ht, String titleStr, color background, color foreground) {
    x = xpos;
    y = ypos;
    w = wt;
    h = ht;
    title = titleStr;
    bkgrnd = background;
    txtColor = foreground;
  }

  void display(int id) {
    if (brick[id].show) {
      fill(bkgrnd); // background color
      stroke(0);
      rect(x, y, w, h);
      fill(txtColor); // text color
      textSize(42);
      textAlign(CENTER, CENTER);
      text(title, x, y, w, h);
    }
  }

  void press(int id) {
    println("brick id = ", id + ": show =", brick[id].show);
  }
}

void brickGrid() {
  int left = 0;
  int top = 0;
  int vg = 0; // Space between cols (vert.gutter)
  int hg = 0; // Space between rows (horz.gutter)
  int rows = 5; 
  int cols = 5;
  int id = 0;

  brick = new Brick[rows*cols]; // creates brick array
  for (int k = 0; k < cols; k++) {
    for (int j = 0; j < rows; j++) {
      left = _brickGridX + j * (_brickW + vg);
      top = _brickGridY + k * (_brickH + hg);      
      brick[id] = new Brick(left, top, _brickW, _brickH, str(id), LTGRAY, BLACK);
      brick[id].show = true;
      id++;
    }
  }
}

void setup() {
  size(800, 600);
  background(BLUE);
  brickGrid();
}

void draw() {
  background(BLUE);
  for (int i = 0; i < brick.length; i++) {
    brick[i].display(i);
  }
}

void mousePressed() {
  for (int i = 0; i < brick.length; i++) {
    if ((mouseX >= brick[i].x) && (mouseX <= brick[i].x + _brickW) && (mouseY >= brick[i].y) && (mouseY <= brick[i].y + _brickH)) {
      brick[i].show = false;
     // brick[i].press(i);
    }
  }
}

Another possible technique shown below uses a brick class array. The class has a 'show' boolean associated with it and the bricks are only displayed when brick[id].show is true. In this demo when the brick is selected with a mouse brick[id].show is set to false and that brick is not displayed. For your project you would have to do this when a brick is hit by the ball.

/*
 Creates a grid of rectangles from a Brick class array.
 Syntax: brick[id] = new Brick( x, y, w, h, "title", bkgrndColor, txtColor); 
 ID is taken from position in array.
 */

final int _brickGridX = 40;
final int _brickGridY = 60;
final int _brickW = 120;
final int _brickH = 60;

color BLUE = color(64, 124, 188);
color LTGRAY = color(185, 180, 180);
color YELLOW = color(245, 250, 13);
color RED = color(255, 0, 0);
color BLACK = color(0, 0, 0);
color WHITE = color(255, 255, 255);

Brick[] brick;

class Brick {
  float x, y, w, h;
  String title;
  color bkgrnd;
  color txtColor;
  boolean show;

  // Constructor
  Brick(int xpos, int ypos, float wt, float ht, String titleStr, color background, color foreground) {
    x = xpos;
    y = ypos;
    w = wt;
    h = ht;
    title = titleStr;
    bkgrnd = background;
    txtColor = foreground;
  }

  void display(int id) {
    if (brick[id].show) {
      fill(bkgrnd); // background color
      stroke(0);
      rect(x, y, w, h);
      fill(txtColor); // text color
      textSize(42);
      textAlign(CENTER, CENTER);
      text(title, x, y, w, h);
    }
  }

  void press(int id) {
    println("brick id = ", id + ": show =", brick[id].show);
  }
}

void brickGrid() {
  int left = 0;
  int top = 0;
  int vg = 0; // Space between cols (vert.gutter)
  int hg = 0; // Space between rows (horz.gutter)
  int rows = 5; 
  int cols = 5;
  int id = 0;

  brick = new Brick[rows*cols]; // creates brick array
  for (int k = 0; k < cols; k++) {
    for (int j = 0; j < rows; j++) {
      left = _brickGridX + j * (_brickW + vg);
      top = _brickGridY + k * (_brickH + hg);      
      brick[id] = new Brick(left, top, _brickW, _brickH, str(id), LTGRAY, BLACK);
      brick[id].show = true;
      id++;
    }
  }
}

void setup() {
  size(800, 600);
  background(BLUE);
  brickGrid();
}

void draw() {
  background(BLUE);
  for (int i = 0; i < brick.length; i++) {
    brick[i].display(i);
  }
}

void mousePressed() {
  for (int i = 0; i < brick.length; i++) {
    if ((mouseX >= brick[i].x) && (mouseX <= brick[i].x + _brickW) && (mouseY >= brick[i].y) && (mouseY <= brick[i].y + _brickH)) {
      brick[i].show = false;
     // brick[i].press(i);
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文