```
#include
#include
#include
#include
#include
#define BOARD_WIDTH 20 // 跳板寬度
#define BOARD_HEIGHT 2 // 跳板高度
#define SCREEN_WIDTH 80 // 屏幕寬度
#define SCREEN_HEIGHT 25 // 屏幕高度
#define PLAYER_WIDTH 3 // 小人寬度
#define PLAYER_HEIGHT 3 // 小人高度
#define JUMP_HEIGHT 3 // 跳躍高度
#define JUMP_LENGTH 4 // 跳躍長度void gotoxy(int x, int y) {
COORD coord = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void hidecursor() {
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void setColor(int color) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}
void drawPlayer(int x, int y) {
setColor(FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
gotoxy(x, y);
printf(" ^ ");
gotoxy(x, y + 1);
printf("/_\\");
}
void clearPlayer(int x, int y) {
gotoxy(x, y);
printf(" ");
gotoxy(x, y + 1);
printf(" ");
}
void drawBoard(int x, int y, int width) {
setColor(FOREGROUND_INTENSITY | FOREGROUND_BLUE);
for (int i = 0; i < width; i++) {
gotoxy(x + i, y);
printf("_");
}
for (int i = 0; i < BOARD_HEIGHT; i++) {
gotoxy(x - 1, y + i);
printf("|");
gotoxy(x + width, y + i);
printf("|");
}
}
void clearBoard(int x, int y, int width) {
for (int i = 0; i < width; i++) {
gotoxy(x + i, y);
printf(" ");
}
for (int i = 0; i < BOARD_HEIGHT; i++) {
gotoxy(x - 1, y + i);
printf(" ");
gotoxy(x + width, y + i);
printf(" ");
}
}
void movePlayerLeft(int *x) {
if (*x > 0) {
clearPlayer(*x, SCREEN_HEIGHT - PLAYER_HEIGHT);
*x -= 1;
}
}
void movePlayerRight(int *x) {
if (*x < SCREEN_WIDTH - PLAYER_WIDTH) {
clearPlayer(*x, SCREEN_HEIGHT - PLAYER_HEIGHT);
*x += 1;
}
}
int jump(int *x, int *y, int *sign) {
clearPlayer(*x, *y);
if (*sign == -1) {
// 上升階段
for (int i = 0; i < JUMP_LENGTH; i++) {
if (*y > SCREEN_HEIGHT - PLAYER_HEIGHT - JUMP_HEIGHT) {
*y -= 1;
drawPlayer(*x, *y);
} else {
*sign = 1;
return 1;
}
Sleep(40); // 控制跳躍速度
}
} else {
// 下落階段
if (*y < SCREEN_HEIGHT - PLAYER_HEIGHT) {
*y += 1;
drawPlayer(*x, *y);
} else {
return -1;
}
Sleep(40);
}
return 0;
}
int main() {
int player_x = SCREEN_WIDTH / 2 - PLAYER_WIDTH / 2; // 初始小人位置
int board_x = rand() % (SCREEN_WIDTH - BOARD_WIDTH); // 跳板隨機(jī)位置
int board_y = SCREEN_HEIGHT - PLAYER_HEIGHT - BOARD_HEIGHT; // 初始跳板位置
int score = 0; // 初始得分
int jump_sign = 1; // 跳躍狀態(tài):1-下落,-1-上升
int fail = 0; // 游戲是否失敗
srand(time(NULL)); // 初始化隨機(jī)數(shù)種子
hidecursor(); // 隱藏光標(biāo)
drawPlayer(player_x, SCREEN_HEIGHT - PLAYER_HEIGHT); // 畫出初始小人
drawBoard(board_x, board_y, BOARD_WIDTH); // 畫出初始跳板
while (!fail) {
if (_kbhit()) {
int ch = _getch();
if (ch == 'a' || ch == 'A') {
movePlayerLeft(&player_x);
} else if (ch == 'd' || ch == 'D') {
movePlayerRight(&player_x);
}
}
fail = jump(&player_x, &board_y, &jump_sign);
if (fail == 1) {
score++;
clearBoard(board_x, board_y, BOARD_WIDTH);
board_x = rand() % (SCREEN_WIDTH - BOARD_WIDTH);
board_y -= 2;
drawBoard(board_x, board_y, BOARD_WIDTH);
}
gotoxy(0, SCREEN_HEIGHT + 1);
printf("當(dāng)前得分:%d", score);
}
gotoxy(SCREEN_WIDTH / 2 - 4, SCREEN_HEIGHT / 2);
printf("游戲失敗,得分:%d", score);
Sleep(3000); // 等待3秒后關(guān)閉窗口
return 0;
}
標(biāo)簽: