首页 > 代码库 > 贪吃蛇,c,c++

贪吃蛇,c,c++

学了c++半个学期了,自己动手做了一个游戏。(有什么不好的地方,欢迎指出<img alt="大笑" src=http://www.mamicode.com/"http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif" />)
#include <windows.h>
#include <conio.h>
#include <ctime>
#include <iostream>
#include <cmath>
#include <mmsystem.h>
#include<stdio.h>
#include <stdlib.h>
# pragma comment (lib, "Winmm.lib")//导入Winmm.lib库 
using namespace std;       
int node; //蛇的节数
int life; //生命,0活着,1死亡
int Q;//控制食物颜色
int fa,fb;//食物坐标
int q;//控制上下左右
int ti=0,ti1=0;//time
int nd=0;//难度
char k;
int a[4][2]={{-1,0},{1,0},{0,-1},{0,1}}; // 上下左右
struct food
{
	int f1;
	int f2;
	int need;
}f;
struct she
{
	int x;
	int y;
}s[1000],t;
HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE); //光标定位 
COORD coord;  

void hide() //隐藏光标 
{  
	CONSOLE_CURSOR_INFO cursor_info={1,0};  
	SetConsoleCursorInfo(hout, &cursor_info);  
}  

void gotoxy(int x, int y)
{
	CONSOLE_SCREEN_BUFFER_INFO cs;
	HANDLE hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
	GetConsoleScreenBufferInfo(hConsoleOut, &cs);
	cs.dwCursorPosition.X = y;
	cs.dwCursorPosition.Y = x;
	SetConsoleCursorPosition(hConsoleOut, 
		cs.dwCursorPosition);
}
void color(int a)//颜色函数
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void weiqiang(void)//围圈
{
	color(14);
	int a=25,b=20,c=25,d=20;
	while(a--)
	{
		cout << '-';
	}
	cout << endl;
	while(b--)
	{
		cout << '|';
		cout << endl;
	}
	while(d--)
	{
		gotoxy(20-d,24);
		cout << '|';
		cout << endl;
	}
	while(c--)
	{
		cout << '-';
	}
	cout << endl;
	gotoxy(1,1);
}
void food1(void)//食物
{
	f.f1=5;
	f.f2=5;
	int i;
	srand((unsigned)time(NULL)); 
	while (1)
	{
loop:f.f1=rand()%19;//随机数
		f.f2=rand()%24;
		for (i=0;i<node;i++)
		{
			if (f.f1==s[i].x||f.f2==s[i].y)
			{
				goto loop;
			}
		}
		if (f.f1==0||f.f2==0)//防止超出框框
		{
			continue;
		} 
		else
		{
			break;
		}
	}
	if(f.need==0)
	{
		fa=f.f1;fb=f.f2;//f.f1,f.f2赋值给全局变量
		gotoxy(f.f1,f.f2);
		while (1)
		{
			Q=rand()%16;
			if (Q>9)
			{
				break;
			}
		}
		color(Q);
		cout << "*";
	}
}
int died(void)//判断死亡
{
	int i,flag=0;
	for (i=1;i<node;i++)
	{
		if (s[0].x==s[i].x&&s[0].y==s[i].y)
		{
			flag=1;
		}

	}
	if ((s[0].x==0||s[0].x==21||s[0].y==0||s[0].y==24)||flag==1)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}
void tyd(char c)
{
	if (abs(c-k)!=3&&abs(c-k)!=4&&abs(c-k)!=0)//控制蛇头走身体后面
	{
		if(c=='a')
		{
			q=2;
		}
		else if (c=='s')
		{
			q=1;
		}
		else if (c=='w')
		{
			q=0;
		}
		else if (c=='d')
		{
			q=3;
		}
	}
	s[0].x+=a[q][0];
	s[0].y+=a[q][1];
}
void yd(void)//移动方向
{
	char c='d';
	int i,a=200;
	s[0].x=1;
	s[0].y=1;
	node=2;
	do
	{
		ti+=1;//必须将其重新归0,不然游戏会出错
		ti1+=ti;
		if (ti>1000)
		{
			ti=0;
		}
		if (ti%150==0)
		{
			if (a>25)
			{
				nd++;
				a-=25;
			}
		}
		food1();
		t.x=s[node-1].x;//保存最后一个*
		t.y=s[node-1].y;
		for (i=node-1;i>0;i--)//身体移动
		{
			s[i].x=s[i-1].x;
			s[i].y=s[i-1].y;
		}
		for (i=1;i<node;i++)
		{
			color(13);
			gotoxy(s[i].x,s[i].y);
			cout << "*" ;
		}
		gotoxy(t.x,t.y);//消去最后那个*
		cout << " ";
		if (_kbhit())//接受键盘输入的上下左右,并以此改变方向
		{
			k=c;
			c=_getch();
		}
		tyd(c);//移动
		gotoxy(s[0].x,s[0].y);//头移动
		cout << "#";
		if (s[0].x==fa&&s[0].y==fb)//控制食物每次只能有一个
		{
			f.need=0;
			node++;
		}
		else
		{
			f.need=1;
		}
		Sleep(a);
		if(died()==0)//判断是否
		{
			system("cls");//清屏
			break;
		}
		color(12);
		gotoxy(22,0);
		cout << "食物统计: " << node-2 ;
		gotoxy(1,27);
		color(11);
		cout << "游戏难度: " << nd;
	}while(1);
}
void jieshu(void)
{
	color(12);
	cout << "花费时间:" << ti1/60 << " s" << endl;
	cout << "    o(╥﹏╥)o      o(╥﹏╥)o       o(╥﹏╥)o  " << endl;
	cout << " ----------------------------------------------- " << endl;
	cout << "|                   You lose!                   |" << endl;
	cout << " ----------------------------------------------- " << endl;
}
int main()
{
	PlaySound (TEXT("D:\\新建文件夹 (3)\\张学友-用余生去爱.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);//循环播放
	//SND_ASYNC | SND_NODEFAULT (单次播放)
	while (1)
	{
		weiqiang();
		yd();
		jieshu();
		system("pause");
		break;
	}
	return 0;
}

贪吃蛇,c,c++