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

