简单贪吃蛇小游戏

制作一个简单的贪吃蛇游戏

1.首先做出一条蛇出来,在随机生成一个果子(可以用button)
2.让蛇动起来(可以做一个定时器 改变按钮的位置)
3.按下相应的按钮让蛇能够相应的动起来 (KeyDown事件)
4.蛇与果子相撞时–(蛇身体增长,果子消失在随机产生一个)
5.结束游戏
具体实现代码
先创建一个蛇的类
class Snake
{
//构造方法
public Snake(int maxLength, Point point)
{
this.maxLength = maxLength;
this.Body = new List();
this.Direction = 1;
//默认一个头,三个身体
Button head = new Button();
head.BackColor = Color.Black;
head.Location = point;
head.Size = new System.Drawing.Size(20, 20);
head.Enabled = false;//键盘事件侦听到这个按钮
Body.Add(head);
for (int i = 0; i < 3; i++)
{
Button body = new Button();
body.BackColor =Color.Gray;
body.Location = new Point(head.Location.X,head.Location.Y+(i+1)*20);
body.Size = new System.Drawing.Size(20, 20);
body.Enabled = false;//键盘事件侦听到这个按钮
Body.Add(body);
}
}
private int maxLength;
//蛇的最大长度
public int MaxLength
{ get => maxLength;
set {
if (value>50)
{
maxLength = 50;
}
else
{
maxLength = value;
}
}
}
public List Body { get; set; }
//方向 1上 2右 3下 4左
public int Direction { get; set; }
public void GrowUp()
{
if (this.Body.Count>this.maxLength)
{
return;
}
Button body = new Button();
body.Size = new Size(20,20);
body.Location = this.Body[0].Location;
body.BackColor = Color.Gray;
body.Enabled = false;
Body.Add(body);
}
}

界面代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsSnake
{
public partial class Form1 : Form
{
//声明分数
int score;
Snake snake;
Button fruit;
Random r = new Random();
public Form1()
{
InitializeComponent();
}

    private void Form1_Load(object sender, EventArgs e)
    {
        //缓存
        this.DoubleBuffered = true;
        //初始化蛇的位置
        snake = new Snake(50,new Point(100,100));
        //初始化水果
        InitFruit();
        DrawGame();
    }
    /// <summary>
    /// 绘制蛇和水果
    /// </summary>
    public void DrawGame()
    {
        this.Controls.Clear();
        this.Controls.AddRange(snake.Body.ToArray());
        this.Controls.Add(fruit);

    }

    /// <summary>
    /// 初始化水果
    /// </summary>
    public void InitFruit()
    {
        int randomX = r.Next(0,this.ClientSize.Width/20)*20;
        int randomY = r.Next(0, this.ClientSize.Height / 20) * 20;
        //初始化水果
        fruit = new Button();
        fruit.Size = new Size(20, 20); ;
        fruit.Location = new Point(randomX,randomY);//随机位置
        fruit.BackColor = Color.Red;
        fruit.Enabled = false;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        //蛇动起来
        UpdateGame();
        //绘制界面
        DrawGame();
        //结束游戏
        GameEnd();
    }
    public void UpdateGame()
    {
        //身体变化
        for (int i = snake.Body.Count-1; i > 0; i--)
        {
            //后面一个身体跟着前面一个身体走
            int bodyX = snake.Body[i].Location.X;
            int bodyY = snake.Body[i].Location.Y;
            bodyX = snake.Body[i - 1].Location.X;
            bodyY = snake.Body[i - 1].Location.Y;
            snake.Body[i].Location = new Point(bodyX,bodyY);
        }
        //蛇头变化
        int headX = snake.Body[0].Location.X;
        int headY = snake.Body[0].Location.Y;
        switch (snake.Direction)
        {
            case 1://上
                headY -= 20;
                break;
            case 2://右
                headX += 20;
                break;
            case 3://下.
                headY += 20;
                break;
            case 4://左
                headX -= 20;
                break;
        }
        snake.Body[0].Location = new Point(headX, headY);
        //吃水果
        if (snake.Body[0].Location == fruit.Location)//蛇头和水果位置一致
        {
            //累积得分
            score += 10;
            //果子被吃,在随机出现
            int randomX = r.Next(0, this.ClientSize.Width / 20) * 20;
            int randomY = r.Next(0, this.ClientSize.Height / 20) * 20;
            fruit.Location = new Point(randomX, randomY);
            //蛇的身体增加
            snake.GrowUp();
        }
        else
        {
            if (HitSelf())
            {
                this.timer1.Stop();
                MessageBox.Show("游戏结束,得分:" + score, "系统提示");
            }
        }
            


    }
    /// <summary>
    /// 键盘控制
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        switch (e.KeyCode)
        {
            case Keys.Up:
                if (snake.Direction!=3)
                {
                    snake.Direction = 1;
                }
                break;
            case Keys.Right:
                if (snake.Direction != 4)
                {
                    snake.Direction = 2;
                }
                break;
            case Keys.Down:
                if (snake.Direction != 1)
                {
                    snake.Direction = 3;
                }
                break;
            case Keys.Left:
                if (snake.Direction != 2)
                {
                    snake.Direction = 4;
                }
                break;
        }
    }
    public void GameEnd()
    {
        //蛇撞墙
        if (snake.Body[0].Location.X<20||snake.Body[0].Location.X>500|| snake.Body[0].Location.Y < 20 || snake.Body[0].Location.Y > 500)
        {
            this.timer1.Stop();
            MessageBox.Show("游戏结束,得分:"+score, "系统提示");
        }
    }
   public bool HitSelf()
    {
        //获取头部位置
        int x = snake.Body[0].Location.X;
        int y = snake.Body[0].Location.Y;
        for (int i = 3; i < this.snake.Body.Count; i++)
        {
            if ((x == this.snake.Body[i].Location.X) && (y == this.snake.Body[i].Location.Y))
            {
                return true;
            }
        }
        return false;
    }
}

}

猜你喜欢

转载自blog.csdn.net/m0_51422301/article/details/114273054