Ich arbeite an einem Breakout-Spiel. Ich habe fast alles außer der Kollision für die Ziegel getan. Wenn der Ball den Stein trifft, springt der Stein zurück, aber der Stein verschwindet nicht. Wenn mir jemand helfen könnte, wäre das großartig.Brick Kollision für Breakout in Java
Hauptklasse:
public class Breakout extends Applet implements Runnable{
Thread thread = new Thread(this);
boolean running = true;
Brick2 b;
Paddle p;
Ball ba;
Image dbImage;
Graphics dbg;
public void init(){
setSize(800,600);
b = new Brick2();
p = new Paddle(this);
ba = new Ball(this);
}
public void start(){
thread.start();
}
public void destroy(){
running = false;
}
public void stop(){
running = false;
}
public void run(){
while(running){
b.update(ba);
p.update(this);
ba.update(this,p);
repaint();
try{
thread.sleep(20);
}
catch (InterruptedException e){
System.out.println("AN ERROR HAS OCCURED");
}
}
}
public void update(Graphics g){
dbImage = createImage(getWidth(),getHeight());
dbg = dbImage.getGraphics();
paint(dbg);
g.drawImage(dbImage,0,0,this);
}
public void paint(Graphics g){
g.fillRect(0,0,800,600);
b.paint(g);
p.paint(g,this);
ba.paint(g,this);
}
}
Brick Klasse:
public class Brick2{
private int x;
private int y;
public Brick2(){
}
public void update(Ball ba){
collision(ba);
}
public void collision(Ball ba){
int bX = ba.getX();
int bY = ba.getY();
int bHeight = ba.getImageHeight();
int bWidth = ba.getImageWidth();
for(int x=0; x <= 800; x+=55){
for (int y=0; y<= 100; y+=25){
if (bX-bWidth<=x && bX+bWidth>=x && bY-bHeight<=y && bY+bHeight>=y){
ba.setXVel(6);
}
}
}
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public void paint(Graphics g){
for(int x=0; x <= 800; x+=55){
for (int y=0; y<= 100; y+=25){
g.setColor(Color.RED);
g.fillRect(x,y,50,20);
}
}
} }
Kugel Klasse:
public class Ball {
private int x=355 ;
private int y=200;
private int speed = 6;
private int xVel = -speed;
private int yVel = speed;
private boolean gameOver = false;
private Image ball;
public Ball (Breakout bR){
ball = bR.getImage(bR.getDocumentBase(),"ball.png");
}
public void update(Breakout bR, Paddle p){
x += xVel;
y += yVel;
if (x < 0){
xVel = speed;
}
else if (x > bR.getWidth()){
xVel = -speed;
}
if(y > bR.getHeight()){
gameOver = true;
}
else if (y < 0){
yVel = speed;
}
collision(p);
}
public void collision(Paddle p){
int pX = p.getX();
int pY = p.getY();
int pHeight = p.getImageHeight();
int pWidth = p.getImageWidth();
if (pX<=x && pX+pWidth>=x && pY-pHeight<=y && pY+pHeight>=y){
yVel = -speed;
}
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public int getImageWidth(){
return ball.getWidth(null);
}
public int getImageHeight(){
return ball.getHeight(null);
}
public void setXVel(int xv){
yVel = xv;
}
public void paint (Graphics g, Breakout bR){
g.drawImage(ball,x,y,bR);
if (gameOver){
g.setColor(Color.WHITE);
g.drawString("Game Over", 100,300);
}
}
}
Vielen Dank für Ihre Hilfe.
Siehe [Erkennung/Fix für die hängenden schließen Klammer eines Codeblocks] (http://meta.stackexchange.com/q/251795/155831) für ein Problem, das ich nicht mehr gestört werden könnte zu reparieren. –
1) Warum ein Applet codieren? Wenn es aufgrund der Angabe des Lehrers geschieht, verweisen Sie bitte auf [Warum CS-Lehrer ** aufhören sollten ** Java-Applets zu unterrichten] (http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should -stop-teaching-Java-Applets /). 2) Siehe [Java Plugin-Unterstützung nicht mehr unterstützt] (http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) und [Wechsel zu einem Plugin-freien Web] (https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free). .. –
.. 3) Warum AWT verwenden? Siehe [diese Antwort] (http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) aus vielen guten Gründen, AWT mit Komponenten zugunsten von Swing zu verlassen. –