📄 strikebrick1.java
字号:
public void paint(Graphics g1)
{
Graphics2D g=(Graphics2D)g1;
g.setColor(c);
g.fill(new Ellipse2D.Double(x,y,r,r));
if(pause&&!drawStage)
{
g.setColor(Color.white);
g.setFont(font1);
g.drawString("暂 停",280,400);
}
if(drawStage)
{
g.setColor(Color.white);
g.setFont(new Font("黑体",Font.PLAIN,20));
g.drawString("第 "+stage+" 关",250,400);
}
}
//****************************重绘小球,碰撞事件
public void run()
{
double deltX;double deltY;
while(thread!=null)
{
deltX=speed*Math.cos(theta*Math.PI/180);
deltY=speed*Math.sin(theta*Math.PI/180);
x=x+deltX;y=y-deltY;
//砖块反射
int i=(int)((y-50)/20);int j=(int)((x-50)/50);
if(i>=0&&i<=7&&j>=0&&j<=9)
{
if(brick.bricks[i][j]!=0)
{
if((scores+100)%5000==0) optionpane.txtLife.setText(""+(lifes++));
scores+=100;
optionpane.txtScroe.setText(""+scores);
if(deltX>=0&&deltY>0)
{
if(x>50+j*50-ball.r) theta=-theta;
if(y<=65+i*20) theta=180-theta;
}
else if(deltX>=0&&deltY<0)
{
if(x>50+j*50-ball.r-5) theta=180-theta;
if(y>=50+i*20-ball.r-10) theta=-theta;
}
else if(deltX<0&&deltY>=0)
{
if(x<=95+j*50) theta=-theta;
if(y<65+i*20) theta=180-theta;
}
else if(deltX<0&&deltY<=0)
{
if(x<=95+j*50) theta=180-theta;
if(y>50+i*20-ball.r-10) theta=-theta;
}
//if(x>95+j*50-ball.r||x<50+j*50) theta=180-theta;
//if(y>65+i*20-ball.r||y<50+i*20) theta=-theta;
}
if(brick.bricks[i][j]>0) brick.bricks[i][j]--;
else if(brick.bricks[i][j]<0)
{
if(brick.bricks[i][j]==-1) special=new Special("B",j*50+15,i*20+15);
else if(brick.bricks[i][j]==-2) special=new Special("S",j*50+15,i*20+15);
else if(brick.bricks[i][j]==-3) special=new Special("L",j*50+15,i*20+15);
GamePane.add(special);
brick.bricks[i][j]=0;
}
}
//墙壁反射
if(x>600-r||x<=0) theta=180-theta;
if(y<=2) theta=-theta;
else if(y>530-r&&y<535&&x>baffle.x&&x<baffle.x+baffle.length)
{
theta=-theta;y=530-r;
if(baffle.direction==-1) theta-=10;
else if(baffle.direction==1) theta+=10;
}
else if(y>560)
{
ball.ballReset();baffle.baffleReset();
lifes--;
if(lifes>=0) optionpane.txtLife.setText(""+lifes);
timer.stop();pause=true;
if(lifes<0)
{
JOptionPane.showMessageDialog(null,"<html><font face='隶书' size=6 color=blue>游戏结束</font></html>");
new Record(scores);
stage=1;gameReset();
}
}
//过关
if(brick.isEmpty())
{
timer.stop();
stage++;pause=true;
ball.ballReset();ball.drawStage=true;
baffle.baffleReset();
GamePane.removeAll();
GamePane.add(ball);GamePane.add(baffle);
brick=new Brick(stage);GamePane.add(brick);
optionpane.txtLevel.setText(""+stage);
//optionpane.txtScroe.setText("0");
getRootPane().updateUI();
}
repaint();
try
{
Thread.currentThread().sleep(sleeptime);
}
catch(Exception err){}
}
}
public void pause()
{
sleeptime=100000000;
}
public void restart()
{
sleeptime=11;thread.interrupt();
}
public void ballReset()
{
r=10;
x=295;
y=520;
theta=60;
sleeptime=1000000000;
c=Color.yellow;
speed=5;
}
public void doubleSize()
{
r=20;
}
public void speedUp()
{
speed+=5;
}
}
//********************************************特殊砖块*********************************************//
class Special extends JLabel implements Runnable
{
String drawStr;
protected int y=0;
protected int x=0;
protected Thread thread=null;
public Special(String str,int xx,int yy)
{
setSize(50,550);
x=xx+50;
setLocation(x,0);
drawStr=str;y=yy+45;
thread=new Thread(this);
thread.start();
}
public void paint(Graphics g1)
{
Graphics2D g=(Graphics2D)g1;
g.setColor(Color.red);
g.setFont(new Font("impact",Font.BOLD,20));
g.drawString(drawStr,0,y);
}
public void run()
{
while(thread!=null)
{
y++;
if(y+15>530&&y<535&&baffle.x<x+10&&(baffle.x+baffle.length)>x)
{
if(drawStr.equals("L")) baffle.addLength();
else if(drawStr.equals("B")) ball.doubleSize();
else if(drawStr.equals("S")) ball.speedUp();
y=580;
}
if(y>580) thread=null;
repaint() ;
try
{
Thread.currentThread().sleep(20);
}
catch(Exception err){}
}
}
public String getChars()
{
return drawStr;
}
}
}
//********************************************档板类*********************************************//
class Baffle extends JLabel
{
protected int x=250;
protected int length=100;
protected int direction=0;
public Baffle()
{
setLocation(0,530);
setSize(600,5);
}
public void paint(Graphics g1)
{
Graphics2D g=(Graphics2D)g1;
g.setColor(Color.white);
g.fillRect(x,0,length,5);
g.setColor(Color.yellow);
}
public void baffleReset()
{
x=250;
length=100;
}
public void addLength()
{
length+=100;
if(x>500) x=500;
}
public void moveLeft()
{
if(x>0) x-=10;
}
public void moveRight()
{
if(x<600-length) x+=10;
}
public int baffleX()
{
return x;
}
}
//********************************************砖块*********************************************//
class Brick extends JLabel
{
int bricks[][];
public Brick(int stage)
{
setSize(500,160);
setLocation(50,50);
bricks=new StageArrange().getArrangment(stage);
}
public void paint(Graphics g1)
{
Graphics2D g=(Graphics2D)g1;
for(int i=0;i<8;i++)
{
for(int j=0;j<10;j++)
{
if(bricks[i][j]==1) g.setColor(new Color(255,128,192));
else if(bricks[i][j]==2) g.setColor(new Color(128,255,0));
else if(bricks[i][j]==3) g.setColor(new Color(255,255,128));
else if(bricks[i][j]==4) g.setColor(new Color(0,128,192));
if(bricks[i][j]>0) g.fillRect(j*50,i*20,45,15);
g.setColor(Color.white);
if(bricks[i][j]<0)
{
g.fillRect(j*50,i*20,45,15);
g.setColor(Color.red);
g.setFont(new Font("Arial Black",Font.BOLD,20));
if(bricks[i][j]==-1) g.drawString("B",j*50+15,i*20+15);
else if(bricks[i][j]==-2) g.drawString("S",j*50+15,i*20+15);
else if(bricks[i][j]==-3) g.drawString("L",j*50+15,i*20+15);
}
}
}
}
public boolean isEmpty()
{
for(int i=0;i<8;i++)
{
for(int j=0;j<10;j++)
{
if(bricks[i][j]!=0) return false;
}
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -