欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Flappy Bird 一款C语言小游戏

程序员文章站 2022-07-13 08:34:12
...

下落的小鸟

Flappy Bird初始beta版

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>

int high, width;
int x, y;
int bar_y, bar_D, bar_T;

void gotoxy(int x, int y)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos); 
}

void HideCursor()
{
	CONSOLE_CURSOR_INFO cursor_info = {1, 0};
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

void startup()
{
	high = 15;
	width = 20;
	x = 0;
	y = width / 3;
}

void show()
{
	gotoxy(0, 0);
	HideCursor();
	int i, j;
	
	for(i = 0; i < high; i++)
	{
		for(j = 0; j < width; j++)
		{
			if((i == x) && (j == y))
				printf("@");
			else
				printf(" ");
		}
		printf("\n");
	}
}

void updateWithoutInput()
{
	x++;
	sleep(1);
}

void updateWithInput()
{
	char input;
	if(kbhit())
	{
		input = getch();
		if(input == ' ')
			x -= 2;
	}
}


int main()
{
	startup();
	while(1)
	{
		show();
		updateWithoutInput();
		updateWithInput();
	}
	return 0;
} 

添加一个移动的障碍物

小鸟与移动的障碍物

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>

int high, width;
int x, y;
int bar_y, bar_D, bar_T;	//障碍物的坐标 
int score;

void gotoxy(int x, int y)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos); 
}

void HideCursor()
{
	CONSOLE_CURSOR_INFO cursor_info = {1, 0};
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

void startup()
{
	high = 15;
	width = 20;
	x = high / 2;
	y = 3;
	bar_y = width / 2;
	bar_D = high / 4;
	bar_T = high / 2;
	score = 0;
}

void show()
{
	gotoxy(0, 0);
	HideCursor();
	int i, j;
	
	for(i = 0; i < high; i++)
	{
		for(j = 0; j < width; j++)
		{
			if((i == x) && (j == y))
				printf("@");
			else if((j == bar_y) && ((i < bar_D) || (i > bar_T)))
				printf("*");
			else
				printf(" ");
		}
		printf("\n");
	}
	for(j = 0; j < width; j++)
		printf("-");
}

void updateWithoutInput()
{
	x++;
	bar_y--;
	sleep(1);
}

void updateWithInput()
{
	char input;
	if(kbhit())
	{
		input = getch();
		if(input == ' ')
			x -= 2;
	}
}


int main()
{
	startup();
	while(1)
	{
		show();
		updateWithoutInput();
		updateWithInput();
	}
	return 0;
} 

添加得分机制

Flappy Bird 一款C语言小游戏

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>

int high, width;
int x, y;
int bar_y, bar_D, bar_T;	//障碍物的坐标 
int score;

void gotoxy(int x, int y)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos); 
}

void HideCursor()
{
	CONSOLE_CURSOR_INFO cursor_info = {1, 0};
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

void startup()
{
	high = 15;
	width = 20;
	x = high / 2;
	y = 3;
	bar_y = width / 2;
	bar_D = high / 4;
	bar_T = high / 2;
	score = 0;
}

void show()
{
	gotoxy(0, 0);
	HideCursor();
	int i, j;
	
	for(i = 0; i < high; i++)
	{
		for(j = 0; j < width; j++)
		{
			if((i == x) && (j == y))
				printf("@");
			else if((j == bar_y) && ((i < bar_D) || (i > bar_T)))
				printf("*");
			else
				printf(" ");
		}
		if(i == 0)
			printf("得分:%d", score);
		printf("\n");
	}
	for(j = 0; j < width; j++)
		printf("-");
	
}

void updateWithoutInput()
{
	x++;
	bar_y--;
	if(y == bar_y)
	{
		if((x >= bar_D) && (x <= bar_T))
			score++;
		else
		{
			printf("游戏失败\n");
			system("cls");
			exit(0);
		}
	 } 
	sleep(1);
}

void updateWithInput()
{
	char input;
	if(kbhit())
	{
		input = getch();
		if(input == ' ')
			x -= 2;
	}
}


int main()
{
	startup();
	while(1)
	{
		show();
		updateWithoutInput();
		updateWithInput();
	}
	return 0;
} 

障碍循环出现

躲避循环为难小鸟的障碍物

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>

int high, width;
int x, y;
int bar_y, bar_D, bar_T;	//障碍物的坐标 
int score;

void gotoxy(int x, int y)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos); 
}

void HideCursor()
{
	CONSOLE_CURSOR_INFO cursor_info = {1, 0};
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

void startup()
{
	high = 15;
	width = 20;
	x = high / 2;
	y = 3;
	bar_y = width / 2;
	bar_D = high / 4;
	bar_T = high / 2;
	score = 0;
}

void show()
{
	gotoxy(0, 0);
	HideCursor();
	int i, j;
	
	for(i = 0; i < high; i++)
	{
		for(j = 0; j < width; j++)
		{
			if((i == x) && (j == y))
				printf("@");
			else if((j == bar_y) && ((i < bar_D) || (i > bar_T)))
				printf("*");
			else
				printf(" ");
		}
		if(i == 0)
			printf("得分:%d", score);
		printf("\n");
	}
	for(j = 0; j < width; j++)
		printf("-");
	
}

void updateWithoutInput()
{
	x++;
	bar_y--;
	if(y == bar_y)
	{
		if((x >= bar_D) && (x <= bar_T))
			score++;
		else
		{
			printf("\n游戏失败\n");
			system("pause");
			exit(0);
		}
	 } 
	if(x == high)
	{
		printf("\n游戏失败\n");
		system("pause");
		exit(0);
	}
	if(bar_y <= 0)
	{
		bar_y = width;
		int tmp = rand() * 10 % (int)(high * 0.8);
		bar_D = tmp - high / 10;
		bar_T = tmp + high / 10;
	}
	sleep(1);
}

void updateWithInput()
{
	char input;
	if(kbhit())
	{
		input = getch();
		if(input == ' ')
			x -= 2;
		if(x < 0)
			x = 0;
	}
}


int main()
{
	startup();
	while(1)
	{
		show();
		updateWithoutInput();
		updateWithInput();
	}
	return 0;
} 

刺激Flappy Bird终极版

刺激Flappy Bird游戏终结版

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>

int high, width;
int x, y;
int bar_y, bar_D, bar_T;	//障碍物的坐标
int bar2_y, bar2_D, bar2_T; 
int bar3_y, bar3_D, bar3_T;
int score;

void gotoxy(int x, int y)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos); 
}

void HideCursor()
{
	CONSOLE_CURSOR_INFO cursor_info = {1, 0};
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

void startup()
{
	high = 30;
	width = 60;
	x = high / 2;
	y = 3;
	bar_y = width / 3;
	bar_D = high / 4;
	bar_T = high / 2;
	bar2_y = width / 2 + 8;
	bar2_D = high / 3;
	bar2_T = high / 2 + 1;
	bar3_y = width - 3;
	bar3_D = high / 3 - 3;
	bar3_T = high / 2 + 3;
	score = 0;
}

void show()
{
	gotoxy(0, 0);
	HideCursor();
	int i, j;
	
	for(i = 0; i < high; i++)
	{
		for(j = 0; j < width; j++)
		{
			if((i == x) && (j == y))
				printf("@");
			else if((j == bar_y) && ((i < bar_D) || (i > bar_T)))
				printf("*");
			else if((j == bar2_y) && ((i < bar2_D) || (i > bar2_T)))
				printf("*");
			else if((j == bar3_y) && ((i < bar3_D) || (i > bar3_T)))
				printf("*");
			else
				printf(" ");
		}
		if(i == 0)
			printf("Your score:%d", score);
		printf("\n");
	}
	for(j = 0; j < width; j++)
		printf("-");
	
}

void updateWithoutInput()
{
	x++;
	bar_y--;
	bar2_y--;
	bar3_y--;
	if(y == bar_y || y == bar2_y || y == bar3_y)
	{
		if(((x >= bar_D) && (x <= bar_T)) || ((x >= bar2_D) && (x <= bar2_T)) || ((x >= bar3_D) && (x <= bar3_T)) )
			score++;
		else
		{
			printf("\nGame Over\n");
			system("pause");
			exit(0);
		}
	 } 
	if(x == high)
	{
		printf("\nGame Over\n");
		system("pause");
		exit(0);
	}
	if(bar_y <= 0)
	{
		bar_y = width;
		int tmp = rand() * 10 % (int)(high * 0.8);
		bar_D = tmp - high / 10;
		bar_T = tmp + high / 10;
	}
	if(bar2_y <= 0)
	{
		bar2_y = width;
		int tmp = rand() * 10 % (int)(high * 0.8);
		bar2_D = tmp - high / 10;
		bar2_T = tmp + high / 10;
	}
	if(bar3_y <= 0)
	{
		bar3_y = width;
		int tmp = rand() * 10 % (int)(high * 0.8);
		bar3_D = tmp - high / 10;
		bar3_T = tmp + high / 10;
	}
//	sleep(1);
}

void updateWithInput()
{
	char input;
	if(kbhit())
	{
		input = getch();
		if(input == ' ')
			x -= 2;
		if(x < 0)
			x = 0;
	}
}


int main()
{
	startup();
	while(1)
	{
		show();
		updateWithoutInput();
		updateWithInput();
	}
	return 0;
} 

看到这里有没有一点童年的感觉,嘻嘻!抓住五一最后一天的小尾巴好好玩一玩吧。

如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持,明天我们不见不散!!!