用C++实现打砖块游戏

上课摸鱼摸了一整节课的程序…
可以粗糙实现打砖块游戏

/*
Maker:XerDazzle
新版改良:
1,砖块不穿模
2,边界限制
3,五行砖块
4,生命限制
5,不报错
-----------
1,加入主界面
2,速度随砖块数变化
*/
#include 
#include 
#include 
#include 
#define Framelen 80
#define Framedep 25
#define Pi 3.14
double xx = 5,yy = 15;
int x,y,bx = 2,drct = 20,bnum = 40,hp = 8;
bool end = 0;
int brick[5][8] = {{1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1}};
void setpos(int x,int y){
    COORD pos = {x,y};
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hOut, pos);
}
void drctmove(int speed,int drct){
	//setpos(0,0);
	//printf("%d,%d",xx,yy);
	xx += (cos((Pi/180)*drct))*speed;
	yy -= (sin((Pi/180)*drct))*speed;
	x = int(xx);y = int(yy);
	
}
void drawsq(int x1,int y1,int x2,int y2,char f){
	for(int i = x1;i <= x2;i ++){
		setpos(i,y1);printf("%c",f);
	}
	for(int i = x1;i <= x2;i ++){
		setpos(i,y2);printf("%c",f);
	}
	for(int i = y1;i <= y2;i ++){
		setpos(x1,i);printf("%c",f);
	}
	for(int i = y1;i <= y2;i ++){
		setpos(x2,i);printf("%c",f);
	}
}
void drawbrick(){
	for(int i = 0;i < 5;i ++){
		for(int j = 0;j < 8;j ++){
			if(brick[i][j]){
				for(int k = 1;k <= 8;k ++){
					setpos(j*10+k+1,i*2+2);
					printf("^");
				}
			}
			else{
				for(int k = 1;k <= 8;k ++){
					setpos(j*10+k+1,i*2+2);
					printf(" ");
				}
			}
		}
	}
}
void drawboard(char f){
	for(int i = bx;i <= bx+8;i ++){
		setpos(bx+i,Framedep-2);
		printf("%c",f);
	}
}
void collidetest(){
	bool brk = 0;
	if(x == 2 || x == Framelen-2){
		drct = 180-drct;
	}
	else if(y == 2){
		drct *= -1;
	}
	else if(y == Framedep - 3){
		for(int i = bx-1;i <= bx+9;i ++){
			if(bx+i == x){
				drct *= -1;
				break;
			}
		}
	}
	else if(y == Framedep-1){
		//gameend();
		drct = 20;
		xx = 5;
		yy = 15;
		hp --;
    }
	else if(y == 11){
		for(int i = 0;i < 8;i ++){
			for(int j = 0;j <= 10;j ++){
				if(x == i*10+j && brick[4][i] == 1){
					brick[4][i] = 0;
					drawbrick();
					drct *= -1;
					brk = 1;
					bnum --;
					break;
				}
			}
			if(brk)
				break;
		}
	}
	else if(y == 9){
		for(int i = 0;i < 8;i ++){
			for(int j = 0;j <= 10;j ++){
				if(x == i*10+j && brick[3][i] == 1){
					brick[3][i] = 0;
					drawbrick();
					drct *= -1;
					brk = 1;
					bnum --;
					break;
				}
			}
			if(brk)
				break;
		}
	}
	else if(y == 7){
		for(int i = 0;i < 8;i ++){
			for(int j = 0;j <= 10;j ++){
				if(x == i*10+j && brick[2][i] == 1){
					brick[2][i] = 0;
					drawbrick();
					drct *= -1;
					brk = 1;
					bnum --;
					break;
				}
			}
			if(brk)
				break;
		}
	}
	else if(y == 5){
		for(int i = 0;i < 8;i ++){
			for(int j = 0;j <= 10;j ++){
				if(x == i*10+j && brick[1][i] == 1){
					brick[1][i] = 0;
					drawbrick();
					drct *= -1;
					brk = 1;
					bnum --;
					break;
				}
			}
			if(brk)
				break;
		}
	}
	else if(y == 3){
		for(int i = 0;i < 8;i ++){
			for(int j = 0;j <= 10;j ++){
				if(x == i*10+j && brick[0][i] == 1){
					brick[0][i] = 0;
					drawbrick();
					drct *= -1;
					brk = 1;
					bnum --;
					break;
				}
			}
			if(brk)
				break;
		}
	}
}
int main(){
	system("color 0E");
	setpos(Framelen/2,0);
	printf("按下空格开始游戏!");
	setpos(Framelen/2,2);
	printf("A/D键操控反弹板");
	setpos(Framelen/2,4);
	printf("Maker : XER DAZZLE");
	char key;
	while(1){
		key = getch();
		if(key == ' '){
		system("cls");
		break;
		}
	}
	drawsq(1,1,Framelen,Framedep,'*');
	drawbrick();
	drawboard('-');
	while(1){
		setpos(x,y);printf(" ");
		drctmove(1,drct);
		setpos(x,y);printf("〇");
		collidetest();
		Sleep(20+bnum);
		if(kbhit()){
			key = getch();
			if(key == 'a' || key == 'd'){
				drawboard(' ');
					switch(key){
					case 'a':{
						if(bx >= 2)
							bx --;
						break;
					}
					case 'd':{
						if(bx <= 34)
							bx ++;
						break;
					}
				}
				drawboard('-');
			}
		}
 		if(hp == 0){
 			system("cls");
 			setpos(0,0);
			printf("Failed!");
  			setpos(0,1);
			printf("Maker:XerDazzle");
			return 0;
		}
		if(bnum == 0){
			system("cls");
 			setpos(0,0);
			printf("Success!");
  			setpos(0,1);
			printf("Maker:XerDazzle");
			return 0;
		}
		setpos(0,0);
		printf("HP:%i",hp);
	}
	return 0;
}

你可能感兴趣的:(C++成果展示)