我正在制作突破游戏,它不会仅仅显示砖块和球拍和球。我认为它在breakin类中的for语句中。我在paddles.getW,paddles中添加了。得到Y + paddles.getH,对于下一个语句,我添加了balls.getD。
import java.awt.*;
import java.applet.*;
public class Breakin extends Applet implements Runnable
{
/**
*
*/
private static final long serialVersionUID = 1L;
Thread k;
int rows=7;
int columns=8;
Paddle paddles=new Paddle(50,200,10,40);
Ball balls=new Ball (40,150,10);
Brick [] [] bricks =new Brick [rows][columns];
Graphics dbg;
Image dbImage;
Boolean win=false;
public void init(){
int xin=0;
int yin=-15;
for(int r=0; r<rows; r++){
yin +=15;
xin=0;
for(int c=0; c<columns; c++){
int game=(int)(Math.random()*4)+1;
switch(game){
case 1:bricks[r][c]=new Brick(xin,yin,(Color.yellow),20,10);break;
case 2:bricks[r][c]=new Brick(xin,yin,(Color.green),20,10);break;
case 3:bricks[r][c]=new Brick(xin,yin,(Color.orange),20,10);break;
case 4:bricks[r][c]=new Brick(xin,yin,(Color.red),20,10);break;
}
xin+=25;
}
}
balls.setVelocity(2, 2);
}
public void start(){
k=new Thread(this);
if(k!=null){
k.start();
}
}
public void run1 ()
{
while (true)
{
balls.updateVelocity();
paddles.updateXVelo();
if (balls.intersects(paddles.getX(),paddles.getY(),paddles.getX() + paddles.getW(),paddles.getY()+ paddles.getH()));
balls.flipX();
if(balls.getX()<0 || balls.getX()>(200-balls.getD()))
balls.flipX();
for(int r=0; r<rows; r++){
for(int c=0; c<columns; c++){
if(bricks[r][c]!=null){
if(bricks[r][c].intersects(balls.getX(),balls.getY(),balls.getX()+balls.getD(),balls.getY()+ balls.getD())){
bricks[r][c]=null;
balls.flipY();}
}
}
}
repaint();
try{
Thread.sleep(20);
}
catch(InterruptedException ex)
{
}
}
}
public void destory ()
{
k.stop();
}
public void update(Graphics g)
{
if(dbImage==null)
{
dbImage=createImage(this.getSize().width, this.getSize().height);
dbg=dbImage.getGraphics();
}
dbg.setColor(getBackground());
dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
dbg.setColor(getForeground());
paint(dbg);
g.drawImage(dbImage,0,0,this);
}
public boolean keyDown(Event e, int key)
{
if(key==Event.LEFT)
{
paddles.setVelocity(0);
if (paddles.getX()>0)
paddles.setVelocity(-3);
}
if (key==Event.RIGHT)
{
paddles.setVelocity(0);
if (paddles.getX()<200-paddles.getW())//check this out
paddles.setVelocity(3);
}
return true;
}
public boolean keyUp(Event e, int Key)
{
paddles.setVelocity(0);
return true; //check
}
public void paint(Graphics g){
for(int r=0; r<rows; r++){
for(int c=0; c<columns; c++){
if(bricks[r][c]!=null){
bricks[r][c].drawBrick(g);
}
}
}
balls.pinT(g);
paddles.showT(g);
}
@Override
public void run() {
// TODO Auto-generated method stub
}
}
Paddle.java:
import java.awt.*;
public class Paddle
{
private int x;
private int y;
private int h;
private int w;
private int xVelocity;
public Paddle (int x, int y, int h, int w){
this.x=x;
this.y=y;
this.w=w;
this.h=h;
}
public void begin(int d, int a){
if(d==1){
x+=a;
}
if (d<1){
x-=a;
}
}
public void updateXVelo(){
x+=xVelocity;
}
public void showT(Graphics g)
{
g.setColor(Color.red);
g.fillRect(x, y, w, h);
g.setColor(Color.black);
g.drawRect(x,y,w,h);
}
public int getH(){
return h;
}
public int getW(){
return w;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public void setVelocity(int x){
xVelocity=x;
}
}
Ball.java:
import java.awt.*;
public class Ball{
public int x;
public int y;
public int d;
public int yvelocity;
public int xvelocity;
public Ball (int x, int y, int d)
{
this.x=x;
this.y=y;
this.d=d;
}
public void pinT(Graphics g)
{
g.setColor(Color.green);
g.fillOval(x,y,d,d);
g.setColor(Color.black);
}
public void flipX(){
xvelocity=-xvelocity;
}
public void flipY(){
yvelocity=-yvelocity;
}
public void setX(int x){
this.x=x;
}
public void setY(int y){
this.y=y;
}
public int getX(){
return x;
}
public int getD(){
return d;
}
public int getY(){
return y;
}
public void setVelocity(int x, int y){
xvelocity=x;
yvelocity=y;
}
public void updateVelocity(){
y+=yvelocity;
x+=xvelocity;
}
public boolean intersects(int x1,int y1,int x2,int y2)
{
return(x2>=x)&&(x+d>=x1)&&(y2>=y)&&(y+d>=y1);
}
}
Brick.java:
import java.awt.*;
public class Brick {
private int x;
private int y;
private Color color=(Color.red);
int w;
int h;
public Brick(int x, int y, Color color, int w, int h){
this.x=x;
this.y=y;
this.color=color;
this.h=h;
this.w=w;
}
public boolean intersects(int x1, int y1, int x2, int y2)
{
return( x2>=x)&&(x+w>=x1)&&(y2>=y)&&(y+h>y1);
}
public void setX(int x){
this.x=x;
}
public void setY(int y){
this.y=y;
}
public void setColor (Color color){
this.color=color;//the :
}
public void drawBrick(Graphics g){
g.setColor(color);
g.fillRect(x, y, w, h);
g.setColor(Color.black);
g.drawRect(x,y,w,h);
}
}