【汇智学堂】JAVA多线程实现的小游戏-龟兔争霸-6(完)

应用join,完善程序
在这里插入图片描述

/**
 * 创建赛道
 * 角色入场
 * 兔子出征
 * 乌龟出征
 * 结束的判断
 * 加入join
 */
package com.huizhi;

import javax.swing.*;
import java.awt.*;
import java.awt.image.ImageObserver;

public class TwoQRun extends JFrame implements ImageObserver {

    static int RecWidth=50,RecHeight=50;
    static int PositionA=50,PositionB=50,distanceAll=1600;
    static boolean blIsOver;
    static char charWinner;

    TwoQRun(){
        setTitle("多线程:龟兔争霸");
        setBackground(Color.WHITE);
        setSize(1600, 500);
        setLocation(0, 200);
        setResizable(false);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void paint(Graphics g){
        g.clearRect(0, 0, 1600, 900);      

        Image image = new ImageIcon("D:\\综合案例\\10\\src\\tu.png").getImage();
        //获取图片资源
        g.drawImage(image, PositionA - 50, 100, RecWidth, RecHeight,this);//绘制图像      

        g.setColor(Color.BLUE);      
        Image image2 = new ImageIcon("D:\\综合案例\\10\\src\\gui.png").getImage();
        //获取图片资源
        g.drawImage(image2, PositionB - 50, 300, RecWidth, RecHeight,this);//绘制图像

        //游戏结束提示
        if (blIsOver) {
            g.setFont(new Font("宋体", ALLBITS, 50));
            if (charWinner == 'A') {
                g.setColor(Color.RED);
                g.drawString(new String("兔子胜!"), 550, 250);

            } else if (charWinner == 'B') {
                g.setColor(Color.BLUE);
                g.drawString(new String("乌龟胜!"), 550, 250);
            }
        }
    }

    public static void main(String[] args) {

        ThreadA threadA=new ThreadA();
        ThreaB threaB=new ThreaB();
        ThreadC threadC=new ThreadC();

        threadA.start();
        threaB.start();
        threadC.start();

        try {
            threadC.join();
            threaB.join();
            threadA.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class  ThreadA extends Thread{

    public void run(){
        while (true){
            int distance = (int) (Math.random() * 100000) % 100;
            TwoQRun.PositionA += distance;

            try {
                sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if(TwoQRun.PositionA>=TwoQRun.distanceAll){
                TwoQRun.PositionA=TwoQRun.distanceAll-100;
                TwoQRun.charWinner='A';
                TwoQRun.blIsOver=true;
               // JOptionPane.showMessageDialog(null,"over","11",JOptionPane.INFORMATION_MESSAGE);
            }
           }
    }
}

class  ThreaB extends Thread{
    public void run(){
        while (true){
            int distance = (int) (Math.random() * 100000) % 100;
            TwoQRun.PositionB += distance;

            try {
                sleep(1500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if(TwoQRun.PositionB>=TwoQRun.distanceAll){
                TwoQRun.PositionB=TwoQRun.distanceAll-100;
                TwoQRun.charWinner='B';
                TwoQRun.blIsOver=true;
                //JOptionPane.showMessageDialog(null,"overB","11",JOptionPane.INFORMATION_MESSAGE);
            }
        }
    }
}
class  ThreadC extends Thread{
    TwoQRun twoQRun=new TwoQRun();
    public void run(){
        while (!TwoQRun.blIsOver){
            twoQRun.repaint();
        }
    }
}
发布了268 篇原创文章 · 获赞 47 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_39593940/article/details/103595889
今日推荐