Java写的生命游戏

本文详细描述了一个基于Java编写的简单生命游戏实现,包括常量类定义、生命对象的创建、窗口类中的游戏逻辑以及使用双缓冲技术进行图形更新的代码片段。
摘要由CSDN通过智能技术生成

上次写的生命游戏有问题,这次补个好的

先定义常量类

package com.LifeGame2;

public class Constant {
    static final int LENGTH = 30;//每格生命所占屏幕像素的长度
    static final long FREQUENCY = 400;//页面刷新频率400ms/次
}

在定义一个生命类

package com.LifeGame2;

import java.awt.*;

import static com.LifeGame2.Constant.*;

public class Life {
    //生命的坐标
    int x;
    int y;
    //定义两个live
    boolean live1 = false;
    boolean live2 = false;

    public Life(int x, int y) {
        this.x = x;
        this.y = y;
    }
    //在图像上画的方法
    public void paint(Graphics g) {
        g.fillRect(x*LENGTH+2, y*LENGTH+2, LENGTH-3, LENGTH-3);
    }
}

在定义一个窗口类

package com.LifeGame2;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import static com.LifeGame2.Constant.LENGTH;

public class MyFrame {
    public static void main(String[] args) {
        new Run().launch();
    }
}

class Run extends Frame {
    public void launch() {
        this.setSize(1980, 1080);
        this.setLocation(0, 0);
        this.setVisible(true);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        for (int i = 0; i < f.length; i++) {
            for (int j = 0; j < f[i].length; j++) {
                f[i][j] = new Life(i, j);
            }
        }

        {
            f[20][20].live1 = true;
            f[20][21].live1 = true;
            f[21][20].live1 = true;
            f[19][20].live1 = true;
            f[20][22].live1 = true;
            f[18][20].live1 = true;
            f[22][21].live1 = true;
            f[21][22].live1 = true;
            f[18][19].live1 = true;
            f[21][23].live1 = true;
            f[21][18].live1 = true;

            f[30][30].live1 = true;
            f[31][29].live1 = true;
            f[32][29].live1 = true;
            f[32][30].live1 = true;
            f[32][31].live1 = true;

            for (int i=0;i<8;i++){
                f[i+10][10].live1 = true;
            }
        }

        new PaintThread().start();
    }

    int width = 1920 / LENGTH;
    int length = 1080 / LENGTH;
    Life[][] f = new Life[width][length];
    long time = 0;
    long time0 = System.currentTimeMillis();


    @Override
    public void paint(Graphics g) {
        time = (System.currentTimeMillis() - time0) / 1000;
        g.setColor(new Color(42, 17, 21));
        g.fillRect(0, 0, 1920, 1080);
        super.paint(g);
        g.setColor(new Color(147, 147, 147));
        paintLine(g);
        g.setColor(new Color(28, 159, 32));
        paintLife(g);
    }

    //画出分隔线
    private void paintLine(Graphics g) {
        for (int i = 0; i < width; i++) {
            g.drawLine(LENGTH * i, 0, LENGTH * i, 1080);
        }
        for (int i = 0; i < length; i++) {
            g.drawLine(0, LENGTH * i, 1920, LENGTH * i);
        }
    }

    boolean b = true;

    //画出生命
    private void paintLife(Graphics g) {
        for (int i = 1; i < f.length - 1; i++) {
            for (int j = 1; j < f[i].length - 1; j++) {
                int number = judge(i, j, b);
                if (b) {
                    if (f[i][j].live1) {
                        f[i][j].paint(g);
                        number--;
                        if (number < 2 || number > 3) {
                            f[i][j].live2 = false;
                        }else {
                            f[i][j].live2 = f[i][j].live1;
                        }
                    } else {
                        if (number == 3) {
                            f[i][j].live2 = true;
                        }else {
                            f[i][j].live2 = f[i][j].live1;
                        }
                    }
                } else {
                    if (f[i][j].live2) {
                        f[i][j].paint(g);
                        number--;
                        if (number < 2 || number > 3) {
                            f[i][j].live1 = false;
                        }else {
                            f[i][j].live1 = f[i][j].live2;
                        }
                    } else {
                        if (number == 3) {
                            f[i][j].live1 = true;
                        }else {
                            f[i][j].live1 = f[i][j].live2;
                        }
                    }
                }
            }
        }
        b = !b;
    }

    //判断每个生命身边存活数
    private int judge(int i, int j, boolean status) {
        int number = 0;
        for (int ii = 0; ii <= 2; ii++) {
            for (int jj = 0; jj <= 2; jj++) {
                if (status) {
                    if (this.f[i + 1 - ii][j + 1 - jj].live1) {
                        number++;
                    }
                } else {
                    if (this.f[i + 1 - ii][j + 1 - jj].live2) {
                        number++;
                    }
                }
            }
        }
        return number;
    }

    //线程
    class PaintThread extends Thread {
        @Override
        public void run() {
            while (true) {
                repaint();
                try {
                    Thread.sleep(com.LifeGame2.Constant.FREQUENCY);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //双缓冲
    private Image offScreenImage = null;
    public void update(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = this.createImage(1920, 1080);
        }
        Graphics gOff = offScreenImage.getGraphics();
        paint(gOff);
        g.drawImage(offScreenImage, 0, 0, null);
    }
}

这次就没有问题了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值