今天我们就是大概谈了一下改进计划,以下是更新后的form.cs代码
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.Xml; 10 using System.Media; 11 using PlayPlane_01.BLL; 12 13 namespace PlayPlane_01 14 { 15 public partial class Form1 : Form 16 { 17 Plane plane; 18 19 Timer t_draw; 20 21 List<Enemy> enemy_lsit = new List<Enemy>(); 22 List<Bullet> bullet_lsit = new List<Bullet>(); 23 List<Explosion> explosion_list = new List<Explosion>(); 24 List<Reward> reward_list = new List<Reward>(); 25 int score = 0; 26 int boom_count = 5; 27 bool pause = false; 28 Bitmap background; 29 30 public Form1() 31 { 32 //this.SetStyle(ControlStyles.UserPaint, true); 33 //this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景. 34 //this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); // 双缓冲 35 this.StartPosition = FormStartPosition.CenterScreen; 36 this.Load += new System.EventHandler(this.Form1_Load); 37 this.MouseMove += new MouseEventHandler(Form1_MouseMove); 38 this.MouseClick += new MouseEventHandler(Form1_MouseClick); 39 this.KeyPress += new KeyPressEventHandler(Form1_KeyPress); 40 InitializeComponent(); 41 } 42 43 private void Form1_Load(object sender, EventArgs e) 44 { 45 //this.FormBorderStyle = FormBorderStyle.None; 46 //this.WindowState = FormWindowState.Maximized; 47 EntityFactory.InitFactory("resource/plane.xml"); 48 background = new Bitmap(Image.FromFile(@"resource/bg_02.jpg")); 49 plane = EntityFactory.GenPlane("normal"); 50 this.Cursor.Dispose(); 51 Cursor.Position = new Point(plane.X + this.Location.X, plane.Y + this.Location.Y); 52 t_draw = new Timer(); 53 t_draw.Interval = 20; 54 send_interval = 100 / t_draw.Interval; 55 block_interval = 260 / t_draw.Interval; 56 reward_interval = 5000 / t_draw.Interval; 57 t_draw.Tick += new EventHandler(t_draw_Tick); 58 t_draw.Start(); 59 } 60 61 void Form1_MouseClick(object sender, MouseEventArgs e) 62 { 63 if (!pause && e.Button == MouseButtons.Right) 64 { 65 if (boom_count > 0) 66 { 67 boom_count--; 68 for (int i = 0; i < enemy_lsit.Count; i++) 69 { 70 //socre ++ 71 if (enemy_lsit[i].Name == "small") score += 1000; 72 else if (enemy_lsit[i].Name == "mid") score += 6000; 73 else if (enemy_lsit[i].Name == "big") score += 25000; 74 //add to explosion 75 explosion_list.Add(EntityFactory.GenExplosion("small", enemy_lsit[i].X, enemy_lsit[i].Y)); 76 77 new DXPlay(this, @"resource/BOMB3.wav").ThreadPlay(); 78 } 79 enemy_lsit.Clear(); 80 } 81 } 82 } 83 84 void Form1_MouseMove(object sender, MouseEventArgs e) 85 { 86 if (!pause) 87 { 88 plane.X = e.X; 89 plane.Y = e.Y; 90 } 91 } 92 93 void Form1_KeyPress(object sender, KeyPressEventArgs e) 94 { 95 if (e.KeyChar == ' ') 96 { 97 pause = !pause; 98 if (pause) 99 { 100 this.Cursor = new Cursor (Cursors.Arrow.CopyHandle()); 101 } 102 else 103 { 104 this.Cursor.Dispose(); 105 Cursor.Position = new Point(plane.X + this.Location.X, plane.Y + this.Location.Y); 106 } 107 } 108 /*else if (e.KeyChar == 27) 109 { 110 this.WindowState = FormWindowState.Normal; 111 } 112 else if (e.KeyChar == '\r') 113 { 114 this.WindowState = FormWindowState.Maximized; 115 }*/ 116 } 117 118 int block_time = 1; 119 int block_interval = 0; 120 int send_time = 0; 121 int send_interval = 0; 122 int reward_time = 1; 123 int reward_interval = 0; 124 int rwd_bullet_stnTime = 0; 125 126 int backY = 800; 127 128 //DateTime dis = DateTime.Now; 129 130 private void t_draw_Tick(object sender, EventArgs e) 131 { 132 //Console.Write("\n ALLTime--->" + (DateTime.Now - dis)); 133 if (pause) 134 { 135 this.CreateGraphics().DrawString("暂 停", new Font("微软雅黑", 22), Brushes.Red, new PointF(this.Width / 2 - 30, this.Height / 2 - 50)); 136 return; 137 } 138 139 ////////////////////////////////////////////////////////////////// 140 /////////////////// ///////////////////////////// 141 ////////////////// Create ////////////////////////////// 142 ///////////////// /////////////////////////////// 143 ////////////////////////////////////////////////////////////////// 144 145 /*------send bullets-----*/ 146 if (send_time > send_interval) 147 { 148 if (rwd_bullet_stnTime > 0) 149 { 150 bullet_lsit.Add(EntityFactory.GenBullet("blue", plane.X - 6, plane.Y - 50)); 151 bullet_lsit.Add(EntityFactory.GenBullet("blue", plane.X + 6, plane.Y - 50)); 152 rwd_bullet_stnTime -= t_draw.Interval * send_interval; 153 } 154 else 155 { 156 bullet_lsit.Add(EntityFactory.GenBullet("red", plane.X, plane.Y - 50)); 157 } 158 new DXPlay(this, @"resource/shoot.wav").ThreadPlay(); 159 send_time = 0; 160 } 161 162 /*------generate enemy-----*/ 163 if (block_time % block_interval == 0) 164 { 165 int speedBase = 0; 166 if (block_interval < 2) 167 speedBase = 1; 168 if (block_interval < 5) 169 speedBase = 2; 170 else if (block_interval < 10) 171 speedBase = 1; 172 173 if (block_time % (block_interval * 20) == 0) 174 { 175 enemy_lsit.Add(EntityFactory.GenEnemy("big",speedBase)); 176 } 177 else if (block_time % (block_interval * 10) == 0) 178 { 179 enemy_lsit.Add(EntityFactory.GenEnemy("mid", speedBase)); 180 } 181 else 182 { 183 enemy_lsit.Add(EntityFactory.GenEnemy("small", speedBase)); 184 } 185 } 186 187 /*-----reward-----*/ 188 if (reward_time == reward_interval) 189 { 190 if (new Random().Next(10000) % 2 == 0) 191 { 192 reward_list.Add(EntityFactory.GenReward("bullet_add", new Random().Next(50, this.Width - 50), 0)); 193 } 194 else 195 { 196 reward_list.Add(EntityFactory.GenReward("boom_add", new Random().Next(50, this.Width - 50), 0)); 197 } 198 reward_time = 0; 199 } 200 201 send_time++; 202 block_time++; 203 reward_time++; 204 205 ////////////////////////////////////////////////////////////////// 206 /////////////////// ///////////////////////////// 207 ////////////////// Judge ////////////////////////////// 208 ///////////////// /////////////////////////////// 209 ////////////////////////////////////////////////////////////////// 210 211 /*-----plane level up-----*/ 212 if (send_interval>0&&score > plane.Level * plane.Level * 50000) 213 { 214 plane.LevelUp(); 215 send_interval--; 216 } 217 218 /*-----enemy lv up-----*/ 219 if (block_interval > 1 && block_time % 300 == 300-1) 220 { 221 block_interval--; 222 } 223 224 /*-----enemy crash-----*/ 225 for (int i = 0; i < enemy_lsit.Count; i++) 226 { 227 for (int j = 0; j < bullet_lsit.Count; j++) 228 { 229 if (Math.Abs(bullet_lsit[j].X - enemy_lsit[i].X) < (bullet_lsit[j].Width + enemy_lsit[i].Width) / 2 && Math.Abs(bullet_lsit[j].Y - enemy_lsit[i].Y) < (bullet_lsit[j].Height + enemy_lsit[i].Height) / 2) 230 { 231 enemy_lsit[i].HP--; 232 233 if (enemy_lsit[i].HP == 0)//explose 234 { 235 //socre ++ 236 if (enemy_lsit[i].Name == "small") score += 1000; 237 else if (enemy_lsit[i].Name == "mid") score += 6000; 238 else if (enemy_lsit[i].Name == "big") score += 25000; 239 //add to explosion 240 explosion_list.Add(EntityFactory.GenExplosion("small", enemy_lsit[i].X, enemy_lsit[i].Y)); 241 242 new DXPlay(this, @"resource/explosion.wav").ThreadPlay(); 243 244 //remove both 245 enemy_lsit.Remove(enemy_lsit[i]); 246 bullet_lsit.Remove(bullet_lsit[j]); 247 } 248 else 249 { 250 //g.FillRectangle(Brushes.Red,new Rectangle(bullet_lsit[j].X,bullet_lsit[j].Y-bullet_lsit[j].Width/2,30,5)); 251 bullet_lsit.Remove(bullet_lsit[j]); 252 } 253 break; 254 } 255 } 256 } 257 258 /*-----get reward-----*/ 259 for (int i = 0; i < reward_list.Count; i++) 260 { 261 if (Math.Abs(plane.X - reward_list[i].X) < (plane.Width + reward_list[i].Width) / 2 && Math.Abs(plane.Y - reward_list[i].Y) < (plane.Height + reward_list[i].Height) / 2) 262 { 263 if (reward_list[i].Name == "bullet_add") 264 { 265 rwd_bullet_stnTime += reward_list[i].StnTimes; 266 } 267 else if (reward_list[i].Name == "boom_add") 268 { 269 boom_count++; 270 } 271 reward_list.Remove(reward_list[i]); 272 } 273 } 274 275 /*-----plane crash-----*/ 276 for (int i = 0; i < enemy_lsit.Count; i++) 277 { 278 bool isCrashed = false; 279 if (Math.Abs(plane.X - enemy_lsit[i].X) < (plane.Width / 4 + enemy_lsit[i].Width) / 2 && Math.Abs(plane.Y - enemy_lsit[i].Y) < (plane.Height - 30 + enemy_lsit[i].Height) / 2) 280 { 281 isCrashed = true; 282 } 283 if (isCrashed) 284 { 285 t_draw.Stop(); 286 this.CreateGraphics().DrawString("Game Over", new Font("微软雅黑", 22), Brushes.Red, new PointF(this.Width / 2 - 100, this.Height / 2 - 50)); 287 //enemy_lsit.Remove(enemy_lsit[i]); 288 return; 289 } 290 }