足球风云

java萌新自制了一款足球游戏(欢迎吐槽)
游戏不复杂:玩家需控制小人将球踢入对方门中
我利用了刚学习的线程,共包含3个线程:时间、玩家一移动、玩家二移动
由于刚学习java所以对代码的整合还不熟练 见谅
先上主页面的代码:

public class Main extends JFrame{
 static JLabel p1 =new JLabel();//p1
 static JLabel p2 = new JLabel();//p2
 static JLabel introduce = new JLabel("<html><p>游戏说明:(游戏类型:休闲)</p><p>玩家需控制对应的角色将球踢入对方门中得分</p><p>时间结束后,分数高者获胜</p></html>");
 static int p1_score = 0 ;
 static int p2_score = 0;
 static JLabel football = new JLabel();//足球
 static JLabel Time = new JLabel();
 static Thread move1 ;
 static Thread move2 ;
 static JLabel p1_state = new JLabel("Player1分数:"+p1_score);
 static JLabel p2_state = new JLabel("Player2分数:"+p2_score);
 public void init(Main main) {
  //分数清空
  p1_score = 0;
  p2_score = 0;
  int choose = JOptionPane.showConfirmDialog(null, "开始游戏?", "足球英雄",JOptionPane.YES_NO_OPTION);
  
  //窗口属性设置
  setVisible(true);
  setLocation(300, 200);
  setSize(758, 570);
  setResizable(false);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setTitle("足球风云");
  //拒绝开始游戏
  if(choose==1) {
   dispose(); 
  }
  JPanel jp = (JPanel) getContentPane();//内容面板
  jp.setOpaque(false);//透明
  //设置背景图片
  ImageIcon icon = new ImageIcon("C:\\Users\\zjj70\\Desktop\\足球场2.jpg");
  JLabel photo = new JLabel(icon);
  photo.setBounds(0, 0, getWidth(), getHeight());
  getLayeredPane().add(photo, new Integer(Integer.MIN_VALUE));
  
  JPanel jp_main = new JPanel();
  jp_main.setLayout(null);
  jp_main.setOpaque(false);//透明
  
  
  jp.add(jp_main);
  jp_main.add(p1);
  jp_main.add(p2);
  jp_main.add(football);
  jp_main.add(Time);
  jp_main.add(p1_state);
  jp_main.add(p2_state);
  jp_main.add(introduce);
  //游戏说明
  introduce.setBounds(60, -10, 500, 200);
  introduce.setFont(new Font("黑体", Font.PLAIN, 16));
  introduce.setForeground(new Color(255, 255, 255));
  //计分器
  p1_state.setBounds(80, -30, 350, 100);
  p1_state.setFont(new Font("宋体", Font.BOLD, 20));
  p1_state.setForeground(Color.WHITE);
  p2_state.setBounds(550, -30, 350, 100);
  p2_state.setFont(new Font("宋体", Font.BOLD, 20));
  p2_state.setForeground(Color.WHITE);
  //修建图片
  ImageIcon icon_p1 = new ImageIcon("C:\\Users\\zjj70\\Desktop\\足球员1.jpg");
  Image temp1 = icon_p1.getImage().getScaledInstance(60, 60, icon_p1.getImage().SCALE_DEFAULT);
  icon_p1 = new ImageIcon(temp1);
  
  p1.setIcon(icon_p1);
  p1.setBounds(180, 260, 60, 60);
  //修建图片
  ImageIcon icon_p2 = new ImageIcon("C:\\Users\\zjj70\\Desktop\\足球员2.jpg");
  Image temp2 = icon_p2.getImage().getScaledInstance(60, 60, icon_p2.getImage().SCALE_DEFAULT);
  icon_p2 = new ImageIcon(temp2);
  
  p2.setIcon(icon_p2);
  p2.setBounds(510, 260, 60,60);
  
  //修建图片
  ImageIcon icon_football = new ImageIcon("C:\\Users\\zjj70\\Desktop\\足球2.jpg");
  Image temp3 = icon_football.getImage().getScaledInstance(50, 50, icon_football.getImage().SCALE_DEFAULT);
  icon_football = new ImageIcon(temp3);
  
  football.setIcon(icon_football);
  football.setBounds(356, 260, 50, 50);
  
  Time.setBounds(300, -30, 200, 100);
  Time.setFont(new Font("宋体", Font.BOLD, 25));
  Time.setForeground(Color.WHITE);
  //执行时间线程
  Time time = new Time(main);
  Thread thread  = new Thread(time);
  thread.start();
  //p1监听事件
  addKeyListener(new KeyListener() {
   
   @Override
   public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub
    
   }
   
   @Override
   public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub
    
   }
   
   @Override
   public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();
    
    //执行p1线程
    if(key==KeyEvent.VK_UP||key==KeyEvent.VK_DOWN||key==KeyEvent.VK_LEFT||key==KeyEvent.VK_RIGHT) {
     
     Player1_move p1 = new Player1_move(key);
     move1 = new Thread(p1);
     move1.start();
    }
    
    //执行p2线程
    if(key==KeyEvent.VK_W||key==KeyEvent.VK_S||key==KeyEvent.VK_A||key==KeyEvent.VK_D) {
     
     Player2_move p2 = new Player2_move(key);
     move2 = new Thread(p2);
     move2.start();
    }
    
    
   }
  });
  
 }
 public void close() {
  dispose();
 }
 public static void main(String[] args) {
  Main main = new Main();
  main.init(main);
 }
 
}

接下来是时间线程的代码:

public class Time implements Runnable{
Main main;
 public void run() {
  int Time = 90;
  while(Time>=0) {
  try {
    Main.Time.setText("剩余时间:"+Time+"s");
    Thread.sleep(1000);
    Time--;
    
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
 
  }
  if(Main.p1_score>Main.p2_score) {
   JOptionPane.showMessageDialog(null,"P1 黑寡妇获胜","游戏结束", JOptionPane.WARNING_MESSAGE);
  }else if(Main.p1_score>Main.p2_score) {
   JOptionPane.showMessageDialog(null,"P2 赵云获胜","游戏结束", JOptionPane.WARNING_MESSAGE);
  }else {
   JOptionPane.showMessageDialog(null,"平局","游戏结束", JOptionPane.WARNING_MESSAGE);
  }
  int choose = JOptionPane.showConfirmDialog(null, "是否再战?", "请选择",JOptionPane.YES_NO_OPTION);
  if(choose==0) {
   main.init(main); 
  }else {
   main.close();
  }
  
 }
 public  Time(Main main) {
  this.main=main;
 }
 }

然后是玩家一移动的代码:

public class Player1_move extends JFrame implements Runnable {
 int key;
 boolean flag1=false;//判断是否进球
 boolean flag2=false;//判断是否进球
 public void run() {
 int i,dis;
  if (key == KeyEvent.VK_UP) {
   for (i = 1; i <= 30; i++) {
    try {
     Thread.sleep(5);
     dis = (int) (Math.random()*2)+1;
     Main.p1.setLocation(Main.p1.getX(), Main.p1.getY() - dis);
     if ((Main.p1.getX() >= (Main.football.getX() - 20))
       && (Main.p1.getX() <= (Main.football.getX() + 20))
       && (Main.p1.getY() >= (Main.football.getY() - 20))
       && (Main.p1.getY() <= (Main.football.getY() + 20))) {
      if (Main.p1.getY() - Main.football.getY() > 0) { // 正下
       for (i = 1; i <= 250; i++) {
        Thread.sleep(2);
        dis = (int) (Math.random() * 2);
        Main.football.setLocation(Main.football.getX(), Main.football.getY() - dis);
        //得分
        if((Main.football.getX()>=30)&&(Main.football.getX()<=60)&&(Main.football.getY()>=230)&&(Main.football.getY()<=270)) {
         //足球归位
         Main.football.setBounds(356, 260, 50, 50);
         
          flag2=true;
          
         break;
        }else if((Main.football.getX()>=670)&&(Main.football.getX()<=720)&&(Main.football.getY()>=230)&&(Main.football.getY()<=270)) {
         //足球归位
         Main.football.setBounds(356, 260, 50, 50);
         
          flag1=true;
         
         break;
        }
       }
      } else if ((Main.p1.getX() - Main.football.getX() < 0)
        && (Main.p1.getY() - Main.football.getY() > 0)) // 左下
      {
       for (i = 1; i <= 250; i++) {
        Thread.sleep(2);
         dis = (int) (Math.random() * 2);
        Main.football.setLocation(Main.football.getX() + dis, Main.football.getY() - dis);
        //得分
        if((Main.football.getX()>=30)&&(Main.football.getX()<=60)&&(Main.football.getY()>=230)&&(Main.football.getY()<=270)) {
         //足球归位
         Main.football.setBounds(356, 260, 50, 50);
         
          flag2=true;
         
         break;
        }else if((Main.football.getX()>=670)&&(Main.football.getX()<=720)&&(Main.football.getY()>=230)&&(Main.football.getY()<=270)) {
         //足球归位
         Main.football.setBounds(356, 260, 50, 50);
          flag1=true;
         break;
        }
       }
      } else if ((Main.p1.getX() - Main.football.getX() > 0)
        && (Main.p1.getY() - Main.football.getY() > 0)) { // 右下
       for (i = 1; i <= 250; i++) {
        Thread.sleep(2);
        dis = (int) (Math.random() * 2);
        Main.football.setLocation(Main.football.getX() - dis, Main.football.getY() - dis);
        //得分
        if((Main.football.getX()>=30)&&(Main.football.getX()<=60)&&(Main.football.getY()>=230)&&(Main.football.getY()<=270)) {
         //足球归位
         Main.football.setBounds(356, 260, 50, 50);
         flag1=true;        
         break;
        }
       }
      }
     }
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    }
  } else if (key == KeyEvent.VK_DOWN) {
   for (i = 1; i <= 30; i++) {
    try {
     Thread.sleep(5);
     dis = (int) (Math.random()*2)+1;
     Main.p1.setLocation(Main.p1.getX(), Main.p1.getY() + dis);
     if ((Main.p1.getX() >= (Main.football.getX() - 20))
       && (Main.p1.getX() <= (Main.football.getX() + 20))
       && (Main.p1.getY() >= (Main.football.getY() - 20))
       && (Main.p1.getY() <= (Main.football.getY() + 20))) {
      if (Main.p1.getY() - Main.football.getY() < 0) { // 正上
       for (i = 1; i <= 250; i++) {
        Thread.sleep(2);
         dis = (int) (Math.random() * 2);
        Main.football.setLocation(Main.football.getX(), Main.football.getY() + dis);
        //得分
        if((Main.football.getX()>=30)&&(Main.football.getX()<=60)&&(Main.football.getY()>=230)&&(Main.football.getY()<=270)) {
         //足球归位
         Main.football.setBounds(356, 260, 50, 50);         
          flag2=true;         
         break;
        }else if((Main.football.getX()>=670)&&(Main.football.getX()<=720)&&(Main.football.getY()>=230)&&(Main.football.getY()<=270)) {
         //足球归位
         Main.football.setBounds(356, 260, 50, 50);
         flag1=true;         
         break;
        }
       }
      } else if ((Main.p1.getX() - Main.football.getX() < 0)
        && (Main.p1.getY() - Main.football.getY() < 0))// 左上
      {
       for (i = 1; i <= 250; i++) {
        Thread.sleep(2);
         dis = (int) (Math.random() * 2);
        Main.football.setLocation(Main.football.getX() + dis, Main.football.getY() + dis);
        //得分
        if((Main.football.getX()>=30)&&(Main.football.getX()<=60)&&(Main.football.getY()>=230)&&(Main.football.getY()<=270)) {
         //足球归位
         Main.football.setBounds(356, 260, 50, 50);
         flag2=true;      
         break;
        }else if((Main.football.getX()>=670)&&(Main.football.getX()<=720)&&(Main.football.getY()>=230)&&(Main.football.getY()<=270)) {
         //足球归位
         Main.football.setBounds(356, 260, 50, 50);      
         flag1=true;     
         break;
        }
       }
       } else if ((Main.p1.getX() - Main.football.getX() > 0)
        && (Main.p1.getY() - Main.football.getY() < 0)) { // 右上
       for (i = 1; i <= 250; i++) {
        Thread.sleep(2);
         dis = (int) (Math.random() * 2);
        Main.football.setLocation(Main.football.getX() - dis, Main.football.getY() + dis);
        //得分
        if((Main.football.getX()>=30)&&(Main.football.getX()<=60)&&(Main.football.getY()>=230)&&(Main.football.getY()<=270)) {
         //足球归位
         Main.football.setBounds(356, 260, 50, 50);
         flag2=true;         
         break;
        }else if((Main.football.getX()>=670)&&(Main.football.getX()<=720)&&(Main.football.getY()>=230)&&(Main.football.getY()<=270)) {
         //足球归位
         Main.football.setBounds(356, 260, 50, 50);        
          flag1=true;        
         break;
        }
       }
      }
     }
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    }
  } else if (key == KeyEvent.VK_LEFT) {
   for (i = 1; i <= 30; i++) {
    try {
     Thread.sleep(5);
     dis = (int) (Math.random()*2)+1;
     Main.p1.setLocation(Main.p1.getX() - dis, Main.p1.getY());
     if ((Main.p1.getX() >= (Main.football.getX() - 20))
       && (Main.p1.getX() <= (Main.football.getX() + 20))
       && (Main.p1.getY() >= (Main.football.getY() - 20))
       && (Main.p1.getY() <= (Main.football.getY() + 20))) {
      if ((Main.p1.getX() - Main.football.getX() > 0)) // 正右
      {
       for (i = 1; i <= 250; i++) {
        Thread.sleep(2);
         dis = (int) (Math.random() * 2);
        Main.football.setLocation(Main.football.getX() - dis, Main.football.getY());
        //得分
        if((Main.football.getX()>=30)&&(Main.football.getX()<=60)&&(Main.football.getY()>=230)&&(Main.football.getY()<=270)) {
         //足球归位
         Main.football.setBounds(356, 260, 50, 50);
         flag2=true;        
         break;
        }
       }
      } else if ((Main.p1.getX() - Main.football.getX() > 0)
        && (Main.p1.getY() - Main.football.getY() < 0)) { // 右上
       for (i = 1; i <= 250; i++) {
        Thread.sleep(2);
         dis = (int) (Math.random() * 2);
        Main.football.setLocation(Main.football.getX() - dis, Main.football.getY() + dis);
        //得分
        if((Main.football.getX()>=30)&&(Main.football.getX()<=60)&&(Main.football.getY()>=230)&&(Main.football.getY()<=270)) {
         //足球归位
         Main.football.setBounds(356, 260, 50, 50);
         flag2=true;        
         break;
        }
       }
      } else if ((Main.p1.getX() - Main.football.getX() > 0)
        && (Main.p1.getY() - Main.football.getY() > 0)) { // 右下
       for (i = 1; i <= 250; i++) {
        Thread.sleep(2);
         dis = (int) (Math.random() * 2);
        Main.football.setLocation(Main.football.getX() - dis, Main.football.getY() - dis);
        //得分
        if((Main.football.getX()>=30)&&(Main.football.getX()<=60)&&(Main.football.getY()>=230)&&(Main.football.getY()<=270)) {
         //足球归位
         Main.football.setBounds(356, 260, 50, 50);
         flag2=true;        
         break;
        }
       }
      }
     }
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    }
  } else if (key == KeyEvent.VK_RIGHT) {
   for (i = 1; i <= 30; i++) {
    try {
     Thread.sleep(5);
     dis = (int) (Math.random()*2)+1;
     Main.p1.setLocation(Main.p1.getX() + dis, Main.p1.getY());
     if ((Main.p1.getX() >= (Main.football.getX() - 20))
       && (Main.p1.getX() <= (Main.football.getX() + 20))
       && (Main.p1.getY() >= (Main.football.getY() - 20))
       && (Main.p1.getY() <= (Main.football.getY() + 20))) {
      if ((Main.p1.getX() - Main.football.getX() < 0)) // 正左
      {
       for (i = 1; i <= 250; i++) {
        Thread.sleep(2);
         dis = (int) (Math.random() * 2);
        Main.football.setLocation(Main.football.getX() + dis, Main.football.getY());
        //得分
         if((Main.football.getX()>=670)&&(Main.football.getX()<=720)&&(Main.football.getY()>=230)&&(Main.football.getY()<=270)) {
         //足球归位
         Main.football.setBounds(356, 260, 50, 50);
flag1=true;      
         break;
        }
       }
      } else if ((Main.p1.getX() - Main.football.getX() < 0)
        && (Main.p1.getY() - Main.football.getY() < 0))// 左上
      {
       for (i = 1; i <= 250; i++) {
        Thread.sleep(2);
         dis = (int) (Math.random() * 2);
        Main.football.setLocation(Main.football.getX() + dis, Main.football.getY() + dis);
        //得分
         if((Main.football.getX()>=670)&&(Main.football.getX()<=720)&&(Main.football.getY()>=230)&&(Main.football.getY()<=270)) {
         //足球归位
         Main.football.setBounds(356, 260, 50, 50);
         flag1=true;       
         break;
        }
       }
      } else if ((Main.p1.getX() - Main.football.getX() < 0)
        && (Main.p1.getY() - Main.football.getY() > 0)) // 左下
      {
       for (i = 1; i <= 250; i++) {
        Thread.sleep(2);
         dis = (int) (Math.random() * 2);
        Main.football.setLocation(Main.football.getX() + dis, Main.football.getY() - dis);
        //得分
         if((Main.football.getX()>=670)&&(Main.football.getX()<=720)&&(Main.football.getY()>=230)&&(Main.football.getY()<=270)) {
         //足球归位
         Main.football.setBounds(356, 260, 50, 50);
         Main.football.setBounds(356, 260, 50, 50);      
          flag1=true;    
         break;
        }
       }
      }
     }
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    }
  }
  // 如果p1越界
  if (Main.p1.getX() > 758) {
   Main.p1.setLocation(Main.p1.getX() % 758 - 20, Main.p1.getY());
  } else if (Main.p1.getX() < 0) {
   Main.p1.setLocation(730, Main.p1.getY());
  } else if (Main.p1.getY() > 569) {
   Main.p1.setLocation(Main.p1.getX(), -100);
  } else if (Main.p1.getY() < -80) {
   Main.p1.setLocation(Main.p1.getX(), 520);
  }
  // 如果足球越界
  if (Main.football.getX() > 758) {
   Main.football.setBounds(356, 260, 50, 50);
  } else if (Main.football.getX() < 0) {
   Main.football.setBounds(356, 260, 50, 50);
  } else if (Main.football.getY() > 570) {
   Main.football.setBounds(356, 260, 50, 50);
  } else if (Main.football.getY() < -80) {
   Main.football.setBounds(356, 260, 50, 50);
  }
  //p2进球得分
  if(flag2==true) {
   flag2=false;
   Main.p2_score++;
   Main.p2_state.setText("Player2分数:"+Main.p2_score);
  }
  //p1进球得分
  if(flag1==true) {
   flag1=false;
   Main.p1_score++;
   Main.p1_state.setText("Player1分数:"+Main.p1_score);
  }
 }
 public Player1_move(int key) {
  this.key = key;
 }
 }

玩家二的移动代码和玩家一移动代码几乎完全一样
只不过是 类名变了 对应的构造函数名字也变了

public Player2_move(int key) {
  this.key = key;
 }

以及4个条件语句都变为

if (key == KeyEvent.VK_W)
.......
else if(key == KeyEvent.VK_S)
.......
else if(key == KeyEvent.VK_A)
.......
else if(key == KeyEvent.VK_D)
.......

话不多说上游戏截图
由于游戏图片是本地保存所以只有在我这里能看到
欢迎大佬们来光顾!
d述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值