首先,需要在vs里下载EasyX插件,链接:https://easyx.cn/ 。之后需要在项目文件夹中添加需要的图片:
之后是编写代码,需要用到一些绘图和单链表相关知识:
#include#include #include #include #include using namespace std; //开始界面 void beginLayout() { //准备文字 char beginString[] = "开始游戏"; char closeString[] = "退出游戏"; char tipString[] = "游戏说明:跳跃:j/J 翻滚:空格"; //界面布局 setfillcolor(LIGHTBLUE); solidrectangle(240, 100, 380, 150); solidrectangle(240, 160, 380, 160+50); solidrectangle(0, 360, 640, 400); //文字布局 settextcolor(RED); setbkmode(TRANSPARENT);//去掉文字背景 settextstyle(30, 0, "楷体"); outtextxy(240 + 10, 100 + 10, beginString); outtextxy(240 + 10, 160 + 10, closeString); outtextxy(120, 250, tipString); //鼠标交互 while (1) { MOUSEMSG m = GetMouseMsg(); if (m.x >= 240 && m.x <= 380 & m.y <= 150 && m.y >= 100) { setlinecolor(RED); rectangle(240-5, 100-5, 380+5, 150+5); if (m.uMsg == WM_LBUTTONDOWN) { break; } } else if(m.x >= 240 && m.x <= 380 & m.y <= 210 && m.y >= 160) { setlinecolor(RED); rectangle(240 - 5, 160 - 5, 380 + 5, 210 + 5); if (m.uMsg == WM_LBUTTONDOWN) { exit(0); } } else { setlinecolor(WHITE); rectangle(240 - 5, 160 - 5, 380 + 5, 210 + 5); rectangle(240 - 5, 100 - 5, 380 + 5, 150 + 5); } } } //定时器 int Timer(time_t num, int id) { static time_t start[10]; time_t end = clock(); if (end - start[id] > num) { start[id] = end; return 1; } return 0; } //障碍物 struct Bricks { int x; int y; int width; int height; }; struct Node* list = NULL; //创建障碍物 struct Bricks* createBricks(int x, int y, int width, int height) { struct Bricks* pB = (struct Bricks*)malloc(sizeof(struct Bricks)); assert(pB);//断言 pB->x = x; pB->y = y; pB->width = width; pB->height = height; return pB; } //画障碍物 void drawBricks(struct Bricks* pB) { solidrectangle(pB->x, pB->y, pB->x + pB->width, pB->y + pB->height); } //移动障碍物 void moveBricks(struct Bricks* pB) { pB->x -= 25; } //单链表 struct Node { struct Bricks* pB; struct Node* next; }; struct Node* createList() { struct Node* headNode = (struct Node*)malloc(sizeof(struct Node)); assert(headNode); headNode->pB = NULL; headNode->next = NULL; return headNode; } struct Node* createNode(struct Bricks* pB) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); assert(newNode); newNode->pB = pB; newNode->next = NULL; return newNode; } void insertNode(struct Node* headNode, struct Bricks* pB) { struct Node* newNode = createNode(pB); newNode->next = headNode->next; headNode->next = newNode; } void printList(struct Node* headNode) { struct Node* pMove = headNode->next; while (pMove != NULL) { drawBricks(pMove->pB); moveBricks(pMove->pB); pMove = pMove->next; } } //火柴人 IMAGE roll[8]; IMAGE move[8]; IMAGE jump; HWND hwnd = NULL;//弹窗 //碰撞 int hitBricks(int x, int y, int width, int height, struct Node* list) { struct Node* pMove = list->next; while (pMove!=NULL) { if (pMove->pB->x >= (x-pMove->pB->width) && pMove->pB->x <= x+width) { if (pMove->pB->y>=y-pMove->pB->height && pMove->pB->y <=y+height) { return 1; } } pMove = pMove->next; } return 0; } void loadResource() { loadimage(&jump,"jump.jpg",70,100); for (int i = 1; i <= 8;++i) { char fileName[20] = ""; sprintf_s(fileName,"gun8_%d.jpg",i); loadimage(roll+i-1,fileName,50,50); sprintf_s(fileName, "move8_%d.jpg", i); loadimage(move + i - 1, fileName, 70, 100); } } //1、跑步 void roleMove(int frameNum) { BeginBatchDraw(); int i = 0; while (i 运行结果:



