JAVA期中作业——弹球游戏+砖块+人机对战+多条命

package pinball;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Pinball {
    //Panel
    public  int  LENGTH=600;
    public  int  WIDTH1=600;
    public  int  START_X=300;
    public  int  START_Y=50;
    //Ball
    public int BALL_SIZE=30; 
    public int ball_x=40;
    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;
    //Brick 
     public int B_LENGTH=30;
     public int B_WIDTH=100;
     public int B_LNUMBER=4;
     public int B_WNUMBER=5;
     
     
     //模式
     public int game=0;//0单人游戏 1双人游戏 2人机对战 3砖块游戏
     //双人模式
     public int scorea=0;
     public int scoreb=0;
     public int player=0;
     public int lifea=3;
     public int lifeb=3;
     
     //人机对战
     public int HIGHSPEED=30;
     public int board2=board1;
     
     public boolean lose=false;
     
     
    
    MPanel mp = new MPanel();
    Timer t1;
    brick[][] lev1;
  
    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); 
   
     add(mp);
     t1 = new Timer(50, move);
     t1.start();
     addMouseMotionListener(mouse);
      addMouseListener(mouseclick);
     
     
     lev1=new brick[B_LNUMBER][B_WNUMBER];
     int b_x_now=LENGTH/2-B_LENGTH-40,b_y_now;
      for(int m=0;m=LENGTH-BALL_SIZE-20))&&(ball_y>=board1-100)&&(ball_y<=board1))){
            speed_x=-speed_x;
            if(ball_x<=20)
            {  player=0;
                scorea++;}
            else { player=1;
                   scoreb++;}
            }
        else if((((ball_x<=20)||(ball_x>=LENGTH-BALL_SIZE-20))&&(ball_y>=board2-100)&&(ball_y<=board2))){
            speed_x=-speed_x;
            if(ball_x<=20)
            {  player=0;
                scorea++;}
            else { player=1;
                   scoreb++;}
            }
        //lose
        else if((ball_x<=20)||(ball_x>=LENGTH-BALL_SIZE-20)){
            t1.stop();
            lose=true;
            if(player==0)
            {   lifea--;}
            else lifeb--;
        }
        //碰到上下边
       else if((ball_y<=15)||(ball_y>=WIDTH1-BALL_SIZE)){
            speed_y=-speed_y;
        }
        for(int m=0;m0){
                int distance=((LENGTH-ball_x)/speed_x*speed_y+ball_y)-(board2-50);
                int time=(LENGTH-ball_x)/speed_x;
                if(time==4)
                {time=time-1;}  
                int speed=distance/(time-4);
               if(speed>=10)
                   speed=10;
               if(speed<=-10)
                   speed=-10;
                board2+=(speed);
            }
           
        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(); 
 }
   };
  
  MouseListener mouseclick=new MouseListener(){
            public void mouseClicked(MouseEvent e) {
                if(lose==true){
                    t1.start();
                      ball_x=40;
                     // ball_y=WIDTH1/2;
                     // board1=board2=WIDTH1/2;
                      if(player==0)
                      {speed_x=11;
                       ball_x=40;
                      //speed_y=7;
                      }
                      else{
                          speed_x=-11;
                          ball_x=LENGTH-80;
                     // speed_y=7;
                      }
                   
                      
                    lose=false;
                }
            }
                
            public void mousePressed(MouseEvent e) {
            }
            public void mouseReleased(MouseEvent e) {
            }
            public void mouseEntered(MouseEvent e) {
            }
            public void mouseExited(MouseEvent e) {
            }
        };


           }

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.blue);
    g.fillRect(0, board1-100, 20, 100); 
    g.fillRect(LENGTH-20, board2-100, 20, 100);
    g.setColor(Color.yellow);
    for(int m=0;m=x-BALL_SIZE)&&(p_x<=x+B_LENGTH)&&(p_y>=y-BALL_SIZE)&&(p_y<=y+B_WIDTH)){
         visable=false;
         if(player==0)
         scorea++;
         else scoreb++;
         int m=((BALL_SIZE-x+p_x)<=(x+B_LENGTH-p_x))?BALL_SIZE-x+p_x:x+B_LENGTH-p_x;
         int n=((BALL_SIZE-y+p_y)<=(y+B_WIDTH-p_y))?BALL_SIZE-y+p_y:y+B_WIDTH-p_y;
         if(m<=n)
             pattern=1 ;
         else pattern=2 ;
     }
     else pattern=0;
   }
    }
    }

你可能感兴趣的:(JAVA)