【C#】实现字符雨动效(VS2019)(mac)

C#字符雨

一·题目描述

C# 是一个简单的、现代的、通用的、面向对象的编程语言,C# 编程是基于 C 和 C++ 编程语言的,推荐学习C#有用的网站可以看看咯,such as: C# Programming Guide
那么有许多强大的编程功能的C#来实现一些有趣的东西同样十分nice!比如我们这一篇文章的目标是实现字符雨,实现字符雨呢用其他的前端语言等同样可以实现,但就不偏题了,先来讲讲我们队C#实现吧!
首先设计编程内部的函数方法等要先摸清楚字符雨的出现过程中各种要求的功能,再把每个小功能一一设计准确,并正确的连接调用,当然,最重要的还是我们在其中的一种debug的体验过程,在这里,我的老师在课堂上发出这一图片:

字符雨

二·代码设计

在字符雨里面,首先是随机的各种字符字母出现,然后各个字符的位置和颜色随机变动,一个字符出现后得消失空隐一下以出现闪烁画面,下一个随机颜色的随机字符即替换,其中需要判断。多个字符组成的字符雨即是一个字符数组,创建多个对象,对设计出的类以及方法进行使用,不断测试:
1:创建一个类Character,包含三大属性分别是位置,颜色,字符。
public class Character
{
    //位置
    int m_posX, m_posY;
    //颜色
    ConsoleColor m_color;
    //字符
    char m_char;
}
2:在类Character里面,声明实例化一个随机数rand,创建构造函数且其中对属性的值进行设置,比如使用Random.Next 方法 (minValue , maxValue )返回一个指定范围内的随机数。并且要显示出字符则创建方法showChar,随后创建字符移动方法Move… 如下:
public class Character
 {
        //位置;
        int m_posX, m_posY;
        //颜色;
        ConsoleColor m_color;
        //字符;
        char m_char;
        static Random rand = new Random();
        public Character()
        {
            m_posX = rand.Next(0, Console.WindowWidth);
            m_posY = rand.Next(0, Console.WindowHeight / 2);//上半部分
            m_color = (ConsoleColor)rand.Next((int)ConsoleColor.Black + 1, (int)ConsoleColor.White + 1);
            m_char = (char)rand.Next('A', 'Z' + 1);//左闭右开
            showChar(m_char);
        }
        private void showChar(char c)
        {
            Console.ForegroundColor = m_color;
            Console.SetCursorPosition(m_posX, m_posY);
            Console.Write(m_char);
        }
        //移动();
        public void Move()
        {
            //showChar(' ');
            m_posY++;
            if (!isDead())
             showChar(m_char);
        }
        //是否死掉();
        public bool isDead()
        {
            return m_posY >= Console.WindowHeight;
        }
    }
3:然后我们在Main创建N个字符数组,我这里暂时设置的100个,循环对每个对象进行方法函数的调用,其中我们使用sleep方法,Thread.Sleep(100),设置为100毫秒。
static void Main(string[] args)
        {
            //字符数组=创建N个Character对象
            Character[] classArry = new Character[100];
            for (int i = 0; i < classArry.Length; i++)
            {
                classArry[i] = new Character();
            }
            while (true)
            {
                Console.Clear();
                for (int i = 0; i < classArry.Length;i++)
                {
                    classArry[i].Move();
                    if (classArry[i].isDead())
                    {
                        classArry[i] = new Character();
                    }
                }
                Thread.Sleep(100);
            }

            Console.ReadKey();
        }

三·代码实现

整体实现可有:
using System;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Collections.Generic;
using System.Linq;

using System.Timers;
namespace _01
{
    public class Character
    {
        //位置;
        int m_posX, m_posY;
        //颜色;
        ConsoleColor m_color;
        //字符;
        char m_char;
        static Random rand = new Random();
        public Character()
        {
            m_posX = rand.Next(0, Console.WindowWidth);
            m_posY = rand.Next(0, Console.WindowHeight / 2);//上半部分
            m_color = (ConsoleColor)rand.Next((int)ConsoleColor.Black + 1, (int)ConsoleColor.White + 1);
            m_char = (char)rand.Next('A', 'Z' + 1);//左闭右开
            showChar(m_char);
        }
        private void showChar(char c)
        {
            Console.ForegroundColor = m_color;
            Console.SetCursorPosition(m_posX, m_posY);
            Console.Write(m_char);
        }
        //移动();
        public void Move()
        {
            //showChar(' ');
            m_posY++;
            if (!isDead())
                showChar(m_char);
        }
        //是否死掉();
        public bool isDead()
        {
            return m_posY >= Console.WindowHeight;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //字符数组=创建N个Character对象
            Character[] classArry = new Character[100];
            for (int i = 0; i < classArry.Length; i++)
            {
                classArry[i] = new Character();
            }
            while (true)
            {
                Console.Clear();
                for (int i = 0; i < classArry.Length;i++)
                {
                    classArry[i].Move();
                    if (classArry[i].isDead())
                    {
                        classArry[i] = new Character();
                    }
                }
                Thread.Sleep(100);
            }
            Console.ReadKey();
        }
    }
}

运行结果如下:

字符雨运行结果

四·归纳总结

任何一个可以实现的功能性操作都需要很细节和严谨的把控,当然其中可以不断改进优化自己的代码,实现更加简洁的代码和更好的效果,在每一步的设计和debug中训练自己达到想要的目的。

…running…

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!要从一个字符串中获取数据,您可以使用C#中的各种方法和函数。以下是几种常见的方法: 1. 使用字符串的索引和子字符串方法。您可以使用字符串的索引来访问特定位置的字符,并使用Substring方法从字符串中提取子字符串。例如: ```csharp string str = "Hello, World!"; char firstChar = str[0]; // 获取第一个字符 'H' string subStr = str.Substring(7, 5); // 获取从索引7开始的5个字符,结果为 "World" ``` 2. 使用正则表达式。如果您需要从字符串中提取特定模式的数据,可以使用正则表达式来匹配和提取数据。例如: ```csharp string str = "Name: John Doe, Age: 30"; Match nameMatch = Regex.Match(str, @"Name: (\w+)"); Match ageMatch = Regex.Match(str, @"Age: (\d+)"); string name = nameMatch.Groups[1].Value; // 获取名字 "John Doe" int age = int.Parse(ageMatch.Groups[1].Value); // 获取年龄 30 ``` 3. 使用字符串分割。如果您的字符串包含多个数据项,并且这些数据项之间使用特定的分隔符进行分隔,您可以使用Split方法将字符串拆分成多个子字符串。例如: ```csharp string str = "Apple,Orange,Banana"; string[] fruits = str.Split(','); // 使用逗号作为分隔符拆分字符串 // fruits 现在是一个包含三个元素的字符串数组:["Apple", "Orange", "Banana"] ``` 这里只是列举了一些常见的方法,具体取决于您的需求和数据的格式。希望对您有所帮助!如果您有任何进一步的问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值