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


相关推荐

  • http返回错误状态_状态码返回0

    http返回错误状态_状态码返回0状态码为405表示请求的方式不对,请求的方式有get、post、head、put……常用的为post和get。代码里面我刚刚开始的时候使用的是HttpPost发的请求,另外一边呢,刚刚开始的时候只有一个get请求在那等着呢。等我看到405之后,哦,类型,没对上,又在controller一边加了个post请求的这么一下就OK了。问题不大,简单记录一下。为的是下次…

    2022年10月24日
  • ORACLE EXP命令

    ORACLE EXP命令

    2021年12月9日
  • 霍夫曼树为何物

    霍夫曼树为何物

    2021年9月16日
  • Mysql 多表查询详解

    Mysql 多表查询详解纸上得来终觉浅1.前言:上篇讲到Mysql中关键字执行的顺序,只涉及了一张表;实际应用大部分情况下,查询语句都会涉及到多张表格:1)多表连接有哪些分类;2)针对这些分类有哪些连接方法?3)这些连接方法分别作用于哪些应用场景?这篇针对这三个点通过实例来讲述,目的是穷尽所有的场景和所有的方法,并且对每个方法的使用做实例。2.假设有两张表格A和B,把表格当作一个集合,那么表格中

    2022年5月31日
  • VS 2013安装教程「建议收藏」

    VS 2013安装教程「建议收藏」1.下载资源此版本是旗舰版,其他版本自行下载http://download.microsoft.com/download/9/3/E/93EA27FF-DB02-4822-8771-DCA0238957E9/vs2013.5_ult_chs.iso?type=ISO2.装载光盘3.右击应用程序,选择“以管理员身份运行”4.选择安装路径并同意许可,点击下一步。5.选择安装的功能(根据自己需要选择),点击安装6.等待安装即可7….

    2022年9月10日
  • Jenkins学习——Jenkins是什么

    Jenkins学习——Jenkins是什么Jenkins是什么     对于Jenkins是什么,百度百科给的答案是这样的:Jenkins是一个开源软件项目,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能。     通过这句话,我们可以得到这样的一个信息——Jenkins是一个开源的东东,好像跟持续集成开发有关系(然而我们并不知道持续集成开发是什么东东)。     通俗的说,就我目前的理解

    2022年5月5日

发表回复

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

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