貪吃蛇大作戰單人版完整代碼以及詳細解釋
貪吃蛇總結:
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?
- using System;
- using System.Collections.Generic;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Threading;
- namespace 貪吃蛇1._0
- {
- /// <summary>
- /// MainWindow.xaml 的交互邏輯
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- this.Background = Brushes.Pink;
- }
- string str = "";
- Border bd;
- DispatcherTimer tm1;
- Border food;
- List<Border> snake = new List<Border>();
- //創建蛇的位置集合
- List<Border> snakeback = new List<Border>();
- List<Border> foodback = new List<Border>();
- Random ra = new Random();
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- 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);
- //創建蛇的位置border
- Border bdback = new Border();
- bdback.Height = bdback.Width = 10;
- Canvas.SetTop(bdback, 200 + i * bd.Height);
- Canvas.SetLeft(bdback, 200);
- snakeback.Add(bdback);
- }
- tm1 = new DispatcherTimer();
- tm1.Interval = TimeSpan.FromMilliseconds(200);
- tm1.Tick += Tm1_Tick;
- tm1.Start();
- this.KeyDown += MainWindow_KeyDown;
- for (int i = 0; i < 30; i++)
- {
- //創建食物
- food = new Border();
- food.Height = food.Width = 10;
- food.CornerRadius = new CornerRadius(5);
- Canvas.SetTop(food, ra.Next(60) * 10);
- Canvas.SetLeft(food, ra.Next(80) * 10);
- food.Background = Brushes.Orange;
- back.Children.Add(food);
- foodback.Add(food);
- }
- }
- private void MainWindow_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.Key == Key.Up && str != "d")
- {
- str = "up";
- }
- if (e.Key == Key.Down && str != "up")
- {
- str = "d";
- }
- if (e.Key == Key.Left && str != "r")
- {
- str = "l";
- }
- if (e.Key == Key.Right && str != "l")
- {
- str = "r";
- }
- }
- private void Tm1_Tick(object sender, EventArgs e)
- {
- 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();
- }
- for (int i = 0; i < 30; i++)
- {
- if (Canvas.GetTop(snake[0]) == Canvas.GetTop(foodback[i]) && Canvas.GetLeft(snake[0]) == Canvas.GetLeft(foodback[i]))
- {
- Canvas.SetTop(foodback[i], ra.Next(60) * 10);
- Canvas.SetLeft(foodback[i], ra.Next(80) * 10);
- //定義一個增加的蛇尾的border
- Border bdfood = new Border();
- bdfood.Height = bdfood.Width = 10;
- bdfood.CornerRadius = new CornerRadius(5);
- bdfood.Background = Brushes.Black;
- 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);
- }
- }
- 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!");
- }
- }
- 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]));
- }
- }
- }
- }
貪吃蛇大作戰單人版完整代碼以及詳細解釋