課件資源放在文末
1.線程-應用到坦克大戰
1.1 坦克大戰 0.3
【坦克類:包括坦克的基本屬性,以及坦克的移動方法】?
package com.hspedu.tankgame03;/*** @author 韓順平* @version 1.0*/
public class Tank {private int x;//坦克的橫坐標private int y;//坦克的縱坐標private int direct = 0;//坦克方向 0 上1 右 2下 3左private int speed = 1;public int getSpeed() {return speed;}public void setSpeed(int speed) {this.speed = speed;}//上右下左移動方法public void moveUp() {y -= speed;}public void moveRight() {x += speed;}public void moveDown() {y += speed;}public void moveLeft() {x -= speed;}public int getDirect() {return direct;}public void setDirect(int direct) {this.direct = direct;}public Tank(int x, int y) {this.x = x;this.y = y;}public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}
}
?【我方坦克類】繼承了坦克類,在這個版本中增加了發射子彈的方法【其實就是創建一個子彈對象,并且每一個子彈對象就是一個線程】
package com.hspedu.tankgame03;/*** @author 韓順平* @version 1.0* 自己的坦克*/
public class Hero extends Tank {//定義一個Shot對象Shot shot =null;public Hero(int x, int y) {super(x, y);}//射擊public void shotEnemyTank(){//創建 Shou 對象,根據當前Hero對象的位置和方向來創建switch (getDirect()){//得到hero方向case 0://向上shot =new Shot(getX()+20,getY(),0);break;case 1://向右shot=new Shot(getX()+60,getY()+20,1);break;case 2://向下shot=new Shot(getX()+20,getY()+60,2);break;case 3://向左shot=new Shot(getX(),getY()+20,3);break;}//啟動子彈線程new Thread(shot).start();}
}
【敵方坦克類】
package com.hspedu.tankgame03;/*** @author 韓順平* @version 1.0* 敵人的坦克*/
public class EnemyTank extends Tank {public EnemyTank(int x, int y) {super(x, y);}
}
【子彈類】包括子彈的基本屬性 ,以及子彈進程什么時候結束及其運動過程的變化
package com.hspedu.tankgame03;/*** @author 林然* @version 1.0*/
public class Shot implements Runnable{//設計子彈int x;//子彈x坐標int y;//子彈y坐標int direction =0;//子彈方向int speed = 2;//子彈宿舍boolean isLive = true;//是否存活//構造器public Shot(int x, int y, int direction) {this.x = x;this.y = y;this.direction = direction;}@Overridepublic void run() {while (true){try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}//根據方向來改變switch (direction){case 0:y-=speed;break;//上case 1:x+=speed;break;//右case 2:y+=speed;break;//下case 3:x-=speed;break;//左}System.out.println("子彈x="+x+"子彈y="+y);if(!(x>=0&&x<=1000&&y>=0&&y<=750)){isLive=false;break;}}}
}
【面板類:主要在之前的基礎上新增了繪制子彈的功能以及將面板也作為一個線程,實現Runnable,每隔一段時間進行面板重繪】
package com.hspedu.tankgame03;import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;/*** @author 韓順平* @version 1.0* 坦克大戰的繪圖區域*///為了監聽 鍵盤事件, 實現KeyListener//為了讓Panel 不停地重繪子彈,需要將MyPanelRunnable,當做一個線程使用
public class MyPanel extends JPanel implements KeyListener,Runnable {//定義我的坦克Hero hero = null;//定義敵人坦克,放入到VectorVector<EnemyTank> enemyTanks = new Vector<>();int enemyTankSize = 3;public MyPanel() {hero = new Hero(100, 100);//初始化自己坦克//初始化敵人坦克for (int i = 0; i < enemyTankSize; i++) {//創建一個敵人的坦克EnemyTank enemyTank = new EnemyTank((100 * (i + 1)), 0);//設置方向enemyTank.setDirect(2);//加入enemyTanks.add(enemyTank);}}@Overridepublic void paint(Graphics g) {super.paint(g);g.fillRect(0, 0, 1000, 750);//填充矩形,默認黑色//畫出自己坦克-封裝方法drawTank(hero.getX(), hero.getY(), g, hero.getDirect(), 1);//畫出hero射擊的子彈if(hero.shot!=null&&hero.shot.isLive!=false){g.draw3DRect(hero.shot.x,hero.shot.y,5,5,false);}//畫出敵人的坦克, 遍歷Vectorfor (int i = 0; i < enemyTanks.size(); i++) {//取出坦克EnemyTank enemyTank = enemyTanks.get(i);drawTank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirect(), 0);}}//編寫方法,畫出坦克/*** @param x 坦克的左上角x坐標* @param y 坦克的左上角y坐標* @param g 畫筆* @param direct 坦克方向(上下左右)* @param type 坦克類型*/public void drawTank(int x, int y, Graphics g, int direct, int type) {//根據不同類型坦克,設置不同顏色switch (type) {case 0: //敵人的坦克g.setColor(Color.cyan);break;case 1: //我的坦克g.setColor(Color.yellow);break;}//根據坦克方向,來繪制對應形狀坦克//direct 表示方向(0: 向上 1 向右 2 向下 3 向左 )//switch (direct) {case 0: //表示向上g.fill3DRect(x, y, 10, 60, false);//畫出坦克左邊輪子g.fill3DRect(x + 30, y, 10, 60, false);//畫出坦克右邊輪子g.fill3DRect(x + 10, y + 10, 20, 40, false);//畫出坦克蓋子g.fillOval(x + 10, y + 20, 20, 20);//畫出圓形蓋子g.drawLine(x + 20, y + 30, x + 20, y);//畫出炮筒break;case 1: //表示向右g.fill3DRect(x, y, 60, 10, false);//畫出坦克上邊輪子g.fill3DRect(x, y + 30, 60, 10, false);//畫出坦克下邊輪子g.fill3DRect(x + 10, y + 10, 40, 20, false);//畫出坦克蓋子g.fillOval(x + 20, y + 10, 20, 20);//畫出圓形蓋子g.drawLine(x + 30, y + 20, x + 60, y + 20);//畫出炮筒break;case 2: //表示向下g.fill3DRect(x, y, 10, 60, false);//畫出坦克左邊輪子g.fill3DRect(x + 30, y, 10, 60, false);//畫出坦克右邊輪子g.fill3DRect(x + 10, y + 10, 20, 40, false);//畫出坦克蓋子g.fillOval(x + 10, y + 20, 20, 20);//畫出圓形蓋子g.drawLine(x + 20, y + 30, x + 20, y + 60);//畫出炮筒break;case 3: //表示向左g.fill3DRect(x, y, 60, 10, false);//畫出坦克上邊輪子g.fill3DRect(x, y + 30, 60, 10, false);//畫出坦克下邊輪子g.fill3DRect(x + 10, y + 10, 40, 20, false);//畫出坦克蓋子g.fillOval(x + 20, y + 10, 20, 20);//畫出圓形蓋子g.drawLine(x + 30, y + 20, x, y + 20);//畫出炮筒break;default:System.out.println("暫時沒有處理");}}@Overridepublic void keyTyped(KeyEvent e) {}//處理wdsa 鍵按下的情況@Overridepublic void keyPressed(KeyEvent e) {System.out.println(e.getKeyCode());if (e.getKeyCode() == KeyEvent.VK_W) {//按下W鍵//改變坦克的方向hero.setDirect(0);////修改坦克的坐標 y -= 1hero.moveUp();} else if (e.getKeyCode() == KeyEvent.VK_D) {//D鍵, 向右hero.setDirect(1);hero.moveRight();} else if (e.getKeyCode() == KeyEvent.VK_S) {//S鍵hero.setDirect(2);hero.moveDown();} else if (e.getKeyCode() == KeyEvent.VK_A) {//A鍵hero.setDirect(3);hero.moveLeft();}if(e.getKeyCode()==KeyEvent.VK_J){hero.shotEnemyTank();}//讓面板重繪this.repaint();}@Overridepublic void keyReleased(KeyEvent e) {}@Overridepublic void run() {while (true){try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}this.repaint();}}
}
【主方法類:將面板作為一個線程啟動】
package com.hspedu.tankgame03;import javax.swing.*;/*** @author 韓順平* @version 1.0*/
public class HspTankGame03 extends JFrame {//定義MyPanelMyPanel mp = null;public static void main(String[] args) {HspTankGame03 hspTankGame01 = new HspTankGame03();}public HspTankGame03() {mp = new MyPanel();Thread thread =new Thread(mp);thread.start();this.add(mp);//把面板(就是游戲的繪圖區域)this.setSize(1000, 750);this.addKeyListener(mp);//讓JFrame 監聽mp的鍵盤事件this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);}
}
1.2 坦克大戰 0.4 版
在本次項目中大量在使用線程,我們也要知道即使線程結束了,但是其對象也是依然存在的,對于不同的邏輯操作我們可以在對應的類中寫出對應的方法并不斷重繪,對于需要反復判斷的我們也要在Mypanel的run方法中不斷進行調用?
package com.hspedu.tankgame04;import javax.swing.*;/*** @author 林然* @version 1.0*/
public class HspTankGame04 extends JFrame {//定義MyPanelMyPanel mp = null;public static void main(String[] args) {HspTankGame04 hspTankGame01 = new HspTankGame04();}public HspTankGame04() {mp = new MyPanel();Thread thread =new Thread(mp);thread.start();this.add(mp);//把面板(就是游戲的繪圖區域)this.setSize(1000, 750);this.addKeyListener(mp);//讓JFrame 監聽mp的鍵盤事件this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);}
}
package com.hspedu.tankgame04;import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;/*** @author linran* @version 1.0* 坦克大戰的繪圖區域*///為了監聽 鍵盤事件, 實現KeyListener//為了讓Panel 不停地重繪子彈,需要將MyPanelRunnable,當做一個線程使用
public class MyPanel extends JPanel implements KeyListener,Runnable {//定義我的坦克Hero hero = null;//定義敵人坦克,放入到VectorVector<EnemyTank> enemyTanks = new Vector<>();int enemyTankSize = 3;//定義一個Vector用于存放炸彈//當子彈擊中坦克時,就加入Vector<Bomb> bombs =new Vector<>();//定義三張圖片用于顯示爆炸效果Image image1 = null;Image image2 =null;Image image3 =null;public MyPanel() {hero = new Hero(100, 100);//初始化自己坦克//初始化敵人坦克for (int i = 0; i < enemyTankSize; i++) {//創建一個敵人的坦克EnemyTank enemyTank = new EnemyTank((100 * (i + 1)), 0);//設置方向enemyTank.setDirect(2);//啟動坦克線程,讓他動起來new Thread(enemyTank).start();//加入一顆子彈Shot shot = new Shot(enemyTank.getX() + 20, enemyTank.getY() + 60, enemyTank.getDirect());//加入enemyTank的Vector成員進行管理enemyTank.shots.add(shot);//啟動shot對象new Thread(shot).start();//加入enemyTanks.add(enemyTank);}//初始化炸彈圖片image1=Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/bomb_1.gif"));image2=Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/bomb_2.gif"));image3=Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/bomb_3.gif"));}@Overridepublic void paint(Graphics g) {super.paint(g);g.fillRect(0, 0, 1000, 750);//填充矩形,默認黑色//畫出自己坦克-封裝方法if(hero.isLife){drawTank(hero.getX(), hero.getY(), g, hero.getDirect(), 1);}//畫出hero射擊的子彈for (int i=0;i<hero.shots.size();i++){Shot shot = hero.shots.get(i);if(shot!=null&&shot.isLive!=false){g.draw3DRect(shot.x,shot.y,5,5,false);}else {//從vector移除hero.shots.remove(shot);}}for(int i=0;i<bombs.size();i++){Bomb bomb = bombs.get(i);//根據當前對象的life值去畫圖片if(bomb.life>6){g.drawImage(image1,bomb.x,bomb.y,60,60,this);}else if(bomb.life>3){g.drawImage(image2,bomb.x,bomb.y,60,60,this);}else {g.drawImage(image3,bomb.x,bomb.y,60,60,this);}//讓炸彈生命值減少bomb.lifeDown();if(bomb.life==0){bombs.remove(i);//如果生命為0就刪除}}//畫出敵人的坦克, 遍歷Vectorfor (int i = 0; i < enemyTanks.size(); i++) {//取出坦克EnemyTank enemyTank = enemyTanks.get(i);if(enemyTank.isLive){//當子彈存活才畫出坦克drawTank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirect(), 0);//畫出enemyTank的所有子彈for (int j=0;j<enemyTank.shots.size();j++){//取出子彈Shot shot = enemyTank.shots.get(j);if(shot.isLive!=false){g.draw3DRect(shot.x,shot.y,5,5,false);}else {enemyTank.shots.remove(shot);}}}}}//編寫方法,畫出坦克/*** @param x 坦克的左上角x坐標* @param y 坦克的左上角y坐標* @param g 畫筆* @param direct 坦克方向(上下左右)* @param type 坦克類型*/public void drawTank(int x, int y, Graphics g, int direct, int type) {//根據不同類型坦克,設置不同顏色switch (type) {case 0: //敵人的坦克g.setColor(Color.cyan);break;case 1: //我的坦克g.setColor(Color.yellow);break;}//根據坦克方向,來繪制對應形狀坦克//direct 表示方向(0: 向上 1 向右 2 向下 3 向左 )//switch (direct) {case 0: //表示向上g.fill3DRect(x, y, 10, 60, false);//畫出坦克左邊輪子g.fill3DRect(x + 30, y, 10, 60, false);//畫出坦克右邊輪子g.fill3DRect(x + 10, y + 10, 20, 40, false);//畫出坦克蓋子g.fillOval(x + 10, y + 20, 20, 20);//畫出圓形蓋子g.drawLine(x + 20, y + 30, x + 20, y);//畫出炮筒break;case 1: //表示向右g.fill3DRect(x, y, 60, 10, false);//畫出坦克上邊輪子g.fill3DRect(x, y + 30, 60, 10, false);//畫出坦克下邊輪子g.fill3DRect(x + 10, y + 10, 40, 20, false);//畫出坦克蓋子g.fillOval(x + 20, y + 10, 20, 20);//畫出圓形蓋子g.drawLine(x + 30, y + 20, x + 60, y + 20);//畫出炮筒break;case 2: //表示向下g.fill3DRect(x, y, 10, 60, false);//畫出坦克左邊輪子g.fill3DRect(x + 30, y, 10, 60, false);//畫出坦克右邊輪子g.fill3DRect(x + 10, y + 10, 20, 40, false);//畫出坦克蓋子g.fillOval(x + 10, y + 20, 20, 20);//畫出圓形蓋子g.drawLine(x + 20, y + 30, x + 20, y + 60);//畫出炮筒break;case 3: //表示向左g.fill3DRect(x, y, 60, 10, false);//畫出坦克上邊輪子g.fill3DRect(x, y + 30, 60, 10, false);//畫出坦克下邊輪子g.fill3DRect(x + 10, y + 10, 40, 20, false);//畫出坦克蓋子g.fillOval(x + 20, y + 10, 20, 20);//畫出圓形蓋子g.drawLine(x + 30, y + 20, x, y + 20);//畫出炮筒break;default:System.out.println("暫時沒有處理");}}//判斷我方子彈是否擊中敵人坦克public void hittank(Vector<Shot> shots,EnemyTank enemyTank){//判斷s擊中坦克for (int i=0;i<shots.size();i++){Shot s = shots.get(i);switch (enemyTank.getDirect()){case 0://上case 2://下if(s.x>enemyTank.getX()&&s.x<enemyTank.getX()+40&&s.y>enemyTank.getY()&&s.y<enemyTank.getY()+60){ s.isLive=false;enemyTank.isLive=false;//創建Bomb對象加入集合中Bomb bomb =new Bomb(enemyTank.getX(),enemyTank.getY());bombs.add(bomb);enemyTanks.remove(enemyTank);}break;case 1: //左右case 3:if(s.x>enemyTank.getX()&&s.x<enemyTank.getX()+60&&s.y>enemyTank.getY()&&s.y<enemyTank.getY()+40){ s.isLive=false;enemyTank.isLive=false;//創建Bomb對象加入集合中Bomb bomb =new Bomb(enemyTank.getX(),enemyTank.getY());bombs.add(bomb);enemyTanks.remove(enemyTank);}break;}}}//判斷我方子彈是否擊中敵人坦克public void hitHero(Vector<Shot> shots,Hero hero){//判斷s擊中坦克for (int i=0;i<shots.size();i++){Shot s = shots.get(i);if(s.isLive&&hero.isLife){switch (hero.getDirect()){case 0://上case 2://下if(s.x>hero.getX()&&s.x<hero.getX()+40&&s.y>hero.getY()&&s.y<hero.getY()+60){ s.isLive=false;//創建Bomb對象加入集合中Bomb bomb =new Bomb(hero.getX(),hero.getY());bombs.add(bomb);hero.isLife=false;}break;case 1: //左右case 3:if(s.x>hero.getX()&&s.x<hero.getX()+60&&s.y>hero.getY()&&s.y<hero.getY()+40){ s.isLive=false;hero.isLife=false;//創建Bomb對象加入集合中Bomb bomb =new Bomb(hero.getX(),hero.getY());bombs.add(bomb);}break;}}}}@Overridepublic void keyTyped(KeyEvent e) {}//處理wdsa 鍵按下的情況@Overridepublic void keyPressed(KeyEvent e) {System.out.println(e.getKeyCode());if (e.getKeyCode() == KeyEvent.VK_W) {//按下W鍵//改變坦克的方向hero.setDirect(0);////修改坦克的坐標 y -= 1hero.moveUp();} else if (e.getKeyCode() == KeyEvent.VK_D) {//D鍵, 向右hero.setDirect(1);hero.moveRight();} else if (e.getKeyCode() == KeyEvent.VK_S) {//S鍵hero.setDirect(2);hero.moveDown();} else if (e.getKeyCode() == KeyEvent.VK_A) {//A鍵hero.setDirect(3);hero.moveLeft();}if(e.getKeyCode()==KeyEvent.VK_J){//if(!(hero.shot!=null&&hero.shot.isLive)) {發射一顆子彈if(hero.shots.size()<5)hero.shotEnemyTank();//發射多顆子彈,在面板最多有5g顆子彈//}}//讓面板重繪this.repaint();}@Overridepublic void keyReleased(KeyEvent e) {}@Overridepublic void run() {while (true){try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}//判斷是否擊中坦克if(hero.shots.size()>0){//當前我的子彈存活//遍歷敵人所有坦克for (int i = 0; i < enemyTanks.size(); i++) {EnemyTank enemyTank =enemyTanks.get(i);hittank(hero.shots,enemyTank);}}//判斷是否擊中我方坦克for (int i=0;i<enemyTanks.size();i++){hitHero(enemyTanks.get(i).shots,hero);}this.repaint();}}
}
package com.hspedu.tankgame04;/*** @author 林然* @version 1.0*/
public class Tank {private int x;//坦克的橫坐標private int y;//坦克的縱坐標private int direct = 0;//坦克方向 0 上1 右 2下 3左private int speed = 5;public int getSpeed() {return speed;}public void setSpeed(int speed) {this.speed = speed;}//上右下左移動方法public void moveUp() {if(y>0)//判斷邊界條件,個人感覺在這里設置的話我方和敵人坦克都可以直接控制{y -= speed;}}public void moveRight() {if(x<1000&&(x+80)<1000){x += speed;}}public void moveDown() {if(y<750&&(y+100)<750){y += speed;}}public void moveLeft() {if(x>0){x -= speed;}}public int getDirect() {return direct;}public void setDirect(int direct) {this.direct = direct;}public Tank(int x, int y) {this.x = x;this.y = y;}public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}
}
package com.hspedu.tankgame04;import java.util.Vector;/*** @author 林然* @version 1.0* 敵人的坦克*/
public class EnemyTank extends Tank implements Runnable{Vector <Shot> shots=new Vector<>();boolean isLive =true;public EnemyTank(int x, int y) {super(x, y);}//射擊public void shotHeroTank(){//創建 Shou 對象,根據當前EnemyTank對象的位置和方向來創建Shot shot =null;switch (getDirect()){//得到EnemyTank方向case 0://向上shot =new Shot(getX()+20,getY(),0);break;case 1://向右shot=new Shot(getX()+60,getY()+20,1);break;case 2://向下shot=new Shot(getX()+20,getY()+60,2);break;case 3://向左shot=new Shot(getX(),getY()+20,3);break;}//加入子彈集合shots.add(shot);//啟動子彈線程new Thread(shot).start();}@Overridepublic void run() {int count=0;while (isLive){if(shots.size()<5){shotHeroTank();}count++;//根據坦克方向來繼續移動switch (getDirect()){case 0://向上moveUp();break;case 1://向右moveRight();break;case 2://向下moveDown();break;case 3://向左moveLeft();break;}try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}//然后隨機的改變坦克方向[0-4).連續走10步后換方向if(count%10==0){setDirect((int)(Math.random()*4));count=0;}//寫并發程序一定要考慮線程什么時候結束}}
}
package com.hspedu.tankgame04;import java.util.Vector;/*** @author linran* @version 1.0* 自己的坦克*/
public class Hero extends Tank {//定義一個Shot對象Shot shot =null;boolean isLife =true;//可以發送多顆子彈Vector<Shot> shots =new Vector<>();public Hero(int x, int y) {super(x, y);}//射擊public void shotEnemyTank(){//創建 Shou 對象,根據當前Hero對象的位置和方向來創建switch (getDirect()){//得到hero方向case 0://向上shot =new Shot(getX()+20,getY(),0);break;case 1://向右shot=new Shot(getX()+60,getY()+20,1);break;case 2://向下shot=new Shot(getX()+20,getY()+60,2);break;case 3://向左shot=new Shot(getX(),getY()+20,3);break;}//加入子彈集合shots.add(shot);//啟動子彈線程new Thread(shot).start();}
}
package com.hspedu.tankgame04;/*** @author 林然* @version 1.0*/
public class Shot implements Runnable{//設計子彈int x;//子彈x坐標int y;//子彈y坐標int direction =0;//子彈方向int speed = 2;//子彈宿舍boolean isLive = true;//是否存活//構造器public Shot(int x, int y, int direction) {this.x = x;this.y = y;this.direction = direction;}@Overridepublic void run() {while (true){try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}//根據方向來改變switch (direction){case 0:y-=speed;break;//上case 1:x+=speed;break;//右case 2:y+=speed;break;//下case 3:x-=speed;break;//左}System.out.println("子彈x="+x+"子彈y="+y);//當子彈碰到面板邊界時應該被銷毀//當子彈碰到敵人坦克時,也應該結束進程if(!(x>=0&&x<=1000&&y>=0&&y<=750&&isLive)){isLive=false;break;}}}
}
package com.hspedu.tankgame04;/*** @author 林然* @version 1.0* 炸彈*/
public class Bomb {int x,y;//炸彈坐標int life = 9;//炸彈的生命周期boolean isLife =true;//是否還存活public Bomb(int x, int y) {this.x = x;this.y = y;}//減少生命值public void lifeDown(){if(life>0){life--;}else{isLife=false;}}
}
2.課件資源
鏈接:https://pan.baidu.com/s/1anOddH3cE47HuAUHyAGiVg?pwd=msmz?
提取碼:msmz