躲石头

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

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

#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;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值