Java小游戏之数字彩虹雨

package caihong;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.MemoryImageSource;
import java.util.Random;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Rain extends JDialog implements ActionListener {

    private Random random = new Random();
    private Dimension screenSize;
    private JPanel graphicsPanel;
    //行高,列宽
    private final static int gap = 20;
    //存放雨点顶部的位置信息(marginTop)
    private int[] posArr;
    //行数
    private int lines;
    //列数
    private int columns;

    public Rain() {
        initComponents();
    }

    private void initComponents() {
        setLayout(new BorderLayout());
        graphicsPanel = new GraphicsPanel();
        add(graphicsPanel, BorderLayout.CENTER);
        //设置光标不可见
        Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
        Image image = defaultToolkit.createImage(new MemoryImageSource(0, 0, null, 0, 0));
        Cursor invisibleCursor = defaultToolkit.createCustomCursor(image, new Point(0, 0), "cursor");
        setCursor(invisibleCursor);
        //ESC键退出
        KeyPressListener keyPressListener = new KeyPressListener();
        this.addKeyListener(keyPressListener);
        //this.setAlwaysOnTop(true);
        //去标题栏
        this.setUndecorated(true);
        //全屏
        this.getGraphicsConfiguration().getDevice().setFullScreenWindow(this);
        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        setVisible(true);

        screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        lines = screenSize.height / gap;
        columns = screenSize.width / gap;

        posArr = new int[columns + 1];
        random = new Random();
        for (int i = 0; i < posArr.length; i++) {
            posArr[i] = random.nextInt(lines);
        }

        //每秒10帧
        new Timer(100, this).start();
    }

    /**
     * @return 随机字符
     */
    private char getChr() {
        return (char) (random.nextInt(94) + 33);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        graphicsPanel.repaint();
    }

    private class GraphicsPanel extends JPanel {
        @Override
        public void paint(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
            g2d.setFont(getFont().deriveFont(Font.BOLD));
            g2d.setColor(Color.BLACK);
            g2d.fillRect(0, 0, screenSize.width, screenSize.height);
            //当前列
            int currentColumn = 0;
            for (int x = 0; x < screenSize.width; x += gap) {
                int endPos = posArr[currentColumn];
                g2d.setColor(Color.CYAN);
                g2d.drawString(String.valueOf(getChr()), x, endPos * gap);
                int cg = 0;
                for (int j = endPos - 15; j < endPos; j++) {
                    //颜色渐变
                    cg += 20;
                    if (cg > 255) {
                        cg = 255;
                    }
                    g2d.setColor(new Color(0, cg, 0));
                    g2d.drawString(String.valueOf(getChr()), x, j * gap);
                }
                //每放完一帧,当前列上雨点的位置随机下移1~5行
                posArr[currentColumn] += random.nextInt(5);
                //当雨点位置超过屏幕高度时,重新产生一个随机位置
                if (posArr[currentColumn] * gap > getHeight()) {
                    posArr[currentColumn] = random.nextInt(lines);
                }
                currentColumn++;    
            }
        }
    }

    private class KeyPressListener extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                System.exit(0);
            }
        }
    }

    public static void main(String[] args) {
        new Rain();
    }
}

  • 10
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
以下是一个简单的 Java 彩虹游戏代码示例: ```java import java.awt.*; import java.awt.event.*; import java.util.Random; import javax.swing.*; public class RainbowGame extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private static final int WIDTH = 400; private static final int HEIGHT = 400; private static final int DELAY = 50; private static final int FONT_SIZE = 30; private static final String[] COLORS = {"red", "orange", "yellow", "green", "blue", "indigo", "violet"}; private static final Random rand = new Random(); private Timer timer; private JLabel textLabel; private Color currentColor; public RainbowGame() { super("Rainbow Game"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(WIDTH, HEIGHT); setResizable(false); setLocationRelativeTo(null); setLayout(new BorderLayout()); textLabel = new JLabel("Press the space bar to start.", JLabel.CENTER); textLabel.setFont(new Font("Arial", Font.BOLD, FONT_SIZE)); add(textLabel, BorderLayout.CENTER); timer = new Timer(DELAY, this); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == timer) { currentColor = getRandomColor(); textLabel.setForeground(currentColor); } } private Color getRandomColor() { int index = rand.nextInt(COLORS.length); switch (COLORS[index]) { case "red": return Color.RED; case "orange": return Color.ORANGE; case "yellow": return Color.YELLOW; case "green": return Color.GREEN; case "blue": return Color.BLUE; case "indigo": return new Color(75, 0, 130); // dark blue case "violet": return new Color(148, 0, 211); // dark violet default: return Color.BLACK; } } private void startGame() { textLabel.setText("Rainbow Game!"); textLabel.setForeground(Color.BLACK); timer.start(); } private void endGame() { timer.stop(); textLabel.setText("Game over."); textLabel.setForeground(Color.BLACK); } public static void main(String[] args) { RainbowGame game = new RainbowGame(); game.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_SPACE) { if (game.timer.isRunning()) { game.endGame(); } else { game.startGame(); } } } }); } } ``` 这个小游戏的规则很简单:按下空格键开始游戏,每隔一段时间,文字颜色会随机变化,直到用户再次按下空格键结束游戏

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值