惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
博客园 - 司徒正美
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
A
About on SuperTechFans
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
M
MIT News - Artificial intelligence
L
LangChain Blog
aimingoo的专栏
aimingoo的专栏
Engineering at Meta
Engineering at Meta
Martin Fowler
Martin Fowler
H
Help Net Security
B
Blog
Y
Y Combinator Blog
小众软件
小众软件
S
SegmentFault 最新的问题
I
InfoQ
爱范儿
爱范儿
Hugging Face - Blog
Hugging Face - Blog
D
Docker
博客园 - 【当耐特】
J
Java Code Geeks
阮一峰的网络日志
阮一峰的网络日志

博客园 - 华腾智算

逐个注释 前端学习全景图 · HTML5 + CSS3 全部知识点 前端学习全景图 从零实现跨平台 3D 引擎架构方案 2026年的跨平台 GUI 开发趋势 杀毒 现代图形学的本质 C++快速掌握之一 rust控制流-各种循环 rust控制流 rust函数和注释 rust复核类型--元组tuple和数组 rust的类型 rust常量和变量 rust正确的猜数游戏 rust猜数游戏2 rust猜数游戏 字体选型 20260810代码 Rust 标准库 `std` 中最常用、最核心的模块和类型 Rust 标准库std RUST教程 rust理解一 rust多文件组织 - 华腾智算 Cargo.toml Rust 语言教程 C#常用 C#第六天异常 通俗易懂解释编程-适用于任何编程语言 C#第五天 快速理解整个框架和思想(四层结构,放在哪里,谁能用)
理解图形学之一入门
华腾智算 · 2026-08-10 · via 博客园 - 华腾智算
use std::io::{stdout, Write};
use std::thread::sleep;
use std::time::Duration;

// 颜色
struct Color(u8, u8, u8);

// 虚拟窗口
struct Window {
    width: usize,
    height: usize,
    buffer: Vec<u32>, // 每个像素的颜色值
}

impl Window {
    fn new(width: usize, height: usize) -> Self {
        Window {
            width,
            height,
            buffer: vec![0x000000; width * height], // 黑色
        }
    }

    fn set_pixel(&mut self, x: usize, y: usize, color: Color) {
        if x < self.width && y < self.height {
            let c = ((color.0 as u32) << 16) | ((color.1 as u32) << 8) | (color.2 as u32);
            self.buffer[y * self.width + x] = c;
        }
    }

    fn clear(&mut self, color: Color) {
        let c = ((color.0 as u32) << 16) | ((color.1 as u32) << 8) | (color.2 as u32);
        self.buffer.fill(c);
    }

    // 将缓冲区内容显示到终端(用 ANSI 背景色)
    fn present(&self) {
        let mut out = String::new();
        out.push_str("\x1b[1;1H"); // 移动光标到左上角
        for y in 0..self.height {
            for x in 0..self.width {
                let pixel = self.buffer[y * self.width + x];
                let r = ((pixel >> 16) & 0xFF) as u8;
                let g = ((pixel >> 8) & 0xFF) as u8;
                let b = (pixel & 0xFF) as u8;
                // 用背景色填充两个空格,模拟一个正方形“像素”
                out.push_str(&format!("\x1b[48;2;{r};{g};{b}m  "));
            }
            out.push_str("\x1b[0m\n"); // 行尾重置颜色
        }
        print!("{out}");
        stdout().flush().unwrap();
    }
}

fn main() {
    let mut win = Window::new(40, 20); // 40×20 的“像素”网格
    let mut x = 20.0_f32;
    let mut y = 10.0_f32;
    let mut vx = 0.5;
    let mut vy = 0.3;

    print!("\x1b[2J"); // 初始清屏
    loop {
        win.clear(Color(20, 20, 40)); // 深蓝紫色背景

        // 画一个金色小球(以及它的十字光晕)
        let px = x as usize;
        let py = y as usize;
        win.set_pixel(px, py, Color(255, 200, 0));
        if px > 0 { win.set_pixel(px - 1, py, Color(255, 200, 0)); }
        if px < win.width - 1 { win.set_pixel(px + 1, py, Color(255, 200, 0)); }
        if py > 0 { win.set_pixel(px, py - 1, Color(255, 200, 0)); }
        if py < win.height - 1 { win.set_pixel(px, py + 1, Color(255, 200, 0)); }

        win.present(); // 把画好的帧显示出来

        // 移动小球,碰到边界反弹
        x += vx;
        y += vy;
        if x <= 0.0 || x >= win.width as f32 - 1.0 { vx = -vx; }
        if y <= 0.0 || y >= win.height as f32 - 1.0 { vy = -vy; }

        sleep(Duration::from_millis(50));
    }
}