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…