憤怒的小鳥

#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 bar_y,bar_xDown,bar_xTop;//障礙物座標
int score;          //最後的得分

void HideCursor()//清除光標
{
    CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

void gotoxy(int x, int y) //將光標調整到(x,y)的位置
{
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(handle, pos);
}

void startup()
{
	high = 20;
	width = 25;

	bird_x = 0;
	bird_y = width / 2;
	
	bar_y = width;
	bar_xDown = high /2;
	bar_xTop = high / 4;

	mciSendString("open birdbackground.mp3 alias bkmusic", NULL, 0, NULL);//打開背景音樂
	mciSendString("play bkmusic repeat", NULL, 0, NULL);  // 循環播放

	score = 0;
}

void show()//顯示畫面
{
	int i,j;
	gotoxy(0,0);
	HideCursor();
	for ( i = 0; i <= high+1; i ++)
	{
		for ( j = 0;j <= width; j ++)
		{
			if (i==bird_x && bird_y==j)
				printf("@");//輸出小鳥
			else if (j==bar_y&&((i<=bar_xTop)||(i>=bar_xDown)))
				printf("*");//輸出障礙物
			else
				printf(" ");
		}
		printf ("\n");
	}
	printf("你的成績;%d\n",score);
}
void updatewithoutinput()//與用戶輸入無關的數據
{

	bird_x++;

	bar_y--;

	if (bird_y==bar_y)
	{
		if ((bird_x<bar_xDown) && (bird_x>bar_xTop))
		{	
			mciSendString("close psmusic", NULL, 0, NULL); // 先把前面一次的音樂關閉
			mciSendString("open pass.mp3 alias psmusic", NULL, 0, NULL); // 打開跳動音樂
            mciSendString("play psmusic", NULL, 0, NULL); // 僅播放一次 
			score++;
		}
		else 
		{
			mciSendString("close bkmusic", NULL, 0, NULL); // 先把前面一次的音樂關閉
			mciSendString("open gameover.mp3 alias gomusic", NULL, 0, NULL); // 打開跳動音樂
            mciSendString("play gomusic", NULL, 0, NULL); // 僅播放一次

			printf("Game Over!\n");
			system("pause");//遊戲暫停顯示結果

			exit(0);
		}
	}
	if (bar_y == 0)
	{
		bar_y = width;	

		int randPositon = rand() % (high-5);
		bar_xDown =randPositon+ high / 4;
		bar_xTop =randPositon;
	}

	Sleep(150);

}
void updatewithinput()//與用戶輸入有關的數據
{
	char input; 
	if (kbhit())//檢查是否有鍵盤輸入;
	{
		input = getch();
		if (input == ' ')
		{
			bird_x = bird_x - 2;
		}	
	}
}
int main()
{
	startup();//數據初始化

	while(1)
	{
		show();//顯示畫面
		updatewithoutinput();//與用戶輸入無關的數據
		updatewithinput();//與用戶輸入有關的數據
	}
	return 0;
}




發佈了7 篇原創文章 · 獲贊 1 · 訪問量 340
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章