C#实现贪吃蛇(两条贪吃蛇)

本文详细介绍了如何使用C#编程语言实现一个支持双人游玩的经典贪吃蛇游戏,涵盖了游戏逻辑、用户交互及双蛇独立移动的实现细节。
摘要由CSDN通过智能技术生成
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#贪吃蛇精简 public void 创建蛇身() { //创建一个Label标签 Label lbl = new Label(); //设置背景颜色,如果是蛇头就添加不同的颜色 if (arr.Count == 0) {//蛇头 lbl.BackColor = Color.Blue; } else {//蛇身 lbl.BackColor = Color.Red; } //设置宽和高 lbl.Width = 19; lbl.Height = 19; //设置起始位置 lbl.Left = -20; //添加到Panel中 this.panel1.Controls.Add(lbl); //将这一节身体保存在ArrayList中,以方便将来对蛇的身体进行移动操作 arr.Add(lbl); } //定时器 private void timer1_Tick(object sender, EventArgs e) { //先移动身体,从尾巴开始移动 for (int i = arr.Count - 1; i > 0; i--) { //得到第i节身体 Label 身体i = (Label)arr[i]; //得到第i节身体的前一节身体 Label 前一节 = (Label)arr[i - 1]; //移动第i节身体 身体i.Left = 前一节.Left; 身体i.Top = 前一节.Top; } //得到蛇头 Label 蛇头 = (Label)arr[0]; //根据方向移动蛇头 if (fx == "上") 蛇头.Top -= 20; else if (fx == "下") 蛇头.Top += 20; else if (fx == "左") 蛇头.Left -= 20; else if (fx == "右") 蛇头.Left += 20; //判断撞墙了没有 if (蛇头.Left < 0 || 蛇头.Top < 0 || 蛇头.Left >= this.panel1.Width || 蛇头.Top >= this.panel1.Height) { this.timer1.Stop(); MessageBox.Show("你撞墙了。"); 重新开始游戏(); return; } //判断有没有撞自己 for (int i = 1; i < arr.Count; i++) { //取出第i节蛇身 Label 蛇身 = (Label)arr[i]; //判断有没有相撞 if (蛇头.Left == 蛇身.Left && 蛇头.Top == 蛇身.Top) { this.timer1.Stop(); MessageBox.Show("你撞到自己了。"); 重新开始游戏(); return; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值