【C语言】【windows】--新人,发个,我自己做的贪吃蛇
erbi_lucifer吧
全部回复
仅看楼主
level 7
chenwillbeup 楼主
一楼度娘
2012年03月22日 08点03分 1
level 7
chenwillbeup 楼主
三楼上代码
2012年03月22日 08点03分 2
level 7
chenwillbeup 楼主
刚发了一次,不知道传上来没有
2012年03月22日 08点03分 6
level 7
chenwillbeup 楼主

class Snake
{
public:
POINT Que[901];
int dire;
int head;
int tail;
public:
void add_point(POINT point,HDC hdc)
{
Que[tail]=point;
tail=(tail+1)%900;
SnakeYard[point.x][point.y]=1;
PaintPoint(point,hdc,RGB(0,255,0));
sum--;
}
void delete_point(HDC hdc)
{
SnakeYard[Que[head].x][Que[head].y]=0;
PaintPoint(Que[head],hdc,RGB(255,0,0));
head=(head+1)%900;
sum++;
}
inline bool JudgeMeal()
{
POINT point;
point.x=Que[forward(tail)].x+dir[dire][0];
point.y=Que[forward(tail)].y+dir[dire][1];
if(point.x!=Meal.x||point.y!=Meal.y)
return true;
return false;
}
inline bool Judgeblock()
{
POINT point;
point.x=Que[forward(tail)].x+dir[dire][0];
point.y=Que[forward(tail)].y+dir[dire][1];
if(point.x<0||point.x>29||point.y<0||point.y>29||(SnakeYard[point.x][point.y]==1))
return true;
return false;
}
bool move(HDC hdc)
{
if(JudgeMeal())
{
POINT point;
point.x=Que[forward(tail)].x+dir[dire][0];
point.y=Que[forward(tail)].y+dir[dire][1];
add_point(point ,hdc);
delete_point(hdc);
return false;
}
else
{
POINT point;
point.x=Que[forward(tail)].x+dir[dire][0];
point.y=Que[forward(tail)].y+dir[dire][1];
add_point(point ,hdc);
return true;
}
}
};
void PaintScreen(COLORREF rgb,HDC hdc)
{
HBRUSH hBrush=::CreateSolidBrush(rgb);
HBRUSH hOldBrush=(HBRUSH)::SelectObject(hdc,hBrush);
for(int i=0;i<30;i++)
for(int j=0;j<30;j++)
{
POINT point;
point.x=i;
point.y=j;
if(!SnakeYard[i][j])
PaintPoint(point,hdc);
else if(SnakeYard[i][j]==1)
PaintPoint(point,hdc,RGB(0,255,0));
else
PaintPoint(point,hdc,RGB(0,0,255));
}
::SelectObject(hdc,hOldBrush);
::DeleteObject(hBrush);
}
void InitScreen(HDC hdc,HWND hwnd)
{
RECT rt;
::GetClientRect(hwnd,&rt);
::SetMapMode(hdc,MM_ANISOTROPIC);
::SetWindowExtEx(hdc,30,30,NULL);
::SetViewportExtEx(hdc,rt.right,rt.bottom,NULL);
::SetViewportOrgEx(hdc,0,0,NULL);
}
Snake snake;
void InitData(HDC hdc)
{
memset(SnakeYard,0,sizeof(SnakeYard));
snake.head=0;
snake.tail=0;
snake.dire=1;
POINT point;
point.x=15;
point.y=15;
snake.add_point(point,hdc);
}
void Cook(HDC hdc,COLORREF rgb)
{
srand((unsigned)time(NULL));
int n=rand()%sum;
for(int i=0;i<30;i++)
{
for(int j=0;j<30;j++)
if(!SnakeYard[i][j])
if(!(n--))
{
Meal.x=i;
Meal.y=j;
break;
}
if(n==-1)
break;
}
SnakeYard[Meal.x][Meal.y]=2;
PaintPoint(Meal,hdc,rgb);
}

2012年03月22日 08点03分 10
level 7
chenwillbeup 楼主
算了,我发现东西多就传不上来,就传了一段控制贪吃蛇的代码
2012年03月22日 08点03分 11
level 7
chenwillbeup 楼主
static int CubeLength;
static int times=0;
static int sum=900;
char SnakeYard[30][30];活动区域,30*30
int dir[4][2]={0,-1,1,0,0,1,-1,0};方向,四个方向
POINT Meal; 这个是食物的地方,一个point结构
int speed=500; 这个是移动速度
int screen=-1; 这个忘了是啥,记得应该有定义方向的

2012年03月22日 09点03分 12
level 7
chenwillbeup 楼主
先去吃饭了
2012年03月22日 09点03分 13
level 10
首先热烈欢迎 @chenwillbeup ……[憧憬]
话说度娘吞楼不是一天两天的了……[心碎]
所以之前发帖就把每一个函数放一楼(长的话就拆成几楼),貌似每楼内容少的话度娘就会口下留情……[寻死]

2012年03月22日 09点03分 14
level 7
chenwillbeup 楼主
#include<Windows.h>
#include"resource.h"
#include<time.h>
static int CubeLength;
static int times=0;
static int sum=900;
char SnakeYard[30][30];
int dir[4][2]={0,-1,1,0,0,1,-1,0};
POINT Meal;
int speed=500;
int screen=-1;
inline int forward(int number)
{
return number==0?899:number-1;
}
inline void PaintPoint(POINT point,HDC hdc)
{
::Rectangle(hdc,point.x,point.y,point.x+1,point.y+1);
}
inline void PaintAgain(HDC hdc,HWND hwnd)//这就是我的程序
{
RECT rt;
::GetClientRect(hwnd,&rt);
::SetMapMode(hdc,MM_ANISOTROPIC);
::SetWindowExtEx(hdc,30,30,NULL);
::SetViewportExtEx(hdc,rt.right,rt.bottom,NULL);
::SetViewportOrgEx(hdc,0,0,NULL);
}
void PaintPoint(POINT point,HDC hdc,COLORREF rgb)
{
HBRUSH hBrush=::CreateSolidBrush(rgb);
HBRUSH hOldBrush=(HBRUSH)::SelectObject(hdc,hBrush);
PaintPoint(point,hdc);
::SelectObject(hdc,hOldBrush);
::DeleteObject(hBrush);
}
这是第一部分
2012年03月22日 12点03分 15
level 7
chenwillbeup 楼主
第三部分,就是主函数了
LRESULT CALLBACK MainWndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
char szClassName[]="snake";
WNDCLASSEX wndclass;
wndclass.cbSize = sizeof(wndclass);// 结构的大小
wndclass.style = CS_HREDRAW|CS_VREDRAW;// 指定如果大小改变就重画
wndclass.lpfnWndProc = MainWndProc;// 窗口函数指针
wndclass.cbClsExtra = 0;// 没有额外的类内存
wndclass.cbWndExtra = 0;// 没有额外的窗口内存
wndclass.hInstance = hInstance;// 实例句柄
wndclass.hIcon = ::LoadIcon(hInstance,(LPSTR)IDI_SNAKE);// 使用预定义图标
wndclass.hCursor = ::LoadCursor(NULL,
IDC_ARROW);// 使用预定义的光标
wndclass.hbrBackground = (HBRUSH)
::GetStockObject(WHITE_BRUSH);// 使用白色背景画刷
wndclass.lpszMenuName = NULL;// 不指定菜单
wndclass.lpszClassName = szClassName ;// 窗口类的名称
wndclass.hIconSm = NULL;// 没有类的小图标
::RegisterClassEx(&wndclass);
// 创建主窗口
HWND hwnd = ::CreateWindowEx(
0,// dwExStyle,扩展样式
szClassName,// lpClassName,类名
"snake",// lpWindowName,标题
WS_OVERLAPPEDWINDOW,// dwStyle,窗口风格
CW_USEDEFAULT,// X,初始 X 坐标
CW_USEDEFAULT,// Y,初始 Y 坐标
CW_USEDEFAULT,// nWidth,宽度
CW_USEDEFAULT,// nHeight,高度
NULL,// hWndParent,父窗口句柄
NULL,// hMenu,菜单句柄
hInstance,// hlnstance,程序实例句柄
NULL) ;// lpParam,用户数据
if(hwnd == NULL)
{
::MessageBox(NULL, "创建窗口出错!", "error", MB_OK);
return -1;
}
// 显示窗口,刷新窗口客户区
::ShowWindow(hwnd, nCmdShow);
::UpdateWindow(hwnd);
// 从消息堆中取出消息
MSG msg;
while(::GetMessage(&msg, NULL, 0, 0))
{
// 转化键盘消息
::TranslateMessage(&msg);
// 将消息发送到相应的窗口函数
::DispatchMessage(&msg);
}
// 当GetMessage返回0时程序结束
return msg.wParam;
}

2012年03月22日 12点03分 16
level 7
chenwillbeup 楼主
本人学api也没多久,估计会有不少bug和不规范,大家将就一下了
2012年03月22日 12点03分 18
level 10
顶楼主……
这是程序运行时的截图:
[顶]

2012年03月22日 14点03分 19
level 7
chenwillbeup 楼主
谢谢捧场
2012年03月23日 04点03分 20
1