回 帖 发 新 帖 刷新版面

主题:[原创]c#贪吃蛇原代码

学习了2个月的C#从连创建都不会,一步步走来.
前2天头脑发热,(因为找工作总是碰壁,一生气,回来自己编了贪吃蛇的游戏.界面设计的非常难看,不过还可以入眼吧.)开发时间总共用了一天时间,内部的一些算法是非常的不好,在这里发表下,大家顺便给提下意见,我看书很少,就看了一本<<C#从入门到精通>>,其他的一些东西都是上网下载别人的程序学习.工作都还没有呢.下面是原代码.可能太长了,简短下,只发我认为比较重要的地方了.其他的都在附件里面
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace 贪吃蛇
{
    public partial class Form1 : Form
    {
        PictureBox[,] picbox = new PictureBox[15, 15];//这个是画游戏界面的
        Point[] abc = new Point[3];//这个是负责与蛇的SS数组进行交互使用的,因为他们要相互复制
        snack a= new snack(5, new Point [3], false);//定义一条蛇
        bool havefood=false;//初始化数据为假
        int moveroad=39;//初始化移动方向
        int speed = 0;
        public Form1()
        {
            InitializeComponent();
            iniyialsizemap();//画游戏界面
            iniyialsizesnack();//画蛇专用
        }
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            this.moveroad = e.KeyValue;
        }
        private void iniyialsizemap()
        { 
            int x, y;//定义一个X,Y
            Random ran = new Random();//定义一个随机数的对象,它能帮助我们随机的在游戏界面上设置食物
            int b;//用来接收随机数的
            for (y = 0; y < 15; y++)
            {
                    for (x = 0; x < 15; x++)
                    {
                        picbox[x, y] = new PictureBox();//在picbox的位置声名一个PIURECBOX对象
                        picbox[x, y].Location = new Point(x * 30, y * 30);//这个是设置它的位置默认情况应该是picbox[x, y].Location = new Point(x * 20+groupbox1.location.x, y * 20+groupbox1.location.y);我是把GROUPBOX1的位置坐标改为了0,0所以没加
                        picbox[x, y].Size = new Size(30, 30);//大小
                        picbox[x, y].BackColor = Color.Blue;//默认空地的颜色是BLUE(蓝色)
                        picbox[x, y].Visible = true;//让它可以显示
                        b = ran.Next(0, 5);//接收随机数这里是获取1-100之间的随机数
                        picbox[x, y].SizeMode = PictureBoxSizeMode.StretchImage;//显示图片的格式,我这里为按照控件的大小填充近来
                        if (b == 1)
                        {
                            picbox[x, y].Image = global ::贪吃蛇.Properties.Resources.YX_B_26;//设置该点的图画即食物
                        }
                        groupBox1.Controls.Add(picbox[x, y]);//将这个控件添加进GROUPBOX1里面来
                    }
            }
        }
        private void iniyialsizesnack()//初始化蛇的函数
        {
            abc[0].X = 0;
            abc[0].Y = 0;
            abc[1].X = 1;
            abc[1].Y = 0;
            abc[2].X = 2;
            abc[2].Y = 0;
            this.picboxnack();//这里蛇的长度为3,且位置很明确的标明为左上角的并排3个位置
        }
        //下面这个第一按钮函数无效,当时想用这个函数来重新开始的,后来没实现。就没管它了,反正关了再开,就是新局了
        private void button1_Click(object sender, EventArgs e)
        {//不好意思啊。这个函数被我做出来了^-^
            timer1.Enabled = false;
            MessageBox.Show("您要重新开始不,让你别乱点,非得乱点,晚了吧,重新开始吧", "别乱点", MessageBoxButtons.OK);
            this.qingchusuoyou();
            speed = Convert.ToInt16(comboBox1.Text.Trim());
            if (speed >= 10)
                speed = 10;
            this.textBox1.Focus();
            timer1.Interval = 501 - 50 * speed;
            timer1.Enabled = true;
            this.button2.Enabled = true;
            this.button3.Enabled = true;
            this.button4.Enabled = true;
            this.button5.Enabled = true;
        }
        private void picboxnack()//画蛇的函数
        {
            for (int i = abc.Length - 1; i >= 0; i--)
            {
                int x = abc[i].X;
                int y = abc[i].Y;
                if (picbox[x, y].Image != null)
                {
                    picbox[x, y].Image = null;
                    picbox[x, y].BackColor = Color.Red;//这句也许你会有疑问,其实这句如果不加,那么蛇吃了食物的时候,蛇头就消失了,不过再按下酒出来了,加上这句就不会出现那种情况了

                }
                if (i == abc.Length - 1)
                {
                    picbox[x, y].BackColor = Color.Red;//标记蛇头的颜色是红色
                }
                else
                {
                    picbox[x, y].BackColor = Color.Black;//蛇身的颜色是黑色,因为俺喜欢毒蛇^_^
                }
            }
            
        }
        private bool have(int i)//确定蛇头的移动方向是否有食物
        {
            if (i == 1)//蛇头的右方是否有食物
            {
                int x = abc[abc.Length - 1].X + 1;
                int y = abc[abc.Length - 1].Y;
                if (picbox[x, y].Image != null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else if (i == 2)//蛇头的左方是否有食物
            {
                int x = abc[abc.Length - 1].X - 1;
                int y = abc[abc.Length - 1].Y;
                if (picbox[x, y].Image != null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else if (i == 4)//蛇头的上方是否有食物
            {
                int x = abc[abc.Length - 1].X ;
                int y = abc[abc.Length - 1].Y-1;
                if (picbox[x, y].Image != null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else//蛇头的下方是否有食物
            {
                int x = abc[abc.Length - 1].X;
                int y = abc[abc.Length - 1].Y +1;
                if (picbox[x, y].Image != null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        private bool dead(int i)//这个是判断是否死亡的
        {
            int x, y;
            if (i == 1)//右移动是否死
            {
                 x = abc[abc.Length - 1].X + 1;
                 y = abc[abc.Length - 1].Y;
                 if (x >= 15)//首先右移动,只有可能是蛇头超过右边界。
                 {
                     return true;
                 }
                 else
                 {
                     if (picbox[x, y].BackColor == Color.Black)//在没有超过的前提下,再判断,它的右方是否是蛇身,自己吃自己总是要流血的。
                     { return true; }
                     return false;
                 }
            }
                //下面的同上
            else if (i == 2)//左移动是否死
            {
                 x = abc[abc.Length - 1].X-1;
                 y = abc[abc.Length - 1].Y;
                 if (x < 0)
                 {
                     return true;
                 }
                 else
                 {
                     if (picbox[x, y].BackColor == Color.Black)
                     {
                         return true;
                     }
                     return false;
                 }
            }
            else if (i == 4)//上移动是否死
            {
                 x = abc[abc.Length - 1].X;
                 y = abc[abc.Length - 1].Y - 1;
                 if (y < 0)
                 {
                     return true;
                 }
                 else
                 {
                     if (picbox[x, y].BackColor == Color.Black)
                     {
                         return true;
                     }
                     return false;
                 }
            }
            else//下移动是否死
            {
                 x = abc[abc.Length - 1].X;
                 y = abc[abc.Length - 1].Y + 1;
                 if (y >= 15)
                 {
                     return true;
                 }
                 else
                 {
                     if (picbox[x, y].BackColor == Color.Black)
                     {
                         return true;
                     }
                     return false;
                 }
            }
        }
        private bool iswin()
        {
            int i, j;
            int cum = 0;
            for (i = 0; i < 15; i++)
            {
                for (j = 0; j < 15; j++)
                {
                    if (picbox[i, j].Image != null)
                    {
                        cum++;
                    }
                }
            }
            if (cum == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        private void qingchusuoyou()
        {
            for (int x = 0; x < 15; x++)
            {
                for (int y = 0; y < 15; y++)
                {
                    this.groupBox1.Controls.Remove(picbox[x, y]);
                }
            }
            this.picbox = new PictureBox[15, 15];
            this.abc = new Point[3];
            this.a = new snack(5, new Point[3], false);
            this.moveroad = 39;
            this.havefood = false;
            this.iniyialsizemap();
            this.iniyialsizesnack();
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            int ab = e.KeyValue;
           // label3.Text = ab.ToString();
            this.moveroad = ab;
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.moveroad == 37)
            {
                this.left();
            }
            else if (this.moveroad == 38)
            {
                this.up();
            }
            else if (this.moveroad == 39)
            {
                this.right();
            }
            else
            { this.down(); }
        }
    }
}

回复列表 (共12个回复)

沙发

这几天又想做个扫雷游戏,等做完了继续发上来,让大家帮点评下.

板凳

支持原创分享

3 楼


看看

4 楼

楼主 我建议你打包一下放在迅雷或是其它的网站下
然后把下载的地址下就行了.
这样更全

5 楼

其实也没什么,你们如果需要就加我QQ,给你们传253105052

6 楼


tks

7 楼


上传的附件在哪啊?

8 楼

上传的附件在哪啊?

9 楼

仁兄,不要气馁,是金子总会发光的。
Good Luck~~~

10 楼

顶一下

我来回复

您尚未登录,请登录后再回复。点此登录或注册