今天利用了软件工程课以及之后的晚自习时间,我们成功完成了对主窗体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 } 291 292 ////////////////////////////////////////////////////////////////// 293 /////////////////// ///////////////////////////// 294 ////////////////// Draw ////////////////////////////// 295 ///////////////// /////////////////////////////// 296 ////////////////////////////////////////////////////////////////// 297 Bitmap bmp = new Bitmap(this.Width, this.Height); 298 Graphics g = Graphics.FromImage(bmp); 299 /*-----clear panel-----*/ 300 g.Clear(this.BackColor); 301 302 /*-----background-----*/ 303 int img_count = 0; 304 if (background.Width < this.Width) 305 { 306 Bitmap tempBg = new Bitmap(this.Width, 1600); 307 while (background.Width * (img_count) < this.Width) 308 { 309 Graphics g_tempBg = Graphics.FromImage(tempBg); 310 g_tempBg.DrawImage(background, background.Width * img_count, 0); 311 g_tempBg.DrawImage(background, background.Width * img_count, 800); 312 img_count++; 313 } 314 background = tempBg; 315 } 316 g.DrawImage(background, new Rectangle(0, 0, this.Width, this.Height), new Rectangle(0, backY, this.Width, this.Height), GraphicsUnit.Pixel); 317 backY -= 2; 318 if (backY < 0) 319 backY = 800; 320 321 /*------plane------*/ 322 323 g.DrawImage(plane.Image, new Point(plane.X - plane.Width / 2, plane.Y - plane.Height / 2)); 324 325 /*-----bullets-----*/ 326 for (int i = 0; i < bullet_lsit.Count; i++) 327 { 328 g.DrawImage(bullet_lsit[i].Image, new Point(bullet_lsit[i].X - bullet_lsit[i].Width / 2, bullet_lsit[i].Y - bullet_lsit[i].Height / 2)); 329 bullet_lsit[i].Y -= bullet_lsit[i].SpeedY; 330 if (bullet_lsit[i].Y < -40) 331 { 332 bullet_lsit.Remove(bullet_lsit[i]); 333 } 334 } 335 336 /*-----draw reward-----*/ 337 for (int i = 0; i < reward_list.Count; i++) 338 { 339 g.DrawImage(reward_list[i].Image, new Point(reward_list[i].X - reward_list[i].Width / 2, reward_list[i].Y - reward_list[i].Height / 2)); 340 reward_list[i].Y += reward_list[i].SpeedY; 341 reward_list[i].X += reward_list[i].SpeedX; 342 if (reward_list[i].Y > this.Height + 20) 343 { 344 reward_list.Remove(reward_list[i]); 345 } 346 } 347 348 /*-----draw boom icon-----*/ 349 Bitmap boom_icon = EntityFactory.GetBoomIcon(); 350 if (boom_count > 0) 351 { 352 g.DrawImage(boom_icon, new Point(10, this.Height - 40 - boom_icon.Height)); 353 g.DrawString("×" + boom_count, new Font("微软雅黑", 18), Brushes.RosyBrown, new Point(10 + boom_icon.Width, this.Height - 40 - boom_icon.Height)); 354 } 355 356 /*-----enemy-----*/ 357 for (int i = 0; i < enemy_lsit.Count; i++) 358 { 359 g.DrawImage(enemy_lsit[i].Image, new Point(enemy_lsit[i].X - enemy_lsit[i].Width / 2, enemy_lsit[i].Y - enemy_lsit[i].Height / 2)); 360 361 enemy_lsit[i].Y += enemy_lsit[i].SpeedY; 362 enemy_lsit[i].X += enemy_lsit[i].SpeedX; 363 if (enemy_lsit[i].X > this.Width || enemy_lsit[i].X < 0) 364 { 365 enemy_lsit[i].SpeedX = -enemy_lsit[i].SpeedX; 366 } 367 if (enemy_lsit[i].Y > this.Width + 20) 368 { 369 enemy_lsit.Remove(enemy_lsit[i]); 370 } 371 } 372 373 /*-----draw explose-----*/ 374 for (int i = 0; i < explosion_list.Count; i++) 375 { 376 Bitmap temp_explose = explosion_list[i].Images[explosion_list[i].Counter / (explosion_list[i].StnTimes / explosion_list[i].Images.Length)]; 377 g.DrawImage(temp_explose, new Point(explosion_list[i].X - temp_explose.Width / 2, explosion_list[i].Y - temp_explose.Height / 2)); 378 explosion_list[i].Counter += 24; 379 if (explosion_list[i].Counter > explosion_list[i].StnTimes) 380 explosion_list.Remove(explosion_list[i]); 381 } 382 383 /*-----score panel-----*/ 384 g.DrawString("分数:" + score, new Font("微软雅黑", 14), Brushes.Green, new PointF(10, 10)); 385 /*-----level panel-----*/ 386 g.DrawString("等级:" + (send_interval == 1 ? "满级" : plane.Level.ToString()), new Font("微软雅黑", 14), Brushes.Green, new PointF(this.Width - 120, 10)); 387 388 g.Dispose(); 389 this.CreateGraphics().DrawImage(bmp, 0, 0); 390 bmp.Dispose(); 391 //dis = DateTime.Now; 392 } 393 394 private void Form1_Load_1(object sender, EventArgs e) 395 { 396 397 } 398 } 399 }