当前位置:首页 > 趣味小程序 > 正文内容

【C++图形化编程】飞机大战1—基础资源导入与基本框架

亿万年的星光4年前 (2021-11-13)趣味小程序2866

0.前言

前面几篇文章中,我们实现了flappy的小游戏,这篇文章中,我们尝试制作飞机大战的游戏。

首先,效果图如下:


  1. 基础资源导入

下载图片及音乐资源:https://box356.lanzoui.com/ilIEfwg5lcb

下载解压后的图片及音乐资源如下:


2.游戏基础框架

#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <stdio.h>
// 引用 Windows Multimedia API
#pragma comment(lib,"Winmm.lib")

#define High 800  // 游戏画面尺寸
#define Width 590

IMAGE img_bk; // 背景图片
float position_x,position_y; // 飞机位置
float bullet_x,bullet_y; // 子弹位置
float enemy_x,enemy_y; // 敌机位置
IMAGE img_planeNormal1,img_planeNormal2; // 正常飞机图片
IMAGE img_planeExplode1,img_planeExplode2; // 爆炸飞机图片
IMAGE img_bullet1,img_bullet2; // 子弹图片
IMAGE img_enemyPlane1,img_enemyPlane2; // 敌机图片
int isExpolde = 0; // 飞机是否爆炸
int score = 0; // 得分

void startup() {

}

void show() {

}

void updateWithoutInput() {

}

void updateWithInput() {

}

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

int main() {
	startup();  // 数据初始化
	while (1) { //  游戏循环执行
		show();  // 显示画面
		updateWithoutInput();  // 与用户输入无关的更新
		updateWithInput();     // 与用户输入有关的更新
	}
	gameover();     // 游戏结束、后续处理
	return 0;
}

3.现实静态资源现实

效果图:

参考代码:

#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <stdio.h>

// 引用 Windows Multimedia API
#pragma comment(lib,"Winmm.lib")

#define High 800  // 游戏画面尺寸
#define Width 590

IMAGE img_bk; // 背景图片
float position_x,position_y; // 飞机位置
float bullet_x,bullet_y; // 子弹位置
float enemy_x,enemy_y; // 敌机位置
IMAGE img_planeNormal1,img_planeNormal2; // 正常飞机图片
IMAGE img_planeExplode1,img_planeExplode2; // 爆炸飞机图片
IMAGE img_bullet1,img_bullet2; // 子弹图片
IMAGE img_enemyPlane1,img_enemyPlane2; // 敌机图片
int isExpolde = 0; // 飞机是否爆炸
int score = 0; // 得分

void startup()
{
	mciSendString("open game_music.mp3 alias bkmusic", NULL, 0, NULL);//打开背景音乐
	mciSendString("play bkmusic repeat", NULL, 0, NULL);  // 循环播放
	initgraph(Width,High);
	loadimage(&img_bk, "background.jpg");
	loadimage(&img_planeNormal1, "planeNormal_1.jpg");
	loadimage(&img_planeNormal2, "planeNormal_2.jpg");
	loadimage(&img_bullet1, "bullet1.jpg");
	loadimage(&img_bullet2, "bullet2.jpg");
	loadimage(&img_enemyPlane1, "enemyPlane1.jpg");
	loadimage(&img_enemyPlane2, "enemyPlane2.jpg");
	loadimage(&img_planeExplode1, "planeExplode_1.jpg");
	loadimage(&img_planeExplode2, "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 (isExpolde==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, "得分:");
	char s[5];
	sprintf(s, "%d", score);
	outtextxy(Width*0.55, High*0.95, s);
	FlushBatchDraw();
	Sleep(2);
}

void updateWithoutInput()
{

}

void updateWithInput()
{

}

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

int main()
{
	startup();  // 数据初始化	
	while (1)  //  游戏循环执行
	{
		show();  // 显示画面
		updateWithoutInput();  // 与用户输入无关的更新
		updateWithInput();     // 与用户输入有关的更新
	}
	gameover();     // 游戏结束、后续处理
	return 0;
}


    扫描二维码推送至手机访问。

    版权声明:本文由青少年编程知识记录发布,如需转载请注明出处。

    分享给朋友:

    相关文章

    【C++图形化编程】flappy bird(1)—基础框架及图形图像

    【C++图形化编程】flappy bird(1)—基础框架及图形图像

    0.前言    前面一篇文章,我们简单介绍了鼠标的一些操作, 这篇文章,我们还是一个实战教程,flappy bird的小游戏。1.导入背景和音乐  &...

    【C++图形化编程】EasyX函数~图形绘制相关函数(1)

    简介:EasyX有很多图形绘制函数,这篇文章简单介绍下:函数或函数类型说明arc画椭圆形circle画无填充的圆clearcircle清空圆形区域clearllipse清空椭圆形区域clearpie清...

    C++小游戏——简单飞机大战(2)——代码与显示优化

    C++小游戏——简单飞机大战(2)——代码与显示优化

    0.前言在上一篇中,我们在C++控制台中简单实现了飞机大战了逻辑,但是代码比较长,显示也不是很好看,这篇文章中,我们对上一篇的代码进行优化下,把很多过程封装成函数形式。让程序看上去更加精简。一个合理化...

    C++小游戏—贪吃蛇(1)

    0.前言c++小游戏来到了第二个,第二个小游戏是贪吃蛇。首先来分析一下需求。我们需要一个函数专门来绘制地图的。在地图上随机生成“食物”。按键函数,用来监听键盘事件。蛇的状态函数。移动函数等。1.参考代...

    【C++图形化编程】flappy bird(2)—游戏逻辑与完善

    【C++图形化编程】flappy bird(2)—游戏逻辑与完善

    0.前言    上一篇中,我们简单完成了flappy的图像导入和基本架构。这一篇文章中,我们继续完善。1.游戏逻辑这个游戏的简单逻辑就是:(1)初始状态(游戏一...

    【C++图形化编程】使用键盘做一个简单画板

    【C++图形化编程】使用键盘做一个简单画板

    参考代码#include <graphics.h> // 引用图形库头文件 #include<cstdio> #include<conio.h&...