【历史遗留】存一下中学时期玩的贪吃蛇cpp源代码

【历史遗留】存一下中学时期玩的贪吃蛇cpp源代码

#include <iostream>

#include <stdio.h>
#include <stdlib.h>

#include <math.h>
#include <algorithm>
#include <climits>  // INT_MAX 2147483647

#include <string>
#include <string.h>

//#include <Winable.h>
#include <conio.h>//can getch()
#include<windows.h>
//基本型态定义。支援型态定义函数。使用者界面函数 图形装置界面函数。
#include<conio.h>	 //用户通过按键盘产生的对应操作 (控制台) 
#include<stdlib.h> 
#include<time.h> //日期和时间头文件 

using namespace std;

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

//获取标准输出的句柄 <windows.h>
//句柄 :标志应用程序中的不同对象和同类对象中的不同的实例 方便操控,
void gotoxy(int x, int y)//设置光标位置
{
     COORD pos = {x*2,y};
	//定义一个字符在控制台屏幕上的坐标POS,COORD<wincon.h>
     
    SetConsoleCursorPosition(hConsole, pos);
	//定位光标位置的函数<windows.h>
 
}

void SetColor(int a,int b=0)//设置颜色[前景a,背景b,0~15]
 {
 	int color=(a%=16)+(b%=16)*16;
     SetConsoleTextAttribute(hConsole, color);
//是API设置字体颜色和背景色的函数 格式:SetConsoleTextAttribute(句柄,颜色);
 }
/*
 Color XX : 
     Dark           Light
    0 = 黑色       8 = 灰色
    1 = 蓝色       9 = 淡蓝色
    2 = 绿色       A = 淡绿色
    3 = 湖蓝色     B = 淡浅绿色
    4 = 红色       C = 淡红色
    5 = 紫色       D = 淡紫色
    6 = 黄色       E = 淡黄色
    7 = 白色       F = 亮白色
*/

#define Func_ inline void
#define rnt register int
#define L_ 20
#define W_ L_
#define Boom_ -140
#define Apple_ -41
#define Speed_min_  5
#define Speed_max_  155

void B_out(char a[],int ms=7)
{
 int a_len=strlen(a);
 for(int i=0;i<a_len;i++)
    {
	 Sleep(ms);
	 printf("%c",a[i]);
	}
}

/* Progress Bar */
void B_load(int ms=1000,int xxx=50,char w='|',char f='>')
{
 int a=0;
 if(xxx)ms/=xxx;
 else
   {
   	ms/=100;
    for(int i=0;i<=100;i++)
       {
	    printf("[%3i%c]",i,37);
	    Sleep(ms);
	    printf("\b\b\b\b\b\b");
	   }
	return;
   }
 for(int i=0;i<xxx;i++)
 	{
	 printf("%c",w);
	 Sleep(3);
	}
 printf("<");
 B_out("  0%",10);
 for(int i=0;i<xxx;i++)
    {
     Sleep(ms);
     printf("\b\b\b\b"); 
     printf("%3i%c",++a,37);
     for(int j=i;j<xxx+5;j++)
        printf("\b");
     printf("%c",f);
     for(int j=i+1;j<xxx;j++)
        printf("%c",w);
     for(int k=1;k<100/xxx;k++)
        {
	     Sleep(ms);
         printf("<%3i%c",++a,37);
		}
    }
 printf("\n\n");
}


typedef unsigned int unt;

char cc,ccc;
unt i,j,k,				// Repeat
    x=W_/2,y=W_/2,		// Location
	z,					// Color
	l=1,				// Long
    em=W_*L_-1,
    app,
//	px=x,py=y,			// Recode
    aa,bb,a1,b1,		// Put Apple/Boom
	tt,ttt=1,			// ttt 间隔控制器 
	tm,
	score,
    level,
	faster=100,			// Speed the small the bigger
	tim;				// Growing
short Shepi='1',
      map[W_][L_],
	  SC[400];
bool guang;

Func_ Put_apple()
{
 aa=(aa+clock())%W_+1;
 a1=(a1+clock())%(W_-2)+2;
// if(em<l)
//   return;
 bb=(bb+clock())%L_+1;
 b1=(b1+clock())%(L_-2)+2;
 if(clock()-tt<ttt)
    return;
// ttt+=5;
 if(clock()%17>3)
   {
    if(map[aa][bb]>0)
      return;
    if(map[aa][bb]!=Apple_)app++;
    map[aa][bb]=Apple_;
    gotoxy(aa,bb);
//    SetColor(12);
    SetColor(rand()%5+10);
    if(clock()%3)
	  printf("¤");
	else
	  printf("◎");
   }
 else
   {
    if(map[a1][b1]!=0||abs((long)(a1-x))<3||abs((long)(b1-y))<3)
      return;
    map[a1][b1]=Boom_;
    gotoxy(a1,b1);
    SetColor(0,15);
    printf("■");
   }
 tt=clock();
 em--;
}

Func_ Eat_apple()
{
 map[x][y]=l;
 tim++;app--;
 if(tim==l)
   l++,tim=0;
 em++;
// Put_apple();
// Put_boom();
}

Func_ Build_map()
{
 Sleep(100);
 SetColor(14);
 B_out("┌");
 for(i=1;i<=W_;i++)
    B_out("┈┈");
 B_out("┐");
 for(i=1;i<=L_;i++)
    gotoxy(0,i),
	B_out("┊"),
	gotoxy(W_+1,i),
	B_out("┊");
 B_out("\n╰");
 for(i=1;i<=W_;i++)
    B_out("┈┈");
 B_out("╯");

 Eat_apple();

 return;
}

int main();
Func_ Game_over()
{
// system("cls");
 gotoxy(W_+8,2);
 SetColor(12);
 B_out("Game Over !");
 SetColor(7);
 gotoxy(W_+2,L_+1);
 B_out("Saving Data . . .\n",20);
 B_load();
 gotoxy(W_,L_+2);
 B_out("\nOne more game ? Y/N \n",20);
 while(ccc=getch())
 {
  switch(ccc)
  {
   case 'Y':case 'y':
                     for(i=1;i<=W_;i++)
                        for(j=1;j<=L_;j++)
                           map[i][j]=0;system("cls");system("C#贪吃蛇.exe");
   case 'N':case 'n':exit(0);
   default :continue;
  }
 }
}

Func_ Map_print()
{
 for(i=1;i<=W_;i++)
    for(j=1;j<=L_;j++)
       if(map[i][j]>0)
         {
	      gotoxy(i,j);
		  map[i][j]--;
	      switch(Shepi)
	      {
	       case '0': SetColor(map[i][j]%14+(map[i][j]>0),rand()*(bool)(map[i][j]>0)*guang);	break;
	       case '1': SetColor(15*(bool)(map[i][j]>0),rand()*(bool)(map[i][j]>0)*guang);		break;
	       case '2': SetColor( 8*(bool)(map[i][j]>0),rand()*(bool)(map[i][j]>0)*guang);	 	break;
	       case '3': SetColor( 9*(bool)(map[i][j]>0),rand()*(bool)(map[i][j]>0)*guang);		break;
	       case '4': SetColor(10*(bool)(map[i][j]>0),rand()*(bool)(map[i][j]>0)*guang);		break;
	       case '5': SetColor(11*(bool)(map[i][j]>0),rand()*(bool)(map[i][j]>0)*guang);		break;
	       case '6': SetColor(12*(bool)(map[i][j]>0),rand()*(bool)(map[i][j]>0)*guang);		break;
	       case '7': SetColor( 7*(bool)(map[i][j]>0),rand()*(bool)(map[i][j]>0)*guang);		break;
	       case '8': SetColor(14*(bool)(map[i][j]>0),rand()*(bool)(map[i][j]>0)*guang);		break;
	       case '9': SetColor( 6*(bool)(map[i][j]>0),rand()*(bool)(map[i][j]>0)*guang);		break;
		  }
		  printf("■"); 
//		  printf("%2i",map[i][j]);
	     }
 gotoxy(x,y);
 switch(Shepi)
	      {
	       case '0': SetColor(15,rand()*(bool)(map[i][j]>0)*guang);		break;
	       case '1': SetColor(15,rand()*(bool)(map[i][j]>0)*guang);		break;
	       case '2': SetColor( 8,rand()*(bool)(map[i][j]>0)*guang);	 	break;
	       case '3': SetColor( 9,rand()*(bool)(map[i][j]>0)*guang);		break;
	       case '4': SetColor(10,rand()*(bool)(map[i][j]>0)*guang);		break;
	       case '5': SetColor(11,rand()*(bool)(map[i][j]>0)*guang);		break;
	       case '6': SetColor(12,rand()*(bool)(map[i][j]>0)*guang);		break;
	       case '7': SetColor( 7,rand()*(bool)(map[i][j]>0)*guang);		break;
	       case '8': SetColor(14,rand()*(bool)(map[i][j]>0)*guang);		break;
	       case '9': SetColor( 6,rand()*(bool)(map[i][j]>0)*guang);		break;
		  }
 printf("◆");

 gotoxy(W_+4,L_/5);
 SetColor(11);
 printf("Score : %05i",score*5);
 gotoxy(W_+4,L_/5*2);
 SetColor(7);
 printf("Speed : %05i",(Speed_max_-faster)*200);
 gotoxy(W_+4,L_/5*3);
 SetColor(6);
 printf("Lenth : %05i",l-1);
 gotoxy(W_+4,L_/5*4);
 SetColor(5);
 printf("Times : %05i",clock()/1000);
 map[x][y]=l;
}


Func_ Wait_move()
{
 if(clock()-tm<faster)
   return;
 tm=clock();

 if(kbhit())
   {cc=getch();
    if(cc==ccc)
      faster=(faster-5)<Speed_min_?Speed_min_:faster-5;
    if(cc>='0'&&cc<='9')
      Shepi=cc;
    if(cc=='+')
	   Shepi+=Shepi<'9';
	if(cc=='-')
	   Shepi-=Shepi>'0';
	if(cc==' ')
	  guang=!guang;
   }

 if(
   ((cc=='w'||cc=='W')&&(ccc=='s'||ccc=='S'))||
   ((cc=='s'||cc=='S')&&(ccc=='w'||ccc=='W'))||
   ((cc=='a'||cc=='A')&&(ccc=='d'||ccc=='D'))||
   ((cc=='d'||cc=='D')&&(ccc=='a'||ccc=='A'))
   )
   {
    cc=ccc;
    faster=(faster+5)>Speed_max_?Speed_max_:faster+5;
   }
 switch(cc)
   {
    case 'w':case 'W':
    case 'a':case 'A':
    case 's':case 'S':
    case 'd':case 'D':
      ccc=cc;
    default :
	  cc=ccc;
	  switch(cc)
        {
         case 'w':case 'W':
           y=y>1?y-1:L_;
           break;
         case 'a':case 'A':
           x=x>1?x-1:W_;
           break;
         case 's':case 'S':
           y=y<L_?y+1:1;
           break;
         case 'd':case 'D':
           x=x<W_?x+1:1;
           break;
        }
      break;
   }
 if((map[x][y]==Boom_||map[x][y]>0)&&cc)
   Game_over();
 if(map[x][y]==Apple_)
   Eat_apple(),score++;
 Map_print();
}

int main()
{
  // B_load();
	i=j=k=0,
    x=W_/2,y=W_/2,
	l=1,
    em=W_*L_-1,
    app=0,
	tt=0,ttt=1000,
	tm=0,
	score=0,
    level=0,
	faster=100,
	tim=0;
	cc=0;
 system("title C#Snake.Game% tech_by_041");
 Build_map();
 Wait_move();
 Put_apple();
 while(!kbhit());
do{

 Wait_move();

 Put_apple();
// Put_boom();
}while(true);
 return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值