感兴趣的,可以下载源码运行https://download.csdn.net/download/qq285679784/8706547,也可以找我免费要源码qq285679784
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SnakeGame
{
public partial class frmSnake : Form
{
public Point FoodLct = new Point();//缓存食物的坐标
public int snakeLen = 6;
public const int SnakeMaxLength = 500;
public Point[] snakeArr = new Point[SnakeMaxLength];
public int snakeDrt = 2;
public bool panDuan = true;
public frmSnake()
{
InitializeComponent();
int temp = 0;
for (int i = snakeLen - 1; i >= 0; i--)
{
snakeArr[i].X = temp; snakeArr[i].Y = 0;
temp += 15;
}
}
/*
前言
说到贪吃蛇,大家可能小时候都玩过,小菜最近在整理Winfrom的学习系列,那我觉得有兴趣才会有学习,就从这个小游戏讲起吧。
其实我刚开始学习编程的时候,感觉写个贪吃蛇的程序会很难,因为涉及到画图什么的,其实现在来看,实现很简单。
实现贪吃蛇首先有几个元素:
蛇
食物
然后有几个行为:
吃食物和吃不到食物
撞墙和撞自己
说到这有点oo的意思啊,这篇就不啰嗦,只是简单实现,下篇会优化下。
其实整个贪吃蛇的难点就在于画图,可能用其他语言实现有点复杂,但是强大的.net提供了GDI+绘图机制,实现起来就很方便了,其次就是细节的处理,比如坐标的定位,蛇的行走路线等。
我们简单一点来看,食物可以看成一个小方格,蛇是有N个小方格组成,那我们就可以用GDI+这样实现:
///
/// 画一个小方块
///
public void DrawShape(int x, int y)
{
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.Blue, 2);
g.DrawRectangle(pen, x, y, 15, 15);
g.FillRectangle(Brushes.Green, x, y, 15, 15);
}
///
/// 画一个食物
///
public void DrawFood(int x, int y)
{
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.Red, 2);
SolidBrush brush = new SolidBrush(Color.Green);
g.DrawRectangle(pen, x, y, 15, 15);
g.FillRectangle(brush, x, y, 15, 15);
}
Graphics这个类我就不多说,大家可以看看MSDN上介绍的用法,上面是画蛇的最小单元-方格,和一个食物方格,蛇的方格大小是15*15,边框颜色是Blue,填充色是Green;食物方格的大小是15*15,边框颜色是Red,填充色是Green。
画好了基本元素,那下面就是用基本元素来表现蛇了,可以用Point数组来存储蛇的坐标,也就是每个方格的坐标,我们先看下代码:
///
/// 设置Point数组坐标
///
public void Forward(int drt)
{
Point temp = snakeArr[0];
for (int i = snakeLen - 1; i > 0; i--)
{
snakeArr[i].X = snakeArr[i - 1].X;
snakeArr[i].Y = snakeArr[i - 1].Y;
}
switch (drt)
{
case 1:
snakeArr[0].X = temp.X;
snakeArr[0].Y = temp.Y - 15;
break; //上
case 2:
snakeArr[0].X = temp.X + 15;
snakeArr[0].Y = temp.Y;
break; //右
case 3:
snakeArr[0].X = temp.X;
snakeArr[0].Y = temp.Y + 15;
break; //下
case 4:
snakeArr[0].X = temp.X - 15;
snakeArr[0].Y = temp.Y;
break; //左
}
}
drt参数是键盘上上下左右键对应的数字,snakeLen是数组的长度也就是方格的个数,上面那个for循环主要的作用是把前一个数组的坐标赋值给下一个,就像是毛毛虫爬行一样,后一节会按照前一节的路线来爬,下面那个switch的作用是,设置蛇头的行进路线。
再下面就是判断蛇是否吃到食物、是否撞到墙和撞到自己,因为蛇和食物都是用坐标存储的,所以只要判断蛇头坐标是否等于食物坐标就可以了:
///
/// 判断是否吃到食物
///
public bool EatedFoot(Point FoodLct)
{
if (snakeArr[0].X == FoodLct.X && snakeArr[0].Y == FoodLct.Y)
{
if (snakeLen < SnakeMaxLength)
{
snakeLen++;
snakeArr[snakeLen].X = snakeArr[snakeLen - 1].X;
snakeArr[snakeLen].Y = snakeArr[snakeLen - 1].Y;
}
return true;
}
else
return false;
}
///
/// 判断是否撞到自己
///
public bool CheckSnakeHeadInSnakeBody()
{
return this.CheckInSnakeBody(this.snakeArr[0].X, this.snakeArr[0].Y, 1);
}
///
/// 检查输入的坐标是否在蛇的身上
///
public bool CheckInSnakeBody(int x, int y, int snkHead)
{
for (int i = snkHead; i < snakeLen; i++)
{
if (x == this.snakeArr[i].X && y == this.snakeArr[i].Y)
{
return true;
}
} return false;
}
///
/// 判断是否撞墙
///
///
public bool CheckSnakeBodyInFrm()
{
if (this.snakeArr[0].X >= 594 || this.snakeArr[0].Y >= 399 - 32 || this.snakeArr[0].X < 0 || this.snakeArr[0].Y < 0)
return true;
else
return false;
}*/
/*实现上面的几个步骤,简单版的贪吃蛇基本上就完成了,再加上一个timer控件,这样蛇就会“动”起来了,就这么简单。
完整代码:*/
///
/// 画一个小方块
///
public void DrawShape(int x, int y)
{
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.Blue, 2);
g.DrawRectangle(pen, x, y, 15, 15);
g.FillRectangle(Brushes.Green, x, y, 15, 15);
}
///
/// 画一个食物
///
public void DrawFood(int x, int y)
{
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.Red, 2);
SolidBrush brush = new SolidBrush(Color.Green);
g.DrawRectangle(pen, x, y, 15, 15);
g.FillRectangle(brush, x, y, 15, 15);
}
///
/// 设置Point数组坐标
///
public void Forward(int drt)
{
Point temp = snakeArr[0];
for (int i = snakeLen - 1; i > 0; i--)
{
snakeArr[i].X = snakeArr[i - 1].X;
snakeArr[i].Y = snakeArr[i - 1].Y;
}
switch (drt)
{
case 1: snakeArr[0].X = temp.X; snakeArr[0].Y = temp.Y - 15; break; //上
case 2: snakeArr[0].X = temp.X + 15; snakeArr[0].Y = temp.Y; break; //右
case 3: snakeArr[0].X = temp.X; snakeArr[0].Y = temp.Y + 15; break; //下
case 4: snakeArr[0].X = temp.X - 15; snakeArr[0].Y = temp.Y; break; //左
}
}
///
/// 时间事件 ///
private void timer1_Tick(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
g.Clear(Color.DarkKhaki);//清除整个画面
Forward(snakeDrt);
for (int i = 0; i < snakeLen; i++)
{
DrawShape(snakeArr[i].X, snakeArr[i].Y);
}
if (panDuan)
{
ShowFood();//DrawFood(FoodLct.X, FoodLct.Y);
panDuan = false;
}
if (EatedFoot(FoodLct))
{
ShowFood();
DrawFood(FoodLct.X, FoodLct.Y);
}
else
{
DrawFood(FoodLct.X, FoodLct.Y);
}
if (CheckSnakeHeadInSnakeBody() || CheckSnakeBodyInFrm())
{
this.timer1.Enabled = false;
MessageBox.Show("游戏结束!");
}
}
///
/// 按下方向键
///
private void frmSnake_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up) snakeDrt = 1;
else if (e.KeyCode == Keys.Down)
snakeDrt = 3;
else if (e.KeyCode == Keys.Right)
snakeDrt = 2;
else if (e.KeyCode == Keys.Left)
snakeDrt = 4;
}
///
/// 判断是否撞到自己
///
public bool CheckSnakeHeadInSnakeBody()
{
return this.CheckInSnakeBody(this.snakeArr[0].X, this.snakeArr[0].Y, 1);
}
///
/// 检查输入的坐标是否在蛇的身上
///
public bool CheckInSnakeBody(int x, int y, int snkHead)
{
for (int i = snkHead; i < snakeLen; i++)
{
if (x == this.snakeArr[i].X && y == this.snakeArr[i].Y)
{
return true;
}
}
return false;
}
///
/// 判断是否撞墙
///
///
public bool CheckSnakeBodyInFrm()
{
if (this.snakeArr[0].X >= 594 || this.snakeArr[0].Y >= 399 - 32 || this.snakeArr[0].X < 0 || this.snakeArr[0].Y < 0)
return true;
else
return false;
}
///
/// 随机显示食物
///
public void ShowFood()
{
Random rmd = new Random();
int x, y; x = rmd.Next(0, this.Width / 15) * 15;
y = rmd.Next(0, this.Height / 15) * 15;
//while (this.CheckInSnakeBody(x, y, 1))
//{
// x = rmd.Next(0, 32) * 15;
// y = 32 + rmd.Next(0, 30) * 15;
//}
FoodLct.X = x;
FoodLct.Y = y;
}
///
/// 判断是否吃到食物
///
public bool EatedFoot(Point FoodLct)
{
if (snakeArr[0].X == FoodLct.X && snakeArr[0].Y == FoodLct.Y)
{
if (snakeLen < SnakeMaxLength)
{
snakeLen++;
snakeArr[snakeLen].X = snakeArr[snakeLen - 1].X;
snakeArr[snakeLen].Y = snakeArr[snakeLen - 1].Y;
} return true;
}
else
return false;
}
}
}