📄 strikebrick.java
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.awt.geom.*;
import java.net.*;
class StrikeBrick extends JFrame
{
Font font1=new Font("宋体",Font.PLAIN,15);
Font font2=new Font("impact",Font.BOLD,20);
Color color1=new Color(0,64,128);
OptionPane optionpane=new OptionPane();
JPanel GamePane=null;
Ball ball=null;
Baffle baffle=null;
Brick brick=null;
boolean pause=true; //暂停状态
protected int width=800;
protected int height=600;
protected Timer timer=null;
protected Special special=null;
protected int stage=1;
protected int lifes=5;
protected int scores=0;
protected int times=0;
public StrikeBrick()
{
setTitle("打砖块");
setBounds(100,50,width,height);
setResizable(false);
Container con=getContentPane();
con.setLayout(null);
JMenuBar menuBar=new JMenuBar();
menuBar.setBackground(Color.white);
JMenu jm1=new JMenu("游 戏(G)");
jm1.setForeground(color1);jm1.setBackground(Color.white);jm1.setFont(font1);
JMenu jm2=new JMenu("关 于(A)");
jm2.setForeground(color1);jm2.setBackground(Color.white);jm2.setFont(font1);
JMenu jm3=new JMenu("等级");
jm3.setForeground(color1);jm3.setBackground(Color.white);jm3.setFont(font1);
jm1.setMnemonic('G');
jm2.setMnemonic('A');
JMenuItem j1=new JMenuItem("开局");
JMenuItem j2=new JMenuItem("初级");
JMenuItem j3=new JMenuItem("中级");
JMenuItem j4=new JMenuItem("高级");
JMenuItem j5=new JMenuItem("退出");
JMenuItem j6=new JMenuItem("关于…");
JMenuItem j7=new JMenuItem("排行榜");
JMenuItem j8=new JMenuItem("帮助");
j1.setForeground(color1);j1.setBackground(Color.white);j1.setFont(font1);
j2.setForeground(color1);j2.setBackground(Color.white);j2.setFont(font1);
j3.setForeground(color1);j3.setBackground(Color.white);j3.setFont(font1);
j4.setForeground(color1);j4.setBackground(Color.white);j4.setFont(font1);
j5.setForeground(color1);j5.setBackground(Color.white);j5.setFont(font1);
j6.setForeground(color1);j6.setBackground(Color.white);j6.setFont(font1);
j7.setForeground(color1);j7.setBackground(Color.white);j7.setFont(font1);
j8.setForeground(color1);j8.setBackground(Color.white);j8.setFont(font1);
j1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_K,ActionEvent.CTRL_MASK));
j2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1,ActionEvent.CTRL_MASK));
j3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2,ActionEvent.CTRL_MASK));
j4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_3,ActionEvent.CTRL_MASK));
j5.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T,ActionEvent.CTRL_MASK));
j6.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B,ActionEvent.CTRL_MASK));
j7.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,ActionEvent.CTRL_MASK));
j8.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,ActionEvent.CTRL_MASK));
jm3.add(j2);jm3.add(j3);jm3.add(j4);
jm1.add(j1);jm1.add(jm3);jm1.add(j5);
jm2.add(j6);jm2.add(j7);jm2.add(j8);
menuBar.add(jm1);menuBar.add(jm2);
setJMenuBar(menuBar);
j1.addActionListener(new MenuSelect());j2.addActionListener(new MenuSelect());
j3.addActionListener(new MenuSelect());j4.addActionListener(new MenuSelect());
j5.addActionListener(new MenuSelect());j6.addActionListener(new MenuSelect());
j7.addActionListener(new MenuSelect());j8.addActionListener(new MenuSelect());
GamePane=new JPanel();
GamePane.setLayout(null);
brick=new Brick(stage);GamePane.add(brick);
baffle=new Baffle();GamePane.add(baffle);
ball=new Ball();GamePane.add(ball);
//Toolkit tk=Toolkit.getDefaultToolkit();
//GamePane.setCursor(tk.createCustomCursor(tk.getImage(""),new Point(0,0),null));
//JLabel bgimg=new JLabel(new ImageIcon("back1.jpg"));
//bgimg.setBounds(0,0,600,550);
//GamePane.add(bgimg);
GamePane.setBounds(0,0,600,550);
GamePane.setBackground(new Color(0,20,40));
con.add(GamePane);
con.add(optionpane);
setVisible(true);
timer=new Timer(1000,new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
optionpane.txtTime.setText(""+times++);
}
});
addKeyListener(new Keyevent());
GamePane.addMouseMotionListener(new Mousevent());
}
//键盘事件类
class Keyevent implements KeyListener
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_LEFT&&baffle.x>=0) baffle.x-=15;
else if(e.getKeyCode()==KeyEvent.VK_RIGHT&&baffle.x<=494) baffle.x+=15;
else if(e.getKeyCode()==KeyEvent.VK_SPACE)
{
if(pause)
{
ball.drawStage=false;
ball.restart();
pause=false;timer.start();
}
else
{
ball.pause();
pause=true;timer.stop();
}
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
}
//鼠标事件类
class Mousevent implements MouseMotionListener
{
int mousetemp=0;
public void mouseDragged(MouseEvent e){}
public void mouseMoved(MouseEvent e)
{
if(!pause)
{
if(e.getX()<mousetemp) {baffle.moveLeft();baffle.direction=-1;}
else if(e.getX()>mousetemp) {baffle.moveRight();baffle.direction=1;}
else baffle.direction=0;
mousetemp=e.getX();
}
}
}
//菜单事件
class MenuSelect implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String choice=((JMenuItem)e.getSource()).getText();
choice=choice.trim();
if(choice.equals("退出"))
{
System.exit(0);
}
else if(choice.equals("初级"))
{
}
else if(choice.equals("中级"))
{
}
else if(choice.equals("高级"))
{
}
else if(choice.equals("开局"))
{
gameReset();
}
else if(choice.equals("排行榜"))
{
new Record();
}
else if(choice.equals("帮助"))
{
JOptionPane.showMessageDialog(null," 操作说明\n开 局:Ctrl+K、空格、回车\n暂 停:空格\n设置难度:初级Ctrl+1、中级Ctrl+2、高级Ctrl+3\n查看版本:Ctrl+B\n排 行 榜:Ctrl+P\n查看帮助:Ctrl+H\n退 出:Esc、Ctrl+T");
}
else if(choice.equals("关于…"))
{
new AboutMe(" 打 砖 块 ");
}
}
}
//主方法
public static void main(String args[])
{
StrikeBrick sb=new StrikeBrick();
sb.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
//重新开始
public void gameReset()
{
ball.ballReset();ball.drawStage=true;
baffle.baffleReset();pause=true;
GamePane.removeAll();
GamePane.add(ball);GamePane.add(baffle);
brick=new Brick(stage);GamePane.add(brick);
lifes=5;times=0;scores=0;stage=1;
optionpane.txtLife.setText(""+lifes);
optionpane.txtTime.setText(""+times);
optionpane.txtScroe.setText(""+scores);
optionpane.txtLevel.setText(""+stage);
getRootPane().updateUI();
}
//***********************************************右面板**********************************************//
class OptionPane extends JPanel
{
JLabel labelLife=new JLabel("球 数:");
JLabel labelTime=new JLabel("时 间:");
JLabel labelScroe=new JLabel("分 数:");
JLabel labelLevel=new JLabel("关 数:");
JLabel txtLife=new JLabel("5");
JLabel txtTime=new JLabel("0");
JLabel txtScroe=new JLabel("0");
JLabel txtLevel=new JLabel("1");
public OptionPane()
{
setBounds(610,0,180,550);
setBackground(Color.white);
setBorder(new EtchedBorder(EtchedBorder.LOWERED,Color.white,Color.gray));
labelLife.setFont(font1);labelLife.setForeground(Color.black);
labelTime.setFont(font1);labelTime.setForeground(Color.black);
labelScroe.setFont(font1);labelScroe.setForeground(Color.black);
labelLevel.setFont(font1);labelLevel.setForeground(Color.black);
txtLife.setFont(font1);txtLife.setForeground(Color.black);
txtTime.setFont(font1);txtTime.setForeground(Color.black);
txtScroe.setFont(font1);txtScroe.setForeground(Color.black);
txtLevel.setFont(font1);txtLevel.setForeground(Color.black);
GridBagLayout gbl=new GridBagLayout();
gbl.columnWidths=new int[]{20,60,90};
gbl.rowHeights=new int[]{20,40,40,40,40,390};
setLayout(gbl);
GridBagConstraints gbc=new GridBagConstraints();
gbc.anchor=GridBagConstraints.CENTER;
gbc.gridx=1;
gbc.gridy=1;
add(labelLife,gbc);
gbc.gridy++;
add(labelTime,gbc);
gbc.gridy++;
add(labelScroe,gbc);
gbc.gridy++;
add(labelLevel,gbc);
gbc.gridx=2;
gbc.gridy=1;
add(txtLife,gbc);
gbc.gridy++;
add(txtTime,gbc);
gbc.gridy++;
add(txtScroe,gbc);
gbc.gridy++;
add(txtLevel,gbc);
gbc.gridx=0;gbc.gridy=0;
gbc.gridheight=6;gbc.gridwidth=3;
gbc.weightx=1;gbc.weighty=1;
URL back = AboutMe.class.getResource("back.jpg");
add(new JLabel(new ImageIcon(back)),gbc);
}
}
//*********************************************小球**********************************************//
//*********************************************小球**********************************************//
class Ball extends JLabel implements Runnable
{
protected int r=10;
protected double x=295;
protected double y=520;
protected int theta=80;
protected Thread thread=null;
protected int sleeptime=1000000000;
protected boolean drawStage=true; //小球状态false为静止,true为移动
protected Color c=Color.yellow;
protected int speed=5;
public Ball()
{
setSize(600,550);
setLocation(0,0);
thread=new Thread(this);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -