C++课程设计贪吃蛇小游戏(内附完整源码与附件)

// 初始化蛇尾 tailX = tailY =

N/ 2 - 1; N/ 2;

TAIL ;

RIGHT; // 记录蛇尾的移动方向

map[tailY][tailX] = directionMap[tailY][tailX] =

// 初始化食物

srand( unsigned (time( NULL))); do {

foodX = rand() % ( foodY = rand() % (

N- 1) + 1; N- 1) + 1;

// 产生随机数 1~(N-1)

/ / 随机播种

} while ( EMPTY!= map[foodY][foodX]); map[foodY][foodX] = // 首次显示游戏界面 for (i = 0; i <

for (j = 0; j <

{

case WALL:cout case HEAR:cout case BODY: case TAIL :cout case FOOD:cout default :

cout << \\; break ; } cout << endl; } }

<< \●\; break ; << \★\break ; << \■\; break ; << \○\; break ;

N; i++) {

N; j++) FOOD;

switch (map[i][j])

void RetroSnaker ::gotoXY( int {

COORDcoord; coord.X = 2* coord.Y = }

void RetroSnaker ::againFood() {

x; y;

x, int y)

SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDL),Ecoord);

srand( unsigned (time( NULL))); / / 随机播种

do {

foodX = rand() % ( foodY = rand() % ( map[foodY][foodX] = gotoXY(foodX, foodY); cout << \★\}

void RetroSnaker ::erasingTheTail() {

gotoXY(tailX, tailY); cout << \\; map[tailY][tailX] =

EMPTY;

// 擦除尾巴 N- 1) + 1; N- 1) + 1; FOOD;

// 产生随机数 1~(N-1)

} while ( EMPTY!= map[foodY][foodX]);

if ( UP == directionMap[tailY][tailX]) {

directionMap[tailY][tailX] = tailY--; }

else if ( DOWN== directionMap[tailY][tailX]) {

directionMap[tailY][tailX] = tailY++; }

else if ( LEFT == directionMap[tailY][tailX]) {

directionMap[tailY][tailX] = tailX--; } else {

directionMap[tailY][tailX] = tailX++; }

map[tailY][tailX] = }

void RetroSnaker ::erasingTheHear( {

int x= hearX, y = hearY; if ( UP == ch)

--hearY;

else if ( DOWN== ch)

++hearY;

else if ( LEFT == ch)

char ch)

TAIL ;

EMPTY; EMPTY; EMPTY; EMPTY;

hearX--; else

hearX++;

map[y][x] = gotoXY(x, y); cout << \●\

BODY; // 擦除蛇头,绘制蛇身

directionMap[hearY][hearX] = directionMap[y][x]; // 绘制蛇头

map[hearY][hearX] = HEAR;

gotoXY(hearX, hearY); cout << \○\; }

int main( void ) {

char ch; bool sign =

false ;

DWORDkeyTime=0; RetroSnaker r; while ( true ) {

if (r.gameOver) {

// 游戏结束 _getch(); break ; }

if (_kbhit()) {

// 检测是否按下键盘

ch = _getch(); // 获取该按键,并不回显在屏幕上

sign = true ;

}

if (GetTickCount()- keyTime >

GMAE_TIME&&sign) { GMAE_TIME毫秒则移动蛇

r.move(ch); // 移动蛇

keyTime = GetTickCount();

sign = false ;

} else {

// 没按下键盘,则默认处理 if (GetTickCount() - keyTime >

GMAE_TIME) { GMAE_TIME毫秒则移动蛇

r.move(); // 默认移动

keyTime = GetTickCount();

}

// 两次按键的间隔时间大于// 两次按键的间隔时间大于

main.cpp

件贪吃蛇VC6.0++ 项目 .zip

} }

return 0; }

联系客服:779662525#qq.com(#替换为@)