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

乌龟出征:
在这里插入图片描述

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

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

            if(TwoQRun.PositionB>=TwoQRun.distanceAll){
                TwoQRun.PositionB=TwoQRun.distanceAll-100;
                JOptionPane.showMessageDialog(null,"overB","11",JOptionPane.INFORMATION_MESSAGE);
            }
        }
    }
}

完整代码

/**
 * 创建赛道
 * 角色入场
 * 兔子出征
 * 乌龟出征
 */
package com.huizhi;

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

public class TwoQRun extends JFrame {

    static int RecWidth=50,RecHeight=50;
    static int PositionA=50,PositionB=50,distanceAll=1600;

    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);//绘制图像

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

    }

    public static void main(String[] args) {

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

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

class  ThreadA extends Thread{

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

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

            if(TwoQRun.PositionA>=TwoQRun.distanceAll){
                TwoQRun.PositionA=TwoQRun.distanceAll-100;
                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(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if(TwoQRun.PositionB>=TwoQRun.distanceAll){
                TwoQRun.PositionB=TwoQRun.distanceAll-100;
                JOptionPane.showMessageDialog(null,"overB","11",JOptionPane.INFORMATION_MESSAGE);
            }
        }
    }
}

class  ThreadC extends Thread{
    TwoQRun twoQRun=new TwoQRun();
    public void run(){

        while (true){
            twoQRun.repaint();
        }

    }
}
发布了268 篇原创文章 · 获赞 47 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_39593940/article/details/103595738