Java多執行緒實現方塊賽跑小遊戲
阿新 • • 發佈:2020-07-20
本文例項為大家分享了Java實現方塊賽跑小遊戲的具體程式碼,供大家參考,具體內容如下
在一個圖形介面上構造兩個位於同一起跑線方塊,起跑線位於介面靠左位置, A 方塊先開始運動,向右移動 50 畫素後停止,B 方塊開始運動,向右移動 100 畫素後停 止,A 方塊繼續向右運動 100 畫素後停止,B 方塊開始運動,如此迴圈接替執行,直至 某一個方塊到達終點,介面顯示該方塊勝利資訊。
1) 自定義一個threadA,ThreadB,ThreadFrame類(均繼承自Thread)。
2) 定義全域性變數,方塊的位置,總長度,冠軍,睡眠時間等,布林值方塊等待變數、遊戲繼續變數、繪圖變數
3) ThreadA(ThreadB):等待waitA(waitB)變數釋放,即:等待另一個方塊更新完位置;然後隨機產生要移動的長度,檢查運動後位置與總長度的關係,以此判斷遊戲是否結束。更新位置資訊,更改繪圖變數,通知繪圖執行緒重繪。自鎖本身,釋放另一個方塊執行緒。
4) ThreadFrame:建立類物件,重寫run函式,等待繪圖變數的命令。接到繪圖命令,重繪,判斷遊戲釋放結束,重置繪圖命令。
5) 建構函式,paint函式繪製,並進行遊戲是否結束的判斷,若結束,則列印冠軍是誰,退出
6) 主函式,定義threadA,ThreadFrame類的物件,執行。用jion函式等待子執行緒的結束。
import java.awt.*; import javax.swing.*; public class four3 extends JFrame { // 全域性變數 static int positionA = 50,positionB = 50,distanceAll = 1600; static int RecWidth = 50,RecHeight = 50; static char winner; static long sleeptime = 300; static boolean waitA = true,waitB = true,gaming = true,unrepaint = true; //建構函式 public four3() { setTitle("多執行緒:方塊賽跑"); setBackground(Color.WHITE); setSize(1600,500); setLocation(0,200); setResizable(false); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); } //繪圖函式 public void paint(Graphics g) { // TODO Auto-generated method stub g.clearRect(0,1600,900); g.setColor(Color.RED); g.fillRect(positionA - 50,100,RecWidth,RecHeight); g.setColor(Color.BLUE); g.fillRect(positionB - 50,300,RecHeight); if (!gaming) { g.setFont(new Font("宋體",ALLBITS,50)); if (winner == 'A') { g.setColor(Color.RED); g.drawString(new String("Winner Is The Red One!"),550,250); } else if (winner == 'B') { g.setColor(Color.BLUE); g.drawString(new String("Winner Is The Blue One!"),250); } } } public static void main(String[] args) { waitA = false; waitB = true; unrepaint = false; threadframe tf = new threadframe(); threadA tA = new threadA(); threadB tB = new threadB(); tf.start(); tA.start(); tB.start(); try { tf.join(); tA.join(); tB.join(); } catch (Exception e) { // TODO: handle exception } return; } //紅色方塊執行緒 public static class threadA extends Thread { public void run() { while (gaming) { while (waitA) { if (!gaming)return; System.out.print(""); } try { sleep(sleeptime); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } int distance = (int) (Math.random() * 100000) % 100; positionA += distance; if (positionA >= distanceAll) { positionA = distanceAll; unrepaint = false; gaming = false; winner = 'A'; } unrepaint = false; waitA = true; waitB = false; } } } //藍色方塊執行緒 public static class threadB extends Thread { public void run() { while (gaming) { while (waitB) { if (!gaming)return; System.out.print(""); } try { sleep(sleeptime); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } int distance = (int) (Math.random() * 100000) % 100; positionB += distance; if (positionB >= distanceAll) { positionB = distanceAll; unrepaint = false; gaming = false; winner = 'B'; } unrepaint = false; waitB = true; waitA = false; } } } //框架重新整理執行緒 public static class threadframe extends Thread { four3 jiemian = new four3(); public void run() { while (gaming) { while (unrepaint) { if (!gaming)return; System.out.print(""); } jiemian.repaint(); unrepaint = true; } } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。