贪吃蛇 c语言 闪屏,C语言贪吃蛇闪屏问题,求大神!!!

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

#include

#include

#include

#include

#include

#include

using namespace std;

#define HEAD 'H'

#define BODY 'X'

#define WALL '*'

#define money 'S'

#define N 50

#define n 20

void output();

void snakemove(int a,int b);

void putmoney();

#define MAX 20

int snakex[MAX] = {1,1,1,1,1};

int snakey[MAX] = {1,2,3,4,5};

char snakemap[n][N] =

{"********************************************",

"*XXXXH *",

"* *",

"* *",

"* *",

"* *",

"* *",

"* *",

"* *",

"* *",

"* *",

"* *",

"* *",

"* *",

"* *",

"* *",

"* *",

"* *",

"* *",

"********************************************"

};

int length = 5;

int alive = 1;

char direction = 'D';

int main()

{

char ch;

system("color 00");

system("color A4");

putmoney();

output();

while(alive)

{

if(kbhit())

{

ch = getch();

switch(ch)

{

case 'A':

case 'a':if(direction == 'd')break;direction = tolower(ch);snakemove(0,-1);break;

case 'D':

case 'd':if(direction == 'a')break;direction = tolower(ch);snakemove(0,1);break;

case 'S':

case 's':if(direction == 'w')break;direction = tolower(ch);snakemove(1,0);break;

case 'W':

case 'w':if(direction == 's')break;direction = tolower(ch);snakemove(-1,0);break;

}

Sleep(50);

output();

}

else

{

switch(direction)

{

case 'a':snakemove(0,-1);break;

case 'd':snakemove(0,1);break;

case 's':snakemove(1,0);break;

case 'w':snakemove(-1,0);break;

}Sleep(50);

output();

}

}

system("cls");

cout<

return 0;

}

void snakemove(int a,int b)

{

int x,y;

x = snakex[length-1] + a;

y = snakey[length-1] + b;

if(snakemap[x][y] == HEAD || snakemap[x][y] == BODY || snakemap[x][y] == WALL)

alive = 0;

else if(snakemap[x][y] == money && length < 20)

{

length++;

putmoney();

snakex[length-1] = x;

snakey[length-1] = y;

snakemap[snakex[length-1]][snakey[length-1]] = HEAD;

snakemap[snakex[length-2]][snakey[length-2]] = BODY;

}

else

{

int i;

int x1,y1;

x1 = snakex[0];

y1 = snakey[0];

for(i = 0;i

{

snakex[i] = snakex[i+1];

snakey[i] = snakey[i+1];

}

snakemap[x1][y1] = ' ';

snakex[length-1] = x;

snakey[length-1] = y;

snakemap[snakex[length-1]][snakey[length-1]] = HEAD;

snakemap[snakex[length-2]][snakey[length-2]] = BODY;

}

}

void output()

{

system("cls");

int i,j;

cout<

for(i = 0;i

{

for(j = 0;j

cout<

cout<

}system("color 00");

system("color A4");

}

void putmoney()

{

srand(time(0));

int x = 0,y = 0;

do

{

x = rand() % 15 + 1;

y = rand() % 40 + 1;

}while(snakemap[x][y] == WALL && snakemap[x][y] == BODY && snakemap[x][y] ==HEAD);

snakemap[x][y] = money;

}

代码如上,怎么样才能实现不闪屏,听说要用什么双缓冲技术,求会的大神帮我修改下代码让它不闪屏!!!小弟感激不尽!!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的贪吃蛇C语言代码,供参考: ```c #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <windows.h> #define WIDTH 20 #define HEIGHT 20 #define MAX_LENGTH (WIDTH*HEIGHT) int snake[MAX_LENGTH][2]; int food[2]; int score; int length; int direction; int running; void init() { // 初始化贪吃蛇的位置 snake[0][0] = WIDTH / 2; snake[0][1] = HEIGHT / 2; snake[1][0] = WIDTH / 2 - 1; snake[1][1] = HEIGHT / 2; snake[2][0] = WIDTH / 2 - 2; snake[2][1] = HEIGHT / 2; length = 3; // 初始化食物的位置 food[0] = rand() % WIDTH; food[1] = rand() % HEIGHT; // 初始化分数 score = 0; // 初始化方向 direction = 'd'; // 初始化运行标志 running = 1; } void draw() { system("cls"); // 画出地图 for (int i = 0; i <= HEIGHT; i++) { for (int j = 0; j <= WIDTH; j++) { if (i == 0 || i == HEIGHT || j == 0 || j == WIDTH) { printf("#"); } else if (i == snake[0][1] && j == snake[0][0]) { printf("@"); } else if (i == food[1] && j == food[0]) { printf("*"); } else { int flag = 0; for (int k = 1; k < length; k++) { if (i == snake[k][1] && j == snake[k][0]) { printf("#"); flag = 1; break; } } if (!flag) { printf(" "); } } } printf("\n"); } // 显示分数 printf("Score: %d\n", score); } void update() { // 记录蛇尾位置 int tail[2]; tail[0] = snake[length - 1][0]; tail[1] = snake[length - 1][1]; // 移动蛇身 for (int i = length - 1; i > 0; i--) { snake[i][0] = snake[i - 1][0]; snake[i][1] = snake[i - 1][1]; } // 根据方向移动蛇头 switch (direction) { case 'w': snake[0][1]--; break; case 'a': snake[0][0]--; break; case 's': snake[0][1]++; break; case 'd': snake[0][0]++; break; } // 判断是否吃到食物 if (snake[0][0] == food[0] && snake[0][1] == food[1]) { // 食物位置随机生成 food[0] = rand() % WIDTH; food[1] = rand() % HEIGHT; // 分数加一 score++; // 蛇身长度加一 length++; } // 判断是否碰到墙壁或自己的身体 if (snake[0][0] == 0 || snake[0][0] == WIDTH || snake[0][1] == 0 || snake[0][1] == HEIGHT) { running = 0; } for (int i = 1; i < length; i++) { if (snake[0][0] == snake[i][0] && snake[0][1] == snake[i][1]) { running = 0; break; } } } void input() { // 获取键盘输入 if (_kbhit()) { char c = _getch(); if (c == 'w' || c == 'a' || c == 's' || c == 'd') { // 只有输入合法的方向键才会改变方向 if ((c == 'w' && direction != 's') || (c == 'a' && direction != 'd') || (c == 's' && direction != 'w') || (c == 'd' && direction != 'a')) { direction = c; } } } } int main() { init(); while (running) { draw(); update(); input(); Sleep(100); } printf("Game over\n"); printf("Your score: %d\n", score); return 0; } ``` 这个代码使用了Windows.h库中的一个函数`Sleep()`,该函数可以让程序暂停一段时间,单位是毫秒。在这个代码中,每100毫秒刷新一次屏幕,相当于一秒钟刷新10次。如果需要在其他平台上运行此代码,需要修改画图函数和暂停函数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值