一、效果展示
1.游戏开始时的界面:
2.游戏中的画面
3.游戏失败后的界面
二、准备开发环境
- 在官网上下载Easyx安装包文件(https://easyx.cn/)
- 双击exe文件,点击下一步
- 选择VC++2022
- 弹出安装成功提示
- 在VS2022上新建一个空项目工程
二、实现思路
1.引入库头文件
代码如下:
#include<easyx.h>
2.游戏对象类的创立
类声明
class Letter
{
private:
int x;
int y;
int speed;
char ch;
public:
//字母偏移量
static int dx[26];
//获取字母接口
char get_ch();
//获取纵坐标接口
int get_y();
//指向下一个结点
Letter* next;
//构造函数
Letter(Letter* LetterList);
//渲染自己
void drawLetter();
//更新自身坐标
void modifyPos();
};
类成员函数实现
#include<iostream>
#include "Letter.h"
#define FPS 90//帧率不能超过90
int Letter::dx[26] = { 22, 22, 22, 22, 24, 24, 21, 20, 26, 22, 22, 24, 17,
20, 20, 23, 21, 22, 24, 22, 20, 21, 16, 21, 22, 22 };
extern IMAGE LetterImg;
char Letter::get_ch() {
return this->ch;
}
int Letter::get_y() {
return this->y;
}
Letter::Letter(Letter* LetterList) {
this->y = 0;
this->speed = rand() % 2 + 1;
//不允许创建对象时字母一样以及萝卜重合
end:
this->ch = 'A' + rand() % 26;
this->x = rand() % (900 - 40) + 40;
for (Letter* p = LetterList; p; p = p->next) {
if (p->ch == this->ch || fabs(p->x - this->x) <= 50) {
goto end;
}
}
}
void Letter::drawLetter() {
//绘制字体
DWORD* dst = GetImageBuffer();
DWORD* draw = GetImageBuffer();
DWORD* src = GetImageBuffer(&LetterImg);
int picture_width = (&LetterImg)->getwidth();
int picture_height = (&LetterImg)->getheight();
int graphWidth = getwidth();
int graphHeight = getheight();
int dstX = 0;
for (int iy = 0; iy < picture_height; iy++)
{
for (int ix = 0; ix < picture_width; ix++)
{
int srcX = ix + iy * picture_width;
int sa = ((src[srcX] & 0xff000000) >> 24);
int sr = ((src[srcX] & 0xff0000) >> 16);
int sg = ((src[srcX] & 0xff00) >> 8);
int sb = src[srcX] & 0xff;
if (ix >= 0 && ix <= graphWidth && iy >= 0 && iy <= graphHeight && dstX <= graphWidth * graphHeight)
{
dstX = (ix + this->x) + (iy + this->y) * graphWidth;
int dr = ((dst[dstX] & 0xff0000) >> 16);
int dg = ((dst[dstX] & 0xff00) >> 8);
int db = dst[dstX] & 0xff;
draw[dstX] = ((sr * sa / 255 + dr * (255 - sa) / 255) << 16)
| ((sg * sa / 255 + dg *