JAVA期中作业——弹球游戏

来源:互联网 发布:淘宝招商骗局 编辑:程序博客网 时间:2024/05/16 15:59
package pinball;import javax.swing.*;import java.awt.*;import java.awt.event.*;public class Pinball {     public  int  LENGTH=600;    public  int  WIDTH1=600;    public  int  START_X=100;    public  int  START_Y=100;    public int  BALL_SIZE=30;         public int ball_x=LENGTH/2;    public int ball_y=WIDTH1/2;     public int speed_x=11;     public int speed_y=7;     public int mouse_x=0;     public int mouse_y=0;     public int board1=WIDTH1/2;             MPanel mp = new MPanel();    Timer t1;      public static void main(String[] args) {        new Pinball().start();        }  public void start(){      MFrame jf=new MFrame();        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        jf.setVisible(true);  }class MFrame extends JFrame{        MFrame(){     setTitle("弹球游戏 By Lucky");     setSize(LENGTH+16,WIDTH1+30);     setLocation(START_X,START_Y);      setBackground(Color.black);        add(mp);     t1 = new Timer(100, move);      t1.start();      addMouseMotionListener(mouse);}   ActionListener move = new ActionListener(){    public void actionPerformed(ActionEvent evt) {        if((((ball_x<=20)||(ball_x>=LENGTH-BALL_SIZE-20))&&(ball_y>=board1-100)&&(ball_y<=board1))){            speed_x=-speed_x;        }        else if((ball_x<=20)||(ball_x>=LENGTH-BALL_SIZE-20)){            t1.stop();        }               else if((ball_y<=15)||(ball_y>=WIDTH1-BALL_SIZE)){            speed_y=-speed_y;        }               ball_x+=speed_x;        ball_y+=speed_y;        mp.repaint();         } };  MouseMotionListener mouse=new MouseMotionListener(){ public void mouseDragged(MouseEvent e) {            } public void mouseMoved(MouseEvent e) {     mouse_x=e.getX();     mouse_y=e.getY();     board1=mouse_y;     mp.repaint(); }   };           }class MPanel extends JPanel{  public void paint(Graphics g) {    super.paint(g);    g.setColor(Color.black);    g.fillRect(0, 0, LENGTH, WIDTH1);    g.setColor(Color.red);    g.fillOval(ball_x, ball_y, BALL_SIZE, BALL_SIZE);    g.setColor(Color.blue);    g.fillRect(0, board1-100, 20, 100);     g.fillRect(LENGTH-20, board1-100, 20, 100);         }  }}