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<B_LNUMBER;m++){
          b_y_now=-B_WIDTH+40;
          b_x_now+=B_LENGTH;
          for(int n=0;n<B_WNUMBER;n++){
              b_y_now+=B_WIDTH;
          lev1[m][n]=new brick(b_x_now,b_y_now);
      }
      }
}
   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;
            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;m<B_LNUMBER;m++)
          for(int n=0;n<B_WNUMBER;n++){
             lev1[m][n].Getin(ball_x, ball_y);
             if(lev1[m][n].pattern==1){//撞到侧面
                  speed_x=-speed_x;
                 }
             if(lev1[m][n].pattern==2){//撞到上下面
                  speed_y=-speed_y;
                 }
      }
       
        ball_x+=speed_x;
        ball_y+=speed_y;
        
    //机器控制板
        if(game==0)
            if(speed_x>0){
                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<B_LNUMBER;m++){
          for(int n=0;n<B_WNUMBER;n++){
              if (lev1[m][n].visable==true)
            g.fillRect(lev1[m][n].x,lev1[m][n].y, B_LENGTH-5, B_WIDTH-5); 
      }
  }
    g.setColor(Color.red);
    g.fillOval(ball_x, ball_y, BALL_SIZE, BALL_SIZE);
   // g.setFont(new Font(f,PLAIN,30));
    g.drawString("0"+scorea, 30, WIDTH1-100);
    g.drawString("0"+scoreb, LENGTH-30, WIDTH1-100);
     g.setColor(Color.white);
    for(int n=0;n<lifea;n++)
    {g.fillOval(50+n*20, WIDTH1-100, BALL_SIZE-10, BALL_SIZE-10);}
     for(int m=0;m<lifeb;m++)
    {g.fillOval(LENGTH-50-m*20, WIDTH1-100, BALL_SIZE-10, BALL_SIZE-10);}
    
    
}
}

class brick{
 public   int x=0;//砖左上角(x,y)
 public   int y=0;
 int pattern=0;
    brick(int b_x,int b_y){
        x=b_x;
        y=b_y;
        }
    private Boolean visable=true;
    Boolean Setvisable(int m){
        if(m==0)
         visable=true;
        if(m==1)
         visable=false; 
        return visable;
    }
   void Getin(int p_x,int p_y){//球是否碰到砖 球左上角(p_x,p_y) 
   if(visable==false)
         pattern= 0;
     else if((p_x>=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;
   }
    }
    }

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
package org.crazyit.ball; import java.awt.Image; import java.io.File; import javax.imageio.ImageIO; import java.io.IOException; /** * 小球对象 * * @author yangenxiong yangenxiong2009@gmail.com * @author Kelvin Mak kelvin.mak125@gmail.com * @version 1.0 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br>Copyright (C), 2009-2010, yangenxiong * <br>This program is protected by copyright laws. */ public class Ball extends BallComponent { // 定义球的竖向速度 private int speedY = 10; // 定义弹球的横向速度 private int speedX = 8; // 定义是否在运动 private boolean started = false; // 定义是否结束运动 private boolean stop = false; /** * m 有参数构造器 * * @param panelWidth * int 画板宽度 * @param panelHeight * int 画板高度 * @param offset * int 位移 * @param path * String 图片路径 */ public Ball(int panelWidth, int panelHeight, int offset, String path) throws IOException { // 调用父构造器 super(panelWidth, panelHeight, path); // 设置y坐标 this.setY(panelHeight - super.getImage().getHeight(null) - offset); } /** * 设置横向速度 * * @param speed * int 速度 * @return void */ public void setSpeedX(int speed) { this.speedX = speed; } /** * 设置竖向速度 * * @param speed * int 速度 * @return void */ public void setSpeedY(int speed) { this.speedY = speed; } /** * 设置是否在运动 * * @param b * boolean * @return void */ public void setStarted(boolean b) { this.started = b; } /** * 设置是否结束运动 * * @param b * boolean * @return void */ public void setStop(boolean b) { this.stop = b; } /** * 返回横向速度 * * @return int 速度 */ public int getSpeedX() { return this.speedX; } /** * 返回竖向速度 * * @return int 速度 */ public int getSpeedY() { return this.speedY; } /** * 是否在运动 * * @return boolean 是否在运动 */ public boolean isStarted() { return this.started; } /** * 是否已经结束运动 * * @return boolean 是否已经结束运动 */ public boolean isStop() { return this.stop; } }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值