using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace 贪吃蛇
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.WindowStyle = WindowStyle.None;
this.AllowsTransparency = true;
}
Random a = new Random();
Brush snakeColor;
DispatcherTimer time = new DispatcherTimer();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Maximized;
this.Background = Brushes.Transparent;
BG.Width = 800;
BG.Height = 600;
BG.Background = Brushes.SkyBlue;
BG.Opacity = 0.9;
CreateSnake();
time.Interval = TimeSpan.FromMilliseconds(300);
time.Tick += Time_Tick;
time.Start();
this.KeyDown += MainWindow_KeyDown;
CreateFood();
}
//常量蛇的初始长度
const int snakeLength = 4;
//常量蛇每节身体的大小
const int snakeSize = 20;
//动态数组不需要声明长度,数组内容根据填充的类型随意改变数量
#region 蛇1
/// <summary>
/// 动态数组,用来存储蛇
/// </summary>
List<Border> snake = new List<Border>();
/// <summary>
/// 用来存储位置
/// </summary>
List<Border> snakeLocation = new List<Border>();
#endregion
#region 蛇2
List<Border> snake02 = new List<Border>();
List<Border> snakeLocation02 = new List<Border>();
#endregion
#region 创建蛇
/// <summary>
/// 创建蛇的方法
/// </summary>
void CreateSnake()
{
for (int i = 0; i < snakeLength; i++)
{
//创造蛇1的border
Border br = new Border();
br.Width = br.Height = snakeSize;
br.CornerRadius = new CornerRadius(10);
Canvas.SetTop(br,100);
Canvas.SetLeft(br, 100 - i * snakeSize);
//创造蛇1的位置的border
Border location = new Border();
Canvas.SetTop(location, Canvas.GetTop(br));
Canvas.SetLeft(location, Canvas.GetLeft(br));
//创造蛇2的border
Border br02 = new Border();
br02.Width = br02.Height = snakeSize;
C#实现贪吃蛇(两条贪吃蛇)
最新推荐文章于 2021-05-25 02:55:28 发布
本文详细介绍了如何使用C#编程语言实现一个支持双人游玩的经典贪吃蛇游戏,涵盖了游戏逻辑、用户交互及双蛇独立移动的实现细节。
摘要由CSDN通过智能技术生成