【2024】Merry Christmas!一起用Rust绘制一颗圣诞树吧

博主未授权任何人或组织机构转载博主任何原创文章,感谢各位对原创的支持!
博主链接

博客内容主要围绕:

5G/6G协议讲解

高级C语言讲解

Rust语言讲解

文章目录

一起用Rust绘制一颗圣诞树吧

又是一年圣诞节,我们使用Rust的控制台输出来绘制一颗动态的圣诞树,效果如下:

一、 Rust Cargo.toml配置文件

yaml 复制代码
[package]
name = "helloworld"
version = "0.1.0"
edition = "2021"

[dependencies]
text-colorizer = "1.0.0"
rand = "0.8.5"
clearscreen = "3.0.0"

二、代码实现

  • print_rand_leaf():随机化树叶的形状;
  • triangle():绘制圣诞树的每一层;
  • print_tree():调用triangle()完成整个树冠的绘制;
  • print_log():完成整个树杆的绘制;

几个关键常量含义如下:

  • REF_RATE:圣诞树的刷新频率,越小刷新越快;
  • RANDOMNESS:控制树叶中' * '的密度,越大密度越高;
  • WIDTH_FACTOR:控制圣诞树在窗口中的位置,最小值为2。值越大越往右边偏移;
rust 复制代码
use text_colorizer::*;
use std::thread::sleep;
use std::time::Duration;
use rand::Rng;

static RANDOMNESS:u8 = 5; // 越高树叶越少
static REF_RATE:u64 = 500;
static WIDTH_FACTOR:u16 = 4;

fn print_rand_leaf()
{
    let leaf_types:[&str;5] = [".", "*", "⭐", "🍎", "0"];
    let mut rng = rand::thread_rng();
    let temp = rng.gen_range(0..RANDOMNESS);

    // Giving preference to *
    if temp == 1
    {
        let other = rng.gen_range(0..leaf_types.len());

        if other == 3
        {
            print!("{} ", leaf_types[other].red());
        }
        else if other == 2
        {
            print!("{} ", leaf_types[other].yellow());
        }
        else
        {
            print!("{} ", leaf_types[other]);
        }
    }
    else
    {
        print!("{} ", leaf_types[1].green());
    }

}

fn triangle(f:u16, n:u16, toth:u16)
{
    let _i = WIDTH_FACTOR * toth - 2;
    let _j = WIDTH_FACTOR * toth - 2;
    let mut k = WIDTH_FACTOR * toth - 2;

    for _i in 0..f-1
    {
       k -= 1;
    }

    // number of rows
    for i in f - 1..n
    {
        // space handler
        for _j in 0..k
        {
            print!(" ");
        }

        // decrementing k after each loop
        k = k - 1;

        // number of columns, printing stars
        for _j in 0..=i
        {
            print_rand_leaf();
        }

        print!("\n");
    }
}

// Prints bottom part.
fn print_log(n:u16)
{
    let _i = WIDTH_FACTOR * n - 4;
    let _j = WIDTH_FACTOR * n - 4;
    let k = WIDTH_FACTOR * n - 4;

    for _i in 1..7
    {
        // space handler
        for _j in 0..k
        {
            print!(" ");
        }

        for _j in 1..7
        {
            print!("\x1b[38;5;58m#\x1b[0m");
        }

        print!("\n");
    }
}


// Prints multiple triangles
fn print_tree(h:u16)
{
    let mut start = 1;
    let mut stop = 0;
    let mut diff = 3;

    while stop < h + 1
    {
        stop = start + diff;
        triangle(start, stop, h);
        diff += 1;
        start = stop - 2;
    }
}


fn main() {

    let ht:u16 = 9;

    println!("\n*********MERRY CHRISTMAS*********\n\n");

    loop {
        clearscreen::clear().expect("failed to clear screen");
        print!("\n\n\n");

        print_tree(ht);
        print_log(ht);

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

祝各位读者,圣诞节快乐

《【2023】Merry Christmas!一起用C语言绘制一个动态的圣诞树吧》


相关推荐
lly2024063 分钟前
Highcharts 饼图:数据可视化利器
开发语言
lw向北.9 分钟前
Qt For Android之环境搭建(Qt 5.12.11 Qt下载SDK的处理方案)
android·开发语言·qt
万亿少女的梦1689 分钟前
基于Spring Boot的网络购物商城的设计与实现
java·spring boot·后端
IT女孩儿30 分钟前
JavaScript--WebAPI查缺补漏(二)
开发语言·前端·javascript·html·ecmascript
m0_7482389231 分钟前
webgis入门实战案例——智慧校园
开发语言·ios·swift
Clockwiseee1 小时前
PHP伪协议总结
android·开发语言·php
小灰灰搞电子1 小时前
Qt实现Android的图案密码(图形解锁)源码分享
开发语言·qt
吴冰_hogan2 小时前
JVM(Java虚拟机)的组成部分详解
java·开发语言·jvm
开心工作室_kaic2 小时前
springboot485基于springboot的宠物健康顾问系统(论文+源码)_kaic
spring boot·后端·宠物
0zxm2 小时前
08 Django - Django媒体文件&静态文件&文件上传
数据库·后端·python·django·sqlite