跳到正文

科技·工程

Google Dino Game (Beta version v 0.1.3)

sky_of_stars约 12 分钟
原文
文章信息与版本

文章信息

发布
2024年1月6日
收录
2026年4月12日
更新
2026年11月8日
篇幅
约 12 分钟

版本

  1. v2当前
  2. v1

声明:此游戏非作者原创,是与用户@James_L一起创作的

作者独白:这其实我朋友+同班同学@James_L搞的一个叫“Dinosaur”的小游戏,规则类似Google小恐龙。后来,我碰巧路过,试玩了一下,结果发现有很多Bug。后来我联系了他,改了一下,就有了这个游戏。


本次新更新的东西:

  1. 修复了死亡页面未弹出的Bug,并改进了死亡页面

  2. 新更新了分数系统和最高分数

  3. 增加了“速度增加”的功能,也就是每过一段时间游戏速度会增加


提示,本代码有点长,因为我朋友自创了个“game.h”头文件,而我没有,所以我把game.h包括的内容复制了下来。

程序共229行,但真正是游戏的部分第162行才开始

C++
#include<bits/stdc++.h>
#include<windows.h>
//#include<cmd.h>
#include<conio.h>
#include<bitset>
#include<winuser.h>
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
//#define SHAKE 5
using namespace std;
extern const int
BLACK=0,
DARKBLUE=1,DBLUE=1,
GREEN=2,
BLUE=3,
RED=4,
PURPLE=5,
DIRT=6,YELLOWGREY=6,
LIGHTGREY=7,LGREY=7,
GREY=8,DGREY=8,DARKGREY=8,
SKYBLUE=9,SBLUE=9,
LGREEN=10,LIGHTGREEN=10,
CANYON=11,
LRED=12,LIGHTRED=12,BRED=12,BRIGHTRED=12,
LPURPLE=13,LIGHTPURPLE=13,
LYELLOW=14,LIGHTYELLOW=14,
WHITE=15
;
typedef BOOL (WINAPI *PROCSETCONSOLEFONT)(HANDLE, DWORD);
PROCSETCONSOLEFONT SetConsoleFont;
void ResizeFont(int a){
	HMODULE hKernel32=GetModuleHandle("kernel32");
	SetConsoleFont=(PROCSETCONSOLEFONT)GetProcAddress(hKernel32,"SetConsoleFont");
	SetConsoleFont(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void Color(int text,int background){
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),text+background*16);
}
void BColor(int background){
    if(background==15)Color(0,background);
    else Color(15,background);
}
void BColor(string background){
	int len=background.length();
	for(int i=0;i<len;i++){
		if(background[i]>96)background[i]-=32;
	}
	if(background=="BLACK")Color(15,0);
	if(background=="DARKBLUE"||background=="DBLUE")Color(15,1);
	if(background=="GREEN")Color(15,2);
	if(background=="BLUE")Color(15,3);
	if(background=="RED")Color(15,4);
	if(background=="PURPLE")Color(15,5);
	if(background=="DIRT"||background=="YELLOWGREY")Color(15,6);
	if(background=="LIGHTGREY"||background=="LGREY")Color(15,7);
	if(background=="GREY"||background=="DGREY"||background=="DARKGREY")Color(15,8);
	if(background=="SKYBLUE"||background=="SBLUE")Color(15,9);
	if(background=="LGREEN"||background=="LIGHTGREEN")Color(15,10);
	if(background=="CANYON")Color(15,11);
	if(background=="LRED"||background=="LIGHTRED"||background=="BRED"||background=="BRIGHTRED")Color(15,12);
	if(background=="LPURPLE"||background=="LIGHTPURPLE")Color(15,13);
	if(background=="LYELLOW"||background=="LIGHTYELLOW")Color(15,14);
	if(background=="WHITE")Color(0,15);
}
void cls(){
	BColor(BLACK);
	system("cls");
}
struct CTRL{
	void SetPointerVisual(bool visual){
		//1=show     0=hide
		HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
		CONSOLE_CURSOR_INFO cci;
		GetConsoleCursorInfo(hOut,&cci);
		cci.bVisible=visual;
		SetConsoleCursorInfo(hOut,&cci);
	}
};
extern CTRL Ctrl;
struct Bas{
	void Hi(){
		cout<<"Hello world!";
	}
};
extern Bas Basic;
void SLowPrint(string str,int t){
	int len=str.size();
	for(int i=0;i<len;i++)
	{
		Sleep(t);
		cout<<str[i];
	}
	cout<<endl;
}
struct BigNum
{
	string num;
	int length(){
		return num.length();
	}
	void Print(){
		bool m=0;
		for(int i=0;i<length();i++)
		{
			if(num[i]!='0')m=1;
			if(m)cout<<num;
		}
	}
	string operator+(const BigNum& B){
		//string a=this->num;
		string b=B.num;
		return "1";
	};
};
struct TO{
	template < typename T >
	string String( T& n ){
        std::ostringstream stm ;
        stm<<fixed<<setprecision(16)<<n;
        return stm.str() ;
    }
};
extern TO To;
int GetWindowPosX(){
	RECT rect;
	HWND hwnd=GetForegroundWindow();
    GetWindowRect(hwnd,&rect);
    return rect.left;
}
int GetWindowPosY(){
	RECT rect;
	HWND hwnd=GetForegroundWindow();
    GetWindowRect(hwnd,&rect);
    return rect.top;
}
int GetCursorPosX(){
	POINT pt={0,0};
	GetCursorPos(&pt);
	return pt.x;
}
int GetCursorPosY(){
	POINT pt={0,0};
	GetCursorPos(&pt);
	return pt.y;
}
int GetSubX(){
	return GetCursorPosX()-GetWindowPosX();
}
int GetSubY(){
	return GetCursorPosY()-GetWindowPosY();
}
void gotoxy(int x,int y){
	int xx=0x0b;
	HANDLE hOutput;
	COORD loc;
	loc.X=x;
	loc.Y=y;
	hOutput=GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hOutput,loc);
}

#define KEY_PRESSED(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
const int LEN=20;
bool world[LEN+1];
int PlayerY,Jump;
unsigned long long score,maxscore,speed=0;
void PrintScreen(){
    if(rand()%5==0&&world[LEN-2]==0&&world[LEN-3]==0&&world[LEN-4]==0)world[LEN]=1;
    else world[LEN]=0;
    gotoxy(0,0);
    BColor(WHITE);
    for(int i=0;i<4;i++){
        BColor(WHITE);
        for(int j=0;j<LEN;j++){
            printf("  ");
        }
        BColor(BLACK);
        putchar('\n');
    }
    for(int i=0;i<LEN;i++){
        if(world[i])BColor(LIGHTGREY);
        else BColor(WHITE);
        printf("  ");
        world[i]=world[i+1];
    }
    gotoxy(2,4-PlayerY);
    BColor(GREY);
    cout<<"  ";
    gotoxy(0,5);
    BColor(BLACK);
    cout<<"Y: "<<PlayerY<<"  "<<"DY: "<<Jump<<"  score:" << score << "  speed:" << speed;
}
int main(){
	system("mode con cols=40 lines=6");
    BColor(WHITE);
    for(int i=0;i<5;i++){
        for(int j=0;j<LEN;j++){
            cout<<"  ";
        }
        cout<<endl;
    }
    while(1){
        PrintScreen();
        if(GetKeyState(' ')<0&&PlayerY==0)Jump=3;
        if(world[0]&&PlayerY==0){
			if(score>maxscore)maxscore=score;
			speed=0;
        	score=0;
            cls();
			cout << "OPPS!\nYour best score is:" << maxscore << "\nPlease press 'y' to continue...";
            while(1){
            	if(kbhit()){
            		char c=getch();
            		if(c=='y')break;
				}
			}
			for(int i=0;i<LEN;i++){
            world[i]=0;
        	}
            cls();
            main();
        }
        score++;
        if(Jump)Jump--,PlayerY++;
        else if(PlayerY)PlayerY--;
        Sleep(200-speed);
        if(score%20==0)speed+=5;
        if(speed>150)speed=150;
    }
}
原文