C语言实现pong游戏

#include
#include
#include 
#include
#include
#include
using namespace std;
const int long_l = 90, wide_w = 40;
const int b_l = 2, b_w = 8;
const int b_v = 2;
void gotoxy(int x, int y) {
	COORD coord = { x,y };
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void hide() {
	CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
int left_b_y = 20, right_b_y = 20;
int v_x = 1, v_y = 1;
int q_x = 0, q_y = 0;
int score1 = 0, score2 = 0;
char keys;
inline void map() {
	for (int i = 0; i <= wide_w + 1; i++) {
		for (int j = 0; j <= long_l + 1; j++) {
			if (i == q_y && j == q_x) printf("O");
			else if (i == 0 || i == wide_w + 1) printf("-");
			else if (j == 0 || j == long_l + 1 || j == long_l / 2) printf("|");
			else if (i >= left_b_y && i < left_b_y + b_w && j >= 0 && j <= 1) printf("U");
			else if (i >= right_b_y && i < right_b_y + b_w && j >= long_l && j <= long_l + 1) printf("U");
			else if (i == 2 && j == 21) {
				printf("score1: %d", score1);
				if (score1 < 10) j += 8;
				if (score1 >= 10) j += 9;
			}
			else if (i == 2 && j == 61) {
				printf("score2: %d", score2);
				if (score2 < 10) j += 8;
				if (score2 >= 10) j += 9;
			}
			else printf(" ");
		}
		printf("\n");
	}
}
int main() {
	srand(static_cast<unsigned int>(time(0)));
return1:
	q_x = 41 + rand() % 10;
	q_y = 17 + rand() % 8;
	while (true) {
		if (_kbhit()) {
			keys = _getch();
			if (keys == 'w' && left_b_y != 1) left_b_y -= b_v;
			else if (keys == 's' && left_b_y != 33) left_b_y += b_v;
			else if (keys == 80 && right_b_y != 33) right_b_y += b_v;
			else if (keys == 72 && right_b_y != 1) right_b_y -= b_v;
		}
		q_x += v_x;
		q_y += v_y;
		if ((q_y >= left_b_y && q_y < left_b_y + b_w && q_x <= 2) || (q_y >= right_b_y && q_y < right_b_y + b_w && q_x >= long_l - 1)) {
			v_x = -v_x;
		}
		if (q_y <= 1 || q_y >= wide_w) v_y = -v_y;
		if (q_x == 0) {
			score2++;
			goto return1;
		}
		if (q_x == long_l + 1) {
			score1++;
			goto return1;
		}
		map();
		gotoxy(0, 0);
		hide();
	}
}

你可能感兴趣的:(C语言实现pong游戏)