闲的蛋疼,写的打字游戏

目前还有2个问题:1、快速的时候苹果会闪;2、重叠问题

 

下面代码:

字母信息类

View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 
 5 namespace CharDown
 6 {
 7     class CharClass
 8     {
 9         public CharClass(int _x, int _y, char _char, bool _isChecked, int _moveSize)
10         {
11             X = _x;
12             Y = _y;
13             Char_key = _char;
14             IsChecked = _isChecked;
15             MoveSize = _moveSize;
16         }
17         private int x;
18 
19         /// <summary>
20         /// X
21         /// </summary>
22         public int X
23         {
24             get { return x; }
25             set { x = value; }
26         }
27         private int y=0;
28 
29         /// <summary>
30         /// Y
31         /// </summary>
32         public int Y
33         {
34             get { return y; }
35             set { y = value; }
36         }
37         private char char_key;
38 
39         /// <summary>
40         /// 当前字母
41         /// </summary>
42         public char Char_key
43         {
44             get { return char_key; }
45             set { char_key = value; }
46         }
47         private bool isChecked=false;
48 
49         /// <summary>
50         /// 是否击中
51         /// </summary>
52         public bool IsChecked
53         {
54             get { return isChecked; }
55             set { isChecked = value; }
56         }
57         private int moveSize=10;
58 
59         /// <summary>
60         /// 移动距离
61         /// </summary>
62         public int MoveSize
63         {
64             get { return moveSize; }
65             set { moveSize = value; }
66         }
67     }
68 }

主窗体

View Code
  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Text;
  7 using System.Windows.Forms;
  8 using System.Threading;
  9 
 10 namespace CharDown
 11 {
 12     public partial class Form1 : Form
 13     {
 14         public Form1()
 15         {
 16             InitializeComponent();
 17         }
 18 
 19         List<CharClass> charClassList = new List<CharClass>();
 20         List<char> charlist = new List<char>();
 21         static object _obj = new object();
 22         int panelWidth = 0;
 23         int panelHeigh = 0;
 24         bool isStop = false;
 25         int sleepMin = 0;
 26         int sleepMax = 0;
 27         bool islianxi = true;
 28         private char GetChar()
 29         {
 30             lock (_obj)
 31             {
 32                 char c;
 33                 string strs = "ABCDEFGHIGKLMNOPQRSTUVWXYZ";
 34                 char[] chars = strs.ToCharArray();
 35                 Random r = new Random();
 36                 int _index = r.Next(0, chars.Length);
 37                 c = chars[_index];
 38                 return c;
 39             }
 40         }
 41 
 42         private void 开始ToolStripMenuItem_Click(object sender, EventArgs e)
 43         {
 44             switch (this.toolStripComboBox1.SelectedIndex)
 45             {
 46                 case 0: sleepMin = 100; sleepMax = 200; break;
 47                 case 1: sleepMin = 50; sleepMax = 100; break;
 48                 case 2: sleepMin = 10; sleepMax = 50; break;
 49             }
 50             if (this.toolStripComboBox2.SelectedIndex == 0)
 51             {
 52                 islianxi = true;
 53             }
 54             else
 55             {
 56                 islianxi = false;
 57             }
 58             panelWidth = this.panel1.Width - 30;
 59             panelHeigh = this.panel1.Height - 20;
 60             this.toolStripLabel2.Text = "0";
 61             this.toolStripLabel2.ForeColor = Color.Black;
 62             this.Focus();
 63             //this.panel1.Focus();
 64             isStop = false;
 65             Thread server = new Thread(new ThreadStart(Server));
 66             server.IsBackground = true;
 67             server.Start();
 68             // Server();
 69         }
 70 
 71         private void Server()
 72         {
 73             while (true && !isStop)
 74             {
 75                 Thread _th = new Thread(new ThreadStart(Begin));
 76                 _th.IsBackground = true;
 77                 _th.Start();
 78                 Thread.Sleep(500);
 79             }
 80 
 81         }
 82 
 83         private void Begin()
 84         {
 85             char _char;
 86             while (true)
 87             {
 88                 _char = GetChar();
 89                 if (!charlist.Contains(_char))
 90                 {
 91                     break;
 92                 }
 93             }
 94             charlist.Add(_char);
 95             Random r = new Random();
 96             int x = r.Next(0, panelWidth);
 97             int sleeptime = r.Next(sleepMin, sleepMax);
 98             CharClass _class = new CharClass(x, 0, _char, false, 5);
 99             charClassList.Add(_class);
100             Color c = this.panel1.BackColor;
101             Font font = new System.Drawing.Font("宋体", 20, FontStyle.Bold);
102             Graphics g = this.panel1.CreateGraphics();
103             while (true && !isStop)
104             {
105                 Bitmap image = new Bitmap(Application.StartupPath + "\\app.png");
106                 
107                 if (_class.IsChecked)
108                 {
109                     ClearImage(_class, g, c, image);
110                    // ClearString(_class, g, c);
111                     charlist.Remove(_class.Char_key);
112                     charClassList.Remove(_class);
113                     if (!islianxi)
114                     {
115                         this.toolStripLabel2.Text = (Convert.ToInt32(this.toolStripLabel2.Text) + 1).ToString();
116                         if (Convert.ToInt32(this.toolStripLabel2.Text) == 100)
117                         {
118                             isStop = true;
119                             MessageBox.Show("恭喜您通过了");
120                         }
121                     }
122                     break;
123                 }
124                 DrawImage(_class, g, c, font, image);
125                 //DrawString(_class, g, c, font);
126                 if (_class.Y >= panelHeigh)
127                 {
128                     ClearImage(_class, g, c, image);
129                     //ClearString(_class, g, c);
130                     charlist.Remove(_class.Char_key);
131                     charClassList.Remove(_class);
132                     if (!islianxi)
133                     {
134                         this.toolStripLabel2.Text = (Convert.ToInt32(this.toolStripLabel2.Text) - 1).ToString();
135                         if (Convert.ToInt32(this.toolStripLabel2.Text) < 0)
136                         {
137                             isStop = true;
138                             MessageBox.Show("游戏结束");
139                         }
140                     }
141                     return;
142                 }
143                 Thread.Sleep(sleeptime);
144             }
145             g.Clear(c);
146           
147            // ClearString(_class, g, c);
148         }
149         private void DrawString(CharClass _class, Graphics g, Color c, Font font)
150         {
151             if (_class.Y != 0)
152             {
153                 ClearString(_class, g, c);
154             }
155             _class.Y = _class.Y + _class.MoveSize;
156             g.DrawString(_class.Char_key.ToString(), font, new SolidBrush(Color.Black), _class.X, _class.Y);
157         }
158 
159         private void DrawImage(CharClass _class, Graphics gp, Color c, Font font, Bitmap image)
160         {
161             Graphics g = Graphics.FromImage(image);
162             g.DrawString(_class.Char_key.ToString(), font, new SolidBrush(Color.Black), image.Width / 2 - 13, image.Height / 2 - 10);
163 
164             if (_class.Y != 0)
165             {
166                 ClearImage(_class, gp, c, image);
167             }
168             _class.Y = _class.Y + _class.MoveSize;
169             gp.DrawImage(image, _class.X, _class.Y);
170         }
171 
172         private void ClearImage(CharClass _class, Graphics g, Color c, Bitmap image)
173         {
174             g.FillRectangle(new SolidBrush(c), _class.X, _class.Y, image.Width, image.Height);
175         }
176 
177         private void ClearString(CharClass _class, Graphics g, Color c)
178         {
179             g.FillRectangle(new SolidBrush(c), _class.X, _class.Y, 30, 30);
180         }
181 
182         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
183         {
184             Application.Exit();
185         }
186 
187         private void toolStripButton2_Click(object sender, EventArgs e)
188         {
189             isStop = true;
190             Graphics g = this.panel1.CreateGraphics();
191             g.Clear(this.panel1.BackColor);
192         }
193 
194         private void Form1_KeyDown(object sender, KeyEventArgs e)
195         {
196             int key = e.KeyValue;
197             if (key >= 65 && key <= 90)
198             {
199                 if (charlist.Contains(Convert.ToChar(e.KeyCode)))
200                 {
201                     foreach (CharClass c in charClassList)
202                     {
203                         if (c.Char_key.ToString().ToUpper() == e.KeyCode.ToString().ToUpper())
204                         {
205                             c.IsChecked = true;
206                             if (!islianxi)
207                             {
208                                 this.toolStripLabel2.Text = (Convert.ToInt32(this.toolStripLabel2.Text) + 1).ToString();
209                                 if (Convert.ToInt32(this.toolStripLabel2.Text) == 100)
210                                 {
211                                     isStop = true;
212                                     MessageBox.Show("恭喜您通过了");
213                                 }
214                             }
215                             return;
216                         }
217                     }
218                 }
219                 else if (!islianxi)
220                 {
221                     this.toolStripLabel2.Text = (Convert.ToInt32(this.toolStripLabel2.Text) - 1).ToString();
222                     if (Convert.ToInt32(this.toolStripLabel2.Text) < 10)
223                     {
224                         this.toolStripLabel2.ForeColor = Color.Red;
225                     }
226 
227                     if (Convert.ToInt32(this.toolStripLabel2.Text) < 0)
228                     {
229                         isStop = true;
230                         MessageBox.Show("游戏结束");
231                     }
232                 }
233             }
234         }
235 
236         private void toolStripButton3_Click(object sender, EventArgs e)
237         {
238             Application.Exit();
239         }
240 
241         private void Form1_Load(object sender, EventArgs e)
242         {
243             this.toolStripComboBox1.SelectedIndex = 0;
244             this.toolStripComboBox2.SelectedIndex = 0;
245         }
246     }
247 }

 

望批评指正,可执行文件地址http://pan.baidu.com/share/link?shareid=204227&uk=33979446

posted @ 2012-12-27 12:37  冰封一夏  阅读(821)  评论(0编辑  收藏  举报
HZHControls控件库官网:http://hzhcontrols.com