欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C语言链表实现贪吃蛇小游戏

程序员文章站 2022-03-05 22:45:25
本文实例为大家分享了c语言链表实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下项目名称:贪吃蛇小游戏运行环境:linux编程语言:c语言主要语法:链表,指针,函数备注:游戏中可选不同难度模式,1.简...

本文实例为大家分享了c语言链表实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下

项目名称:

贪吃蛇小游戏

运行环境:

linux

编程语言:

c语言

主要语法:

链表,指针,函数

备注:

游戏中可选不同难度模式,

1.简易——easy——速度慢,可穿墙,可触碰自己
2.困难——hard——速度快,不可穿墙,不可触碰自己
3.自动——auto——外挂模式,自动吃食,直到胜利

代码

贪吃蛇小游戏代码:

#include <curses.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>

#define up     1
#define down   -1
#define left   2
#define right  -2

struct snake    //define the snake
{
 int hang;
 int lie;
 struct snake *next;
};

struct snake food; //define the food
struct snake *head= null;
struct snake *tail = null;
int key;
int dir;
int set = 49;
int nub = 4;

void initfood()  //init food
{
 int x = rand()%20;
 int y = (rand()%19)+1;
 food.hang = x;
 food.lie = y;
}

void initncurse() //init ncurse
{
 initscr();
 keypad(stdscr,1); 
 noecho();
}

int hasfood(int i,int j) //judge whether shoule show the food
{
 if(food.hang == i && food.lie == j)
 {
  return 1;
 }
 return 0;
}

int hassnakenode(int i, int j) //judge whether shoule show the snake
{
 struct snake *p;
 p = head;
 while(p != null) 
 {
  if(p->hang == i && p->lie == j)
     { 
                return 1;
  }
         p = p->next;
 }
 return 0;
}

void gamepic()   //show the whole picture
{
 int hang;
 int lie;
 move(0,0);
 for(hang=0;hang<20;hang++)     //20*20 size map
 {
  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(hassnakenode(hang,lie))
    {
     printw("[]");
    }
    else if(hasfood(hang,lie))
    {
     printw("##");
    }
    else
                        printw("  ");
   }
   printw("\n");
  }
  if(hang == 19)
                {
                        for(lie=0;lie<20;lie++)
                        {
                                printw("--");
                        }
   printw("\n");
  }
 }
 printw("by haoqing, happy game!\n");
 switch(dir)
        {
                case up:
                        printw("snake up!   \n");
                        break;
                case down:
   printw("snake down! \n");
                        break;
                case left:
   printw("snake left! \n");
                        break;
                case right:
   printw("snake right!\n");
                        break;
        }
 if(nub >= 380)
 {
  printw("------------------------------------\n");
  printw("==============you win!==============\n");
         printw("------------------------------------\n");
 }
}

void addnode()   //add the node of snake
{
 struct snake *new = (struct snake *)malloc(sizeof(struct snake));
 
 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()                //init snake
{
        struct snake *p;

        dir = right;

        while(head != null)
        {
                p = head;
                head = head->next;
                free(p);
        }
        initfood();
        head = (struct snake *)malloc(sizeof(struct snake));
        head->hang = 1;
        head->lie = 1;
        head->next = null;

        tail = head;
        addnode();
        addnode();
        addnode();
}

void delenode()   //delete the node of snake   
{
 struct snake *p;
 p = head;
 head = head->next;
 free(p);
}

int ifsnakedie()  //judge whether the snake is die
{
 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()  //move the snake
{
 addnode();
 if(hasfood(tail->hang,tail->lie))
 { initfood();
  nub++;  //judge whether win
 }
 else
  delenode();
 if(set != 49 && set != 50 && set != 53)
 {
  if(ifsnakedie())
   initsnake();
 }
}

void refreshjiemian()  //refresh the picture
{
 while(1)
 {
  movesnake();
  gamepic();
  refresh();
  switch(set)
  {
   case 49:
    usleep(200000);
    break;
   case 50:
                                usleep(100000);
                                break;
   case 51:
                                usleep(100000);
                                break;
   case 52:
                                usleep(80000);
                                break;
   case 53:
    usleep(70000);
    break;
   default:
    usleep(150000); 
  }
 }
}

void turn(int direction) //prevent reverse
{
 if(set != 49 && set != 50 && set != 53)
 { if(abs(dir) != abs(direction))
  {
   dir = direction;
  }
 }
 else 
  dir = direction;
}

void changedir()  //change direction
{
 while(1)
 {
  if(set != 53)
  {
   key = getch();
   switch(key)
   {
    case key_down:
     turn(down);   //-1
     break;
    case key_up:
     turn(up);     //1
     break;
    case key_left:
     turn(left);   //2
     break;
    case key_right:
     turn(right);  //-2
     break;
   } 
  }
  else
  {
   if(tail->hang > food.hang)
    turn(up);
   else if(tail->hang < food.hang)
    turn(down);
   if(tail->lie > food.lie)
    turn(left);
   else if(tail->lie < food.lie)
    turn(right); 
  }
 }
}

int main()
{
 initncurse();

 printw("======welcome to snake eating!======\n");
 printw("------------------------------------\n"); 
 printw("please select difficulty level:\n");
 printw("\n");
 printw("   fool,please enter    \"1\"\n");
 printw("   simple,please enter  \"2\"\n");
 printw("   hard,please enter    \"3\"\n");
 printw("   hell,please enter    \"4\"\n");
 printw("   auto,please enter    \"5\"\n");
 printw("------------------------------------\n");
 printw("you want:");

 set = getch();

 pthread_t t1;
 pthread_t t2;

 initsnake();

 gamepic();

 pthread_create(&t1,null,(void *)refreshjiemian,null);
 pthread_create(&t2,null,(void *)changedir,null);

 while(1);

 endwin();
 return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关标签: C语言 贪吃蛇