C#贪吃蛇游戏(全代码)

C#贪吃蛇游戏(全代码)C#贪吃蛇游戏Form方法100毫秒刷新秒刷新(蛇的移动速度由此决定)画蛇创建食物画食物吃掉食物生存还是毁灭游戏结束button点击事件链其他静态变量游戏主体类蛇食物这是本人第一篇博客,感谢收看,之后对游戏做出的修改,将以方法代码块放在最后Form方法100毫秒刷新privatevoidtimer1_Tick(objectsender,EventArgse){…

大家好,又见面了,我是你们的朋友全栈君。

这是本人第一篇博客,感谢收看,之后对游戏做出的修改,将以方法代码块放在最后


在这里插入图片描述


在这里插入图片描述


在这里插入图片描述

Form方法

100毫秒刷新

private void timer1_Tick(object sender, EventArgs e)
        { 
   
            DrawSnake();
            DrawFood();
        }

秒刷新(蛇的移动速度由此决定)

        private void timer2_Tick(object sender, EventArgs e)
        { 
   
            Snake.SnakeMove(Snake.wasd);
            EatFood();
            TobeOrNotTobe();
            label2.Text = score.ToString();
        }

画蛇

public void DrawSnake()
        { 
   
            RectangleF[] rectangles = new RectangleF[Snake.realsnake.Count];

            int num;
            for (num = 0; num < Snake.realsnake.Count; num++)
            { 
   
                rectangles[num] = new RectangleF(Snake.realsnake[num].Location_x,
                    Snake.realsnake[num].Location_y, Snake.width, Snake.height);
            }
            this.Refresh();
            Graphics g = this.CreateGraphics();
            SolidBrush myBrush = new SolidBrush(Color.Black);
            g.FillRectangles(myBrush, rectangles);
        }

创建食物

public void newfood()
        { 
   
            Food food = new Food();
            foodx = food.Location_x;
            foody = food.Location_y;
        }

画食物

public void DrawFood()
        { 
   

            Graphics f = this.CreateGraphics();
            SolidBrush mybrush = new SolidBrush(Color.Red);
            f.FillRectangle(mybrush, foodx, foody, 10, 10);

        }

吃掉食物

 public void EatFood()
        { 
   
            if (Snake.realsnake[Snake.realsnake.Count - 1].Location_x == foodx
                && Snake.realsnake[Snake.realsnake.Count - 1].Location_y == foody)
            { 
   
                newfood();
                Snake.EatLong();
                score += 1;
            }
        }

生存还是毁灭

public void TobeOrNotTobe()
        { 
   
            int x = Snake.realsnake[Snake.realsnake.Count - 1].Location_x;
            int y = Snake.realsnake[Snake.realsnake.Count - 1].Location_y;
            if (!(x <= 500 && x >= 0 && y <= 500 && y >= 0))
            { 
   
                GameOver();
                timer2.Enabled = false;
            }
        }

游戏结束

public void GameOver()
        { 
   
            label3.Visible = true;
            label4.Visible = true;
            label5.Text = score.ToString();
            label5.Visible = true;
            button1.Text = "重新开始";
            button1.Visible = true;
        }

button点击事件链

        private void button1_Click(object sender, EventArgs e)
        { 
   

            Snake.Snakestart();
            newfood();
            score = 0;
            timer1.Enabled = true;
            timer2.Enabled = true;
            button1.Visible = false;
            label3.Visible = false;
            label4.Visible = false;
            label5.Visible = false;
        }

其他静态变量

        static int score;
        static int foodx, foody;

游戏主体类

点击开始游戏,开始游玩。

using System.Collections.Generic;

namespace 贪吃蛇form
{ 
   
    public class Snake
    { 
   
        public int Location_x;//0为x,1为y
        public int Location_y;
        public static int width = 10, height = 10;
        public static List<Snake> realsnake = new List<Snake>();

        //当前方向
        public static int wasd;




        public Snake()
        { 
   
            Location_x = 250;
            Location_y = 250;

        }
        public static void Snakestart()
        { 
   
            wasd = 4;

            realsnake.Clear();
            Snake s0 = new Snake();
            Snake s1 = new Snake();
            Snake s2 = new Snake();
            Snake s3 = new Snake();
            realsnake.Add(s0);

            s1.Location_x = s0.Location_x - 10;
            realsnake.Add(s1);
            s2.Location_x = s1.Location_x - 10;
            realsnake.Add(s2);
            s3.Location_x = s2.Location_x - 10;
            realsnake.Add(s3);
            //添加四个长度,横向添加list 初始化小蛇蛇完成
        }

        public static void SnakeMove(int x)//8426
        { 
   
            int i = realsnake.Count - 1;

            if (x == 8)
            { 
   
                //对list蛇身进行处理
                Snakelist();
                realsnake[i].Location_y -= 10;
            }
            else if (x == 4)
            { 
   
                Snakelist();
                realsnake[i].Location_x -= 10;
            }
            else if (x == 2)
            { 
   
                Snakelist();
                realsnake[i].Location_y += 10;
            }
            else if (x == 6)
            { 
   
                Snakelist();
                realsnake[i].Location_x += 10;

            }
        }

        public static void Snakelist()
        { 
   
            int j = 0;
            int x, y;
            int i = realsnake.Count - 1;
            for (j = 0; j < i; j++)
            { 
   
                x = realsnake[j + 1].Location_x;
                y = realsnake[j + 1].Location_y;
                realsnake[j].Location_y = y;
                realsnake[j].Location_x = x;
            }
        }
        public static void EatLong()
        { 
   
            Snake S_eat = new Snake();
            int xx = realsnake[realsnake.Count - 1].Location_x;
            int yy = realsnake[realsnake.Count - 1].Location_y;
            if (wasd == 8)
            { 
   
                S_eat.Location_y = yy - 10;
                S_eat.Location_x = xx;
                realsnake.Add(S_eat);
            }
            else if (wasd == 4)
            { 
   
                S_eat.Location_x = xx - 10;
                S_eat.Location_y = yy;
                realsnake.Add(S_eat);
            }
            else if (wasd == 2)
            { 
   
                S_eat.Location_y = yy + 10;
                S_eat.Location_x = xx;
                realsnake.Add(S_eat);
            }
            else if (wasd == 6)
            { 
   
                S_eat.Location_y = yy;
                S_eat.Location_x = xx + 10;
                realsnake.Add(S_eat);
            }
        }
    }
}

食物

using System;

namespace 贪吃蛇form
{ 
   
    public class Food
    { 
   
        public int Location_x;
        public int Location_y;
        public Food()
        { 
   
            Random x = new Random();
            Random y = new Random();
            Location_x = x.Next(0, 49) * 10;
            Location_y = y.Next(0, 49) * 10;
        }
    }
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/143842.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 组合数学容斥原理答案_容斥原理例题精讲

    组合数学容斥原理答案_容斥原理例题精讲一.容斥原理就是人们为了不重复计算重叠部分,想出的一种不重复计算的方法。先来认识一下这两个符号:与(如图)蓝色的圈就是c1c2,红色的圈围起来的就是c1c2二.例题:组合数学1.题目1.1.题目描述八是个很有趣的数字啊。八=发,八八=爸爸,88=拜拜。当然最有趣的还是8用二进制表示是1000。怎么样,有趣吧。当然题目和这些都没有关系。某个人很无聊,他想找出[a,b…

    2025年7月5日
    0
  • 【树莓派】树莓派4B新手篇:安装官网Raspbian Buster系统及基础配置

    【树莓派】树莓派4B新手篇:安装官网Raspbian Buster系统及基础配置目录1、前言2、树莓派4B有什么特色?3、树莓派新手准备4、烧录RaspbianBuster系统到MicroSD卡开启SSH及配置无线连接5、启动安装树莓派系统启动树莓派6、树莓派的基本配置6.1系统配置6.2更改apt软件源与系统源6.3更改pip源6.4安装远程桌面6.5安装中文字体1、前言对于很多程序员而言,树莓派如今…

    2022年6月12日
    74
  • python3回文数

    python3回文数

    2021年4月16日
    133
  • java二维数组的创建,java二维数组创建方法

    java二维数组的创建,java二维数组创建方法java动态创建二维数组,从零学java笔录-第31篇图解二位数组在内存中存储,java二维数组动态赋值,java二维数组创建方法二维数组的定义typearrayName[][];type[][]arrayNameJava二维数组的声明、初始化和引用二维数组的声明、初始化和引用与一维数组相似,这里不再详……java定义二维数组的几种写法_计算机软件及应用_IT/计算…

    2022年6月10日
    44
  • Google Borg论文[通俗易懂]

    Google Borg论文[通俗易懂]Borg的论文逐字翻译,拒绝机器翻译,有一些自己的理解,不一定对,作为参考就行

    2022年10月22日
    0
  • protel 99se 负片打印「建议收藏」

    protel 99se 负片打印「建议收藏」protel99se负片打印首先得有一个做好的pcb做好的pcb做好的pcb   打开做好的PCB1.      点击下面的mechanical1层再点击place->fill将板子都盖住2.      点击file->new 选择PCBPrinter3.      选中左边的multilayercompositeprint 点击edit-

    2022年5月29日
    62

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号