cjrome://dino c++改编版
  • 板块灌水区
  • 楼主ZnCl2_2887
  • 当前回复0
  • 已保存回复0
  • 发布时间2025/1/20 13:15
  • 上次更新2025/1/20 15:32:14
查看原帖
cjrome://dino c++改编版
1633275
ZnCl2_2887楼主2025/1/20 13:15
#include <bits/stdc++.h>
#include <conio.h> // 用于键盘输入
#include <windows.h> // 用于 Speed 函数

const int WIDTH = 50;
const int HEIGHT = 10;
const char DINO = 'T';
const char CACTUS = '|';
const char BIRD = '^';
const char EMPTY = ' ';
const char HART='*';

bool gameOver;
int dinoPos;
int cactusPos;
int birdPos;
int hartPos;
int score;
int hart;
void Setup() {
    gameOver = false;
    dinoPos = HEIGHT - 2;
    cactusPos = WIDTH - rand()%10;
    birdPos = WIDTH - rand()%50;
    hartPos=rand()%1000;
    score = 0;
    hart=1;
}

void Draw() {
    system("cls"); // 清屏
    for (int i = 0; i < HEIGHT; i++) {
        for (int j = 0; j < WIDTH; j++) {
            if (i == dinoPos && j == 1) {
                std::cout << DINO; // 恐龙
            } 
			else if (i == HEIGHT - 1 && j == cactusPos) {
                std::cout << CACTUS; // 仙人掌
            } 
			else if (i == HEIGHT - 3 && j == birdPos) {
                std::cout << BIRD; // 飞鸟
            } 
            else if (i == HEIGHT-2 && j == hartPos) {
                std::cout << HART; // 血包 
                hartPos+=rand()%1000;
            } 
			else {
                std::cout << EMPTY; // 空白
            }
        }
        std::cout << std::endl;
        
    }

    std::cout << "*Score: " << score << std::endl;
    std::cout<<"*Hart: "<<hart<<std::endl;
    std::cout<<std::endl;
    std::cout<<" //Press 'w' to jump"<<std::endl;
    std::cout<<" //Press 'a' to high jump"<<std::endl;
    std::cout<<" //Press 'd' to run"<<std::endl;
    std::cout<<" //Press 's' to go down"<<std::endl;
    //std::cout<<" //Press 'r' to retry"<<std::endl;
    score++;
}

void Input() {
    if (_kbhit()) { // 检测键盘输入
        char key = _getch();
        if (key == 'w' && dinoPos == HEIGHT - 2) { // 空格键跳跃
            dinoPos = HEIGHT - 8;
        }
        else if(key=='a'&&dinoPos == HEIGHT - 2){
        	dinoPos = HEIGHT - 16;
		}
		else if(key=='d'){
        	cactusPos -= 20;
    		birdPos -= 20;
		}
		else if(key=='s'){
        	dinoPos=HEIGHT-2;
		}
		/*else if(key=='r'){
        	system("./未命名4.cpp");
		}*/
    }
}

void Logic() {
    // 恐龙落地
    if (dinoPos < HEIGHT - 2) {
        dinoPos++;
    }

    // 移动道具 
    cactusPos--;
    birdPos-=2;
    hartPos--;

    // 重置障碍物位置
    if (cactusPos < 0) {
        cactusPos = WIDTH - 1;
    }
    if (birdPos < 0) {
        birdPos = WIDTH - 1;
    }
    if(hartPos<0){
    	hartPos=WIDTH-1;
	}

    // 碰撞检测
    if ((cactusPos == 1 && dinoPos == HEIGHT - 2) || (birdPos == 1 && dinoPos == HEIGHT - 4)) {
    	hart--;
    	if(hart<=0){
    		gameOver=true;
		}
    }
    if(hartPos == 1 && dinoPos == HEIGHT - 2){
    	hart++;
	}
}

int main() {
	
	int cnt=0;
	int s=100;
	srand(time(0));
    Setup();
    while (!gameOver) {
        Draw();
        Input();
        Logic();
        Sleep(s); // 控制游戏速度
        if(cnt%50==0){
        	cnt=0;
			birdPos = WIDTH - rand()%50;
		}
		cnt++;
		if(score%100==0){
			s+=50;
		}
		if(s>=225){
			s=225;
		}
    }

    std::cout << "Game Over! Final Score: " << score << std::endl;
    return 0;
}
2025/1/20 13:15
加载中...