1. 程式人生 > >貪吃蛇大作戰單人版完整代碼以及詳細解釋

貪吃蛇大作戰單人版完整代碼以及詳細解釋

route 一個 next 與此同時 con cti black cor 判斷

貪吃蛇總結:

1. 創建WPF應用程序

2. 添加Canvas標簽。Name=back;

3. 添加loaded事件

4. 在loaded事件中創建蛇(設置蛇的位置,大小,顏色)

this.Left = 0;

this.Top = 0;

this.Width = 800;

this.Height = 600;

for (int i = 0; i < 5; i++)

{

bd = new Border();

bd.Width = bd.Height = 10;

bd.CornerRadius = new CornerRadius(5);

if (i == 0)

bd.Background = Brushes.Red;

else

bd.Background = Brushes.Black;

Canvas.SetTop(bd,200+i*bd.Height);

Canvas.SetLeft(bd,200);

back.Children.Add(bd);

snake.Add(bd);

5. 添加一個timer,(問題:添加timer和winform程序中不一樣

Winform 程序中直接添加。

WPF應用程序中不能直接添加,需要引入命名空間:

Using System.Thearing

DispatcherTimer tm1 = new DispatcherTimer();

tm1.Interval =TimeSpan.FromMilliseconds(200);

tm1.Tick += Tm1_Tick;

tm1.Start();

this.KeyDown += MainWindow_KeyDown;

6. 添加鍵盤事件,讀取鍵盤敲下的字符

if (e.Key==Key.Up&&str!="d" )

{

str = "up";

}

if (e.Key==Key.Down&&str!="up")

{

str = "d";

[csharp] view plain copy print?

{

str = "l";

}

if (e.Key == Key.Right&&str!="l")

{

str = "r";

}

7.在timer事件中實現蛇的移動(問題:在實現向左移動時出現錯誤

解決:添加一個集合,來存儲蛇的位置信息,與此同時添加一個border儲存位置信息border)

//創建蛇的位置集合

List<Border> snakeback =new List<Border>();

//創建蛇的位置border

Border bdback = new Border();

bdback.Height = bdback.Width = 10;

Canvas.SetTop(bdback, 200 + i * bd.Height);

Canvas.SetLeft(bdback, 200);

snakeback.Add(bdback);

if (str == "up")

{

Canvas.SetTop(snake[0],Canvas.GetTop(snake[0]) - snake[0].Height);

run();

}

if (str=="d")

{

Canvas.SetTop(snake[0],Canvas.GetTop(snake[0])+ snake[0].Height);

run();

}

if (str == "l")

{

Canvas.SetLeft(snake[0],Canvas.GetLeft(snake[0]) - snake[0].Height);

run();

}

if (str == "r")

{

Canvas.SetLeft(snake[0],Canvas.GetLeft(snake[0]) + snake[0].Height);

run();

}

private void run()

{

for (int i = 1; i < snake.Count; i++)

{

Canvas.SetTop(snake[i],Canvas.GetTop(snakeback[i - 1]));

Canvas.SetLeft(snake[i],Canvas.GetLeft(snakeback[i - 1]));

}

for (int i = 0; i < snake.Count; i++)

{

Canvas.SetTop(snakeback[i],Canvas.GetTop(snake[i]));

Canvas.SetLeft(snakeback[i],Canvas.GetLeft(snake[i]));

}

}

8. 判斷與食物相撞時,食物改變位置

if (Canvas.GetTop(snake[0])==Canvas.GetTop(food)&&Canvas.GetLeft(snake[0])==Canvas.GetLeft(food))

{

Canvas.SetTop(food, ra.Next(60) * 10);

Canvas.SetLeft(food, ra.Next(80) * 10);

}

9. 在與食物相撞時添加一節蛇尾

10. 添加一個border來作為蛇尾,添加到snake集合中

11. 添加一個border存儲所增加的蛇尾的位置信息

if (Canvas.GetTop(snake[0])==Canvas.GetTop(food)&&Canvas.GetLeft(snake[0])==Canvas.GetLeft(food))

{

Canvas.SetTop(food, ra.Next(60) * 10);

Canvas.SetLeft(food, ra.Next(80) * 10);

//定義一個增加的蛇尾的border

Border bdfood = new Border();

bdfood.Height = bdfood.Width = 10;

bdfood.CornerRadius = new CornerRadius(5);

bdfood.Background = Brushes.Azure;

Canvas.SetTop(bdfood,Canvas.GetTop(snake[snake.Count-1]));

Canvas.SetLeft(bdfood,Canvas.GetLeft(snake[snake.Count - 1]));

back.Children.Add(bdfood);

snake.Add(bdfood);

//定義一個border存儲增加的的集合

Border br = new Border();

br.Height = br.Width = 10;

Canvas.SetTop(br, Canvas.GetTop(snakeback[snakeback.Count - 1]));

Canvas.SetLeft(br, Canvas.GetLeft(snakeback[snakeback.Count - 1]));

snakeback.Add(br);

}

12.判斷蛇與面板四周相撞或者蛇撞到自己,遊戲結束。

for (int i = 1; i < snake.Count; i++)

{

if (Canvas.GetTop(snake[0]) ==Canvas.GetTop(snake[i]) && Canvas.GetLeft(snake[0]) == Canvas.GetLeft(snake[i]))

{

tm1.Stop();

MessageBox.Show("GAME OVER!");

}

}

if (Canvas.GetTop(snake[0]) == 0 ||Canvas.GetTop(snake[0]) == 560 || Canvas.GetLeft(snake[0]) == 0 || Canvas.GetLeft(snake[0]) == 790)

{

tm1.Stop();

MessageBox.Show("GAME OVER!");

}

完整版代碼:

[csharp] view plain copy print?
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Input;
  6. using System.Windows.Media;
  7. using System.Windows.Threading;
  8. namespace 貪吃蛇1._0
  9. {
  10. /// <summary>
  11. /// MainWindow.xaml 的交互邏輯
  12. /// </summary>
  13. public partial class MainWindow : Window
  14. {
  15. public MainWindow()
  16. {
  17. InitializeComponent();
  18. this.Background = Brushes.Pink;
  19. }
  20. string str = "";
  21. Border bd;
  22. DispatcherTimer tm1;
  23. Border food;
  24. List<Border> snake = new List<Border>();
  25. //創建蛇的位置集合
  26. List<Border> snakeback = new List<Border>();
  27. List<Border> foodback = new List<Border>();
  28. Random ra = new Random();
  29. private void Window_Loaded(object sender, RoutedEventArgs e)
  30. {
  31. this.Left = 0;
  32. this.Top = 0;
  33. this.Width = 800;
  34. this.Height = 600;
  35. for (int i = 0; i < 5; i++)
  36. {
  37. bd = new Border();
  38. bd.Width = bd.Height = 10;
  39. bd.CornerRadius = new CornerRadius(5);
  40. if (i == 0)
  41. bd.Background = Brushes.Red;
  42. else
  43. bd.Background = Brushes.Black;
  44. Canvas.SetTop(bd, 200 + i * bd.Height);
  45. Canvas.SetLeft(bd, 200);
  46. back.Children.Add(bd);
  47. snake.Add(bd);
  48. //創建蛇的位置border
  49. Border bdback = new Border();
  50. bdback.Height = bdback.Width = 10;
  51. Canvas.SetTop(bdback, 200 + i * bd.Height);
  52. Canvas.SetLeft(bdback, 200);
  53. snakeback.Add(bdback);
  54. }
  55. tm1 = new DispatcherTimer();
  56. tm1.Interval = TimeSpan.FromMilliseconds(200);
  57. tm1.Tick += Tm1_Tick;
  58. tm1.Start();
  59. this.KeyDown += MainWindow_KeyDown;
  60. for (int i = 0; i < 30; i++)
  61. {
  62. //創建食物
  63. food = new Border();
  64. food.Height = food.Width = 10;
  65. food.CornerRadius = new CornerRadius(5);
  66. Canvas.SetTop(food, ra.Next(60) * 10);
  67. Canvas.SetLeft(food, ra.Next(80) * 10);
  68. food.Background = Brushes.Orange;
  69. back.Children.Add(food);
  70. foodback.Add(food);
  71. }
  72. }
  73. private void MainWindow_KeyDown(object sender, KeyEventArgs e)
  74. {
  75. if (e.Key == Key.Up && str != "d")
  76. {
  77. str = "up";
  78. }
  79. if (e.Key == Key.Down && str != "up")
  80. {
  81. str = "d";
  82. }
  83. if (e.Key == Key.Left && str != "r")
  84. {
  85. str = "l";
  86. }
  87. if (e.Key == Key.Right && str != "l")
  88. {
  89. str = "r";
  90. }
  91. }
  92. private void Tm1_Tick(object sender, EventArgs e)
  93. {
  94. if (str == "up")
  95. {
  96. Canvas.SetTop(snake[0], Canvas.GetTop(snake[0]) - snake[0].Height);
  97. run();
  98. }
  99. if (str == "d")
  100. {
  101. Canvas.SetTop(snake[0], Canvas.GetTop(snake[0]) + snake[0].Height);
  102. run();
  103. }
  104. if (str == "l")
  105. {
  106. Canvas.SetLeft(snake[0], Canvas.GetLeft(snake[0]) - snake[0].Height);
  107. run();
  108. }
  109. if (str == "r")
  110. {
  111. Canvas.SetLeft(snake[0], Canvas.GetLeft(snake[0]) + snake[0].Height);
  112. run();
  113. }
  114. for (int i = 0; i < 30; i++)
  115. {
  116. if (Canvas.GetTop(snake[0]) == Canvas.GetTop(foodback[i]) && Canvas.GetLeft(snake[0]) == Canvas.GetLeft(foodback[i]))
  117. {
  118. Canvas.SetTop(foodback[i], ra.Next(60) * 10);
  119. Canvas.SetLeft(foodback[i], ra.Next(80) * 10);
  120. //定義一個增加的蛇尾的border
  121. Border bdfood = new Border();
  122. bdfood.Height = bdfood.Width = 10;
  123. bdfood.CornerRadius = new CornerRadius(5);
  124. bdfood.Background = Brushes.Black;
  125. Canvas.SetTop(bdfood, Canvas.GetTop(snake[snake.Count - 1]));
  126. Canvas.SetLeft(bdfood, Canvas.GetLeft(snake[snake.Count - 1]));
  127. back.Children.Add(bdfood);
  128. snake.Add(bdfood);
  129. //定義一個border存儲增加的的集合
  130. Border br = new Border();
  131. br.Height = br.Width = 10;
  132. Canvas.SetTop(br, Canvas.GetTop(snakeback[snakeback.Count - 1]));
  133. Canvas.SetLeft(br, Canvas.GetLeft(snakeback[snakeback.Count - 1]));
  134. snakeback.Add(br);
  135. }
  136. }
  137. for (int i = 1; i < snake.Count; i++)
  138. {
  139. if (Canvas.GetTop(snake[0]) == Canvas.GetTop(snake[i]) && Canvas.GetLeft(snake[0]) == Canvas.GetLeft(snake[i]))
  140. {
  141. tm1.Stop();
  142. MessageBox.Show("GAME OVER!");
  143. }
  144. }
  145. if (Canvas.GetTop(snake[0]) == 0 || Canvas.GetTop(snake[0]) == 560 || Canvas.GetLeft(snake[0]) == 0 || Canvas.GetLeft(snake[0]) == 790)
  146. {
  147. tm1.Stop();
  148. MessageBox.Show("GAME OVER!");
  149. }
  150. }
  151. private void run()
  152. {
  153. for (int i = 1; i < snake.Count; i++)
  154. {
  155. Canvas.SetTop(snake[i], Canvas.GetTop(snakeback[i - 1]));
  156. Canvas.SetLeft(snake[i], Canvas.GetLeft(snakeback[i - 1]));
  157. }
  158. for (int i = 0; i < snake.Count; i++)
  159. {
  160. Canvas.SetTop(snakeback[i], Canvas.GetTop(snake[i]));
  161. Canvas.SetLeft(snakeback[i], Canvas.GetLeft(snake[i]));
  162. }
  163. }
  164. }
  165. }

貪吃蛇大作戰單人版完整代碼以及詳細解釋