c语言游戏开发

C语言游戏与图形编程

反弹小球

#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE
#include
#include
#include
#include
//全局变量
int high, width;
int ball_x, ball_y;
int ball_vx, ball_vy;
int position_x, position_y;
int ridus;
int left, right;
int ball_number;

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 = 15;
	width = 20;
	ball_x = 0;
	ball_y = width / 2;
	ball_vx = 1;
	ball_vy = 1;
	ridus = 5;
	position_x = high;
	position_y = width / 2;
	left = position_y - ridus;
	right = position_y + ridus;
	ball_number = 0;
}

void show()
{
	//gotoxy(0, 0);
	int i, j;
	for (i = 0; i <= high + 1; i++)
	{
		//Sleep(50);
		for (j = 0; j <= width; j++)
		{
			if ((i == ball_x) && (j == ball_y))
				printf("0");		//输出小球
			else if (j == width)
				printf("|");
			else if (i == high + 1)
				printf("-");
			else if ((i == high) && (j >= left) && (j <= right))
				printf("*");
			else
				printf(" ");
		}
		printf("\n");
		
	}
	
	printf("反弹小球数:%d\n", ball_number);
}

void updataWithoutInput()
{
	if (ball_x == high - 1)
	{
		if ((ball_y >= left) && (ball_y <= right))
		{
			ball_number++;
			printf("\a");
		}
		else
		{
			printf("游戏失败\n");
			system("pause");
			exit(0);
		}
	}

	ball_x = ball_x + ball_vx;
	ball_y = ball_y + ball_vy;
	if ((ball_x == 0) || (ball_x == high - 1))
		ball_vx = -ball_vx;

	if ((ball_y == 0) || (ball_y == width- 1))
		ball_vy = -ball_vy;

	Sleep(500);
}

void updateWithInput()
{
	char input;
	if (kbhit())
	{
		input = getch();
		if (input == 'a')
		{
			position_y--;
			left = position_y - ridus;
			right = position_y + ridus;
		}

		if (input == 'd')
		{
			position_y++;
			left = position_y - ridus;
			right = position_y + ridus;
		}
	}
}

int main()
{
	startup();					//数据的初始化
	while (1)					//游戏循环执行
	{
		show();					//显示画面
		updataWithoutInput();	//与用户输入无关的更新
		updateWithInput();		//与用户输入有关的更新
	}

	return 0;
}

飞机游戏

#include
#include
#include
#include
#include
#define EnemyNum 5
#define High 25
#define Width 50
int score;
//全局变量
int position_x, position_y;
int enemy_x[EnemyNum], enemy_y[EnemyNum];
int canvas[High][Width] = {0};
int BulletWidth;
int EnemyMoveSpeed;
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()
{
	int i, j;
	position_x = High / 2;
	position_y = Width / 2;
	canvas[position_x][position_y] = 1;
	int k;
	for (k = 0; k < EnemyNum; k++)
	{
		enemy_x[k] = rand() % 2;
		enemy_y[k] = rand() % Width;
		canvas[enemy_x[k]][enemy_y[k]] = 3;
	}
	
	score = 0;
	BulletWidth = 0;
	EnemyMoveSpeed = 10;
}
void show()
{
	gotoxy(0, 0);
	int i, j;
	for (i = 0; i <= High; i++)
	{
		for (j = 0; j <= Width; j++)
		{
			if (canvas[i][j] == 0)
				printf(" ");
			else if (canvas[i][j] == 1)
				printf("*");
			else if (canvas[i][j] == 2)
				printf("|");
			else if (canvas[i][j] == 3)
				printf("@");
		}
		printf("\n");
	}

	printf("得分:%3d\n", score);
	Sleep(20);
}

void updateWithoutInput()
{
	int i, j,k;
	for (i = 0; i < High; i++)
	{
		for (j = 0; j < Width; j++)
		{
			if (canvas[i][j] == 2)
			{
				for (k = 0; k < EnemyNum; k++)
				{
					if ((i == enemy_x[k]) && (j == enemy_y[k]))
					{
						score++;
						if (score % 5 == 0 && EnemyMoveSpeed > 3)
							EnemyMoveSpeed--;
						if (score % 5 == 0)
							BulletWidth++;
						canvas[enemy_x[k]][enemy_y[k]] = 0;
						enemy_x[k] = rand() % 2;
						enemy_y[k]=rand()%Width;
						//enemy_y = rand() % Width;
						canvas[enemy_x[k]][enemy_y[k]] = 3;
						canvas[i][j] = 0;
					}


				}
				
				canvas[i][j] = 0;
				if (i > 0)
					canvas[i - 1][j] = 2;
			}
		}
	}

	

	static int speed = 0;
	if (speed <20)
		speed++;
	for (k = 0; k < EnemyNum; k++)
	{
		if ((position_x == enemy_x[k]) && (position_y == enemy_y[k]))
		{
			printf("失败!\n");
			Sleep(3000);
			system("PAUSE");
			exit(0);
		}

		if (enemy_x[k] > High)
		{
			canvas[enemy_x[k]][enemy_y[k]] = 0;
			enemy_x[k] = rand() % 2;
			enemy_y[k] = rand() % Width;
			canvas[enemy_x[k]][enemy_y[k]] = 3;
			score--;
		}

		if (speed == EnemyMoveSpeed)
		{
			for (k = 0; k < EnemyNum; k++)
			{
				canvas[enemy_x[k]][enemy_y[k]] = 0;
				enemy_x[k]++;
				speed = 0;
				canvas[enemy_x[k]][enemy_y[k]] = 3;
			}
		}

	}
}

void updateWithInput()
{
	char input;
	if (_kbhit())
	{
		input = _getch();
		if (input == 'a')
		{
			canvas[position_x][position_y] = 0;
			position_y--;
			canvas[position_x][position_y] = 1;
		}
		else if (input == 'd')
		{
			canvas[position_x][position_y] = 0;
			position_y++;
			canvas[position_x][position_y] = 1;
		}
		else if (input == 'w')
		{
			canvas[position_x][position_y] = 0;
			position_x--;
			canvas[position_x][position_y] = 1;
		}
		else if (input == 's')
		{
			canvas[position_x][position_y] = 0;
			position_x++;
			canvas[position_x][position_y] = 1;
		}
		else if (input == ' ')
		{
			int left = position_y - BulletWidth;
			int right = position_y + BulletWidth;
			if (left < 0)
				left = 0;
			if(right>Width-1)
				right=Width-1;
				int k;
				for(k=left;k<=right;k++)
					canvas[position_x - 1][k] = 2;
		}
	}

}

int main()
{
	startup();
	while (1)
	{
		
		show();
		updateWithoutInput();
		updateWithInput();
		//system("pause");
	}

	return 0;
}

飞翔小鸟flapbird

#include
#include
#include
#include
//全局变量
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 = 15;
	width = 20;
	bird_x = 0;
	bird_y = width / 3;
	bar1_y = width / 2;
	bar1_xDown = high / 3;
	bar1_xTop = high / 2;
	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 > bar1_xTop)))
			{
				printf("*");
			}
			else
				printf(" ");
		}
		printf("\n");
	}
}

void updataWithoutInput()		//与用户输入无关的更新
{
	bird_x++;
	bar1_y--;
	if (bird_y == bar1_y)
	{
		if ((bird_x >= bar1_xDown) && (bird_x <= bar1_xTop))
			score++;
		else
		{
			/*printf("游戏失败\n");
			system("PAUSE");
			exit(0);*/
		}
	}

	if (bar1_y <= 0)
	{
		bar1_y = width;
		int temp = rand() % int(high * 0.8);
		bar1_xDown = temp - high / 10;
		bar1_xTop = temp + high / 10;
	}
	Sleep(150);
}

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

int main()
{
	startup();
	while (1)
	{
		show();
		updataWithoutInput();
		updataWithInput();
	}

	return 0;
}

多小球反弹(解决重叠问题)

#include
#include
#include
#include
#define High 480
#define Width 640

#define BallNum 15 //小球个数



int main()
{
	float ball_x[BallNum], ball_y[BallNum];				//小球的坐标
	float ball_vx[BallNum], ball_vy[BallNum];			//小球的速度
	float radius;										//小球的半径
	int i, j;
	radius = 20;

	for (i = 0; i < BallNum; i++)
	{
		ball_x[i] = rand() % int(Width - 4 * radius) + 2 * radius;
		ball_y[i] = rand() % int(High - 4 * radius) + 2 * radius;
		ball_vx[i] = (rand() % 2) * 2 - 1;
		ball_vy[i] = (rand() % 2) * 2 - 1;

	}

	//判断两球是否重叠如果重叠对坐标进行调整
	for (i = 0; i < BallNum; i++)
	{
		for (j = 0; j < BallNum; j++)
		{
			if (i != j)		//自己和自己不需要比
			{
				float dist2;
				dist2 = sqrt((ball_x[i] - ball_x[j]) * (ball_x[i] - ball_x[j]) + (ball_y[i] - ball_y[j]) * (ball_y[i] - ball_y[j]));
				if (dist2 < 2 * radius )
				{
					ball_x[j] = rand() % int(Width - 4 * radius) + 2 * radius;
					ball_y[j] = rand() % int(High - 4 * radius) + 2 * radius;
					ball_vx[j] = (rand() % 2) * 2 - 1;
					ball_vy[j] = (rand() % 2) * 2 - 1;
				}
				//if ((ball_vx[i] == ball_vx[j]) && (ball_vy[i] == ball_vy[j])&&(ball_x[i]-ball_x[j]<=radius)&& (ball_y[i] - ball_y[j] <= radius))
				//{
				//	//printf("123131231231\n");
				//	/*ball_vx[i] = -ball_vx[i];
				//	ball_vy[i] =-ball_vy[i];*/
				//}
			}
		}
	}
	initgraph(High, Width);

	BeginBatchDraw();
	while (1)
	{
		//绘制黑线,黑色填充的圆
		setcolor(BLACK);
		setfillcolor(BLACK);
		for (i = 0; i < BallNum; i++)
		{
			fillcircle(ball_x[i], ball_y[i], radius);
		}

		//更新小圆的坐标
		for (i = 0; i < BallNum; i++)
		{
			ball_x[i] = ball_x[i] + ball_vx[i];
			ball_y[i] = ball_y[i] + ball_vy[i];

			//把超出边界的小球拉回来
			if (ball_x[i] < radius)
				ball_x[i] = radius;
			if (ball_y[i] < radius)
				ball_y[i] = radius;
			if (ball_x[i] > Width - radius)
				ball_x[i] = Width - radius;
			if (ball_y[i] > High - radius)
				ball_y[i] = High - radius;
		}

		//判断是否和墙壁碰撞
		for (i = 0; i < BallNum; i++)
		{
			if ((ball_x[i] <= radius) || (ball_x[i] >= Width - radius))
				ball_vx[i] = -ball_vx[i];
			if ((ball_y[i] <= radius) || (ball_y[i]) >= High - radius)
				ball_vy[i] = -ball_vy[i];
		}

		float minDistances2[BallNum][2];		//记录某个小球和它最京小球的距离,以及这个小球的下标

		for (i = 0; i < BallNum; i++)
		{
			minDistances2[i][0] = 9999999;
			minDistances2[i][1] = -1;
		}

		//求所有小球两两之间的距离的平方
		for (i = 0; i < BallNum; i++)
		{
			for (j = 0; j < BallNum; j++)
			{
				if (i != j)		//自己和自己不需要比
				{
					float dist2;
					dist2 = (ball_x[i] - ball_x[j]) * (ball_x[i] - ball_x[j]) + (ball_y[i] - ball_y[j]) * (ball_y[i] - ball_y[j]);
					if (dist2 < minDistances2[i][0])
					{
						minDistances2[i][0] = dist2;
						minDistances2[i][1] = j;
					}
				}
			}
		}
		//判断球之间是否碰撞
		for (i = 0; i < BallNum; i++)
		{
			if (minDistances2[i][0] <= 4 * radius * radius)		//若最小距离小于阈值,发生碰撞
			{
				j = minDistances2[i][1];
				//交换速度
				int temp;
				temp = ball_vx[i];ball_vx[i] = ball_vx[j]; ball_vx[j] = temp;
				temp = ball_vy[i]; ball_vy[i] = ball_vy[j]; ball_vy[j] = temp;

				minDistances2[j][0] = 999999999;			//避免交换两次速度,又回去了

				minDistances2[j][1] = -1;
			}
		}
		//绘制黄线、绿色填充的圆
		setcolor(YELLOW);
		setfillcolor(GREEN);
		for (i = 0; i < BallNum; i++)
			fillcircle(ball_x[i], ball_y[i], radius);
		FlushBatchDraw();
		//延时
		Sleep(3);
	}
	EndBatchDraw();
	closegraph();
	return 0;
}

钟表

#include
#include
#include
#include
#include
#define High 480			//游戏画面尺寸
#define Width 640
#define PI 3.14159

int main()
{
	initgraph(Width, High);			//初始化绘图窗口
	int center_x, center_y;			//中心点的坐标,也是钟表的中心
	center_x = Width / 2;
	center_y = High / 2;
	int secondLength= Width / 5;				//秒针的长度
	int minuteLength = Width / 7;				//分针的长度
	int hourLength = Width /9;					//时针的长度
	float angle = 0;
	int secondEnd_x, secondEnd_y;		//秒针的终点
	int minuteEnd_x,minuteEnd_y;
	int hourEnd_x, hourEnd_y;


	SYSTEMTIME ti;

	/*secondEnd_x = center_x + secondLength;
	secondEnd_y = center_y;*/
	long double secondAngle = 0;				//秒针对应角度
	long double minuteAngle = 0;				//分针对应角度
	long double hourAngle = 0;					//时针对应角度
	//画秒针
	while (1)
	{
		//绘制一个简单的表盘
		setlinestyle(PS_SOLID, 1);
		setlinecolor(WHITE);
		circle(center_x, center_y, Width / 4);

		//画刻度
		int x, y, i;
		for (i = 0; i < 60; i++)
		{
			x = center_x + int(Width / 4.3 * sin(PI * 2 * i / 60));
			y = center_y + int(Width / 4.3 * cos(PI * 2 * i / 60));

			if (i % 15 == 0)
				bar(x - 5, y - 5, x + 5, y + 5);
			else if (i % 5 == 0)
				circle(x, y, 3);
			else
				putpixel(x, y, WHITE);

		}
		
		outtextxy(center_x - 25, center_y + Width / 6, _T("我的时钟"));

		GetLocalTime(&ti);
		//秒针角度的变化
		secondAngle = ti.wSecond * 2 * PI / 60;				//一圈一共2*PI,一圈60秒,一秒钟秒针走过的角度为2*PI/60
		//秒针角度的变化
		minuteAngle = ti.wMinute * 2 * PI / 60;				//一圈一共2*PI,一圈60分钟,一秒钟秒针走过的角度为2*PI/60
		//秒针角度的变化
		hourAngle = ti.wHour * 2 * PI / 12;				//一圈一共2*PI,一圈12小时,一秒钟秒针走过的角度为2*PI/12
		//由角度决定的分针终点坐标
		minuteEnd_x = center_x + minuteLength * sin(minuteAngle);
		minuteEnd_y = center_y - minuteLength * cos(minuteAngle);

		//由角度决定的秒针终点坐标
		secondEnd_x = center_x + secondLength * sin(secondAngle);
		secondEnd_y = center_y - secondLength * cos(secondAngle);

		//由角度决定的时针终点坐标
		hourEnd_x = center_x + hourLength * sin(hourAngle);
		hourEnd_y = center_y - hourLength * cos(hourAngle);


		setlinestyle(PS_SOLID, 2);				//画秒针,宽度为两个像素
		setcolor(WHITE);
		line(center_x, center_y, secondEnd_x, secondEnd_y);


		setlinestyle(PS_SOLID, 4);				//画分针,宽度为4个像素
		setcolor(BLUE);
		line(center_x, center_y, minuteEnd_x, minuteEnd_y);


		setlinestyle(PS_SOLID, 6);				//画秒针,宽度为6个像素
		setcolor(RED);
		line(center_x, center_y, hourEnd_x, hourEnd_y);
		Sleep(10);

		setcolor(BLACK);
		setlinestyle(PS_SOLID, 2);
		line(center_x, center_y, secondEnd_x, secondEnd_y);			//隐藏前一帧的秒针


		setlinestyle(PS_SOLID, 4);				//画分针,宽度为4个像素
		line(center_x, center_y, minuteEnd_x, minuteEnd_y);


		setlinestyle(PS_SOLID, 6);				//画秒针,宽度为6个像素
		line(center_x, center_y, hourEnd_x, hourEnd_y);


		//secondAngle += 6;
		//秒针角度变化
		//secondAngle = angle * 2 * PI / 60;		//一圈一共2*PI,一圈60秒,一秒钟秒针走过的角度为2*PI/60
		//angle += 2 * PI / 60;

	}

	_getch();			//按任意键继续
	
	closegraph();
	return 0;
}

飞翔小鸟(图片与声音)(bug较多,不完整)

//flappy bird
#include
#include
#include
#pragma comment(lib,"Winmm.lib")


#define High 600
#define Width 350
IMAGE img_bk, img_bd1, img_bd2,img_bar_up1, img_bar_up2, img_bar_down1, img_bar_down2;
int bird_x;
int bird_y;
int image_x[] = { 0 };
int image_y[] = { 0 };
void startup()
{
	initgraph( Width, High);
	loadimage(&img_bk, _T("E:\\image\\background.jpg"));
	loadimage(&img_bd1, _T("E:\\image\\bird1.jpg"));
	loadimage(&img_bd2, _T("E:\\image\\bird2.jpg"));
	loadimage(&img_bar_up1, _T("E:\\image\\bar_up1.gif"));
	loadimage(&img_bar_up2, _T("E:\\image\\bar_up2.gif"));
	loadimage(&img_bar_down1, _T("E:\\image\\bar_down1.gif"));
	loadimage(&img_bar_down2, _T("E:\\image\\bar_down2.gif"));

	bird_x = 50;
	bird_y = 200;
	BeginBatchDraw();

	image_x[0] = rand() % 301 + rand() % 75 + 30;
	image_y[0] = rand() % 315 + rand() % 95;

	for (int i = 1; i < 3; i++)
	{
		image_x[i] = image_x[i - 1] + rand() % 77+150;
		image_y[i] = image_y[i - 1] + rand() % 77;

	}

	mciSendString(_T("open E:\\image\\background.mp3 alias bkmusic"), NULL, 0, NULL);		//打开背景音乐

	mciSendString(_T("play bkmusic repeat"), NULL, 0, NULL);			//循环播放

}

void show()
{
	putimage(0, 0, &img_bk);				//显示背景
	int x = 50; //rand() % 49;
	for (int i = 0; i < 2; i++)
	{
		putimage(image_x[i], -300, &img_bar_up1, NOTSRCERASE);
		putimage(image_x[i], -300, &img_bar_up2, SRCINVERT);

		putimage(image_x[i], 400 , &img_bar_up1, NOTSRCERASE);
		putimage(image_x[i], 400 , &img_bar_up2, SRCINVERT);

	}
	


	/*putimage(150, 400, &img_bar_down1, NOTSRCERASE);
	putimage(150, 400, &img_bar_down2, SRCINVERT);*/
	putimage(bird_x, bird_y, &img_bd1, NOTSRCERASE);
	putimage(bird_x, bird_y, &img_bd2, SRCINVERT);

	FlushBatchDraw();
	Sleep(50);

}

void updateWithoutInput()
{
	if (bird_y < 580)
		bird_y = bird_y + 3;

	for (int i = 0; i < 3; i++)
	{
		if (image_x[i] > 0)
			image_x[i] -= 3;
		if (image_x[i] <= 0)
		{
			image_x[i]= image_x[i+1]+250;
			Sleep(100);
		}
	}
}

void updateWithInput()
{
	char input;
	if (_kbhit())
	{
		input = _getch();
		if (input == ' ' && bird_y > 20)
		{
			bird_y = bird_y - 60;
			mciSendString(_T("close jpmusic"), NULL, 0, NULL);
			mciSendString(_T("open E:\\image\\jump.mp3 alias jpmusic"), NULL, 0, NULL);		//打开音乐

			mciSendString(_T("play jpmusic"), NULL, 0, NULL);			//播放一次
		}
	}
}

void gameover()
{
	EndBatchDraw();
	_getch();
	closegraph();
}

int main()
{
	startup();
	while (1)
	{
		show();
		updateWithoutInput();
		updateWithInput();
	}

	gameover();
	


	return 0;

}

飞机大战

#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#pragma comment(lib,"Winmm.lib")


#define High 864
#define Width 591

IMAGE img_bk;
int position_x, position_y;			//飞机的位置
IMAGE img_planeNormal1, img_planeNormal2;		//飞机图片
int bullet_x, bullet_y;			//子弹的位置
IMAGE img_bullet1, img_bullet2;		//子弹图片
int bulletxy[Width][High] = { 0 };
float enemy_x, enemy_y;			//敌机的位置

IMAGE img_enemyPlane1, img_enemyPlane2;		//敌机图片

IMAGE img_planeExplode1, img_planeExplode2;		//飞机爆炸图片

int isExplode;		//飞机是否爆炸
int score;
void startup()
{
	initgraph(Width, High);
	mciSendString(_T("open E:\\image\\plane\\game_music.mp3 alias bkmusic"), NULL, 0, NULL);		//打开背景音乐
	mciSendString(_T("play bkmusic repeat"), NULL, 0, NULL);			//循环播放
	
	loadimage(&img_bk, _T("E:\\image\\plane\\background.jpg"));
	loadimage(&img_planeNormal1, _T("E:\\image\\plane\\planeNormal_1.jpg"));
	loadimage(&img_planeNormal2, _T("E:\\image\\plane\\planeNormal_2.jpg"));

	loadimage(&img_bullet1, _T("E:\\image\\plane\\bullet1.jpg"));
	loadimage(&img_bullet2, _T("E:\\image\\plane\\bullet2.jpg"));
	loadimage(&img_planeNormal2, _T("E:\\image\\plane\\planeNormal_2.jpg"));
	loadimage(&img_planeNormal2, _T("E:\\image\\plane\\planeNormal_2.jpg"));

	loadimage(&img_enemyPlane1, _T("E:\\image\\plane\\enemyPlane1.jpg"));
	loadimage(&img_enemyPlane2, _T("E:\\image\\plane\\enemyPlane2.jpg"));

	loadimage(&img_planeExplode1, _T("E:\\image\\plane\\planeExplode_1.jpg"));
	loadimage(&img_planeExplode2, _T("E:\\image\\plane\\planeExplode_2.jpg"));


	position_x = Width * 0.5;
	position_y = High * 0.7;
	bullet_x = position_x;
	bullet_y = -85;
	enemy_x = Width * 0.5;
	enemy_y = 10;
	BeginBatchDraw();
}

void show()
{
	putimage(0, 0, &img_bk);			//显示背景
	if (isExplode == 0)
	{
		putimage(position_x - 50, position_y - 60, &img_planeNormal1, NOTSRCERASE);		//显示飞机
		putimage(position_x - 50, position_y - 60, &img_planeNormal2, SRCINVERT);

		putimage(bullet_x - 7, bullet_y, &img_bullet1, NOTSRCERASE);				//显示子弹
		putimage(bullet_x - 7, bullet_y, &img_bullet2, SRCINVERT);

		putimage(enemy_x, enemy_y, &img_enemyPlane1, NOTSRCERASE);				//显示敌机
		putimage(enemy_x, enemy_y, &img_enemyPlane2, SRCINVERT);

	}
	else
	{
		putimage(position_x-50, position_y-60, &img_planeExplode1, NOTSRCERASE);				//显示爆炸飞机
		putimage(position_x-50, position_y-60, &img_planeExplode2, SRCINVERT);


	}

	outtextxy(Width * 0.48, High * 0.95,_T("得分:"));
	char s[5];
	sprintf(s, "%d", score);
	outtextxy(Width * 0.55, High * 0.95, (int)s);
	FlushBatchDraw();
	Sleep(2);
}

void updateWithoutInput()
{
	if (isExplode == 0)
	{

		if (bullet_y > -25)
			bullet_y = bullet_y - 2;
		if (enemy_y < High - 25)
			enemy_y = enemy_y + 0.5;
		else
			enemy_y = 10;

		if (abs(bullet_x - enemy_x) + abs(bullet_y - enemy_y) < 80)		//子弹击中敌机
		{
			enemy_x = rand() % Width;
			enemy_y = -40;
			bullet_y = -85;
			mciSendString(_T("close gemusic"), NULL, 0, NULL);			//先把前面一次的音乐关闭
			mciSendString(_T("open E:\\image\\plane\\gotEnemy.mp3 alias gemusic"), NULL, 0, NULL);
			//打开音乐

			mciSendString(_T("play gemusic"), NULL, 0, NULL);		//仅播放一次
			score++;

			if (score > 0 && score % 5 == 0 && score % 2 != 0)
			{
				mciSendString(_T("close 5music"), NULL, 0, NULL);		//先把前面一次的音乐关闭
				mciSendString(_T("open E:\\image\\plane\\5.mp3 alias 5music"), NULL, 0, NULL);	//打开音乐
				mciSendString(_T("play 5music"), NULL, 0, NULL);		//仅播放一次
			}

			if (score % 10 == 0)
			{
				mciSendString(_T("close 10music"), NULL, 0, NULL);		//先把前面一次的音乐关闭
				mciSendString(_T("open E:\\image\\plane\\10.mp3 alias 10music"), NULL, 0, NULL);	//打开音乐
				mciSendString(_T("play 10music"), NULL, 0, NULL);		//仅播放一次
			}
		}

		if (abs(position_x - enemy_x) + abs(position_y - enemy_y) < 150)			//敌机撞击我机
		{
			isExplode = 1;
			mciSendString(_T("close exmusic"), NULL, 0, NULL);		//先把前面一次的音乐关闭
			mciSendString(_T("open E:\\image\\plane\\explode.mp3 alias exmusic"), NULL, 0, NULL);	//打开音乐
			mciSendString(_T("play exmusic"), NULL, 0, NULL);		//仅播放一次
		}
	}
	
}

void updateWithInput()
{
	if (isExplode == 0)
	{
		MOUSEMSG m;				//定义鼠标消息
		while (MouseHit())
		{
			m = GetMouseMsg();
			if (m.uMsg == WM_MOUSEMOVE)
			{
				//飞机的位置等于鼠标所在的位置

				position_x = m.x;
				position_y = m.y;
			}
			else if (m.uMsg == WM_LBUTTONDOWN)
			{
				//按下鼠标左键发射子弹
				bullet_x = position_x;
				bullet_y = position_y - 85;

				mciSendString(_T("close fgmusic"), NULL, 0, NULL);		//先把前面一次的音乐关闭
				mciSendString(_T("open E:\\image\\plane\\f_gun.mp3 alias fgmusic"), NULL, 0, NULL);	//打开音乐

				mciSendString(_T("play fgmusic"), NULL, 0, NULL);		//仅播放一次

			}
		}
	}
	
}

void gameover()
{
	EndBatchDraw();
	_getch();
	closegraph();
}

int main()
{
	startup();
	while (1)
	{
		show();
		updateWithoutInput();
		updateWithInput();

	}

	gameover();
	return 0;
}

你可能感兴趣的:(C/C++,c语言,开发语言,visual,studio,c++)