实例介绍
【实例截图】
【核心代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace HuaRongDao { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private Panel pl; //记录选中的控件ID private void Form1_Load( object sender, EventArgs e) { //初始化时设置各个位置的可用状态 PlState[0, 0] = PlState[0, 1] = PlState[0, 2] = PlState[0, 3] = PlState[1, 0] = PlState[1, 1] = PlState[1, 2] = PlState[1, 3] = PlState[2, 0] = PlState[2, 1] = PlState[2, 2] = PlState[2, 3] = PlState[3, 0] = PlState[3, 1] = PlState[3, 2] = PlState[3, 3] = PlState[4, 0] = PlState[4, 3] = true ; } //初始化各人物方格位置 private void button1_Click( object sender, EventArgs e) { //设置各方格的初始图片 panel1.BackgroundImage = (Image)(Properties.Resources._003); panel2.BackgroundImage = (Image)(Properties.Resources._001); panel3.BackgroundImage = (Image)(Properties.Resources._004); panel4.BackgroundImage = (Image)(Properties.Resources._005); panel5.BackgroundImage = (Image)(Properties.Resources._002); panel6.BackgroundImage = (Image)(Properties.Resources._006); panel7.BackgroundImage = (Image)(Properties.Resources._007); panel8.BackgroundImage = (Image)(Properties.Resources._008); panel9.BackgroundImage = (Image)(Properties.Resources._009); panel10.BackgroundImage = (Image)(Properties.Resources._010); //设置各方格的初始位置 panel1.Location = position[0, 0]; panel2.Location = position[0, 1]; panel3.Location = position[0, 3]; panel4.Location = position[2, 0]; panel5.Location = position[2, 1]; panel6.Location = position[2, 3]; panel7.Location = position[3, 1]; panel8.Location = position[3, 2]; panel9.Location = position[4, 0]; panel10.Location = position[4, 3]; PlState[4, 0] = PlState[4, 3] = true ; //设置最后一行的首尾位置不可用 PlState[4, 1] = PlState[4, 2] = false ; //设置最后一行的中间两个位置可用 } #region 赵云 private void panel1_Click( object sender, EventArgs e) { //设置各方格图片 panel1.BackgroundImage = (Image)(Properties.Resources.a3); panel2.BackgroundImage = (Image)(Properties.Resources._001); panel3.BackgroundImage = (Image)(Properties.Resources._004); panel4.BackgroundImage = (Image)(Properties.Resources._005); panel5.BackgroundImage = (Image)(Properties.Resources._002); panel6.BackgroundImage = (Image)(Properties.Resources._006); panel7.BackgroundImage = (Image)(Properties.Resources._007); panel8.BackgroundImage = (Image)(Properties.Resources._008); panel9.BackgroundImage = (Image)(Properties.Resources._009); panel10.BackgroundImage = (Image)(Properties.Resources._010); PStyle = PStyles.P2V; //记录方格样式 pl = panel1; //记录选中的控件ID PosX = intX(panel1); //记录选中控件在坐标数组中的列索引 PosY = intY(panel1); //记录选中控件在坐标数组中的行索引 } #endregion #region 曹操 private void panel2_Click( object sender, EventArgs e) { panel1.BackgroundImage = (Image)(Properties.Resources._003); panel2.BackgroundImage = (Image)(Properties.Resources.a1); panel3.BackgroundImage = (Image)(Properties.Resources._004); panel4.BackgroundImage = (Image)(Properties.Resources._005); panel5.BackgroundImage = (Image)(Properties.Resources._002); panel6.BackgroundImage = (Image)(Properties.Resources._006); panel7.BackgroundImage = (Image)(Properties.Resources._007); panel8.BackgroundImage = (Image)(Properties.Resources._008); panel9.BackgroundImage = (Image)(Properties.Resources._009); panel10.BackgroundImage = (Image)(Properties.Resources._010); PStyle = PStyles.P4; pl = panel2; PosX = intX(panel2); PosY = intY(panel2); } #endregion #region 张飞 private void panel3_Click( object sender, EventArgs e) { panel1.BackgroundImage = (Image)(Properties.Resources._003); panel2.BackgroundImage = (Image)(Properties.Resources._001); panel3.BackgroundImage = (Image)(Properties.Resources.a4); panel4.BackgroundImage = (Image)(Properties.Resources._005); panel5.BackgroundImage = (Image)(Properties.Resources._002); panel6.BackgroundImage = (Image)(Properties.Resources._006); panel7.BackgroundImage = (Image)(Properties.Resources._007); panel8.BackgroundImage = (Image)(Properties.Resources._008); panel9.BackgroundImage = (Image)(Properties.Resources._009); panel10.BackgroundImage = (Image)(Properties.Resources._010); PStyle = PStyles.P2V; pl = panel3; PosX = intX(panel3); PosY = intY(panel3); } #endregion #region 马超 private void panel4_Click( object sender, EventArgs e) { panel1.BackgroundImage = (Image)(Properties.Resources._003); panel2.BackgroundImage = (Image)(Properties.Resources._001); panel3.BackgroundImage = (Image)(Properties.Resources._004); panel4.BackgroundImage = (Image)(Properties.Resources.a5); panel5.BackgroundImage = (Image)(Properties.Resources._002); panel6.BackgroundImage = (Image)(Properties.Resources._006); panel7.BackgroundImage = (Image)(Properties.Resources._007); panel8.BackgroundImage = (Image)(Properties.Resources._008); panel9.BackgroundImage = (Image)(Properties.Resources._009); panel10.BackgroundImage = (Image)(Properties.Resources._010); PStyle = PStyles.P2V; pl = panel4; PosX = intX(panel4); PosY = intY(panel4); } #endregion #region 关羽 private void panel5_Click( object sender, EventArgs e) { panel1.BackgroundImage = (Image)(Properties.Resources._003); panel2.BackgroundImage = (Image)(Properties.Resources._001); panel3.BackgroundImage = (Image)(Properties.Resources._004); panel4.BackgroundImage = (Image)(Properties.Resources._005); panel5.BackgroundImage = (Image)(Properties.Resources.a2); panel6.BackgroundImage = (Image)(Properties.Resources._006); panel7.BackgroundImage = (Image)(Properties.Resources._007); panel8.BackgroundImage = (Image)(Properties.Resources._008); panel9.BackgroundImage = (Image)(Properties.Resources._009); panel10.BackgroundImage = (Image)(Properties.Resources._010); PStyle = PStyles.P2H; pl = panel5; PosX = intX(panel5); PosY = intY(panel5); } #endregion #region 黄忠 private void panel6_Click( object sender, EventArgs e) { panel1.BackgroundImage = (Image)(Properties.Resources._003); panel2.BackgroundImage = (Image)(Properties.Resources._001); panel3.BackgroundImage = (Image)(Properties.Resources._004); panel4.BackgroundImage = (Image)(Properties.Resources._005); panel5.BackgroundImage = (Image)(Properties.Resources._002); panel6.BackgroundImage = (Image)(Properties.Resources.a6); panel7.BackgroundImage = (Image)(Properties.Resources._007); panel8.BackgroundImage = (Image)(Properties.Resources._008); panel9.BackgroundImage = (Image)(Properties.Resources._009); panel10.BackgroundImage = (Image)(Properties.Resources._010); PStyle = PStyles.P2V; pl = panel6; PosX = intX(panel6); PosY = intY(panel6); } #endregion #region 士兵一 private void panel7_Click( object sender, EventArgs e) { panel1.BackgroundImage = (Image)(Properties.Resources._003); panel2.BackgroundImage = (Image)(Properties.Resources._001); panel3.BackgroundImage = (Image)(Properties.Resources._004); panel4.BackgroundImage = (Image)(Properties.Resources._005); panel5.BackgroundImage = (Image)(Properties.Resources._002); panel6.BackgroundImage = (Image)(Properties.Resources._006); panel7.BackgroundImage = (Image)(Properties.Resources.a7); panel8.BackgroundImage = (Image)(Properties.Resources._008); panel9.BackgroundImage = (Image)(Properties.Resources._009); panel10.BackgroundImage = (Image)(Properties.Resources._010); PStyle = PStyles.P1; pl = panel7; PosX = intX(panel7); PosY = intY(panel7); } #endregion #region 士兵二 private void panel8_Click( object sender, EventArgs e) { panel1.BackgroundImage = (Image)(Properties.Resources._003); panel2.BackgroundImage = (Image)(Properties.Resources._001); panel3.BackgroundImage = (Image)(Properties.Resources._004); panel4.BackgroundImage = (Image)(Properties.Resources._005); panel5.BackgroundImage = (Image)(Properties.Resources._002); panel6.BackgroundImage = (Image)(Properties.Resources._006); panel7.BackgroundImage = (Image)(Properties.Resources._007); panel8.BackgroundImage = (Image)(Properties.Resources.a8); panel9.BackgroundImage = (Image)(Properties.Resources._009); panel10.BackgroundImage = (Image)(Properties.Resources._010); PStyle = PStyles.P1; pl = panel8; PosX = intX(panel8); PosY = intY(panel8); } #endregion #region 士兵三 private void panel9_Click( object sender, EventArgs e) { panel1.BackgroundImage = (Image)(Properties.Resources._003); panel2.BackgroundImage = (Image)(Properties.Resources._001); panel3.BackgroundImage = (Image)(Properties.Resources._004); panel4.BackgroundImage = (Image)(Properties.Resources._005); panel5.BackgroundImage = (Image)(Properties.Resources._002); panel6.BackgroundImage = (Image)(Properties.Resources._006); panel7.BackgroundImage = (Image)(Properties.Resources._007); panel8.BackgroundImage = (Image)(Properties.Resources._008); panel9.BackgroundImage = (Image)(Properties.Resources.a9); panel10.BackgroundImage = (Image)(Properties.Resources._010); PStyle = PStyles.P1; pl = panel9; PosX = intX(panel9); PosY = intY(panel9); } #endregion #region 士兵四 private void panel10_Click( object sender, EventArgs e) { panel1.BackgroundImage = (Image)(Properties.Resources._003); panel2.BackgroundImage = (Image)(Properties.Resources._001); panel3.BackgroundImage = (Image)(Properties.Resources._004); panel4.BackgroundImage = (Image)(Properties.Resources._005); panel5.BackgroundImage = (Image)(Properties.Resources._002); panel6.BackgroundImage = (Image)(Properties.Resources._006); panel7.BackgroundImage = (Image)(Properties.Resources._007); panel8.BackgroundImage = (Image)(Properties.Resources._008); panel9.BackgroundImage = (Image)(Properties.Resources._009); panel10.BackgroundImage = (Image)(Properties.Resources.a10); PStyle = PStyles.P1; pl = panel10; PosX = intX(panel10); PosY = intY(panel10); } #endregion #region 判断是否成功 ///<summary> ///判断是否成功 ///</summary> ///<returns>true表示成功,false表示失败</returns> private bool Successful() { if (panel2.Location == new Point(108, 343)) { MessageBox.Show( "恭喜你,曹操已经成功逃离华容道!" , "提示" , MessageBoxButtons.OK, MessageBoxIcon.Information); return true ; } else return false ; } #endregion //通过按键盘上的上、下、左、右键来移动人物方格 private void Form1_KeyUp( object sender, KeyEventArgs e) { blUp = blDown = blLeft = blRight = false ; //设置上、下、左、右方向移动不可用 switch (e.KeyData) { case Keys.Up: //向上移动 blUp = true ; break ; case Keys.Down: //向下移动 blDown = true ; break ; case Keys.Left: //向左移动 blLeft = true ; break ; case Keys.Right: //向右移动 blRight = true ; break ; } MovePosition(pl, PosX, PosY); //移动人物方格位置 if (Successful()) //判断是否成功 button1_Click(sender, e); //重新开始 } //定义每个人物方格的大小, 共有x*y(x=4, y=5)个人物方格 public enum plEnumerate : int { plSize = 100, //人物方格大小 plX = 4, //定义列 plY = 5, //定义行 }; //判断索引位置是否可用 public bool [,] PlState = new bool [( int )plEnumerate.plY, ( int )plEnumerate.plX]; //定义一个数组,用来记录各人物方格位置 private Point[,] position = new Point[5, 4] { { new Point(8, 43), new Point(108, 43), new Point(208, 43), new Point(308, 43) }, { new Point(8, 143), new Point(108, 143), new Point(208, 143), new Point(308, 143) }, { new Point(8, 243), new Point(108, 243), new Point(208, 243), new Point(308, 243) }, { new Point(8, 343), new Point(108, 343), new Point(208, 343), new Point(308, 343) }, { new Point(8, 443), new Point(108, 443), new Point(208, 443), new Point(308, 443) }, }; //获取人物方格的位置 public Point[,] GetPosition() { return this .position; } //定义各人物方格的形状 public enum PStyles : int { P1 = 0, //口 P4 = 1, //田 P2V = 2, //日 P2H = 3, //口口 }; private int m_PStyle; //获取人物样式的编号 //记录人物样式形状 public PStyles PStyle { get { return (PStyles) this .m_PStyle; } set { this .m_PStyle = ( int )value; } } private int m_X = 0, m_Y = 0; //记录人物方格索引 //返回人物方格列索引 public int PosX { get { return this .m_X; } set { this .m_X = value; } } //返回人物方格行索引 public int PosY { get { return this .m_Y; } set { this .m_Y = value; } } #region 计算选中的人物方格在数组中的列索引 ///<summary> ///计算选中的人物方格在数组中的列索引 ///</summary> ///<param name="pl">选中的Panel控件</param> ///<returns>列索引</returns> private int intX(Panel pl) { int i = pl.Location.X - 8; return i / 100; } #endregion #region 计算选中的人物方格在数组中的行索引 ///<summary> ///计算选中的人物方格在数组中的行索引 ///</summary> ///<param name="pl">选中的Panel控件</param> ///<returns>行索引</returns> private int intY(Panel pl) { int i = pl.Location.Y - 43; return i / 100; } #endregion //定义4个变量,分别用来表示向上、下、左、右移动 private bool blUp, blDown, blLeft, blRight; #region 移动人物位置 ///<summary> ///移动人物位置 ///</summary> ///<param name="pl">要移动的控件名称</param> ///<param name="x">横坐标在坐标数组中的索引</param> ///<param name="y">纵坐标在坐标数组中的索引</param> ///<returns>是否移动成功</returns> public bool MovePosition(Panel pl, int x, int y) { #region 上移 if (blUp && (y - 1) >= 0) { switch (PStyle) { case PStyles.P4: //田 case PStyles.P2H: //口口 if (!PlState[y - 1, x] && !PlState[y - 1, x 1]) { pl.Location = GetPosition()[y - 1, x]; if (PStyle == PStyles.P4) //田 { PlState[y 1, x] = false ; PlState[y 1, x 1] = false ; } else if (PStyle == PStyles.P2H) //口口 { PlState[y, x] = false ; PlState[y, x 1] = false ; } PlState[y - 1, x] = true ; PlState[y - 1, x 1] = true ; PosY -= 1; return true ; } else return false ; case PStyles.P2V: //日 case PStyles.P1: //口 if (!PlState[y - 1, x]) { pl.Location = GetPosition()[y - 1, x]; if (PStyle == PStyles.P2V) //日 { PlState[y 1, x] = false ; } else if (PStyle == PStyles.P1) //口 { PlState[y, x] = false ; } PlState[y - 1, x] = true ; PosY -= 1; return true ; } else return false ; } } #endregion #region 下移 else if (blDown) { switch (PStyle) { case PStyles.P4: //田 if ((y 2) < ( int )plEnumerate.plY && !PlState[y 2, x] && !PlState[y 2, x 1]) { pl.Location = GetPosition()[y 1, x]; PlState[y, x] = false ; PlState[y, x 1] = false ; PlState[y 2, x] = true ; PlState[y 2, x 1] = true ; PosY = 1; return true ; } else return false ; case PStyles.P2V: //日 if ((y 2) < ( int )plEnumerate.plY && !PlState[y 2, x]) { pl.Location = GetPosition()[y 1, x]; PlState[y, x] = false ; PlState[y 2, x] = true ; PosY = 1; return true ; } else return false ; case PStyles.P1: //口 if ((y 1) < ( int )plEnumerate.plY && !PlState[y 1, x]) { pl.Location = GetPosition()[y 1, x]; PlState[y, x] = false ; PlState[y 1, x] = true ; PosY = 1; return true ; } else return false ; case PStyles.P2H: //口口 if ((y 1) < ( int )plEnumerate.plY && !PlState[y 1, x] && !PlState[y 1, x 1]) { pl.Location = GetPosition()[y 1, x]; PlState[y, x] = false ; PlState[y, x 1] = false ; PlState[y 1, x] = true ; PlState[y 1, x 1] = true ; PosY = 1; return true ; } else return false ; } } #endregion #region 左移 else if (blLeft) { switch (PStyle) { case PStyles.P2V: //日 case PStyles.P4: //田 if (x - 1 >= 0 && !PlState[y, x - 1] && !PlState[y 1, x - 1]) { pl.Location = GetPosition()[y, x - 1]; switch (PStyle) { case PStyles.P4: //田 PlState[y, x 1] = false ; PlState[y 1, x 1] = false ; break ; case PStyles.P2V: //日 PlState[y, x] = false ; PlState[y 1, x] = false ; break ; } PlState[y, x - 1] = true ; PlState[y 1, x - 1] = true ; PosX -= 1; return true ; } else return false ; case PStyles.P1: //口 case PStyles.P2H: //口口 if (x - 1 >= 0 && !PlState[y, x - 1]) { pl.Location = GetPosition()[y, x - 1]; if (PStyle == PStyles.P2H) //口口 { PlState[y, x 1] = false ; } else { PlState[y, x] = false ; } PlState[y, x - 1] = true ; PosX -= 1; return true ; } else return false ; } } #endregion #region 右移 else if (blRight) { switch (PStyle) { case PStyles.P4: //田 if (x 2 < ( int )plEnumerate.plX && !PlState[y, x 2] && !PlState[y 1, x 2]) { pl.Location = GetPosition()[y, x 1]; PlState[y, x] = false ; PlState[y 1, x] = false ; PlState[y, x 2] = true ; PlState[y 1, x 2] = true ; PosX = 1; return true ; } else return false ; case PStyles.P1: //口 if (x 1 < ( int )plEnumerate.plX && !PlState[y, x 1]) { pl.Location = GetPosition()[y, x 1]; PlState[y, x] = false ; PlState[y, x 1] = true ; PosX = 1; return true ; } else return false ; case PStyles.P2H: //口口 if (x 2 < ( int )plEnumerate.plX && !PlState[y, x 2]) { pl.Location = GetPosition()[y, x 1]; PlState[y, x] = false ; PlState[y, x 2] = true ; PosX = 1; return true ; } else return false ; case PStyles.P2V: //日 if (x 1 < ( int )plEnumerate.plX && !PlState[y, x 1] && !PlState[y 1, x 1]) { pl.Location = GetPosition()[y, x 1]; PlState[y, x] = false ; PlState[y 1, x] = false ; PlState[y, x 1] = true ; PlState[y 1, x 1] = true ; PosX = 1; return true ; } else return false ; } } #endregion return false ; } #endregion } } |
标签: 华容道
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论