資源簡介
數據結構課程設計之貪吃蛇(源代碼),很不錯的資源哦

代碼片段和文件信息
//?snake6.cpp?:?Defines?the?entry?point?for?the?application.
//
#include?“stdafx.h“
#include?“resource.h“
#include?“stdio.h“
#include?“stdlib.h“
#define?MAX_LOADSTRING?100
HDC?Hdc;
int?Num;
char?Text[20];
#define?MAP_X?260
#define?MAP_Y?50
#define?MAP_LEN?20
#define?MAP_XNUM?20
#define?MAP_YNUM?20
#define?SNAKE_MAXLEN?400
#define?SNAKE_UP????0
#define?SNAKE_RIGHT?1
#define?SNAKE_DOWN??2
#define?SNAKE_LEFT??3
typedef?struct?pos
{int?x;
?int?y;
}pos;
//
//定義蛇
typedef?struct?Snake
{?int?direct;?????????????//蛇頭方向
?pos?body[SNAKE_MAXLEN];?//存放蛇的坐標
??int?Len;
}Snake;
Snake?snake;
//
//定義蘋果
typedef?struct?Apple
{int?x;
?int?y;
}Apple;
Apple?apple;
//
//定義尾巴
typedef?struct?Tail
{int?x;
?int?y;
}Tail;
Tail?tail;
HBRUSH?hBrugreenhBruredhBruwhite;
//
//畫綠格子
void?ColorGreen(HDC?hdcint?iint?j){
Selectobject(hdchBrugreen);
Rectangle(hdciji+MAP_LENj+MAP_LEN);
}
//
//畫紅格子
void?ColorRed(HDC?hdcint?iint?j){
Selectobject(hdchBrured);
Rectangle(hdciji+MAP_LENj+MAP_LEN);
}
//
//畫白格子
void?ColorWhite(HDC?hdcint?iint?j){
Selectobject(hdchBruwhite);
Rectangle(hdciji+MAP_LENj+MAP_LEN);
}
//
//
//畫白布
void?ColorWhite2(HDC?hdc){
HBRUSH?hBru;
hBru=CreateSolidBrush(RGB(255255255));
Selectobject(hdchBru);
Rectangle(hdcMAP_XMAP_YMAP_X+MAP_LEN*MAP_XNUM-MAP_LENMAP_Y+MAP_YNUM*MAP_LEN-MAP_LEN);
}
//
//
//畫蛇
void?PaintSnake(HDC?hdcSnake*?snake){
Selectobject(hdchBrured);
for(int?i=0;iLen;i++){
ColorRed(hdcsnake->body[i].xsnake->body[i].y);
}
}
//
//初始化蛇
void?InitSnake(Snake*?snake){
snake->direct=1;
snake->Len=3;
snake->body[0].y=snake->body[1].y=snake->body[2].y=MAP_Y;
snake->body[0].x=MAP_X;
snake->body[1].x=MAP_X+MAP_LEN;
snake->body[2].x=MAP_X+MAP_LEN*2;
}
//
//畫蘋果
void?PaintApple(HDC?hdcApple*?apple){
ColorGreen(hdcapple->xapple->y);
}
//
//初始化蘋果
void?CreateApple(Snake*?snakeApple*?apple){
int?a=rand()%MAP_XNUM*MAP_LEN+MAP_X;
int?b=rand()%MAP_YNUM*MAP_LEN+MAP_Y;
for(int?i=0;iLen;i++){
if(a==snake->body[i].x&&b==snake->body[i].y){
a=rand()%MAP_XNUM*MAP_LEN+MAP_X;
????????b=rand()%MAP_YNUM*MAP_LEN+MAP_Y;
i=0;}
}
apple->x=a;
apple->y=b;
}
//
//更新蛇的位置
void?UpdateSnake(Snake*?snake){
for(int?i=0;iLen-1;i++){
snake->body[i].x=snake->body[i+1].x;
snake->body[i].y=snake->body[i+1].y;}?
switch(snake->direct)
{case?SNAKE_UP:
????snake->body[snake->Len-1].y-=MAP_LEN;
????break;?
?case?SNAKE_RIGHT:
snake->body[snake->Len-1].x+=MAP_LEN;
break;
?case?SNAKE_DOWN:
snake->body[snake->Len-1].y+=MAP_LEN;
break;
?case?SNAKE_LEFT:
?snake->body[snake->Len-1].x-=MAP_LEN;
}
}
//
//蛇長長函數
/*int?SnakeGrow(Snake*?snakeApple*?apple){
????if(snake->body[snake->Len-1].x==apple->x&&snake->body[snake->Len-1].y==apple->y){
??snake->Len++;
??switch(snake->direct)
{case?SNAKE_UP:
snake->body[snake->Len-1].x=snake->bo
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????163893??2009-03-11?20:18??snake6\Debug\snake6.exe
?????文件?????220900??2009-03-11?20:18??snake6\Debug\snake6.ilk
?????文件??????22070??2009-03-11?20:18??snake6\Debug\snake6.obj
?????文件????1959720??2009-03-10?18:58??snake6\Debug\snake6.pch
?????文件?????443392??2009-03-11?20:18??snake6\Debug\snake6.pdb
?????文件???????2124??2009-03-10?18:58??snake6\Debug\snake6.res
?????文件??????54979??2009-03-10?18:58??snake6\Debug\StdAfx.obj
?????文件??????82944??2009-12-20?00:38??snake6\Debug\vc60.idb
?????文件?????176128??2009-03-11?20:18??snake6\Debug\vc60.pdb
?????文件???????2065??2009-03-10?18:58??snake6\ReadMe.txt
?????文件????????781??2009-03-10?18:58??snake6\resource.h
?????文件????????318??2009-03-10?18:58??snake6\small.ico
?????文件??????12963??2009-03-11?20:18??snake6\snake6.cpp
?????文件???????4481??2009-03-10?18:58??snake6\snake6.dsp
?????文件????????520??2009-03-10?22:10??snake6\snake6.dsw
?????文件????????326??2009-03-10?18:58??snake6\snake6.h
?????文件???????1078??2009-03-10?18:58??snake6\snake6.ico
?????文件??????66560??2009-12-20?00:41??snake6\snake6.ncb
?????文件??????48640??2009-12-20?00:41??snake6\snake6.opt
?????文件????????246??2009-12-20?00:38??snake6\snake6.plg
?????文件???????3064??2009-03-10?18:58??snake6\snake6.rc
?????文件????????293??2009-03-10?18:58??snake6\StdAfx.cpp
?????文件????????936??2009-03-10?18:58??snake6\StdAfx.h
?????目錄??????????0??2010-04-20?22:20??snake6\Debug
?????目錄??????????0??2010-04-20?22:20??snake6
-----------?---------??----------?-----??----
??????????????3268421????????????????????25
- 上一篇:SVM分類器代碼
- 下一篇:《大學英語綜合教程》第四冊上海外語教育出版社課后答案
評論
共有 條評論