游戏初始化
标签: 消灭星星小游戏
学习人数: 13.7k

上一节课,我们将开发环境搭建好了,最后通过显示一张图片来完成测试。

 

这节课我们需要完成游戏的初始化界面。

 

第一步:我们需要准备一些游戏的图片素材

1、一张背景图片(图片大小 450*600 像素)

2、几种不同颜色的星星图片(图片大小统一为 30*30像素)

    

3、一张游戏结束的图片

 

第二步:将游戏的背景图片放上去

我们首先像上一节一样新建一个项目名称叫PopStarGame,或者直接用上一节的项目也可以。

然后新建一个Image文件夹,放在和x64同级的目录下,如下图所示:

并将要用到图片全部放到Image文件夹中,并命名。

接下我们在项目中新建一个main.cpp文件,然后在里面写入代码

#include<opencv2/opencv.hpp>
#include<stdio.h>
using namespace cv;

int main(int argc, char** argv) {
	Mat image_background = imread("./Image/background.png");
	if (image_background.empty()) {
		printf("could not load image...\n");
		return -1;
	}
	namedWindow("PopStarGame", 1);
	imshow("PopStarGame", image_background);
	waitKey(0);

	return 0;
}

然后Ctrl + F5运行,即可看到游戏背景图片显示出来了

 

第三步:将游戏的星星图片放上去

我们先选择一个红色的星星画到背景图片上去,那么需要理解一下像素的含义。

写下代码:

#include<opencv2/opencv.hpp>
#include<stdio.h>
using namespace cv;

int main(int argc, char** argv) {
	Mat image_background = imread("./Image/background.png");
	if (image_background.empty()) {
		printf("could not load image...\n");
		return -1;
	}
	Mat image_red = imread("./Image/red.png");
	int posX = 0;//在背景图片上的偏移量
	int posY = 0;//在背景图片上的偏移量
	for (int h = 0; h < image_red.rows; h++) {
		for (int w = 0; w < image_red.cols; w++) {
			Vec3b& carPixel = image_red.at<Vec3b>(h, w);
			uchar* ptr = image_background.ptr<uchar>(posX + h, pos...
登录查看完整内容


课后作业

完成本节课的内容


登录后开始许愿

暂无评论,来抢沙发