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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • top命令 详解_top命令的用法

    top命令 详解_top命令的用法top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器。下面详细介绍它的使用方法。top是一个动态显示过程,即可以通过用户按键来不断刷新当前状态.如果在前台执行该命令,它将独占前台,直到用户终止该程序为止.比较准确的说,top命令提供了实时的对系统处理器的状态监视.它将显示系统中CPU最“敏感”的任务列表.该命令可以按CPU使用.内存使用和执行时间对任务进行排序;而且该命令的很多特性都可以通过交互式命令或者在个人定制文件中进行设定.1...

    2022年9月25日
    2
  • 敏捷过程模型有哪几种_能力模型的第四层能力

    敏捷过程模型有哪几种_能力模型的第四层能力目录 一、模型简介和思想 二、模型结构 第一部分encoder层 第二部分Convolution Layer卷积层 第三部分Co-Predictor Layer联合

    2025年7月11日
    2
  • 交换机基础

    交换机基础

    2022年2月2日
    81
  • Python计算机二级编程题真题及考点总结(上篇)「建议收藏」

    Python计算机二级编程题真题及考点总结(上篇)「建议收藏」【纯干货】2021全国计算机等级考试Python二级编程题真题详解(上)相较于大多数经验分享类文章,本篇重点在于总结Python二级考试中的编程题(占60分)题型范围及解题技巧,让Python小白能在一周内掌握绝大多数编程题的解题方法,顺利考取证书,为未来应聘岗位添砖加瓦!本文的最终的目的是得到一份能够涵盖编程题部分所有考点的知识点总结脑图!!!

    2022年10月12日
    4
  • oracle触发器类型

    http://www.cnblogs.com/roucheng/p/3506033.html触发器是许多关系数据库系统都提供的一项技术。在ORACLE系统里,触发器类似过程和函数,都有声明,执行和异

    2021年12月23日
    54
  • LAMP架构升级版–LNMMP

    LAMP架构升级版–LNMMP

    2021年9月2日
    53

发表回复

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

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