zhang_game 发表于 2015-2-1 16:54:19

[VC] 贪吃蛇源码,要的来拿呗

/*********************
*****最简单贪吃蛇****
-------------------------------------
****by_狂奔小兔****
********************/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define H 15
#define W 30
#define N 10
void move();
void start();
void draw();
void getturn();
void turn(char direction);
void judge();
void getfood();
void over();
int a, b, x, y, l = 0, n, i, j;
int x1, y1;
char *head, *tail, *tt, map;
char *belly, *food;
char left, right, front, back;
int main()
{
         start();
}

void start()
{
         for (i = 0; i != H; ++i)
               for (j = 0; j != W; ++j)
               {
                         if ((i == 0) || (i == H - 1) || (j == 0) || (j == W - 1))
                                 map = '9';
                         else
                                 map = '0';
               }
         x = W / 2, y = H / 2;
         head = &map;
         belly = &map;
         tail = &map;
         *head = '1';
         *belly = '2';
         *tail = '3';
         left = 'w', right = 's';
         front = 'd', back = 'a';
         turn(front);
         getfood();
               draw();
         while (1)
         {
               usleep(1000000/N);
               getturn();
               move();
               judge();
               clrscr();
               draw();
         }
}

void getturn()
{
         char t, t1;
         while (kbhit())
         {
               t = getch();
               if (t == left)
               {
                         turn(left);
                         left = back;
                         back = right;
                         right = front;
                         front = t;
                         goto gett;
               }
               else if (t == right)
               {
                         turn(right);
                         right = back;
                         back = left;
                         left = front;
                         front = t;
                         goto gett;
               }
         }
         turn(front);
   gett:;
         while (kbhit())
               t = getch();
}

void getfood()
{
         do
         {
               x1 = random() % (W - 2) + 1;
               y1 = random() % (H - 2) + 1;
         }
         while (map != '0');
         food = &map;
         *food = '4';
}

void turn(char direction)
{
         switch (direction)
         {
         case 'w':
               --y;
               break;
         case 's':
               ++y;
               break;
         case 'a':
               --x;
               break;
         case 'd':
               ++x;
               break;
         }

}

void move()
{
         tt = belly;
         for (i = l; i != 0; --i)
               belly = belly;
         belly = head;
         head = &map;
         *belly = '2';
}

void judge()
{
         switch (*head)
         {
         case '2':;
         case '9':
               over();
               break;
         case '4':
               getfood();
               ++l;
               belly = tt;
               break;
         case '0':
               *head = '1';
               *tail = '0';
               tail = tt;
               *tail = '3';
               break;
         }
}

void draw()
{
         for (i = 0; i != H; ++i)
         {
               for (j = 0; j != W; ++j)
                         switch (map)
                         {
                         case '0':
                                 printf(" ");
                                 break;
                         case '9':
                                 printf("#");
                                 break;
                         case '1':
                                 printf("O");
                                 break;
                         case '2':
                                 printf("o");
                                 break;
                         case '3':
                                 printf(".");
                                 break;
                         case '4':
                                 printf("♥");
                                 break;
                         }
               printf("\n");
         }
}

void over()
{
         printf("\tGame over!!!\n");
         printf("\t\tAgain?\n");
         getch();
         clrscr();
         start();
}

zhuzhuli 发表于 2015-3-14 05:38:33

LZ帖子不给力,勉强给回复下吧

unchenchao 发表于 2015-4-8 02:15:00

介是神马?!!

355615517 发表于 2015-4-11 22:34:08

好好的帖子,必须得顶起

Kellyma 发表于 2015-6-25 08:02:51

着玩意还是很有用处的。。。

啊冲 发表于 2015-6-30 07:30:47

好像现在 越来越给力了   呵呵

嗷嗷叫的老马 发表于 2015-7-19 14:57:05

非常感谢楼主分享
页: [1]
查看完整版本: [VC] 贪吃蛇源码,要的来拿呗