liunx系统下用c语言做的贪吃蛇游戏
我在学习c语言编程之后,对于链表和结构体的用法还不太清楚,因而通过来制作这样一个小游戏来加强自己对这两个知识点的应用,同时,因为c语言的一些缺陷,所以我们还得用上ncurses的图形界面库,并使用liunx系统编程中的线程来进行编程,废话不多说,上源码。
#include<stdio.h>
#include<curses.h>
#include<stdlib.h>
#include<pthread.h>
#define UP 1
#define DOWN -1
#define LEFT 2
#define RIGHT -2
struct snake
{
int hang;
int lie;
struct snake *next;
};
struct snake *head=NULL;
struct snake *tail=NULL;
int key;
int dir;
struct snake food;
void initfood()
{
int x=rand()%21;
int y=rand()%21;
food.hang=x;
food.lie=y;
}
void addnode()
{
struct snake *new;
new=(struct snake *)malloc(sizeof(struct snake));
new->hang=tail->hang;
new->lie=tail->lie+1;
new->next=NULL;
switch(dir){
case UP:
new->hang=tail->hang-1;
new->lie=tail->lie;
break;
case DOWN:
new->hang=tail->hang+1;
new->lie=tail->lie;
break;
case LEFT:
new->hang=tail->hang;
new->lie=tail->lie-1;
break;
case RIGHT:
new->hang=tail->hang;
new->lie=tail->lie+1;
break;
}
tail->next=new;
tail=new;
}
void initsnake()
{
struct snake *p=(struct snake *)malloc(sizeof(struct snake));
dir=RIGHT;
while(head!=NULL){
p=head;
head=head->next;
free(p);
}
head=(struct snake *)malloc(sizeof(struct snake));
tail=(struct snake *)malloc(sizeof(struct snake));
head->hang=1;
head->lie=1;
head->next=NULL;
tail=head;
initfood();
addnode();
addnode();
addnode();
}
void deletenode()
{
struct snake *p=(struct snake*)malloc(sizeof(struct snake));
p=head;
head=head->next;
free(p);
}
int judge3()
{
struct snake *p;
p=head;
if(tail->hang<0||tail->lie==0||tail->hang==20||tail->lie==20){
return 1;
}
while(p->next!= NULL){
if(p->hang==tail->hang&&p->lie==tail->lie){
return 1;
}
p=p->next;
}
return 0;
}
void movesnake()
{
addnode();
if(judge2(tail->hang,tail->lie)){
initfood();
}else{
deletenode();
}
if(judge3()){
initsnake();
}
}
void turn(int direction)
{
if(abs(dir)!=abs(direction)){
dir=direction;
}
}
void* changeDir()
{
while(1){
key=getch();
switch(key){
case KEY_DOWN:
turn(DOWN);
break;
case KEY_UP:
turn(UP);
break;
case KEY_LEFT:
turn(LEFT);
break;
case KEY_RIGHT:
turn(RIGHT);
break;
}
}
}
int judge2(int i,int j)
{
if(food.hang==i&&food.lie==j){
return 1;
}else{
return 0;
}
}
int judge(int a,int b)
{
struct snake *p;
p=(struct snake*)malloc(sizeof(struct snake));
p=head;
while(p!=NULL){
if(p->hang==a&&p->lie==b){
return 1;
}
p=p->next;
}
return 0;
}
void initNcurse()
{
initscr();
keypad(stdscr,1);
noecho();
}
void gameCreat()
{
int hang;
int lie;
move(0,0);
for(hang=0;hang<20;hang++){
if(hang==0){
for(lie=0;lie<20;lie++){
printw("--");
}
printw("\n");
}
if(hang>=0 && hang<=19){
for(lie=0;lie<=20;lie++){
if(lie==0 || lie==20){
printw("|");
}else if(judge(hang,lie)){
printw("[]");
}else if(judge2(hang,lie)){
printw("##");
}
else{
printw(" ");
}
}
printw("\n");
}
if(hang==19){
for(lie=0;lie<20;lie++){
printw("--");
}
printw("\n");
printw("by the jiangwencai");
}
}
}
void* refreshInterface()
{
while(1){
movesnake();
gameCreat();
refresh();
usleep(150000);
}
}
int main()
{
pthread_t t1;
pthread_t t2;
initNcurse();
initsnake();
gameCreat();
pthread_create(&t1,NULL,refreshInterface,NULL);
pthread_create(&t2,NULL,changeDir,NULL);
while(1);
getch();
endwin();
return 0;
}
主要应用的是链表和结构体,当然像c语言的基础例如函数调用就不在赘述了,当然这里引入了线程的概念,这是为了实现两个while(1)循环同时运行而采用,另外还应用了ncurses库相关的打印函数,因为这个图形库的输入输出函数要比c标准库的函数好用,不用按回车就能响应,从而提高游戏的可玩性,现在能给大家看看编译时和运行成功后的样子。
编译时:gcc f.c -lncurse -lpthread(一定要链上ncurses和线程的库不然编译时会报错,因为有些函数编译器会不认识)
运行程序后的界面:
因为截图不能体现动态,大家可以自行运行体验游戏。