Java 在不使用二维阵列进行处理的情况下,对砖阵列进行碰撞并生成砖栅格
我对处理技术非常陌生,在大学里的一个项目中,我们需要制作某种动画或游戏。我已经选择为我的动画制作突破,但是我的代码有两部分有问题。第一个问题是,我正试图让砖块以5×6的网格排列在屏幕顶部。我从我的讲师那里得到了一些帮助,但它只排了两行,我想其余的部分都不在屏幕上了。我遇到的第二个问题是,为了我的球和砖块,我要做的是检测命中。我认为这是因为砖块是一个阵列,而不是一个单一的对象,我不知道如何去做。任何帮助或建议都将不胜感激。下面是我到目前为止所做的 主代码Java 在不使用二维阵列进行处理的情况下,对砖阵列进行碰撞并生成砖栅格,java,processing,Java,Processing,我对处理技术非常陌生,在大学里的一个项目中,我们需要制作某种动画或游戏。我已经选择为我的动画制作突破,但是我的代码有两部分有问题。第一个问题是,我正试图让砖块以5×6的网格排列在屏幕顶部。我从我的讲师那里得到了一些帮助,但它只排了两行,我想其余的部分都不在屏幕上了。我遇到的第二个问题是,为了我的球和砖块,我要做的是检测命中。我认为这是因为砖块是一个阵列,而不是一个单一的对象,我不知道如何去做。任何帮助或建议都将不胜感激。下面是我到目前为止所做的 主代码 PImage bg; PlayerName
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;
}
}
}
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);
}
}
公共类桨{
私人浮动XPO、YPO、PALLEH、PALLEW;
公共桨(浮子桨W、浮子桨H){
设置桨W(桨W);
赛斯帕勒;
xPos=宽度/2;
yPos=高度-此为H;
}
公众空虚运动{
xPos=mouseX-pailew/2;
如果(xPos<0){
xPos=0;
}
如果(xPos>(宽度-W)){
xPos=宽度-w;
}
}
公共空间显示(){
填充(#320048);
仰泳();
rect(xPos、yPos、pallew、palleh);
}
公共浮点getXPos(){
返回XPO;
}
公共浮点数getYPos(){
返回YPO;
}
公共浮点数{
返回桨W;
}
公众浮标{
回桨;
}
公共无效SETXPO(浮动XPO){
this.xPos=xPos;
}
公共无效设置yPos(浮动yPos){
this.yPos=yPos;
}
公共无效设置挡板W(浮动挡板W){
如果((桨叶w>=30)&&(桨叶w=10)&(桨叶h我看不到所有内容,但请在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++;
}
intxforbrick=0;
for(int col=0;col
这是一大堆代码。请编译一个。这并不意味着将文章删减到只“相关”代码。我的意思是,开始一个全新的项目,添加功能,直到问题出现,然后发布新项目,在那里问题与您的其他代码隔离。您显然在这个项目中投入了大量的工作,但您问题的格式使我们很难同时帮助您和未来的用户(这里真正的观众是谁)。我向你保证,敦促你阅读和应用指南的评论并不是把关。这个社区非常有帮助,但我们也致力于帮助有类似问题的未来用户,并通过搜索找到你的问题。通过改进你的问题,你将使我们更容易提供帮助,也让子孙后代更容易学习你的经验。当你编辑你的问题时,给我贴上标签,我很乐意帮助你。@CharlieArmstrong非常感谢你,我会记住这一点,因为当我寻求未来的帮助时。我想我只会使用我在分配任务一周后制作的备份游戏并提交。我决定进行突破,因为我认为这对我来说会很有趣试一试。@laancelot感谢您的回答。但如果您不介意我如何设置我的问题格式?我应该把它缩短并直截了当地说出来吗?或者,如果您不介意我问的话,是否有某种方法可以解决这个问题。@laancelot非常感谢您,我非常感谢。我将查看您评论中的链接,然后我将下次会更好。也谢谢你友好的回复。