c语言贪吃蛇prev.next,38行C代码实现贪吃蛇

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

实现仅仅使用了39行,其中条件表达式还断了几行。有撞墙死亡,食物不会刷在身上

源码:

dea53bb3befdcaf89aae2379cb03e332.png

使用效果图:

bdbc850a1f3072a67c8778d857fa265a.png

死亡:

4f89f22384a4db032c25f094f55f4c26.png

文字版源码:

#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");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值