回 帖 发 新 帖 刷新版面

主题:我用C#写的贪食蛇,没有复杂算法与结构,很基础的知识。

这个是花了我2个通宵写的,

代码有点多吧 写了900多行,完全是自己想的,除了图形重绘参考别人的例子。。

里面所有的东西,我完全没有看过别的代码自己写的。

是写的不好,不过这真的是很基础很基础。。。 


就那个所谓的,面向的对象的吧。。。


回复列表 (共30个回复)

11 楼

case 3:
                    
                    for (int i = 0, j = snakelist.Count - 1; i < snakelist.Count; i++, j--)  //移动递增
                    {
 
                        snake = (Snake)snakelist[j];          //填充由最后一个开始
                        Graphics graphicsdraw = this.CreateGraphics();
                        
                        if (Int32.Parse(i.ToString()) == 0)   //最后坐标j:蛇头 
                        {
                            if (Security(snake.X + MoveSize, snake.Y))
                            {
                                break;
                            }

                            graphicsdraw.FillRectangle(headcolor, snake.X + MoveSize, snake.Y, snake.Width, snake.Height);       
                            temp_x = snake.X;
                            temp_y = snake.Y;                       
                            templist.Add(snake.CreatePart(snake.X + MoveSize, snake.Y, snake.Width, snake.Height));
                            if (snake.X +MoveSize== foodx && snake.Y == foody)  //是否吃到食物
                            {
                                Setlevel();
                                isate = true;
                                Snake fitstsnake = ((Snake)snakelist[0]);
                                addsnake.Width = fitstsnake.Width;
                                addsnake.Height = fitstsnake.Height;
                                addsnake.X = fitstsnake.X;
                                addsnake.Y = fitstsnake.Y;
                            }
                        }
                       
                        else //否则后部分补上
                        {
                            graphicsdraw.FillRectangle(snakecolor, temp_x, temp_y, snake.Width, snake.Height);
                            templist.Add(snake.CreatePart(temp_x, temp_y, snake.Width, snake.Height));
                            temp_x = snake.X;
                            temp_y = snake.Y;
                        }
                        //填空最后蛇部分
                        Graphics graphicsclear = this.CreateGraphics();
                        Snake snake2 = (Snake)snakelist[j];
                        graphicsclear.FillRectangle(clearcolor, snake2.X, snake2.Y, snake2.Width, snake2.Height);

                    }

12 楼

if (stop == false)
                    {
                        for (int i = 0, j = snakelist.Count - 1; i < snakelist.Count; i++, j--) //推移坐标
                        {
                            Snake tsnake = (Snake)templist[i];
                            snake = (Snake)snakelist[j];
                            snake.X = tsnake.X;
                            snake.Y = tsnake.Y;
                        }
                    }
                        if (stop == false)
                        {
                            if (isate)                  //吃到食物重新排序
                            {
                                AddScore();
                                for (int i = 0, j = snakelist.Count - 1; i < snakelist.Count; i++, j--) //重新定位坐标-
                                {
                                    Snake tsnake = (Snake)templist[i];
                                    snake = (Snake)snakelist[j];
                                    snake.X = tsnake.X;
                                    snake.Y = tsnake.Y;
                                }
                                snakelist.Insert(0, addsnake);
                                templist.Insert(0, addsnake);
                                Newfood();
                            }
                            else
                            {
                                for (int i = 0, j = snakelist.Count - 1; i < snakelist.Count; i++, j--) //重新定位坐标-
                                {
                                    Snake tsnake = (Snake)templist[i];
                                    snake = (Snake)snakelist[j];
                                    snake.X = tsnake.X;
                                    snake.Y = tsnake.Y;
                                }
                            }
                        }
                        ISstop();
                    break;

13 楼

case 4:
                    
                    for (int i = 0, j = snakelist.Count - 1; i < snakelist.Count; i++, j--)  //移动递增
                    {
                        snake = (Snake)snakelist[j];         //填充由最后一个开始
                        Graphics graphicsdraw = this.CreateGraphics();
                       
                        if (Int32.Parse(i.ToString()) == 0)
                        {
                            if (Security(snake.X, snake.Y+MoveSize))
                            {
                                break;
                            }
                            graphicsdraw.FillRectangle(headcolor, snake.X, snake.Y + MoveSize, snake.Width, snake.Height);
   
                            temp_x = snake.X;
                            temp_y = snake.Y;
                                     
                            templist.Add(snake.CreatePart(snake.X, snake.Y + MoveSize, snake.Width, snake.Height));
                            if (snake.X == foodx && snake.Y + MoveSize == foody)  //是否吃到食物
                            {
                                Setlevel();
                                isate = true;
                                Snake fitstsnake = ((Snake)snakelist[0]);
                                addsnake.Width = fitstsnake.Width;
                                addsnake.Height = fitstsnake.Height;
                                addsnake.X = fitstsnake.X;
                                addsnake.Y = fitstsnake.Y;
                            }
                        }
                         
                        else
                        {
                            graphicsdraw.FillRectangle(snakecolor, temp_x, temp_y, snake.Width, snake.Height);
                            templist.Add(snake.CreatePart(temp_x, temp_y, snake.Width, snake.Height));
                            temp_x = snake.X;
                            temp_y = snake.Y;
                        }
                        Graphics graphicsclear = this.CreateGraphics();
                        Snake snake2 = (Snake)snakelist[j];
                        graphicsclear.FillRectangle(clearcolor, snake2.X, snake2.Y, snake2.Width, snake2.Height);

                    }

14 楼

if (stop == false)
                    {
                        if (isate)
                        {
                            AddScore();
                            for (int i = 0, j = snakelist.Count - 1; i < snakelist.Count; i++, j--) //重新定位坐标-
                            {
                                Snake tsnake = (Snake)templist[i];
                                snake = (Snake)snakelist[j];
                                snake.X = tsnake.X;
                                snake.Y = tsnake.Y;
                            }
                            snakelist.Insert(0, addsnake);
                            templist.Insert(0, addsnake);
                            Newfood();
                            
                        }
                        else
                        {
                            for (int i = 0, j = snakelist.Count - 1; i < snakelist.Count; i++, j--) //重新定位坐标-
                            {
                                Snake tsnake = (Snake)templist[i];
                                snake = (Snake)snakelist[j];
                                snake.X = tsnake.X;
                                snake.Y = tsnake.Y;
                            }
                        }
                    }
                    ISstop(); 
                    break;
            }

        }
        #endregion

15 楼

#region  创建食物
        public void Newfood()
        {
            try
            {
                    //420X 420Y  最大值
                Random random = new Random();
                foodx=0;
                foody=0;
                Snake tsnake;
                int x=0, y=0;
               
                if (snakelist.Count <= 3)       //当蛇的部分为3时
                {
                    while (foodx == 0)
                    {
                        int sum = 0;
                        x = Int32.Parse(foodlocation_X[Int32.Parse(random.Next(0, 15).ToString())].ToString());
                        y = Int32.Parse(foodlocation_Y[Int32.Parse(random.Next(0, 15).ToString())].ToString());
                        for (int i = 0; i < snakelist.Count; i++)
                        {
                            tsnake = ((Snake)snakelist[i]);
                            if (tsnake.X != x && tsnake.Y != y && x != 0 && x != 30 && x != 60)
                            {
                                sum += 1;
                            }
                        }
                        if (sum == snakelist.Count)
                        {
                            foodx = x;
                            foody = y;
                            break;
                        }
                    }
                }

16 楼

else
                {
                    while (foodx == 0)
                    {
                        int sum = 0;
                        x = Int32.Parse(foodlocation_X[Int32.Parse(random.Next(0, 15).ToString())].ToString());
                        y = Int32.Parse(foodlocation_Y[Int32.Parse(random.Next(0, 15).ToString())].ToString());

                        for (int i = 0; i < snakelist.Count; i++)
                        {
                            tsnake = ((Snake)snakelist[i]);
                            if (tsnake.X!=x || tsnake.Y!=y)   
                            {
                                sum += 1;
                            }
                        }
                        if (sum ==snakelist.Count)
                        {
                            foodx = x;
                            foody = y;
                        }
                    }
                }
                
                Graphics food = this.CreateGraphics();
                food.FillRectangle(foodBrush, foodx, foody, snake.Width, snake.Height);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

17 楼

}
        #endregion

        #region 障碍物检测
        public bool Security(int x, int y)
        {
            isagain = true;
            for (int i = 0; i < snakelist.Count; i++)
            {
                Snake tnake = ((Snake)snakelist[i]);
                if (x == tnake.X && y == tnake.Y)
                {
                    stop = true;
                    break;
                }else
                if(x>420)
                {
                 stop = true;
                    break;
                }else
                if (y>420)
                {
                    stop = true;
                    break;
                }else
                    if (x < 0)
                    {
                        stop = true;
                        break;
                    }else
                        if (y < 0)
                        {
                            stop = true;
                            break;
                        }
            }
            isagain=false;
            return stop;
        }
        #endregion

18 楼

#region 判断是否Over
        public void ISstop()
        {
            if (stop)
            {
                timer.Stop();
                timer.Tick -= new EventHandler(timer_Tick_Up);
                timer.Tick -= new EventHandler(timer_Tick_Right);
                timer.Tick -= new EventHandler(timer_Tick_Left);
                timer.Tick -= new EventHandler(timer_Tick_Down);
                isdie = true;
                isagain = false;
                DrawMessage("Game Over", this.CreateGraphics());
                Graphics graphicsclear = this.CreateGraphics();
                for (int j = 0; j < snakelist.Count; j++)
                {          
                    Snake snake2 = (Snake)snakelist[j];
                    if (j == snakelist.Count - 1)
                    {
                        graphicsclear.FillRectangle(headcolor, snake2.X, snake2.Y, snake2.Width, snake2.Height);
                    }
                    else
                    {
                        graphicsclear.FillRectangle(snakecolor, snake2.X, snake2.Y, snake2.Width, snake2.Height);
                    }
                }
            }
            else
            {
                stop = false;
            }
        }
        #endregion

19 楼

#region 新游戏
        public void NewGame()
        {
            try
            {
                timer.Interval = 350;
                this.num_lable.Text = "0";
                this.lnum_label.Text = "0";
                snakelist.Clear();
                again = 3;
                stop = false;
                isdie = false;
                isagain = false;
                paintfood = true;
                Graphics clearblack = this.CreateGraphics();
                clearblack.FillRectangle(clearcolor, 0, 0, 450, 450);

                Graphics part1 = this.CreateGraphics();
                part1.FillRectangle(snakecolor,0, 0, snake.Width, snake.Height);
                snakelist.Add(snake.CreatePart(0, 0, snake.Width, snake.Height));

                part1.FillRectangle(snakecolor,30, 0, snake.Width, snake.Height);
                snakelist.Add(snake.CreatePart(30, 0, snake.Width, snake.Height));

                part1.FillRectangle(headcolor, 60, 0, snake.Width, snake.Height);
                snakelist.Add(snake.CreatePart(60, 0, snake.Width, snake.Height));
                //x 和 y 坐标的构建
                for (int i = 0, j = 0; i <= 14; i++)
                {
                    foodlocation_X.Add(j.ToString());
                    j += 30;
                }
                for (int i = 0, y = 0; i <= 14; i++)
                {
                    foodlocation_Y.Add(y.ToString());
                    y += 30;
                }
                Newfood();
                MoveRight();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        #endregion

20 楼

#region 重绘事件
        protected override void OnPaint(PaintEventArgs e)
        {
            if (isdie)
            {
                DrawMessage("Game Over", e.Graphics);
            }
            else
            {
                if (IsStart)
                {
                    DrawMessage("Pause", e.Graphics);
                }
            }
            
            for (int j = 0; j < snakelist.Count; j++)
            {
                Snake snake2 = (Snake)snakelist[j];
                if (j == snakelist.Count - 1)
                {
                    e.Graphics.FillRectangle(headcolor, snake2.X, snake2.Y, snake2.Width, snake2.Height);
             
                }
                else
                {
                    e.Graphics.FillRectangle(snakecolor, snake2.X, snake2.Y, snake2.Width, snake2.Height);
                }
            }
            if (paintfood)
            {
                e.Graphics.FillRectangle(foodBrush, foodx, foody, snake.Width, snake.Height);
            }
            
           
        }
        #endregion

我来回复

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