线程游戏V0.2

文章介绍了使用Java实现的一个基于多线程的动态小球游戏。当鼠标点击时,会启动多个线程绘制小球并改变其运动方向。游戏UI基于JFrame,小球运动通过自定义DrawBall线程实现,同时使用DrawImageThread更新缓冲图片以减少闪烁。游戏利用CPU的多线程能力,即使硬件限制了同时运行的线程数量,也能通过快速切换实现多个线程的并发效果。
摘要由CSDN通过智能技术生成

线程游戏V0.2

线程的实现方式:

单线程: main 线程

多线程:

1: extends Thread类 重写 run 方法

2: implements Runnable 接口 重写 run 方法

线程的启动方式:

1: 继承 Thread 类型 实现的线程类

创建线程类对象 然后调用start方法

2: 实现 Runnable 接口 实现的线程类

创建实现的线程类的对象

将实现的线程类对象 作为参数 给到Thread构造方法创建对象

使用Thread 对象调用 start 方法

功能:鼠标点击屏幕创建小球进行动态运行

UI:JFrame实现
public class GameUI extends JFrame {
    GameListener gamel=new GameListener();

    public GameUI(){
        setTitle("线程游戏-v0.1");
        setSize(800,600);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        addMouseListener(gamel);
        gamel.g=getGraphics();

    }

    public static void main(String[] args) {
        new GameUI();
    }
}
鼠标监听器:按下就启动一个线程小球,继承MouseAdapter
public class GameListener extends MouseAdapter {
    Graphics g;
    Random ran=new Random();
    DrawImageThread dimt;
    BufferedImage buffimg;
    Graphics bg;
    //重写mousePressed方法
    @Override
    public void mousePressed(MouseEvent e) {
        if(buffimg==null){
            buffimg=new BufferedImage(800,800,2);
        }
        if(bg==null){
            bg=buffimg.getGraphics();
        }

        System.out.println("mousePressed");
        int x=e.getX();
        int y=e.getY();
        for (int i = 0; i <500 ; i++) {
            int speedx= ran.nextInt(10)-5;
            int speedy= ran.nextInt(10)-5;
            Color color=new Color(ran.nextInt(Integer.MAX_VALUE));
            DrawBall dball=new DrawBall(x,y,speedx,speedy,g,color);
            dball.start();
        }

        if(dimt==null){
            dimt =new DrawImageThread(g,buffimg);
            dimt.start();
        }
    }
}

补充:采用使用全局变量的方法使buffimg就创建一次而不用创建多次图像,使得绘制得图像只有一张

if(buffimg==null){
            buffimg=new BufferedImage(800,800,2);
        }
小球线程
public class DrawBall extends Thread{
    int x,y;
    int speedx,speedy;
    Graphics g;
    Color color;

    public DrawBall(int x, int y, int speedx, int speedy, Graphics g,Color color) {
        this.x = x;
        this.y = y;
        this.speedx = speedx;
        this.speedy = speedy;
        this.g = g;
        this.color=color;
    }

    @Override
    public void run() {
        for ( ; ; ) {
            g.setColor(color);
            g.fillOval(x, y, 30, 30);
            try {
                Thread.sleep(30);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            g.setColor(new Color(238,238,238));
            g.fillOval(x,y,30,30);
            x += speedx;
            y += speedy;
            if (x < 0 || x >= 770) {
                speedx = -speedx;
            }
            if (y <= 0 || y >= 570) {
                speedy = -speedy;
            }
        }
    }
}
缓冲图片线程类
public class DrawImageThread extends Thread{
    Graphics g;
    BufferedImage buffimg;

    public DrawImageThread(Graphics g, BufferedImage buffimg) {
        this.g = g;
        this.buffimg = buffimg;
    }

    @Override
    public void run() {
        while (true){
            try {
                Thread.sleep(30);
            } catch (InterruptedException ex) {
                throw new RuntimeException(ex);
            }
            g.drawImage(buffimg,0,0,null);
        }
    }
}

补充:采用while循环绘制出buffimg,来解决闪烁较多得问题

线程介绍:

CPU :

四核心 八线程 CPU

绝对同时下 只能运行八个线程

八核心 16线程 CPU

绝对同时下 只能运行十六个线程

四核八线程处理器 如何同时运行八千线程

短暂快速切换执行到每一个线程

一个线程: 轮流切换运行每个线程一小段

T1:1 -> 100

T2: 200 -> 100

执行:

T1 执行一段时间 1->15 暂停T1

T2 执行一段时间 200 -> 160 暂停 T2

总结:采用监听器来获取鼠标点击得坐标,然后进行绘制,下一步着重解决痕迹严重问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值