反弹球小游戏

循环,分支和函数等知识点的运用,一些功能函数可自行百度。
编译器用的VS2019,有些方法名称可能不一样

#include<stdio.h>
#include<windows.h>
#include<conio.h>
#include<time.h>

int height = 21;		//界面尺寸
int width = 30;
int ball_x, ball_y;		//小球坐标
int ball_vx, ball_vy;	//小球移动速度
int baffle_x, baffle_y;	//挡板中心坐标
int brick_x, brick_y;	//砖块中心坐标
int baffle_radius, baffle_l, baffle_r;	//挡板半径,边界
int brick_radius, brick_l, brick_r;		//砖块半径,边界
int flag;				//游戏结束标志
int score;				//游戏得分

void hideCursor() {			//隐藏光标
	CONSOLE_CURSOR_INFO cursor_info = { 1,0 };//第二个值表示隐藏光标
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void gotoxy(int x, int y) {//光标移动到(x,y)位置
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos);
}

void start() {		//初始化
	ball_x = width / 2;
	ball_y = 1;
	ball_vx = 1;
	ball_vy = 1;
	baffle_x = width / 2;
	baffle_y = height - 1;
	baffle_radius = 3;
	baffle_l = baffle_x - baffle_radius;
	baffle_r = baffle_x + baffle_radius;
	flag = 0;
	score = 0;
	brick_y = rand()%13;
	brick_x = rand() % 24 + 2;
	brick_radius = 2;
	brick_l = brick_x - brick_radius;
	brick_r = brick_x + brick_radius;
}
void show() {			//展示界面
	int i, j;
	gotoxy(0, 0);
	for (i = 0; i <= height; i++) {
		for (j = 0; j <= width; j++) {
			if (i == ball_y && j == ball_x)
				printf("O");
			else if (j == width)
				printf("|");
			else if (i == height)
				printf("-");
			else if (i == baffle_y && j >= baffle_l && j <= baffle_r)
				printf("*");
			else if (i == brick_y && j >= brick_l && j <= brick_r)
				printf("#");
			else
				printf(" ");
		}
		printf("\n");
	}
	printf("得分:%d", score);
	Sleep(100);
}
void updateWithInput() {		
	char input;
	if (_kbhit()) {				//检测是否有输入
		input = _getch();
		switch(input) {
			case 'a':
			case 'A':
				baffle_x--;
				baffle_l = baffle_x - baffle_radius;
				baffle_r = baffle_x + baffle_radius;
				break;
			case 'd':
			case 'D':
				baffle_x++;
				baffle_l = baffle_x - baffle_radius;
				baffle_r = baffle_x + baffle_radius;
				break;
		}
		if (baffle_x == baffle_radius)
			baffle_x++;
		else if (baffle_x == width - 1 - baffle_radius)
			baffle_x--;
	}
}

void updateWithoutInput() {	
	
	if (ball_y == baffle_y - 1 && ball_x >= baffle_l && ball_x <= baffle_r)	//小球碰到挡板
		ball_vy = -ball_vy;
	else if ((ball_y == baffle_y - 1) && (ball_x < baffle_l || ball_x > baffle_r))	//小球没碰到挡板
		flag = 1;

	if (ball_y == brick_y && ball_x >= brick_l && ball_x <= brick_r) {		//小球碰到砖块
		brick_x = rand() % 29;
		brick_y = rand() % 13;
		score += 5;
		ball_vy = -ball_vy;
	}

	ball_x += ball_vx;	//根据速度改变小球坐标,使小球运动
	ball_y += ball_vy;

	if (ball_x == width - 1 || ball_x <= 0)	//小球碰到边界改变速度方向,实现小球反弹
		ball_vx = -ball_vx;
	if (ball_y <= 0)
		ball_vy = -ball_vy;
}

int main() {
	system("color 0A");
	srand((unsigned)time(NULL));		//随机种子
	hideCursor();
	start();
	int k = 1;							//游戏开始标志
	while (1) {
		if (k == 1) {
			start();
			k = 0;
		}
		else {
			gotoxy(0, 0);
			for (int i = 0; i < 10; i++) {
				printf("\n");
			}
			printf("游戏结束,最终得分:%d(按任意键继续)", score);
			_getch();
			system("cls");		//清屏
			start();
		}
		while (!flag) {
			show();
			updateWithoutInput();
			updateWithInput();
		}
	}
	return 0;
}

上一篇:利用JS的Canves实现打砖块小游戏


下一篇:KeePassXC v2.7.0-beta1 翻译文件