#include<stdio.h>
#include<windows.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
char click;
void gotoxy(short x, short y) {
COORD coord = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
typedef struct Snake
{
int x;
int y;
struct Snake *next;
}snake;
snake * head;//定義蛇頭指標;
struct Food
{
int x;
int y;
}food;
void creatmap();//創造地圖
void creatfood();//創造食物
void movebody();//移動身體
void eatfood();//吃到食物
int clickcontrol();//獲取鍵盤資訊
int Judge();//判斷游戲是否結束
void freesnake();//釋放鏈表記憶體
void gotoxyprint(int x, int y);//列印方塊
void gotoxydelet(int x, int y);//洗掉方塊
int main()
{
system("color 0B");
creatmap();
creatfood();
if (clickcontrol() ) return 0;
}
void creatmap()//創造地圖并且初設蛇頭
{
int i;
snake *p, *q;
for (i = 0; i < 50; i += 2)
{
gotoxyprint(0, i);
gotoxyprint(24, i);
}
for (i = 0; i < 24; i += 1)
{
gotoxyprint(i, 0);
gotoxyprint(i, 48);
}
head = (snake*)malloc(sizeof(snake));
p = (snake*)malloc(sizeof(snake));
q = (snake*)malloc(sizeof(snake));
head->x = 5;
head->y = 5;
p->x = 6;
p->y = 5;
q->x = 7;
q->y = 5;
head->next = p;
p->next = q;
q->next = NULL;
gotoxyprint(head->x, head->y);
gotoxyprint(p->x, p->y);
gotoxyprint(q->x, q->y);
}
void gotoxyprint(int x, int y)
{
gotoxy(y, x);
printf("一個方塊");//文章內容 不能含有以下特殊字符
}
void gotoxydelet(int x, int y)
{
gotoxy(y, x);
printf(" ");
}
void creatfood()
{
int flag = 1;
snake *l = head;
srand((int)time(0));
while (flag)
{
flag = 0;
food.x = rand() % (48 - 2 + 1) + 2;
food.y = rand() % (24 - 1 + 1) + 1;
while (1)//排除食物落到蛇身
{
if (l == NULL)
break;
if (food.x == l->x && food.y == l->y)
flag = 1;
l = l->next;
}
}
gotoxy(food.x, food.y);
printf("⊙");
}
int clickcontrol()
{
while (1)
{
if (Judge() == 0) return 0;
if (_kbhit())
click = _getch();
Sleep(150);
movebody();
eatfood();
}
return 1;
}
void movebody()
{
snake *p = head;
switch (click)
{
case 'w':head->x -= 1;
break;
case 's':head->x += 1;
break;
case 'a':head->y -= 2;
break;
case 'd':head->y += 2;
}
while (p->next)
{
p = p->next;
}
gotoxydelet(p->x, p->y);
free(p);
p = NULL;
gotoxyprint(head->x, head->y);
p = (snake*)malloc(sizeof(snake));
p->next = head;
head = p;
}
void eatfood()
{
snake *tail = (snake *)malloc(sizeof(snake)),*p=head;
if (food.x == head->x&&food.y == head->y)
{
while (p->next)
{
p = p->next;
}
p->next = tail;
tail->next = NULL;
}
}
int Judge()//判斷游戲是否結束
{
snake *p = head;
if (head->x == 0 || head->x == 50 || head->y == 0 || head->y == 25)
{
freesnake();
return 0;
}
return 0;
while (p)
{
p = p->next;
if (head->x == p->x&&head->y == p->y)
{
freesnake();
return 0;
}
}
return 1;
}
void freesnake()//釋放鏈表記憶體
{
snake *p;
while (head)
{
p = head;
head = head->next;
free(p);
}
}
uj5u.com熱心網友回復:
移動身體部分邏輯有問題,建議嘗試趙老師的單步除錯大法uj5u.com熱心網友回復:
不要依賴除錯器輸出復雜資料結構!而要將復雜資料結構的整個內容在處理它的每一步使用一小段代碼按自己很容易理解的格式輸出,非常有助于除錯!或者可以說是“基礎設施”參考下面:
資料結構對單鏈表進行資料排序 http://bbs.csdn.net/topics/392201633
uj5u.com熱心網友回復:
最明顯的是Judge()if (head->x == 0 || head->x == 50 || head->y == 0 || head->y == 25)
{
freesnake();
return 0;
}
// return 0;
// while (p)
rueturn 0是干嘛的刪了,while (p)不就死回圈了嘛。movebody()
while (p->next)
{
p = p->next;
}
有死回圈幾率,慢慢除錯吧祝你順利
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/256451.html
標籤:新手樂園
上一篇:zedgraphcontrol坐標系里怎么獲取滑鼠坐標?
下一篇:C++,有課幾何。大一新生求幫助
