Java多线程实现方块赛跑小游戏
本文实例为大家分享了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函数等待子线程的结束。
importjava.awt.*;
importjavax.swing.*;
publicclassfour3extendsJFrame{
//全局变量
staticintpositionA=50,positionB=50,distanceAll=1600;
staticintRecWidth=50,RecHeight=50;
staticcharwinner;
staticlongsleeptime=300;
staticbooleanwaitA=true,waitB=true,gaming=true,unrepaint=true;
//构造函数
publicfour3(){
setTitle("多线程:方块赛跑");
setBackground(Color.WHITE);
setSize(1600,500);
setLocation(0,200);
setResizable(false);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
//绘图函数
publicvoidpaint(Graphicsg){
//TODOAuto-generatedmethodstub
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(newFont("宋体",ALLBITS,50));
if(winner=='A'){
g.setColor(Color.RED);
g.drawString(newString("WinnerIsTheRedOne!"),550,250);
}elseif(winner=='B'){
g.setColor(Color.BLUE);
g.drawString(newString("WinnerIsTheBlueOne!"),550,250);
}
}
}
publicstaticvoidmain(String[]args){
waitA=false;
waitB=true;
unrepaint=false;
threadframetf=newthreadframe();
threadAtA=newthreadA();
threadBtB=newthreadB();
tf.start();
tA.start();
tB.start();
try{
tf.join();
tA.join();
tB.join();
}catch(Exceptione){
//TODO:handleexception
}
return;
}
//红色方块线程
publicstaticclassthreadAextendsThread{
publicvoidrun(){
while(gaming){
while(waitA){
if(!gaming)return;
System.out.print("");
}
try{
sleep(sleeptime);
}catch(InterruptedExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
intdistance=(int)(Math.random()*100000)%100;
positionA+=distance;
if(positionA>=distanceAll){
positionA=distanceAll;
unrepaint=false;
gaming=false;
winner='A';
}
unrepaint=false;
waitA=true;
waitB=false;
}
}
}
//蓝色方块线程
publicstaticclassthreadBextendsThread{
publicvoidrun(){
while(gaming){
while(waitB){
if(!gaming)return;
System.out.print("");
}
try{
sleep(sleeptime);
}catch(InterruptedExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
intdistance=(int)(Math.random()*100000)%100;
positionB+=distance;
if(positionB>=distanceAll){
positionB=distanceAll;
unrepaint=false;
gaming=false;
winner='B';
}
unrepaint=false;
waitB=true;
waitA=false;
}
}
}
//框架刷新线程
publicstaticclassthreadframeextendsThread{
four3jiemian=newfour3();
publicvoidrun(){
while(gaming){
while(unrepaint){
if(!gaming)return;
System.out.print("");
}
jiemian.repaint();
unrepaint=true;
}
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
