云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > C++课程设计贪吃蛇小游戏(内附完整源码与附件)

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

  • 62 次阅读
  • 3 次下载
  • 2025/6/22 21:53:28

// 初始化蛇尾 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; }

  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

// 初始化蛇尾 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] = // 首次显示游戏界面 f

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com