Java多線程實(shí)現(xiàn)方塊賽跑小游戲
本文實(shí)例為大家分享了Java實(shí)現(xiàn)方塊賽跑小游戲的具體代碼,供大家參考,具體內(nèi)容如下
在一個(gè)圖形界面上構(gòu)造兩個(gè)位于同一起跑線方塊,起跑線位于界面靠左位置, A 方塊先開(kāi)始運(yùn)動(dòng),向右移動(dòng) 50 像素后停止,B 方塊開(kāi)始運(yùn)動(dòng),向右移動(dòng) 100 像素后停 止,A 方塊繼續(xù)向右運(yùn)動(dòng) 100 像素后停止,B 方塊開(kāi)始運(yùn)動(dòng),如此循環(huán)接替執(zhí)行,直至 某一個(gè)方塊到達(dá)終點(diǎn),界面顯示該方塊勝利信息。
1) 自定義一個(gè)threadA,ThreadB, ThreadFrame類(lèi)(均繼承自Thread)。
2) 定義全局變量,方塊的位置,總長(zhǎng)度,冠軍,睡眠時(shí)間等,布爾值方塊等待變量、游戲繼續(xù)變量、繪圖變量
3) ThreadA(ThreadB):等待waitA(waitB)變量釋放,即:等待另一個(gè)方塊更新完位置;然后隨機(jī)產(chǎn)生要移動(dòng)的長(zhǎng)度,檢查運(yùn)動(dòng)后位置與總長(zhǎng)度的關(guān)系,以此判斷游戲是否結(jié)束。更新位置信息,更改繪圖變量,通知繪圖線程重繪。自鎖本身,釋放另一個(gè)方塊線程。
4) ThreadFrame:創(chuàng)建類(lèi)對(duì)象,重寫(xiě)run函數(shù),等待繪圖變量的命令。接到繪圖命令,重繪,判斷游戲釋放結(jié)束,重置繪圖命令。
5) 構(gòu)造函數(shù),paint函數(shù)繪制,并進(jìn)行游戲是否結(jié)束的判斷,若結(jié)束,則打印冠軍是誰(shuí),退出
6) 主函數(shù),定義threadA,ThreadB, ThreadFrame類(lèi)的對(duì)象,運(yùn)行。用jion函數(shù)等待子線程的結(jié)束。
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; //構(gòu)造函數(shù) public four3() { setTitle("多線程:方塊賽跑"); setBackground(Color.WHITE); setSize(1600, 500); setLocation(0, 200); setResizable(false); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); } //繪圖函數(shù) public void paint(Graphics g) { // TODO Auto-generated method stub g.clearRect(0, 0, 1600, 900); g.setColor(Color.RED); g.fillRect(positionA - 50, 100, RecWidth, RecHeight); g.setColor(Color.BLUE); g.fillRect(positionB - 50, 300, RecWidth, 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!"), 550, 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; } } } //藍(lán)色方塊線程 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; } } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java實(shí)現(xiàn)excel導(dǎo)出合并單元格的步驟詳解
這篇文章主要介紹了java實(shí)現(xiàn)excel導(dǎo)出合并單元格,通過(guò)使用Apache POI庫(kù),我們可以方便地創(chuàng)建Excel文件、填充數(shù)據(jù)、合并單元格和導(dǎo)出Excel文件,需要的朋友可以參考下2023-04-04Spring中@RequestParam、@RequestBody和@PathVariable的用法詳解
這篇文章主要介紹了Spring中@RequestParam、@RequestBody和@PathVariable的用法詳解,后端使用集合來(lái)接受參數(shù),靈活性較好,如果url中沒(méi)有對(duì)參數(shù)賦key值,后端在接收時(shí),會(huì)根據(jù)參數(shù)值的類(lèi)型附,賦一個(gè)初始key,需要的朋友可以參考下2024-01-01java-RGB調(diào)色面板的實(shí)現(xiàn)(事件監(jiān)聽(tīng)器之匿名內(nèi)部類(lèi))
這篇文章主要介紹了java-RGB調(diào)色面板的實(shí)現(xiàn)(事件監(jiān)聽(tīng)器之匿名內(nèi)部類(lèi)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11淺談Java中Spring Boot的優(yōu)勢(shì)
在本篇文章中小編給大家分析了Java中Spring Boot的優(yōu)勢(shì)以及相關(guān)知識(shí)點(diǎn)內(nèi)容,興趣的朋友們可以學(xué)習(xí)參考下。2018-09-09Java調(diào)用打印機(jī)的2種方式舉例(無(wú)驅(qū)/有驅(qū))
我們平時(shí)使用某些軟件或者在超市購(gòu)物的時(shí)候都會(huì)發(fā)現(xiàn)可以使用打印機(jī)進(jìn)行打印,這篇文章主要給大家介紹了關(guān)于Java調(diào)用打印機(jī)的2種方式,分別是無(wú)驅(qū)/有驅(qū)的相關(guān)資料,需要的朋友可以參考下2023-11-11SpringBoot項(xiàng)目中分頁(yè)插件PageHelper無(wú)效的問(wèn)題及解決方法
這篇文章主要介紹了解決SpringBoot項(xiàng)目中分頁(yè)插件PageHelper無(wú)效的問(wèn)題,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06java之多線程搶火車(chē)票的實(shí)現(xiàn)示例
生活中有很多多線程的案例,購(gòu)票就是一個(gè)很常見(jiàn)的問(wèn)題,本文主要介紹了java之多線程搶火車(chē)票的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02