C言語ゲーム:flappy bird

21677 ワード

初めてCSDNを使って文章を書くのですが、今回持ってきたのはFLAPPY BIRDという小さなゲームで、初めて書いたので本の内容を参照しました(PS:<>)、好きな方はそのままコピーして貼ることができますよ
#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("
"
); } printf("score: %d
"
, 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
"
); printf("score: %d
"
, 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
"
); printf("score: %d
"
, 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; }

音楽の部分は直接自分で取り替えることができますよ.