java—用于砖块数组的冲突,并在不使用2d数组进行处理的情况下生成砖块网格

rhfm7lfc  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(199)

我是一个非常新的处理和一个项目,在大学里,我们需要做一个动画或某种游戏。我已经选择了使我的动画突破,但我有两个部分的问题,我的代码。第一个是我试图让砖块在屏幕的顶部以5×6的网格排列。我从我的讲师那里得到了一些帮助,但它只排了两行,我想剩下的部分都不在屏幕上了。我遇到的第二个问题是如何让击球检测系统为我的球和砖块工作。我想是因为砖块排成一排,没有一个物体,我不知道该怎么做。任何帮助或建议都将不胜感激。下面是我到目前为止所做的。
主代码

PImage bg;
PlayerName pl;
Ball bl;
Paddle pad;
Brick[] bricks;
int numOfCols = 30; // Number of bricks I want in total
int distRows = 1; // The distance between each of the bricks below each other

void setup() {
  size(1280, 720);
  noCursor();
  bg = loadImage("space.jpg");
  pl = new PlayerName(JOptionPane.showInputDialog("Please enter your name (max of 12 char): "));
  bl = new Ball(51);
  pad = new Paddle(300,15);
  // Below is my attempt to try make the bricks spawn across the screen but it only does 2 rows
  bricks = new Brick[numOfCols];
  int xForBrick = 0;
  for(int col = 0; col < numOfCols; col++){
    if(col == 5)
    {
      distRows = 15;
      xForBrick = 0;
    }
    bricks[col] = new Brick(xForBrick*256,distRows);
    xForBrick++;

   }

  }

void draw() {
  background(bg);
   // Ignore these values below. This was just to help me visual stuff for hit detection.
  //text(pl.getName(),500,500);
  text(pad.getYPos(), 500,500);
  text(pad.getXPos(), 525,525);
  text(bl.getXCoord(), 500, 550);
  text(bl.getYCoord(), 525, 575);
  bl.display();  //Dsiplays the ball
  bl.gameOver(); // Controls the balls movement and ends game if ball goes off screen
  pad.display(); // Displays the paddle
  pad.movement(); //Controls the movement of the paddle with the mouse

  for(int col = 0; col < numOfCols; col++){
  bricks[col].display();
  }

  boolean collision = onHit(pad,bl);
  if (collision == true){
    bl.bounce();
  }

  boolean collision2 = onHit2(bl,bricks);
    if(collision2 == true){
    bl.bounce();
  }
}

boolean onHit(Paddle pad, Ball bl){
  float ballDistanceX = abs(bl.getXCoord() - pad.getXPos() - pad.getPaddleW()/2);
  float ballDistanceY = abs(bl.getYCoord() - pad.getYPos());

  if(ballDistanceX > (pad.getPaddleW()/2 + bl.getSize()/2)){
    return false;
  }

  if (ballDistanceY > (pad.getPaddleH()/2)){
    return false;
  }

  return true;
}

boolean onHit2(Ball bl, Brick bricks){
    float ballDistanceX2 = abs(bl.getXCoord() - bricks.getXPos() - bricks.getSizeW()/2);
    float ballDistanceY2 = abs(bl.getYCoord() - bricks.getSizeH());

    if(ballDistanceX2 > (bricks.getSizeW() + bl.getSize()/2)){
      return false;
    }
    if(ballDistanceY2 > (bricks.getSizeH()/2)){
      return false;
    }
    return true;
}

球类

public class Ball {
  private float xCoord, yCoord, size, veloX, veloY;

  Ball(float size) {
    setSize(size);
    death();
  }

  public boolean gameOver() {
    boolean lose = false;
    xCoord = xCoord + veloX;
    yCoord = yCoord + veloY;

    //When ball leaves screen, ball resets
    if (yCoord > height + size/2) {
      death();
      lose = true;
    }
    // Makes ball bounce off of right edge
    if (xCoord > width - size/2) {
      xCoord = width - size/2;
      veloX = veloX * -1;
    }
    //Makes ball bounce off of left edge
    if (xCoord < size/2) {
      xCoord = size/2;
      veloX = veloX * -1;
    }
    // Makes it so the ball bounces off the top edge
    if (yCoord <  size/2) {
      yCoord =  size/2;
      veloY = veloY * -1;
    }
    return lose;
  }

  public void display() {
    fill(120, 70, 200);
    noStroke();
    ellipse(xCoord, yCoord, size, size);
  }

  //Makes ball bounce off of paddle
  public void bounce() {
    veloY = veloY * -1;
    yCoord = yCoord + veloY;
  }

  //When ball goes off the bottom of the screen, it resets back to the center with a different velocity
  private void death() {
    xCoord = width/2;
    yCoord = height/2;
    veloX = 3;
    veloY = -3;
  }

  public float getXCoord() {
    return xCoord;
  }

  public float getYCoord() {
    return yCoord;
  }

  public float getSize() {
    return size;
  }

  public float getVeloX() {
    return veloX;
  }

  public float getVeloY() {
    return veloY;
  }

  public void setXCoord(float xCoord) {
    this.xCoord = xCoord;
  }

  public void setYCoord(float yCoord) {
    this.yCoord = yCoord;
  }

  public void setSize(float size) {
    if ((size >= 30) && (size <= 50)) {
      this.size = size;
    } else {
      this.size = 30;
    }
  }

  public void setVeloX(float veloX) {
    this.veloX = veloX;
  }

  public void setVeloY(float veloY) {
    this.veloY = veloY;
  }
}

砖类

public class Brick{
  private float xPos,yPos,sizeW,sizeH;
  boolean isShown;

  Brick(float xPos, float yPos){
    this.xPos = xPos;
    this.yPos = yPos;
    sizeW = 256;
    sizeH = 15;
  }

  public void display(){
    fill(#8F52EC);
    stroke(0);
    strokeWeight(2);
    rect(xPos, yPos, sizeW, sizeH);
  }

  public float getXPos(){
    return xPos;
  }

  public float getYPos(){
    return yPos;
  }

  public float getSizeW(){
    return sizeW;
  }

  public float getSizeH(){
    return sizeH;
  }

  public boolean getIsShown(){
    return isShown;
  }

  public void setXPos(float xPos){
    this.xPos = xPos;
  }

  public void setYPos(float yPos){
    this.yPos = yPos;
  }

  public void setSizeW(float sizeW){
    this.sizeW = sizeW;
  }

  public void setSizeH(float sizeH){
    this.sizeH = sizeH;
  }
}

桨类

public class Paddle{
  private float xPos, yPos, paddleH, paddleW;

  public Paddle(float paddleW, float paddleH){
    setPaddleW(paddleW);
    setPaddleH(paddleH);

    xPos = width/2;
    yPos = height - this.paddleH;
  }

  public void movement(){
    xPos = mouseX - paddleW/2;

    if (xPos < 0){
      xPos = 0;
    }
    if (xPos > (width - paddleW)){
      xPos = width - paddleW;
    }
  }

  public void display(){
    fill(#320048);
    noStroke();
    rect(xPos,yPos,paddleW,paddleH);
  }

  public float getXPos(){
    return xPos;
  }

  public float getYPos(){
    return yPos;
  }

  public float getPaddleW(){
    return paddleW;
  }

  public float getPaddleH(){
    return paddleH;
  }

  public void setXPos(float xPos){
    this.xPos = xPos;
  }

  public void setYPos(float yPos){
    this.yPos = yPos;
  }

  public void setPaddleW(float paddleW){
    if ((paddleW >= 30) && (paddleW <= width/2)){
      this.paddleW = paddleW;
    }
    else {
      this.paddleW = 300;
    }
  }

  public void setPaddleH(float paddleH){
    if ((paddleH >= 10) && (paddleH <= 20)){
      this.paddleH = paddleH;
    }
    else {
      this.paddleH = 15;
    }
  }
}

playername类

public class PlayerName{
  private String name;

  public PlayerName(String name){
    if(name.length() < 12){
      this.name = name;
    }
    else{
      this.name = name.substring(0,12);
    }
  }

  public String getName(){
    return name;
  }
  public void setName(String name){
    this.name = name.substring(0,12);
  }
}
vuv7lop3

vuv7lop31#

我无法看到所有内容,但请尝试在main中进行以下更改:

int xForBrick = 0;
for(int col = 0; col < numOfCols; col++)
{
    if(col %  5 == 0) //change here
    {
        distRows = distRows + 1; //change here
        xForBrick = 0;
    }
    bricks[col] = new Brick(xForBrick*256,distRows);
    xForBrick++;
}

相关问题