using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace CharDown
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<CharClass> charClassList = new List<CharClass>();
List<char> charlist = new List<char>();
static object _obj = new object();
int panelWidth = 0;
int panelHeigh = 0;
bool isStop = false;
int sleepMin = 0;
int sleepMax = 0;
bool islianxi = true;
private char GetChar()
{
lock (_obj)
{
char c;
string strs = "ABCDEFGHIGKLMNOPQRSTUVWXYZ";
char[] chars = strs.ToCharArray();
Random r = new Random();
int _index = r.Next(0, chars.Length);
c = chars[_index];
return c;
}
}
private void 开始ToolStripMenuItem_Click(object sender, EventArgs e)
{
switch (this.toolStripComboBox1.SelectedIndex)
{
case 0: sleepMin = 100; sleepMax = 200; break;
case 1: sleepMin = 50; sleepMax = 100; break;
case 2: sleepMin = 10; sleepMax = 50; break;
}
if (this.toolStripComboBox2.SelectedIndex == 0)
{
islianxi = true;
}
else
{
islianxi = false;
}
panelWidth = this.panel1.Width - 30;
panelHeigh = this.panel1.Height - 20;
this.toolStripLabel2.Text = "0";
this.toolStripLabel2.ForeColor = Color.Black;
this.Focus();
//this.panel1.Focus();
isStop = false;
Thread server = new Thread(new ThreadStart(Server));
server.IsBackground = true;
server.Start();
// Server();
}
private void Server()
{
while (true && !isStop)
{
Thread _th = new Thread(new ThreadStart(Begin));
_th.IsBackground = true;
_th.Start();
Thread.Sleep(500);
}
}
private void Begin()
{
char _char;
while (true)
{
_char = GetChar();
if (!charlist.Contains(_char))
{
break;
}
}
charlist.Add(_char);
Random r = new Random();
int x = r.Next(0, panelWidth);
int sleeptime = r.Next(sleepMin, sleepMax);
CharClass _class = new CharClass(x, 0, _char, false, 5);
charClassList.Add(_class);
Color c = this.panel1.BackColor;
Font font = new System.Drawing.Font("宋体", 20, FontStyle.Bold);
Graphics g = this.panel1.CreateGraphics();
while (true && !isStop)
{
Bitmap image = new Bitmap(Application.StartupPath + "\\app.png");
if (_class.IsChecked)
{
ClearImage(_class, g, c, image);
// ClearString(_class, g, c);
charlist.Remove(_class.Char_key);
charClassList.Remove(_class);
if (!islianxi)
{
this.toolStripLabel2.Text = (Convert.ToInt32(this.toolStripLabel2.Text) + 1).ToString();
if (Convert.ToInt32(this.toolStripLabel2.Text) == 100)
{
isStop = true;
MessageBox.Show("恭喜您通过了");
}
}
break;
}
DrawImage(_class, g, c, font, image);
//DrawString(_class, g, c, font);
if (_class.Y >= panelHeigh)
{
ClearImage(_class, g, c, image);
//ClearString(_class, g, c);
charlist.Remove(_class.Char_key);
charClassList.Remove(_class);
if (!islianxi)
{
this.toolStripLabel2.Text = (Convert.ToInt32(this.toolStripLabel2.Text) - 1).ToString();
if (Convert.ToInt32(this.toolStripLabel2.Text) < 0)
{
isStop = true;
MessageBox.Show("游戏结束");
}
}
return;
}
Thread.Sleep(sleeptime);
}
g.Clear(c);
// ClearString(_class, g, c);
}
private void DrawString(CharClass _class, Graphics g, Color c, Font font)
{
if (_class.Y != 0)
{
ClearString(_class, g, c);
}
_class.Y = _class.Y + _class.MoveSize;
g.DrawString(_class.Char_key.ToString(), font, new SolidBrush(Color.Black), _class.X, _class.Y);
}
private void DrawImage(CharClass _class, Graphics gp, Color c, Font font, Bitmap image)
{
Graphics g = Graphics.FromImage(image);
g.DrawString(_class.Char_key.ToString(), font, new SolidBrush(Color.Black), image.Width / 2 - 13, image.Height / 2 - 10);
if (_class.Y != 0)
{
ClearImage(_class, gp, c, image);
}
_class.Y = _class.Y + _class.MoveSize;
gp.DrawImage(image, _class.X, _class.Y);
}
private void ClearImage(CharClass _class, Graphics g, Color c, Bitmap image)
{
g.FillRectangle(new SolidBrush(c), _class.X, _class.Y, image.Width, image.Height);
}
private void ClearString(CharClass _class, Graphics g, Color c)
{
g.FillRectangle(new SolidBrush(c), _class.X, _class.Y, 30, 30);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
isStop = true;
Graphics g = this.panel1.CreateGraphics();
g.Clear(this.panel1.BackColor);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int key = e.KeyValue;
if (key >= 65 && key <= 90)
{
if (charlist.Contains(Convert.ToChar(e.KeyCode)))
{
foreach (CharClass c in charClassList)
{
if (c.Char_key.ToString().ToUpper() == e.KeyCode.ToString().ToUpper())
{
c.IsChecked = true;
if (!islianxi)
{
this.toolStripLabel2.Text = (Convert.ToInt32(this.toolStripLabel2.Text) + 1).ToString();
if (Convert.ToInt32(this.toolStripLabel2.Text) == 100)
{
isStop = true;
MessageBox.Show("恭喜您通过了");
}
}
return;
}
}
}
else if (!islianxi)
{
this.toolStripLabel2.Text = (Convert.ToInt32(this.toolStripLabel2.Text) - 1).ToString();
if (Convert.ToInt32(this.toolStripLabel2.Text) < 10)
{
this.toolStripLabel2.ForeColor = Color.Red;
}
if (Convert.ToInt32(this.toolStripLabel2.Text) < 0)
{
isStop = true;
MessageBox.Show("游戏结束");
}
}
}
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Form1_Load(object sender, EventArgs e)
{
this.toolStripComboBox1.SelectedIndex = 0;
this.toolStripComboBox2.SelectedIndex = 0;
}
}
}
2014年10月29日 02点10分
3