躲石头

很简单的躲避石头,但后期可以用它做模板改成打飞机之类的大游

实话说,我发现指针和链表是真香

#include<cstdio>
#include<conio.h>
#include<windows.h>
#include<ctime>
#include<iostream>
using namespace std;
const int N=15,M=20,T=20;
char m[2*N][2*M],ctl;
int dir[4][2]={
    
    {
    
    -1,0},{
    
    1,0},{
    
    0,-1},{
    
    0,1}},t=T;
bool flag;
void mkmap()
{
    
    
	for(int i=0;i<=N+1;i++)
	m[i][0]=m[i][M+1]='#';
	for(int i=0;i<=M+1;i++)
	m[0][i]=m[N+1][i]='#';
}
void print()
{
    
    
	system("cls");
	for(int i=0;i<=N+1;i++)
	{
    
    
		for(int j=0;j<=M+1;j++)
		putchar(m[i][j]);
		putchar('\n');
	}
}
class ship
{
    
    
	public:
	int x,y;
	ship()
	{
    
    
		x=N;
		y=M/2;
		m[x][y]='!';
	}
	void fly(int d)
	{
    
    
		int xx=x+dir[d][0],yy=y+dir[d][1];
		if(m[xx][yy]!='#') 
		{
    
    
			m[x][y]=' ';
			x=xx,y=yy;
			m[x][y]='!';
		}
	}
};
ship plr;
class stone
{
    
    
	public:
	int x,y;
	stone* pre;
	stone* ne;
	bool death;
	stone()
	{
    
    
		y=rand()%M+1,x=1;
		death=0;
		ne=NULL,pre=NULL;
	}
	void fall()
	{
    
    
		m[x][y]=' ';
		x++;
		if(x>N) death=1;
		else m[x][y]='.';
	}
	void check()
	{
    
    
		if(x==plr.x&&y==plr.y)
		flag=1;
	}
};
stone* head=new stone;
stone* tail=new stone;
void fall(stone* he)
{
    
    
	if(he&&he!=tail)
	{
    
    
		he->fall();
		if(he->death)
		{
    
    
			stone* p=he->ne;
			he->pre->ne=p;
			p->pre=he->pre;
			delete he;
			fall(p);
		}
		else
		{
    
    
			he->check();
			fall(he->ne);
		}
	}
}
void create()
{
    
    
	int k=rand()%T?0:1;
	t=max(t-k,1);
	int pd=rand()%t;
	if(!pd)
	{
    
    
		tail->ne=new stone;
		tail->ne->pre=tail;
		tail=tail->ne;
	}
}
int main()
{
    
    
	mkmap();
	head->ne=tail;
	tail->pre=head;
	srand(time(0));
	double st=clock();
	while(!flag)
	{
    
    
		print();
		create();
		fall(head->ne);
		Sleep(50);
		if(!kbhit()) continue;
		ctl=getch();
		if(ctl=='w') plr.fly(0);
		if(ctl=='s') plr.fly(1);
		if(ctl=='a') plr.fly(2);
		if(ctl=='d') plr.fly(3);
	}
	double ed=clock();
	system("cls");
	printf("Game Over!\n");
	printf("Your point is %d",int((ed-st)/1000));
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/weixin_45383207/article/details/110257679