本文實例為大家分享了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,ThreadB, 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, 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;
}
}
}
}以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。
原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/226261.html
微信掃一掃
支付寶掃一掃