C语言游戏:flappy bird

程序源代码

#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;
}

Notes:

  1. 音乐部分可以直接进行自行替换。
  2. 本代码参照了<<C语言课程设计与游戏开发实践教程>>
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码上夏雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值