基本信息
源码名称:C++/CLI Windows窗体应用程序
源码大小:0.80M
文件格式:.rar
开发语言:C/C++
更新时间:2017-12-23
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

一个通过GDI 绘图在picturebox上绘制贪吃蛇游戏的项目。可以在项目基础上联系贪吃蛇A*、最长路径、最短路径、目标追踪、曼哈顿等算法,实现贪吃蛇AI。

#define ROWS 20
#define COLUMNS 20
enum direction{ UP = 0, DOWN, LEFT, RIGHT };
namespace Snake{
using namespace System;
using namespace System::Drawing;
public ref class Snake
{
protected:
array<Point^>^ snakebody;
System::Int32 numbers;
System::Drawing::Color sideColor;
System::Drawing::Color bodyColor;
public:
Snake(){
snakebody = gcnew array<Point^>(ROWS*COLUMNS);
//int n=ROWS*COLUMNS;
//MessageBox::Show(L"" n.ToString());
numbers = 4;
sideColor = System::Drawing::Color::Yellow;
bodyColor = System::Drawing::Color::Green;
}
property array<Point^>^ snakeBody{
array<Point^>^ get(){ return snakebody; }
}
//前行绘制
void clearSnake(int sx0, int sy0)
{
numbers = 4;
snakebody[0] = System::Drawing::Point(sx0, sy0);
for (int i = 1; i<numbers; i )
snakebody[i] = System::Drawing::Point(snakebody[i - 1]->X, snakebody[i - 1]->Y 1);

}
//
void DrawSnake(Graphics^ g, int x0, int y0, int width, int dirt){
Pen^ pen = gcnew Pen(sideColor, 1);
SolidBrush^ brush = gcnew SolidBrush(bodyColor);
SolidBrush^ brush1 = gcnew SolidBrush(System::Drawing::Color::Black);
int x, y;
for (int i = 0; i<numbers; i ){
Rectangle rect = Rectangle(x0 this->snakebody[i]->X*width, y0 this->snakebody[i]->Y*width, width, width);
if (i == 0){
g->FillEllipse(brush, rect);
if (dirt == UP){
x = x0 this->snakebody[i]->X*width;
y = y0 this->snakebody[i]->Y*width width / 2;
rect = Rectangle(x, y, width, width / 2);
}
if (dirt == DOWN){
x = x0 this->snakebody[i]->X*width;
y = y0 this->snakebody[i]->Y*width;
rect = Rectangle(x, y, width, width / 2);
}
if (dirt == LEFT){
x = x0 this->snakebody[i]->X*width width / 2;
y = y0 this->snakebody[i]->Y*width;
rect = Rectangle(x, y, width / 2, width);
}
if (dirt == RIGHT){
x = x0 this->snakebody[i]->X*width;
y = y0 this->snakebody[i]->Y*width;
rect = Rectangle(x, y, width / 2, width);
}
g->FillRectangle(brush, rect);
x = x0 this->snakebody[i]->X*width width / 2 - 2;
y = y0 this->snakebody[i]->Y*width width / 2 - 2;
rect = Rectangle(x, y, 5, 5);
g->FillEllipse(brush1, rect);
}
else
g->FillRectangle(brush, rect);
}

}
void move(int x, int y){
int i;
for (i = numbers - 1; i>0; i--)snakebody[i] = snakebody[i - 1];
snakebody[i] = System::Drawing::Point(x, y);
}
void eatFood(int x, int y){
numbers ;
move(x, y);
}
bool IsEatself(int x, int y){
for (int i = 0; i<numbers; i )
if (snakebody[i]->X == x&&snakebody[i]->Y == y)return true;
return false;
}

};
}