c++游戏谷歌小恐龙

这是一个不太像的《恐龙》

我设计了空中的小鸟,要小心!!!!!!

很难的!!!w键跳跃。

看到这儿,还不给我一个三连吗?

好了,代码来了。

#include<stdio.h> 
#include<conio.h> 
#include<stdlib.h> 
#include<time.h>
#include<windows.h>
#define N 80 
#define up 72 
#define left 75 
#define right 77 
#define down 80 
void run();
void yn();
void print(int [][N]);
void menu();
int scr[22][N]={0},pl=9,shit,width=70,score=0,death=0,jump_time=0,game_time=0,day=0,jump_height=0,shift_time=0;
int main(){
	menu(); 
	run(); 
}
 
void drawplayer(int a[][N],int xlu,int ylu)
{
	for(int i=1;i<=3;i++)
		a[xlu][ylu+i]=1;
	for(int i=0;i<=4;i++)
		a[xlu+1][ylu+i]=1;
	a[xlu+2][ylu+1]=1;
	a[xlu+2][ylu+3]=1;
}
 
bool search_player(int a[][N],int xlu,int ylu)
{
	for(int i=1;i<=3;i++)
		if(a[xlu][ylu+i]==2 || a[xlu][ylu+i]==3)
		    return false;
	for(int i=0;i<=4;i++)
		if(a[xlu+1][ylu+i]==2 || a[xlu+1][ylu+i]==3)
		    return false;
	if(a[xlu+2][ylu+1]==2 || a[xlu+2][ylu+1]==3)
		return false;
	if(a[xlu+2][ylu+3]==2 || a[xlu+2][ylu+3]==3)
		return false;
	return true;
}
 
void days(int q,int a[][N]){//太阳&月亮
	for(int i=3;i<=7;i++){
			for(int j=51;j<=55;j++){
				a[i][j]=0;
			}
		}
	if(q==1){//月亮,开启昼夜才有
	    a[4][52]=4;
	    a[7][52]=4;
	    for(int i=53;i<=54;i++){
			a[4][i]=4;
		    a[5][i]=4;
		    a[6][i]=4;
		    a[7][i]=4;
		}
		a[4][54]=0;
		a[7][54]=0;
		a[5][55]=4;
		a[6][55]=4;
	}
	else{
		for(int i=3;i<=7;i++){
			for(int j=51;j<=56;j++){
				a[i][j]=4;
			}
		}
		a[4][51]=0;
		a[4][56]=0;
		a[7][51]=0;
		a[7][56]=0; 
	} 
}
 
void print(int a[][N]){//绘图函数
	COORD c={0,0};
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
	int i,j;
	for(i=4;i<22;i++){
		a[i][width-1]=4; 
		for(j=0;j<width;j++){ 
			if(a[i][j]==0){
				printf(" ");
			}	 
			if(a[i][j]==1)
				printf("@");
			if(a[i][j]==2)
			    printf("#");
			if(a[i][j]==3)
			    printf("<");
			if(a[i][j]==4){
				printf("*");
			}
			if(i==4&&j==width-1)
				printf("score:%d",score/20);
			if(i==5&&j==width-1) 
				printf("Press Esc to exit");
		} 
		printf("\n"); 
	} 
	for(j=0;j<width;j++)
	    printf("~");
} 
 
void yn(){//死亡界面
	system("cls"); 
	system("color 07"); 
	printf("\n"); 
	printf("\n"); 
	printf("\t\t\t\t   *****GAME OVER*****\n\a"); 
	printf("\t\t\t\t   *****YOU DIED!***** \n");
	printf("\t\t\t\t*****YOUR SCORE:%4d*****\n\n",score/20); 
	printf("\t\t\t press y to continue,n to close(y/n)?\n"); 
	printf("\t\t\t"); 
	end:;
	switch(getch()){
		case 'y': 
		case 'Y':death=0,score=0,memset(scr,0,sizeof(scr)),run();break; 
		case 'n': 
		case 'N':break; 
		default :goto end;break; 
	} 
}
 
void generate_block(int a[][N],int lala=rand()%6+1){//生成障碍,有三种依次为:从上至下的墙,飞弹,从下至上随机高度的墙。。。
	if(lala==5){
		for(int i=0;i<=19;i++){
			a[i][69]=2;
		}
	}
	else if(lala==6){
		int hehe=rand()%5+1;
		if(hehe<=1){
			a[19][69]=3;
		}
		else if(hehe<=4){
			a[14][69]=3;
		}
		else if(hehe==5){
			a[21][69]=3;
		}
	}
	else{
		for(int i=0;i<=lala;i++)
			a[21-i][69]=2;
	}
}
 
void run() {//主函数
	//system("color 70");
	shit=0;
	day=0;
	system("cls");
	srand(time(0));
	while(1){
		score++;
		game_time++;
		if(game_time%60==0)//墙
		    generate_block(scr);
		if(game_time%80==0)//飞弹
		    generate_block(scr,6);
		//if(game_time/1000>=1 && day==0)
		    //system("color 07"),game_time=0,day=1;  //去掉这些注释(run函数里的所有)可开启昼夜更替(但我觉得不好看)。。。
		//else if(game_time/1000>=1 && day==1)
		    //system("color 70"),game_time=0,day=0;
		days(day,scr);
		if(kbhit()) //读取输入
			switch(getch()){
				case down:
				case 's': 
				case 'S':if(jump_time<=15)shift_time=20,jump_time=0;break;
				case up: 
				case 'W': 
				case 'w':if(jump_time<=0)jump_time=25,jump_height=-1,shift_time=0;break;
				case 13 :system("pause");break;
			}
		if(jump_time<=0 && shift_time<=0){//正常
			if(search_player(scr,19,pl))
				drawplayer(scr,19,pl);
			else
			    goto end;//新科技goto...感觉比break好用...
		}
		else if(jump_time>0){//跳
			jump_time--;
			if(jump_time<10){
				jump_height--;
			}
			else if(jump_time>15){
				jump_height++;
			}
			if(search_player(scr,19-jump_height,pl)){
				drawplayer(scr,19-jump_height,pl);
			}
			else
			    goto end;
		}
		else if(shift_time>0){//蹲
			shift_time--;
			if(search_player(scr,20,pl))
				drawplayer(scr,20,pl);
			else
			    goto end;
		}
		for(int i=4;i<22;i++)//移墙
		    for(int j=0;j<=80;j++){
		    	if(scr[i][j]==2){
		    	    scr[i][j]=0;
		    		if(j-1>=0)
		    	    	scr[i][j-1]=2;
				}
			}
		for(int i=4;i<22;i++)//移飞弹,可见速度较墙快了一倍
		    for(int j=0;j<=80;j++){
		    	if(scr[i][j]==3){
		    	    scr[i][j]=0;
		    		if(scr[i][j-1]==1)
		    		      goto end;
		    		else if(scr[i][j-2]==1)
		    		    goto end;
				else if(j-2>=0)
		    	    	    scr[i][j-2]=3;
				}
			}
		print(scr);
		for(int i=4;i<22;i++)
		    for(int j=0;j<=80;j++){
		    	if(scr[i][j]==1)
		    	    scr[i][j]=0;
		    }
	}
	end:;
	yn(); 
} 
 
void menu(){
	system("color 07");
	ShowCursor(false);//这一节照抄,指消去光标
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO cci;
	GetConsoleCursorInfo(hOut,&cci);
	cci.bVisible=false;
	SetConsoleCursorInfo(hOut,&cci);//一直到这里照抄
}

886

  • 14
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
以下是谷歌恐龙的简单实现代码(使用C++和SDL库): ```c++ #include <iostream> #include <SDL.h> using namespace std; const int SCREEN_WIDTH = 600; const int SCREEN_HEIGHT = 200; const int GROUND_HEIGHT = 180; int main(int argc, char* args[]) { SDL_Init(SDL_INIT_VIDEO); SDL_Window* window = SDL_CreateWindow("Google Dino", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); SDL_Texture* dinoTexture = SDL_CreateTextureFromSurface(renderer, SDL_LoadBMP("dino.bmp")); SDL_Texture* cactusTexture = SDL_CreateTextureFromSurface(renderer, SDL_LoadBMP("cactus.bmp")); SDL_Texture* groundTexture = SDL_CreateTextureFromSurface(renderer, SDL_LoadBMP("ground.bmp")); bool quit = false; SDL_Event e; int dinoX = 0; int dinoY = GROUND_HEIGHT; int cactusX = SCREEN_WIDTH; int cactusY = GROUND_HEIGHT - 20; int groundX = 0; int groundY = GROUND_HEIGHT + 20; int dinoVelY = 0; while (!quit) { while (SDL_PollEvent(&e) != 0) { if (e.type == SDL_QUIT) { quit = true; } else if (e.type == SDL_KEYDOWN) { if (e.key.keysym.sym == SDLK_SPACE) { if (dinoY == GROUND_HEIGHT) { dinoVelY = -10; } } } } dinoY += dinoVelY; if (dinoY > GROUND_HEIGHT) { dinoY = GROUND_HEIGHT; dinoVelY = 0; } else if (dinoY < 0) { dinoY = 0; dinoVelY = 0; } cactusX -= 5; if (cactusX < -20) { cactusX = SCREEN_WIDTH; } groundX -= 5; if (groundX < -SCREEN_WIDTH) { groundX = 0; } SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); SDL_RenderClear(renderer); SDL_Rect dinoRect = { dinoX, dinoY, 48, 48 }; SDL_RenderCopy(renderer, dinoTexture, NULL, &dinoRect); SDL_Rect cactusRect = { cactusX, cactusY, 20, 40 }; SDL_RenderCopy(renderer, cactusTexture, NULL, &cactusRect); SDL_Rect groundRect1 = { groundX, groundY, SCREEN_WIDTH, 20 }; SDL_RenderCopy(renderer, groundTexture, NULL, &groundRect1); SDL_Rect groundRect2 = { groundX + SCREEN_WIDTH, groundY, SCREEN_WIDTH, 20 }; SDL_RenderCopy(renderer, groundTexture, NULL, &groundRect2); SDL_RenderPresent(renderer); SDL_Delay(17); } SDL_DestroyTexture(dinoTexture); SDL_DestroyTexture(cactusTexture); SDL_DestroyTexture(groundTexture); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); return 0; } ``` 这只是一个简单的实现,你可以根据需要进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值