ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

2021-09-23

2021-09-23 17:01:23  阅读:132  来源: 互联网

标签:body 23 09 snake board printf define buf 2021


#include stdio.h
#include string.h
#include fcntl.h
#define uint8unsigned char
#define uint32 unsigned int
#define BOARD_HIGH_MIN 3
#define BOARD_WIDTH_MIN 3
#define BOARD_HIGH_MAX 99
#define BOARD_WIDTH_MAX 99
#define MAX_SNAKE_LENGTH (BOARD_HIGH_MAX * BOARD_WIDTH_MAX)
#define UP0
#define RIGHT1
#define DOWN2
#define LEFT3
#define NONE0
#define HEAD 1
#define BODY2
#define FOOD3
#define WALL4
#define DEAD5
#define DIE(-1)
typedef struct point_position
{
uint32 x;
uint32 y;
}body_point;
typedef struct snake_struct
{
body_pointbody[MAX_SNAKE_LENGTH];
body_pointtrace;
uint32length;
uint32speed;
uint8direction_now;
}snake_str;
static snake_str snake;
static uint32 board[BOARD_WIDTH_MAX+2][BOARD_HIGH_MAX+2];
static uint32 score = 0;
int board_width, board_high;
void put_food()
{
int a,b;
do{
a = rand() % (board_width+1);
b = rand() % (board_high+1);
}while(NONE != board[a][b]);
board[a][b] = FOOD;
}
void board_init(uint32 width, uint32 high)
{
int i;
memset(board, 0, sizeof(board));
for(i=0;i0;i–)
snake.body[i] = snake.body[i-1];
}
void head_move(void)
{
switch(snake.direction_now)
{
case UP:
{
snake.body[0].y–;
break;
}
case DOWN:
{
snake.body[0].y++;
break;
}
case LEFT:
{
snake.body[0].x–;
break;
}
case RIGHT:
{
snake.body[0].x++;
break;
}
}
}
void snake_move()
{
body_move();
head_move();
}
void longer(void)
{
snake.body[snake.length] = snake.trace;
snake.length++;
}
int board_update()
{
snake_move();
if(FOOD != board[snake.trace.x][snake.trace.y])
board[snake.trace.x][snake.trace.y] = NONE;
if(FOOD == board[snake.body[0].x][snake.body[0].y])
{
longer();
put_food();
score += snake.speed;
}
else if(BODY == board[snake.body[0].x][snake.body[0].y] ||
WALL == board[snake.body[0].x][snake.body[0].y])
{
board[snake.body[0].x][snake.body[0].y] = DEAD;
board[snake.body[1].x][snake.body[1].y] = BODY;
return DIE;
}
board[snake.body[0].x][snake.body[0].y] = HEAD;
board[snake.body[1].x][snake.body[1].y] = BODY;
return 0;
}
uint8 buf_to_direct(char buf)
{
switch(buf)
{
case ‘w’:
{
return UP;
}
case ‘d’:
{
return RIGHT;
}
case ‘s’:
{
return DOWN;
}
case ‘a’:
{
return LEFT;
}
}
}
void turn(uint8 direction)
{
if(direction == snake.direction_now)
{
if(10 == snake.speed)
return;
snake.speed++;
}
else if(2 == direction+snake.direction_now ||
4 == direction+snake.direction_now)
{
if(1 == snake.speed)
return;
snake.speed–;
}
else
snake.direction_now = direction;
}
void start_info_print()
{
printf("####################################################################\n");
printf("## Magnum Presents. ##\n");
printf("## ##\n");
printf("## ‘w’-up, ‘s’-down, ‘a’-left, ‘d’-right. ##\n");
printf("## ‘q’-Pause. ##\n");
printf("## Press front key to speed up, press offsite key to slow down. ##\n");
printf("####################################################################\n");
}
void debug_output()
{
int i,j;
printf("\nsnake:\n");
printf(“length = %d, \n”,snake.length);
printf(“speed = %d, \n”,snake.speed);
printf(“direction_now = %d, \n”,snake.direction_now);
for(i=0;i0;i–)
for(j=0;j<(11-x)*5000;j++);
}
int main(void)
{
int flag,ret;
char buf;
uint8 direction;
int is_alive;
start_info_print();
do {
printf(“Set the board width (%d~%d): “, BOARD_WIDTH_MIN, BOARD_WIDTH_MAX);
scanf(”%d”, &board_width);
printf(“Set the board high (%d~%d): “, BOARD_HIGH_MIN, BOARD_HIGH_MAX);
scanf(”%d”, &board_high);
} while(board_width > BOARD_WIDTH_MAX ||
board_high > BOARD_HIGH_MAX ||
board_width < BOARD_WIDTH_MIN ||
board_high < BOARD_HIGH_MIN);
system(“stty -icanon”); //关闭缓冲区,输入字符无需回车直接接受
system(“stty -echo”); //取消输入字符时的显示
printf("\nPress any button to start…\n");
//将键盘设置为阻塞
flag &= ~O_NONBLOCK;
fcntl(0,F_SETFL,flag);
while(1)
{
read(0,&buf,1);
flag |= O_NONBLOCK;
fcntl(0,F_SETFL,flag);
break;
}
START:
//将键盘设置为非阻塞
flag = fcntl(0,F_GETFL);
flag |=O_NONBLOCK;
fcntl(0,F_SETFL,flag);
score = 0;
snake_init();
board_init(board_width, board_high);
display();
while(1)
{
buf = 0;
read(0,&buf,1);
if(buf==‘w’ || buf==‘a’ || buf==‘s’ || buf==‘d’)
turn(buf_to_direct(buf));
else if(buf==‘q’)
{
printf("-------------Pause------------\n");
printf(“Press any button to continue…\n”);
//将键盘设置为阻塞
flag &= ~O_NONBLOCK;
fcntl(0,F_SETFL,flag);
while(1)
{
read(0,&buf,1);
flag |= O_NONBLOCK;
fcntl(0,F_SETFL,flag);
break;
}
}
is_alive = board_update();
//debug_output();
display();
speed_keep(snake.speed);
if(DIE == is_alive)
{
printf("--------------Dead-------------\n");
printf(“Score: %d!\n”, score);
printf(“Play again? (y/n)\n”);
//将键盘设置为阻塞
flag &= ~O_NONBLOCK;
fcntl(0,F_SETFL,flag);
while(1)
{
read(0,&buf,1);
if(‘n’ == buf)
{
printf("\nBye~\n");
break;
}
else if(‘y’ == buf)
goto START;
}
break;
}
if(board_width * board_high == snake.length)
{
while(1)
printf("-----卧槽牛逼!!!-----");
}
}
//恢复
system(“stty icanon”);
system(“stty echo”);
return 0;
}

标签:body,23,09,snake,board,printf,define,buf,2021
来源: https://blog.csdn.net/weixin_46211095/article/details/120438593

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有