C語言 貪吃蛇

2021-09-24 04:18:39 字數 4251 閱讀 7742

利用鍊錶儲存貪吃蛇座標,利用游標跳轉列印覆蓋貪吃蛇。

#include

"stdio.h"

#include

"stdlib.h"

#include

"conio.h"

#include

"windows.h"

#include

"time.h"

void

enter()

;//進入介面

void

over()

;//結束介面

void

bawn()

;//圍牆列印

void

gotoprintf

(int x,

int y)

;//跳轉列印

void

gotoxy

(int x,

int y)

;//游標跳轉

void

gotodelete

(int x,

int y)

;//跳轉刪除

void

producefood()

;//產生食物

intgitdirection()

;//獲取方向

intjudge()

;//遊戲結束判斷

void

movesnake()

;//蛇移動

void

eat();

//蛇吃到食物後的變化

void

changexy

(int a,

int b)

;//蛇的座標的變化

void

setcolor

(unsigned

short forecolor,

unsigned

short backgroundcolor)

;//控制前景、背景色

typedef

struct snake//蛇身

snake;

struct food

;snake *head=

null

,*tail=

null

;//蛇的頭尾

food food;

//食物位置

int score =0;

//分數

int click=1;

//方向

int us;

//停頓時間

intmain()

void

enter()

//進入

setcolor(4

,0);

printf

("\n載入完成,即將進入...");

sleep

(2000);

system

("cls");

gotoxy(20

,0);

for(i=

0;i<40;

++i)

printf

("*");

gotoxy(20

,1);

for(i=

0;i<40;

++i)

printf

("*");

gotoxy(30

,3);

printf

("任意鍵進入遊戲");

gotoxy(30

,4);

printf

("方向鍵控制");

gotoxy(20

,6);

for(i=

0;i<40;

++i)

printf

("*");

gotoxy(20

,7);

for(i=

0;i<40;

++i)

printf

("*");

gotoxy(20

,8);

system

("pause");

}void

over()

//結束介面

void

bawn()

//圍牆列印

for(i=

1;i<26;

++i)

gotoxy(63

,15);

printf

("分數:");

gotoxy(77

,15);

printf

("%d"

,score)

; head =

(snake *

)malloc

(sizeof

(snake));

head->x=16;

head->y=15;

tail =

(snake *

)malloc

(sizeof

(snake));

snake *p=

(snake *

)malloc

(sizeof

(snake));

snake *q=

(snake *

)malloc

(sizeof

(snake));

p->x=16;

q->x=16;

p->y=16;

q->y=17;

head->next=p;

p->next=q;

q->next=tail;

tail->x=16;

tail->y=

18;

tail->next=

null

;gotoxy(16

,14);

printf

("頭");

}void

gotoprintf

(int x,

int y)

//跳轉列印

void

gotodelete

(int x,

int y)

//跳轉刪除

void

producefood()

//產生食物

judge=judge->next;

}gotoxy

(food.x,food.y)

;printf

("⊙");

}int

gitdirection()

//獲取方向

movesnake()

;eat()

;}return1;

}int

judge()

//遊戲結束判斷

snake *p=head->next;

while(1

) p=p->next;

}return1;

}void

movesnake()

//蛇移動

switch

(click)

p=head;

while(1

) p=head;

gotoxy(0

,28);

if(count<

10) us=

150;

else

if(count>=

10&&count<

20) us =

100;

else us =50;

sleep

(us);}

void

eat(

)//蛇吃到食物後的變化

p->next=_new;

_new->next=tail;

score+=10

;gotoxy(77

,15);

printf

("%d"

,score);}

}void

changexy

(int a,

int b)

//蛇的座標的變化

p=head->next;

p->x=a;

p->y=b;

}void

gotoxy

(int x,

int y)

//游標跳轉

void

setcolor

(unsigned

short forecolor,

unsigned

short backgroundcolor)

//控制前景、背景色

C語言貪吃蛇

include include include include include include define screen width 40 遊戲螢幕寬度 define screen length 15 遊戲螢幕長度 define start x 16 螢幕起始x座標 define start y ...

C語言 貪吃蛇

include include include define framewidght 22 define frameheight 22 short generaterandnumber short a,short b void posconsolecursor short x,short y voi...

C語言貪吃蛇

親測devc 編譯器完美執行 define crt secure no warnings include include include include void readini file fphead,int score,char argv 建立或開啟乙個和執行檔案對應的ini檔案,讀取最高紀錄 ...