C语言游戏:flappy bird

第一次使用CSDN写文章,这次带来的是FLAPPY BIRD这款小游戏,因为是初次编写所以参照了书中的内容(PS:<<C语言课程设计与游戏开发实践教程>>),喜欢的朋友可以直接复制粘贴哦

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

#pragma comment(lib,"Winmm.lib")

int high, width;
int bird_x, bird_y;
int bar1_y, bar1_xDown, bar1_xTop;
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 startup()
{
	high = 20;
	width = 40;
	bird_x = 0;
	bird_y = width / 3;
	bar1_y = width / 2;
	bar1_xDown = high / 2;
	bar1_xTop = high / 3;
	score = 0;
}

void show()
{
	gotoxy(0, 0);
	int i, j;

	for (i = 0; i <= high; i++)
	{
		for (j = 0; j <= width; j++)
		{
			if ((i == bird_x) && (j == bird_y))
				printf("@");
			else if ((j == bar1_y) && (((i > bar1_xDown)&&(i<high)) || ((i < bar1_xTop)&&(i>0))))
				printf("*");
			else if ((i == 0) || (j == 0) || (i == high) || (j == width))
				printf("#");
			else
				printf(" ");
		}
		printf("\n");
	}
	printf("score: %d\n", score);
}

void UpdateWithoutInput()
{
	bird_x++;
	bar1_y--;
	if (bird_y == bar1_y)
	{
		if ((bird_x <= bar1_xDown) && (bird_x >= bar1_xTop))
			score++;
		else
		{
			system("CLS");
			mciSendString("close bkmusic",NULL,0,NULL);
			mciSendString("open D:\\GameOver.mp3 alias JSmusic", NULL, 0, NULL);
			mciSendString("play JSmusic", NULL, 0, NULL);
			printf("Game Over\n ");
			printf("score: %d\n", score);
			system("pause");
			exit(0); 
		}
	}
	if (bar1_y <= 0)
	{
		bar1_y = width-1;
		int temp = rand() % int(high*0.8);
		bar1_xDown = temp + high / 10;
		bar1_xTop = temp - high / 10;
	}
	if ((bird_x >= high)||(bird_x <= 0))
	{
		system("CLS");
		mciSendString("close bkmusic", NULL, 0, NULL);
		mciSendString("open D:\\GameOver.mp3 alias JSmusic", NULL, 0, NULL);
		mciSendString("play JSmusic", NULL, 0, NULL);
		printf("Game Over\n");
		printf("score: %d\n", score);
		system("pause");
		exit(0);
	}
	Sleep(100);
}

void UpdateWithInput()
{
	char input;
	if (_kbhit())
	{
		input = _getch();
		if (input == ' ')
			bird_x -= 2;
	}
}

void music()
{
	mciSendString("open D:\\MUSIC.mp3 alias bkmusic",NULL,0,NULL);
	mciSendString("play bkmusic repeat", NULL, 0, NULL);
}

int main()
{
	startup();
	music();
	while (1)
	{
		show();
		UpdateWithoutInput();
		UpdateWithInput();
	}
	return 0;
}

音乐部分可以直接进行自行替换哦.

猜你喜欢

转载自blog.csdn.net/weixin_43699716/article/details/88545141