C#做贪吃蛇(含窗体)——可穿墙

设置贪吃蛇的步骤:

step1:设置贪吃蛇的界面,包含一个menustrip,timer,panel和两个label,如下图所示。

menustrip的属性中有item,设置为  如下图所示

         

 setp2:Form1窗体的代码为:

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 贪吃蛇
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static bool ifStart = false;//判断是否开始
        public static int career = 400;//移动的速度
        Snake snake = new Snake();//实例化Snake类
        int snake_W = 20;//骨节的宽度
        int snake_H = 20;//骨节的高度
        public static bool pause = false;//是否暂停游戏

        /// <summary>
        /// 绘制游戏场景
        /// </summary>
        /// <param g="Graphics">封装一个GDI+绘图图面</param>
        public void ProtractTable(Graphics g)
        {
            for (int i = 0; i <= panel1.Width / snake_W; i++)//绘制单元格的纵向线
            {
                g.DrawLine(new Pen(Color.Black, 1), new Point(i * snake_W, 0), new Point(i * snake_W, panel1.Height));
            }
            for (int i = 0; i <= panel1.Height / snake_H; i++)//绘制单元格的横向线
            {
                g.DrawLine(new Pen(Color.Black, 1), new Point(0, i * snake_H), new Point(panel1.Width, i * snake_H));
            }
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = panel1.CreateGraphics();//创建panel1控件的Graphics类
            ProtractTable(g);//绘制游戏场景
            if (!ifStart)//如是没有开始游戏
            {
                Snake.timer = timer1;
                Snake.label = label2;
                snake.Ophidian(panel1, snake_W);//初始化场地及贪吃蛇信息
            }
            else
            {
                for (int i = 0; i < Snake.List.Count; i++)//绘制蛇身
                {
                    e.Graphics.FillRectangle(Snake.SolidB, ((Point)Snake.List[i]).X + 1, ((Point)Snake.List[i]).Y + 1, snake_W - 1, snake_H - 1);
                }
                e.Graphics.FillRectangle(Snake.SolidF, Snake.Food.X + 1, Snake.Food.Y + 1, snake_W - 1, snake_H - 1);//绘制食物
                if (Snake.ifGame)//如果游戏结束
                    //绘制提示文本
                    e.Graphics.DrawString("Game Over", new Font("宋体", 30, FontStyle.Bold), new SolidBrush(Color.DarkSlateGray), new PointF(150, 130));
            }
        }

        private void 开始ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NoviceCortrol(Convert.ToInt32(((ToolStripMenuItem)sender).Tag.ToString()));
            snake.BuildFood();
            textBox1.Focus();
        }

        /// <summary>
        /// 控制游戏的开始、暂停和结束
        /// </summary>
        /// <param n="int">标识</param>
        public void NoviceCortrol(int n)
        {
            switch (n)
            {
                case 1://开始游戏
                    {
                        ifStart = false;
                        Graphics g = pane
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值