打砖块(完全版。吧)

这个游戏应该是我写得最久,最用心的游戏了,虽然还很烂,但,毕竟是自己亲生的呀

get到一种新的输出方式:移动光标到指定地点,每次只需要输出一个字符

#include<iostream>
#include<cstdio>
#include<windows.h>
#include<conio.h>
#include<ctime>
using namespace std;
const int N=11,M=24;
char ma[N+5][M+5],ctl;
bool flag=1;
void hdcs()
{
    
    
	CONSOLE_CURSOR_INFO info={
    
    1,0};
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&info);
}
void print(int x,int y)
{
    
    
	COORD pos;
	pos.X=y;
	pos.Y=x;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
	putchar(ma[x][y]);
}
void setma(char c,int x,int y)
{
    
    
	ma[x][y]=c;
	print(x,y);
}
void mkmap()
{
    
    
	int b=rand()%7+8;
	for(int i=0;i<=N+1;i++)
	setma('#',i,M+1),setma('#',i,0);
	for(int i=0;i<=M+1;i++)
	setma('#',0,i);
	while(b)
	{
    
    
		int x=rand()%(N/3)+1,y=rand()%M+1;
		setma('*',x,y);
		b--;
	}
}
class Wall
{
    
    
	int x,y;
	const int si=3;
	public:
	Wall()
	{
    
    
		x=N,y=M/2;
		set(y);
	}
	void set(int y)
	{
    
    
		for(int i=max(0,y-si);i<=min(M,y+si);i++)
		setma('-',x,i);
	}
	void move(int p)
	{
    
    
		if(y+p-si<1||y+p+si>M) return;
		setma(' ',x,y-si*p);
		set(y+p);
		y+=p;
	}
}wall;
class Ball
{
    
    
	int x,y,dir,ti;
	int d[4][2]={
    
    {
    
    -1,1},{
    
    -1,-1},{
    
    1,1},{
    
    1,-1}};
	public:
	Ball()
	{
    
    
		x=N-1,y=M/2;
		dir=ti=0;
		setma('.',x,y);
	}
	void move()
	{
    
    
		ti^=1;
		if(ti) return;
		int xx=x+d[dir][0],yy=y+d[dir][1];
		if(xx>N)
		{
    
    
			flag=0;
			return;
		}
		if(yy>M||yy<1) dir^=1;
		if(xx<1||ma[xx][yy]=='-'||ma[x][yy]=='-') dir=(dir+2)%4;
		if(ma[xx][y]=='*') dir=(2+dir)%4,setma(' ',xx,y);
		else if(ma[x][yy]=='*') dir^=1,setma(' ',x,yy);
		else if(ma[xx][yy]=='*') dir=(2+dir)%4,setma(' ',xx,yy);
		setma(' ',x,y);
		x+=d[dir][0],y+=d[dir][1];
		setma('.',x,y);
	}
}ball;
int main()
{
    
    
	hdcs();
	srand(time(0));
	mkmap();
	while(flag)
	{
    
    
		Sleep(35);
		ball.move();
		if(!kbhit()) continue;
		ctl=getch();
		if(ctl=='a') wall.move(-1);
		if(ctl=='d') wall.move(1);
	}
	return 0;
}

猜你喜欢

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