放假闲着没事在写了个没有模块的贪吃蛇,bug有亿点多,难得改了就这样.
代码如下:
#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<conio.h>
#pragma warning (disable: 4996)
void gotoxy(int x, int y);
void HideCursor();
void UI();
struct snake {
int x;
int y;
int len;
};
struct body {
int x;
int y;
} sn[100];
struct food {
int x;
int y;
};
int main() {
UI();
int t = 200;
struct snake s;
struct body sn[100];
struct food f;
s.len = 3;
char ch;
s.x = 50;
s.y = 10;
sn[0].x = 50;
sn[0].y = 10;
sn[1].x = 49;
sn[1].y = 10;
sn[2].x = 48;
sn[2].y = 10;
for (int i = 0; i < s.len; i++) {
gotoxy(sn[i].x, sn[i].y);
putch('*');
}
f.x = rand()%50+2;
f.y = rand()%20+2;
gotoxy(f.x, f.y);
putch('@');
while (1) {
HideCursor();
ch = getch();
if (f.x == s.x && f.y == s.y) {
s.len++;
for (int i = s.len - 1; i > 0; i--) {
sn[i].x = sn[i - 1].x;
sn[i].y = sn[i - 1].y;
}
sn[0].x = f.x;
sn[0].y = f.y;
f.x = rand() % 50 + 2;
f.y = rand() % 20 + 2;
gotoxy(f.x, f.y);
putch('@');
}
if (s.x == 0 || s.y == 0 || s.x == 80 || s.y == 30) {
gotoxy(40, 10);
printf("游戏结束");
break;
}
if (ch == &