反彈球小遊戲

循環,分支和函數等知識點的運用,一些功能函數可自行百度。
編譯器用的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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章