Java多線程實現(xiàn)方塊賽跑小游戲
本文實例為大家分享了Java實現(xiàn)方塊賽跑小游戲的具體代碼,供大家參考,具體內(nèi)容如下
在一個圖形界面上構(gòu)造兩個位于同一起跑線方塊,起跑線位于界面靠左位置, A 方塊先開始運動,向右移動 50 像素后停止,B 方塊開始運動,向右移動 100 像素后停 止,A 方塊繼續(xù)向右運動 100 像素后停止,B 方塊開始運動,如此循環(huán)接替執(zhí)行,直至 某一個方塊到達終點,界面顯示該方塊勝利信息。
1) 自定義一個threadA,ThreadB, ThreadFrame類(均繼承自Thread)。
2) 定義全局變量,方塊的位置,總長度,冠軍,睡眠時間等,布爾值方塊等待變量、游戲繼續(xù)變量、繪圖變量
3) ThreadA(ThreadB):等待waitA(waitB)變量釋放,即:等待另一個方塊更新完位置;然后隨機產(chǎn)生要移動的長度,檢查運動后位置與總長度的關(guān)系,以此判斷游戲是否結(jié)束。更新位置信息,更改繪圖變量,通知繪圖線程重繪。自鎖本身,釋放另一個方塊線程。
4) ThreadFrame:創(chuàng)建類對象,重寫run函數(shù),等待繪圖變量的命令。接到繪圖命令,重繪,判斷游戲釋放結(jié)束,重置繪圖命令。
5) 構(gòu)造函數(shù),paint函數(shù)繪制,并進行游戲是否結(jié)束的判斷,若結(jié)束,則打印冠軍是誰,退出
6) 主函數(shù),定義threadA,ThreadB, ThreadFrame類的對象,運行。用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;
}
}
}
//藍色方塊線程
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;
}
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java二維數(shù)組與稀疏數(shù)組相互轉(zhuǎn)換實現(xiàn)詳解
在某些應(yīng)用場景中需要大量的二維數(shù)組來進行數(shù)據(jù)存儲,但是二維數(shù)組中卻有著大量的無用的位置占據(jù)著內(nèi)存空間,稀疏數(shù)組就是為了優(yōu)化二維數(shù)組,節(jié)省內(nèi)存空間2022-09-09
Dubbo在Spring和Spring Boot中的使用詳解
這篇文章主要介紹了Dubbo在Spring和Spring Boot中的使用詳解,需要的朋友可以參考下2017-10-10
Java并發(fā)編程回環(huán)屏障CyclicBarrier
這篇文章主要介紹了Java并發(fā)編程回環(huán)屏障CyclicBarrier,文章繼續(xù)上文所介紹的Java并發(fā)編程同步器CountDownLatch展開主題相關(guān)內(nèi)容,需要的小伙伴可以參考一下2022-04-04
Java性能工具JMeter實現(xiàn)上傳與下載腳本編寫
性能測試工作中,文件上傳也是經(jīng)常見的性能壓測場景之一,那么 JMeter 文件上傳下載腳本怎么做,本文詳細(xì)的來介紹一下,感興趣的可以了解一下2021-07-07

