该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
实现仅仅使用了39行,其中条件表达式还断了几行。有撞墙死亡,食物不会刷在身上
源码:
使用效果图:
死亡:
文字版源码:
#include
#include
#include
#include
enum {NONE = 0, FOOD = 1, HEAD = 2, BODY = 3, UP = 9, DOWN = 11, LEFT = 6, RIGHT = 14, WIDTH = 50, HEIGHT = 50, SPEED = 10, CLOCK = 50};
static struct {char type, next; } panel[HEIGHT][WIDTH] = {0};
int main()
{
struct {short x, y;} head = {WIDTH / 2, HEIGHT / 2}, next = {0, 0}, record_prev;
int move = UP, auto_move, key, i, j, randxy =
(int)(panel[HEIGHT * 3 / 4][WIDTH * 3 / 4].type = FOOD) + (int)(panel[head.y][head.x].next = 0) + (int)(panel[head.y][head.x].type = HEAD);
while (1) {
for (auto_move = SPEED; auto_move; auto_move--)
if ((Sleep(CLOCK), 1) && kbhit() && (int)(key = getch()))
if ((key == 'a' && move != RIGHT && (int)(move = LEFT))
|| (key == 'd' && move != LEFT && (int)(move = RIGHT))
|| (key == 's' && move != UP && (int)(move = DOWN))
|| (key == 'w' && move != DOWN && (int)(move = UP))) break;
randxy = (next.x = head.x + move / 4 - 2, next.y = head.y + move % 4 - 2, 0);
if (next.x < 0 || next.x >= WIDTH || next.y < 0 || next.y >= HEIGHT)
return (printf("\n你输了!\n"), getchar(), 0);
randxy = (panel[next.y][next.x].next = move == LEFT ? RIGHT : move == RIGHT ? LEFT : move == UP ? DOWN : UP, (int)(panel[head.y][head.x].type = BODY));
if (panel[next.y][next.x].type == FOOD && (int)(panel[next.y][next.x].type = HEAD))
while ((int)(randxy = rand() % (WIDTH * HEIGHT)) + 1
&& (panel[randxy / WIDTH][randxy % WIDTH].type != NONE
|| (int)(panel[randxy / WIDTH][randxy % WIDTH].type = FOOD) != FOOD));
else if ((int)(panel[next.y][next.x].type = HEAD))
for (record_prev = next;
panel[head.y][head.x].next != 0 || (int)(panel[record_prev.y][record_prev.x].next = 0) || (int)(panel[head.y][head.x].type = NONE);
record_prev = head, i = head.x, j = head.y, head.x += panel[j][i].next / 4 - 2, head.y += panel[j][i].next % 4 - 2);
head = next;
system("cls");
for (i = 0; i <= HEIGHT; i++)
for (j = 0; j <= WIDTH + 1; j++)
printf("%s", j == (WIDTH + 1) ? "@\n" : i == HEIGHT || j == 0 || i == 0 ? "@" :
panel[i - 1][j - 1].type == FOOD ? "*" : panel[i - 1][j - 1].type == HEAD ? "O" : panel[i - 1][j - 1].type == BODY ? "o" : " ");
printf("W:上 S:下 A:左 D:右\n");
}
}