赞
踩
题目描述
字符雨
呢用其他的前端语言等同样可以实现,但就不偏题了,先来讲讲我们队C#实现吧!代码设计
public class Character
{
//位置
int m_posX, m_posY;
//颜色
ConsoleColor m_color;
//字符
char m_char;
}
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;
}
}
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();
}
}
}
归纳总结
…running…
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。