怎样用c语言给贪吃蛇加倒计时,c++贪吃蛇代码中,哪条代码是让蛇知道前进的

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

#include

#include

#include

#include

using namespace std;

// 刷新当前屏幕

inline void Refresh(char q[][22], int grade, int gamespeed){

system("cls"); // 清屏

int i,j;

cout << endl;

for(i=0;i<22;i++){

cout << "\t";

for(j=0;j<22;j++)

cout<

if(i==0) cout << "\t等级为:" << grade;

if(i==4) cout << "\t自动前进时间";

if(i==6) cout << "\t间隔为:" << gamespeed << "ms";

cout<

}

}

int main(){

char tcsQipan[22][22]; // 贪吃蛇棋盘是一个二维数组(如22*22,包括墙壁)

int i,j;

for(i=1;i<=20;i++)

for(j=1;j<=20;j++)

tcsQipan[i][j]=' '; // 初始化贪吃蛇棋盘中间空白部分

for(i=0;i<=21;i++)

tcsQipan[0][i] = tcsQipan[21][i] = '-'; //初始化贪吃蛇棋盘上下墙壁

for(i=1;i<=20;i++)

tcsQipan[i][0] = tcsQipan[i][21] = '|'; //初始化贪吃蛇棋盘左右墙壁

int tcsZuobiao[2][100]; //蛇的坐标数组

for(i=0; i<4; i++){

tcsZuobiao[0][i] = 1;

tcsZuobiao[1][i] = i + 1;

}

int head = 3,tail = 0;

for(i=1;i<=3;i++)

tcsQipan[1][i]='*'; //蛇身

tcsQipan[1][4]='#'; //蛇头

int x1, y1; // 随机出米

srand(time(0));

do{

x1=rand()%20+1;

y1=rand()%20+1;

}while(tcsQipan[x1][y1]!=' ');

tcsQipan[x1][y1]='*';

cout<

long start;

int grade = 1, length = 4;

int gamespeed = 500; //前进时间间隔

for(i=3;i>=0;i--){

start=clock();

while(clock()-start<=1000);

system("cls");

if(i>0)

cout << "\n\n\t\t进入倒计时:" << i << endl;

else

Refresh(tcsQipan,grade,gamespeed);

}

int timeover;

char direction = 77; // 初始情况下,向右运动

int x,y;

while(1){

timeover = 1;

start = clock();

while((timeover=(clock()-start<=gamespeed))&&!kbhit());

//如果有键按下或时间超过自动前进时间间隔则终止循环

if(timeover){

getch();direction = getch();

}

switch(direction){

case 72: x= tcsZuobiao[0][head]-1; y= tcsZuobiao[1][head];break; // 向上

case 80: x= tcsZuobiao[0][head]+1; y= tcsZuobiao[1][head];break; // 向下

case 75: x= tcsZuobiao[0][head]; y= tcsZuobiao[1][head]-1;break; // 向左

case 77: x= tcsZuobiao[0][head]; y= tcsZuobiao[1][head]+1; // 向右

}

if(!(direction==72||direction==80||direction==75 ||direction==77)){ // 按键非方向键

cout << "\tGame over!" << endl;return 0;

}

if(x==0 || x==21 ||y==0 || y==21){ // 碰到墙壁

cout << "\tGame over!" << endl;return 0;

}

if(tcsQipan[x][y]!=' '&&!(x==x1&&y==y1)){ // 蛇头碰到蛇身

cout << "\tGame over!" << endl;return 0;

}

if(x==x1 && y==y1){ // 吃米,长度加1

length ++;

if(length>=8){

length -= 8;

grade ++;

if(gamespeed>=200)

gamespeed = 550 - grade * 50; // 改变自动前进时间间隔

}

tcsQipan[x][y]= '#';

tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]] = '*';

head = (head+1)%100;

tcsZuobiao[0][head] = x;

tcsZuobiao[1][head] = y;

do

{

x1=rand()%20+1;

y1=rand()%20+1;

}while(tcsQipan[x1][y1]!=' ');

tcsQipan[x1][y1]='*';

Refresh(tcsQipan,grade,gamespeed);

}

else{ // 不吃米

tcsQipan [tcsZuobiao[0][tail]][tcsZuobiao[1][tail]]=' ';

tail=(tail+1)%100;

tcsQipan [tcsZuobiao[0][head]][tcsZuobiao[1][head]]='*';

head=(head+1)%100;

tcsZuobiao[0][head]=x;

tcsZuobiao[1][head]=y;

tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]]='#';

Refresh(tcsQipan,grade,gamespeed);

}

}

return 0;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用C语言编写贪吃蛇游戏的基本代码,仅供参考: ```c #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <windows.h> #define WIDTH 30 #define HEIGHT 20 int map[HEIGHT][WIDTH]; typedef struct Snake { int x; int y; struct Snake *next; } Snake; typedef struct Food { int x; int y; } Food; Snake *head, *tail; Food food; int dir = 1; int score = 0; void init() { int i, j; for (i = 0; i < HEIGHT; i++) for (j = 0; j < WIDTH; j++) map[i][j] = 0; head = (Snake*)malloc(sizeof(Snake)); tail = (Snake*)malloc(sizeof(Snake)); head->x = WIDTH / 2; head->y = HEIGHT / 2; head->next = tail; tail->x = head->x - 1; tail->y = head->y; tail->next = NULL; map[head->y][head->x] = 1; map[tail->y][tail->x] = 1; food.x = rand() % WIDTH; food.y = rand() % HEIGHT; } void draw() { int i, j; system("cls"); printf("Score: %d\n\n", score); for (i = 0; i < HEIGHT; i++) { for (j = 0; j < WIDTH; j++) { if (map[i][j] == 0) printf("."); else if (map[i][j] == 1) printf("*"); else if (map[i][j] == 2) printf("#"); else if (map[i][j] == 3) printf("O"); } printf("\n"); } } void update() { int i, j; Snake *p = (Snake*)malloc(sizeof(Snake)); p->x = head->x; p->y = head->y; p->next = head->next; head->next = p; head->x += dir % 2 * (2 - dir); head->y += (dir + 1) % 2 * (dir - 1); if (head->x < 0 || head->x >= WIDTH || head->y < 0 || head->y >= HEIGHT || map[head->y][head->x] == 1) exit(0); if (head->x == food.x && head->y == food.y) { score++; food.x = rand() % WIDTH; food.y = rand() % HEIGHT; } else { map[tail->y][tail->x] = 0; tail = tail->next; free(tail->next); tail->next = NULL; } p = head; while (p != NULL) { map[p->y][p->x] = 1; p = p->next; } map[food.y][food.x] = 2; map[head->y][head->x] = 3; } void control() { if (_kbhit()) { switch (_getch()) { case 'w': dir = 0; break; case 'a': dir = 3; break; case 's': dir = 2; break; case 'd': dir = 1; break; } } } int main() { init(); while (1) { draw(); update(); control(); Sleep(100); } return 0; } ``` 这只是一个简单的贪吃蛇游戏,你可以根据自己的需要进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值