【C语言小练习】CMD控制台版贪吃蛇

【C语言小练习】CMD控制台版贪吃蛇

 

 

请用VC6或者VS系列编译器编译!

 

 

 

By 蛋疼喵咪(xxMix)

好的,我可以为您提供一个简单的控制台本的贪吃蛇游戏,以下是实现步骤: 1. 首先,需要定义贪吃蛇的数据结构,包括蛇身的长度、蛇头的位置、蛇身的位置等信息。 2. 然后,需要初始化游戏界面,包括游戏面板大小、蛇头的位置、食物的位置等信息。 3. 接着,需要定义蛇的移动方式,包括蛇头的移动、蛇身的移动等。 4. 然后,需要判断蛇是否吃到食物,如果吃到食物,需要更新蛇的长度和食物的位置。 5. 最后,需要实现游戏的结束条件,包括蛇碰到边界、蛇碰到自己的身体等。 以下是C语言实现计时器控制台本的贪吃蛇代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> #include <conio.h> #include <windows.h> #define WIDTH 20 #define HEIGHT 20 #define SNAKE_HEAD '@' #define SNAKE_BODY '#' #define FOOD '$' struct Snake { int x; int y; struct Snake *next; }; struct Snake *head, *tail; int foodX, foodY; int score = 0; void gotoxy(int x, int y) { COORD pos = {x, y}; HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hOut, pos); } void printGamePanel() { int i, j; for (i = 0; i <= HEIGHT; i++) { for (j = 0; j <= WIDTH; j++) { if (i == 0 || j == 0 || i == HEIGHT || j == WIDTH) { printf("#"); } else { printf(" "); } } printf("\n"); } } void initSnake() { head = (struct Snake *)malloc(sizeof(struct Snake)); head->x = WIDTH / 2; head->y = HEIGHT / 2; head->next = NULL; tail = head; } void addBody(int x, int y) { struct Snake *newBody = (struct Snake *)malloc(sizeof(struct Snake)); newBody->x = x; newBody->y = y; newBody->next = NULL; tail->next = newBody; tail = newBody; } void printSnake() { struct Snake *p = head; while (p) { gotoxy(p->x, p->y); if (p == head) { printf("%c", SNAKE_HEAD); } else { printf("%c", SNAKE_BODY); } p = p->next; } } void createFood() { srand((unsigned)time(NULL)); foodX = (rand() % (WIDTH - 1)) + 1; foodY = (rand() % (HEIGHT - 1)) + 1; } void printFood() { gotoxy(foodX, foodY); printf("%c", FOOD); } void moveSnake(int dx, int dy) { int x = head->x + dx; int y = head->y + dy; addBody(x, y); head = head->next; } void eatFood() { if (head->x == foodX && head->y == foodY) { score++; createFood(); } else { struct Snake *p = head->next; while (p->next) { p = p->next; } free(tail); tail = p; p->next = NULL; } } int isGameOver() { if (head->x == 0 || head->x == WIDTH || head->y == 0 || head->y == HEIGHT) { return 1; } struct Snake *p = head->next; while (p) { if (head->x == p->x && head->y == p->y) { return 1; } p = p->next; } return 0; } int main() { printGamePanel(); initSnake(); createFood(); printFood(); printSnake(); int dx = 1, dy = 0; int interval = 500; while (1) { if (kbhit()) { char c = getch(); switch (c) { case 'w': dx = 0; dy = -1; break; case 's': dx = 0; dy = 1; break; case 'a': dx = -1; dy = 0; break; case 'd': dx = 1; dy = 0; break; default: break; } } moveSnake(dx, dy); eatFood(); if (isGameOver()) { gotoxy(WIDTH / 2 - 4, HEIGHT / 2); printf("Game Over!"); break; } system("cls"); printGamePanel(); printFood(); printSnake(); gotoxy(WIDTH + 2, 1); printf("Score: %d", score); Sleep(interval); } return 0; } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值