贪吃蛇 c++结构体版

最近整理老旧的笔记本文件盘,意外发现大一时候学c/c+时候写的贪吃是黑窗体小游戏。记录一下,当作纪念

#include<iostream>
#include<windows.h>
#include<ctime>
#define HUASHE cout<<"*" 
using namespace std;
void position(int x,int y);
int jieshou();
void huaqiang(); 
void game() ;
typedef struct Sheshenti
{
	int x;
	int y;
	Sheshenti *next;
	Sheshenti *last;
}she;
int main()
{
	loop:int a;
		position(3,27);
		cout<<"欢迎进入贪吃蛇小游戏";
		position(7,33);
		cout<<"F1 开始游戏";
		position(8,33);
		cout<<"F2 退出游戏";
	a=jieshou();
	if(a==1)
	{
		system("cls");
		huaqiang();//围墙 
		game();
		goto loop;
	}
	else
	{
		system("cls");
		position(7,33);
		cout<<"再见";
		position(11,40);
		cout<<"刘江制造"<<endl; 
	}	
	return 0;	 
} 
void position(int x,int y)
{
	COORD pos={y,x};
	HANDLE Out=GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(Out,pos);
}
int jieshou()
{
	while(1)
	{
		if(GetAsyncKeyState(VK_F1)) 
			return 1;			
		if(GetAsyncKeyState(VK_F2)) 
			return 2; 
	}
} 
void huaqiang()
{
	for(int i=0;i<=30;i++)
	{
		position(i,0);
		cout<<"*";
		position(i,30);
		cout<<"*";
		position(0,i);
		cout<<"*";
		position(30,i);
		cout<<"*";
	}
} 
 int fangxiang(int s)
 {
 	while(1)
 	{
 		if(GetAsyncKeyState(VK_UP)&&s!=4)
		return 3;
		if(GetAsyncKeyState(VK_DOWN)&&s!=3)
		return 4;
		if(GetAsyncKeyState(VK_LEFT)&&s!=1)
		return 2;
		if(GetAsyncKeyState(VK_RIGHT)&&s!=2)
		return 1;
	 }
 }
she* shetou()
{
 	she *L1=new she,*L2=new she, 
	*L3=new she,*L4=new she;
	L1->next=L2,L2->next=L3,
	L3->next=L4,L4->next=NULL; 
	L1->last=NULL,L2->last=L1,
	L3->last=L2,L4->last=L3;
	L1->x=5,L1->y=8;
	L2->x=5,L2->y=6;
	L3->x=5,L3->y=4;
	L4->x=5,L4->y=2;
	return L1; 
} 
void game()
{
	int n=0;
	int s=4,g=1;
	she *q,*head,*ahead;
	head=shetou();
	int a,b;
	do{
		srand(time(0));
		a=(rand()%30)/2*2;
		b=(rand()%30)/2*2;
	}while(a==0||b==0);
	position(a,b);
	cout<<"*";
	while(1)
	{
		she *head3=new she;
		position(30,7);
		cout<<"分数:"<<n;
		if(g==0)
		{
			loop:srand(time(0));
			a=(rand()%30)/2*2;
			b=(rand()%30)/2*2;
			if(a==0||b==0)
			 goto loop;
			position(a,b);
			cout<<"*";
			g=1;
		}
		she *head2=head;
		while(head!=NULL)
		{
			position(head->x,head->y),HUASHE;
			if(head->next==NULL)
			{
				position(head->x,head->y);
				cout<<" ";
			} 
			 head=head->next;
		}
		if(n<100)
		Sleep(120);
		else if(n>=100&&n<200)
		Sleep(100);
		else if(n>=200&&n<300)
		Sleep(80);
		else if(n>=300&&n<500)
		Sleep(60);
		else 
		Sleep(50);
		if(GetAsyncKeyState(VK_UP)&&s!=2)
		s=1;
		if(GetAsyncKeyState(VK_DOWN)&&s!=1)
		s=2;
		if(GetAsyncKeyState(VK_LEFT)&&s!=4)
		s=3;
		if(GetAsyncKeyState(VK_RIGHT)&&s!=3)
		s=4;
		head=head2;
		while(head->next!=NULL)
		{
			head=head->next;
		}
		while(head->last!=NULL)
		{
			head->x=head->last->x;
			head->y=head->last->y;
			head=head->last;
		}
		if(s==1)
			head->x-=1;
		if(s==2)
			head->x+=1;
		if(s==3)
			head->y-=2;
		if(s==4)
			head->y+=2;
		position(31,7);
		cout<<a<<"\t"<<b<<endl;
		position(32,7);
		cout<<head->x<<"\t"<<head->y<<endl;
		she *ahead=head;	
		if(head->x==a&&head->y==b)
		{
			while(head->next!=NULL)
			{
				head=head->next;
			}
			head3->last=head;
			head->next=head3;
			head3->next=NULL;
			head3->x=head->x;
			head3->y=head->y;
			g=0;
			n+=10;
		}
		if(head->x==0||head->x==30||head->y==0||head->y==30)
		{
			system("cls");
			position(3,29);
			cout<<"你撞到墙了,游戏结束!!"<<endl;
			position(5,29);
			cout<<"得分:"<<n; 
			getchar();
			system("cls");
			return ;
		}
		position(35,7);
		cout<<head->x<<" "<<head->y<<endl;
		head=ahead;
		while(ahead->next!=NULL)
		{
			if(ahead->next->x==head->x&&ahead->next->y==head->y)
			{
				system("cls");
				position(3,27);
				cout<<"在贪吃吃自己啊,你真是人才!!";
				position(5,29);
				cout<<"得分:"<<n;
				getchar();
				system("cls"); 
				return ;
			}
			ahead=ahead->next;
		}
	} 	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值