愤怒的小鸟

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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值