目录

一、游戏玩法

二、完整代码

三、部分细节

透明化人物背景

关于easyx库中怎样贴出透明图片

地图的链表实现

移动检测

碰撞检测

总结


前言:

花两天边看easyx文档边学边写的期末小作业。

学校挂c++的课教c语言入门:)

时间仓促没优化啥,摆了不找了。欢迎大佬帮忙指出不足。

欢迎c+v应付作业

有小小的bug,开局时切换为英文输入法,开局时切换为英文输入法,开局时切换为英文输入法

需要配合地图素材  素材和exe文件的打包下载在结尾。


一、游戏玩法

该游戏将操作主角完成一些小小的任务以脱离密闭的房间。

W  A S D移动开局时切换为英文输入法

玩家将借助游戏中的门进出不同的房间

完成推箱子小游戏关后,玩家可从出口处出门完成游戏全部流程。

卧室

客厅

小游戏关

出口

二、完整代码

#include <easyx.h>
#include <conio.h>
#include<Windows.h>
#include<iostream>
using namespace std;//设置房间链表的节点
typedef struct map {int number;struct map* up;struct map* down;struct map*  left;struct map* right;struct map* next;char* src;
}mapnode;void rungame();mapnode* createmaplist();//创建地图函数
void showmainmenu();//展示主菜单函数
int peng(int x, int y, mapnode* nowroom, int xpx[], int xpy[]);//碰撞检测函数
int ifwin( int* xpx,int* xpy);//游戏胜负条件检测函数int main()
{   showmainmenu();_getch();        closegraph();           return 0;}//创建地图函数
mapnode* createmaplist() {mapnode* head = (mapnode*)malloc(sizeof(mapnode));mapnode* p, * tail = head;p = (mapnode*)malloc(sizeof(mapnode));p->number = 1;p->down = NULL;p->up = NULL;p->right = NULL;p->left = NULL;p->src = (char*)"./MAP001.jpg";tail->next = p;tail = tail->next;p = (mapnode*)malloc(sizeof(mapnode));p->number = 2;p->down = NULL;p->up = tail;p->right = NULL;p->left = NULL;p->src = (char*)"./MAP002.jpg";tail->down = p;tail = tail->down;p = (mapnode*)malloc(sizeof(mapnode));p->number = 3;p->down = NULL;p->up = NULL;p->right = tail;p->left = NULL;p->src = (char*)"./MAP003.jpg";tail->left = p;p = (mapnode*)malloc(sizeof(mapnode));p->number = 4;p->down = NULL;p->up = NULL;p->right = NULL;p->left = tail;p->src = (char*)"./MAP004.jpg";tail->right = p;return head;
}//展示主菜单函数
void showmainmenu() {//初始化窗口initgraph(528, 528);setbkcolor(WHITE);cleardevice();//初始化标题字体settextcolor(BLACK);settextstyle(80, 0, "楷体");setbkmode(TRANSPARENT);char* arr = (char*)"李华传";int x, y;x=264-textwidth(arr)/2;y=100-textheight(arr)/2;outtextxy(x,y,arr);//加了层阴影settextcolor(RGB(154, 167, 177));outtextxy(x+5, y+5, arr);settextcolor(BLACK);//开始游戏按钮和字体初始化setfillcolor(WHITE);setlinecolor(BLACK);setlinestyle(PS_SOLID);arr= (char*)"开始游戏";settextstyle(40, 0, "微软雅黑");x = 264 - textwidth(arr) / 2;y = 250 - textheight(arr) / 2;fillroundrect(x - 20, y - 20, x + textwidth(arr)+20, y + textheight(arr)+20, 20, 20);outtextxy(x,y,arr);arr = (char*)"version 0.1";settextstyle(20, 0, "微软雅黑");outtextxy(450, 500,arr);settextstyle(40, 0, "微软雅黑");arr = (char*)"开始游戏";//接收鼠标信息ExMessage msg;int flag = 0;while (1) {if (peekmessage(&msg, EM_MOUSE)) {switch (msg.message) {case WM_LBUTTONUP:if (msg.x >= x - 20 && msg.x <= x + textwidth(arr) + 20 && msg.y >= y - 20 && msg.y <= y + textheight(arr) + 20) {flag = 1;rungame();break;}case WM_MOUSEMOVE:if (msg.x >= x - 20 && msg.x <= x + textwidth(arr) + 20 && msg.y >= y - 20 && msg.y <= y + textheight(arr) + 20) {setfillcolor(RGB(154, 167, 177));fillroundrect(x - 20+1, y - 20+1, x + textwidth(arr) + 21, y + textheight(arr) + 21, 20, 20);outtextxy(x, y, arr);}else {setfillcolor(WHITE);fillroundrect(x - 20, y - 20, x + textwidth(arr) + 20, y + textheight(arr) + 20, 20, 20);outtextxy(x, y, arr);}break;}}if (flag == 0) {}else {break;}}}void rungame() {//初始化窗口clearrectangle(0, 0, 600, 600);setbkcolor(YELLOW);cleardevice();//初始化地图链表mapnode* head = createmaplist();mapnode* now = head->next;//初始化人物及背景对象IMAGE nowroombk;IMAGE xiang;loadimage(&nowroombk, now->src, 0, 0);IMAGE character1, character2, character3;loadimage(&character1, "./原.jpg", 48, 48);loadimage(&character2, "a.png", 48, 48);loadimage(&character3, "b.png", 48, 48);loadimage(&xiang, "xian.png", 48, 48);int x = 240, y = 240;ExMessage msg;int a;int xpx[3] = { 6 * 48 ,8 * 48, 6 * 48 };int xpy[3] = { 4 * 48 ,5 * 48 ,6 * 48 };//开始主循环while (1) {putimage(0, 0, &nowroombk);putimage(x, y, &character3, SRCAND);putimage(x, y, &character2, SRCPAINT);if (now->number == 3) {putimage(xpx[0], xpy[0], &xiang);putimage(xpx[1], xpy[1], &xiang);putimage(xpx[2], xpy[2], &xiang);}int i,win=0;//检测是否碰撞//这里会不好看  看我文章if (peekmessage(&msg, EM_KEY)) {switch (msg.message) {case WM_KEYDOWN:a = _getch();if (a == 'w' || a == 'W') {//SetWorkingImage(&character);i = peng(x, y - 48, now, xpx, xpy);if (i) {if (i > 5) {i = i - 10;if (peng(xpx[i], xpy[i] - 48, now, xpx, xpy)) {}else {switch (i) {case 0:xpy[0] -= 48;break;case 1:xpy[1] -= 48;break;case 2:xpy[2] -= 48;break;}}}}else {y -= 48;}}else if (a == 'a' || a == 'A') {i = peng(x - 48, y, now, xpx, xpy);if(i){if (i > 5) {i = i - 10;if (peng(xpx[i] - 48, xpy[i], now, xpx, xpy)) {}else {switch (i) {case 0:xpx[0] -= 48;break;case 1:xpx[1] -= 48;break;case 2:xpx[2] -= 48;break;}}}}else {x -= 48;}}else if (a == 's' || a == 'S') {i = peng(x, y + 48, now, xpx, xpy);if (i) {if (i > 5) {i = i - 10;if (peng(xpx[i], xpy[i] + 48, now, xpx, xpy)) {}else {switch (i) {case 0:xpy[0] += 48;break;case 1:xpy[1] += 48;break;case 2:xpy[2] += 48;break;}}}}else {y += 48;}}else if (a == 'd' || a == 'D') {i = peng(x + 48, y, now, xpx, xpy);if (i) {if (i > 5) {i = i - 10;if (peng(xpx[i] + 48, xpy[i], now, xpx, xpy)) {}else {switch (i) {case 0:xpx[0] += 48;break;case 1:xpx[1] += 48;break;case 2:xpx[2] += 48;break;}}}}else {x += 48;}}else if (a == 32) {//cleardevice();}cleardevice();break;}}//房间移动if (x >= 5 * 48 && x <= 6 * 48 && y >= 10 * 48 && y <= 11 * 48) {if (now->down) {now = now->down;x = 5 * 48;y = 2 * 48;loadimage(&nowroombk, now->src, 0, 0);}}if (x >= 5 * 48 && x <= 6 * 48 && y >= 0 * 48 && y < 1 * 48) {if (now->up) {now = now->up;x = 5 * 48;y = 9 * 48;loadimage(&nowroombk, now->src, 0, 0);}}if (x >= 0 * 48 && x <= 1 * 48 && y >= 5 * 48 && y <= 6 * 48) {if (now->left) {now = now->left;x = 9 * 48;y = 5 * 48;loadimage(&nowroombk, now->src, 0, 0);}}if (x >= 10 * 48 && x <= 11 * 48 && y >= 5 * 48 && y <= 6 * 48) {if (now->right) {now = now->right;x = 2 * 48;y = 5 * 48;loadimage(&nowroombk, now->src, 0, 0);}}if (ifwin(xpx, xpy)) {if (now->number == 4, x == 5 * 48, y == 1 * 48) {break;}}}
}
//碰撞检测函数
int peng(int x,int y,mapnode* nowroom,int xpx[],int xpy[]){int flag=0;int i = 0;if (x<0||y<0||x>10*48||y>10*48) {flag = 1;}if (nowroom->number == 1) {if (y <= 3 * 48 && x <= 10 * 48 || y == 6 * 48 && x == 2 * 48 || y == 4 * 48 && x == 9 * 48) {flag = 1;}}if (nowroom->number == 2) {if (y <= 1 * 48 && x <= 4 * 48|| y <= 1 * 48 && x >= 6 * 48&& x <= 10 * 48){flag = 1;}}if (nowroom->number == 3) {if (y==0||y==10 * 48 ||x==0||x<=3 * 48 &&y<=3 * 48 ||x<=3 * 48 &&y>=7 * 48 ||x>=7 * 48 &&y<=4 * 48 ||x>=7 * 48 &&y>=6 * 48) {flag = 1;}for (i=0;i < 3;i++) {if (x == xpx[i] && y == xpy[i]) {flag = i+10;}}}if (nowroom->number == 4) {if (y <= 1 * 48 && x <= 4 * 48 || y <= 1 * 48 && x >= 6 * 48 && x <= 10 * 48) {flag = 1;}}return flag;
}
//游戏胜负条件检测函数
int ifwin( int* xpx, int* xpy) {int i = 0;if (6*48 == xpx[0] && 1 * 48 == xpy[0]&& 1 * 48 == xpx[1] && 5 * 48 == xpy[1] && 6 * 48 == xpx[2] && 9 * 48 == xpy[2]) {return 1;}return 0;
}

三、部分细节

透明化人物背景

IMAGE character1, character2, character3;loadimage(&character1, "./原.jpg", 48, 48);loadimage(&character2, "a.png", 48, 48);loadimage(&character3, "b.png", 48, 48);loadimage(&xiang, "xian.png", 48, 48);

关于easyx库中怎样贴出透明图片

原文链接:https://blog.csdn.net/weixin_45848751/article/details/106983700

地图的链表实现

mapnode* createmaplist() {mapnode* head = (mapnode*)malloc(sizeof(mapnode));mapnode* p, * tail = head;p = (mapnode*)malloc(sizeof(mapnode));p->number = 1;p->down = NULL;p->up = NULL;p->right = NULL;p->left = NULL;p->src = (char*)"./MAP001.jpg";tail->next = p;tail = tail->next;p = (mapnode*)malloc(sizeof(mapnode));p->number = 2;p->down = NULL;p->up = tail;p->right = NULL;p->left = NULL;p->src = (char*)"./MAP002.jpg";tail->down = p;tail = tail->down;p = (mapnode*)malloc(sizeof(mapnode));p->number = 3;p->down = NULL;p->up = NULL;p->right = tail;p->left = NULL;p->src = (char*)"./MAP003.jpg";tail->left = p;p = (mapnode*)malloc(sizeof(mapnode));p->number = 4;p->down = NULL;p->up = NULL;p->right = NULL;p->left = tail;p->src = (char*)"./MAP004.jpg";tail->right = p;return head;
}

没啥好说的src存放地图位置

移动检测

利用了easyx中的ExMessage对象

由于每次循环初都会重新绘制画面上所有东西,所以在循环尾只需要对坐标做处理就会改变下次图形的位置。

 if (peekmessage(&msg, EM_KEY)) {switch (msg.message) {case WM_KEYDOWN:a = _getch();if (a == 'w' || a == 'W') {//SetWorkingImage(&character);i = peng(x, y - 48, now, xpx, xpy);if (i) {if (i > 5) {i = i - 10;if (peng(xpx[i], xpy[i] - 48, now, xpx, xpy)) {}else {switch (i) {case 0:xpy[0] -= 48;break;case 1:xpy[1] -= 48;break;case 2:xpy[2] -= 48;break;}}}}else {y -= 48;}}else if (a == 'a' || a == 'A') {i = peng(x - 48, y, now, xpx, xpy);if(i){if (i > 5) {i = i - 10;if (peng(xpx[i] - 48, xpy[i], now, xpx, xpy)) {}else {switch (i) {case 0:xpx[0] -= 48;break;case 1:xpx[1] -= 48;break;case 2:xpx[2] -= 48;break;}}}}else {x -= 48;}}else if (a == 's' || a == 'S') {i = peng(x, y + 48, now, xpx, xpy);if (i) {if (i > 5) {i = i - 10;if (peng(xpx[i], xpy[i] + 48, now, xpx, xpy)) {}else {switch (i) {case 0:xpy[0] += 48;break;case 1:xpy[1] += 48;break;case 2:xpy[2] += 48;break;}}}}else {y += 48;}}else if (a == 'd' || a == 'D') {i = peng(x + 48, y, now, xpx, xpy);if (i) {if (i > 5) {i = i - 10;if (peng(xpx[i] + 48, xpy[i], now, xpx, xpy)) {}else {switch (i) {case 0:xpx[0] += 48;break;case 1:xpx[1] += 48;break;case 2:xpx[2] += 48;break;}}}}else {x += 48;}}else if (a == 32) {//cleardevice();}

碰撞检测

int peng(int x,int y,mapnode* nowroom,int xpx[],int xpy[]){int flag=0;int i = 0;if (x<0||y<0||x>10*48||y>10*48) {flag = 1;}if (nowroom->number == 1) {if (y <= 3 * 48 && x <= 10 * 48 || y == 6 * 48 && x == 2 * 48 || y == 4 * 48 && x == 9 * 48) {flag = 1;}}if (nowroom->number == 2) {if (y <= 1 * 48 && x <= 4 * 48|| y <= 1 * 48 && x >= 6 * 48&& x <= 10 * 48){flag = 1;}}if (nowroom->number == 3) {if (y==0||y==10 * 48 ||x==0||x<=3 * 48 &&y<=3 * 48 ||x<=3 * 48 &&y>=7 * 48 ||x>=7 * 48 &&y<=4 * 48 ||x>=7 * 48 &&y>=6 * 48) {flag = 1;}for (i=0;i < 3;i++) {if (x == xpx[i] && y == xpy[i]) {flag = i+10;}}}if (nowroom->number == 4) {if (y <= 1 * 48 && x <= 4 * 48 || y <= 1 * 48 && x >= 6 * 48 && x <= 10 * 48) {flag = 1;}}return flag;
}

这段先调用了一次碰撞检测函数peng()将人物是否碰上设定的物体或箱子返回,由于无法返回是哪个箱子,就自作聪明,将返回值改为对应的箱子号+10,这样在上一层函数中-10就能知道是哪个箱子。又调用了一次peng()函数判断箱子是否碰到物体或其他箱子,只要会碰上就不移动。这次返回0以外的其他数就没有什么区别了。

按理说这里应该再写个函数,使代码没那么臃肿,但是我感觉代码量应该不会差太多,加上赶时间。

资源

链接: https://pan.baidu.com/s/1mQCH5MHm24O-P5tuU1Od8g?pwd=58w6 提取码: 58w6 复制这段内容后打开百度网盘手机App,操作更方便哦

站内资源easyx图形库自制RPG类型小游戏配套资源-其他文档类资源-CSDN下载


总结

部分资源使用了rpgmaker的免费素材

c语言/c++大作业基于easyx图形库自制RPG类型小游戏代码(附源码)相关推荐

  1. C语言小游戏大全,C语言贪吃蛇小游戏(附源码)

    一.C语言小游戏大全,C语言贪吃蛇小游戏(附源码) 贪吃蛇小游戏源码和更多C语言课设项目小游戏源码免 费 下 载 链 接 如下: c语言项目课设小游戏源码资料压缩包.zip-C文档类资源-CSDN下载 ...

  2. 分享一个C语言矿井逃生迷宫小游戏【附源码】

    用C语言写的一个迷宫小游戏,游戏玩法是通过鼠标控制帽子上的灯走出迷宫 // 定义常量 #define PI 3.141592653589 // 圆周率 #define UNIT_GROUND 0 // ...

  3. C语言做的接鸡蛋小游戏(附源码注释)【原创】

    //以下是接鸡蛋小游戏源码 .建议在VS中运行调试! /* 头文件 */ # include <windows.h> # include <stdlib.h> # includ ...

  4. 基于Java Swing 的马踏棋盘小游戏(附源码!免费下载!)

    马踏棋盘游戏小项目 设计主要功能 运用的数据结构 运行流程讲解及录像 项目分类截图及源码链接! 设计主要功能 (1)设计内容:设计一个马踏棋盘游戏,马作为棋子,以马走日字的走法,将整个棋盘一次性走完, ...

  5. c语言使用easyX图形库制作打气球小游戏

    大一c语言使用easyX图形库制作打气球小游戏 如果你是入门easyX图形库,那么这个打气球小游戏将会是和不错的入门项目选择,easyX开创了可视化窗口,使用户更加直观的了解到对象的变化,总代码以及素 ...

  6. 基于OpenPose和Human segmentation的游戏人物解析(附源码)

    基于OpenPose和Human segmentation的游戏人物解析(附源码) --基于PaddleHub的真人街霸游戏 Github AI studio 街霸(Street Fighter)是大 ...

  7. 基于stm32、0.96寸OLED实现的贪吃蛇小游戏(详细源码注释)

    简介:本实验基于stm32最小系统.0.96寸OLED(68*128)和摇杆实现一个经典的贪吃蛇小游戏.项目源码地址:点击下载. 硬件设计: 普通摇杆,0.96寸OLED 单色屏幕(SPI协议通讯), ...

  8. 基于stm32、0.96寸OLED实现的俄罗斯方块小游戏(详细源码注释)

    概述:本实验基于stm32最小系统.0.96寸OLED(68*128)和摇杆实现一个经典的俄罗斯方块小游戏.项目源码地址:点击下载. 硬件要求: 普通摇杆,两个电位器和一个开关组成,左右摇动控制一个电 ...

  9. c语言小游戏跳一跳代码及注释,如何获得微信小游戏跳一跳源码以及源代码组合包括哪些...

    原标题:如何获得微信小游戏跳一跳源码以及源代码组合包括哪些 很多小游戏都是由源代码编写而成的,那大家知道源代码组合包括哪些吗?手机游戏源代码怎么使用的呢?还有,如何获得微信小游戏跳一跳源码?下面就由奇 ...

最新文章

  1. 请问一个跨进程调用的问题?
  2. 如何知道现在是否单用户模式_新手运营Shopee现在是否来得及,商品的转化如何提高?...
  3. bzoj1452: [JSOI2009]Count
  4. UI组件之TextView及其子类(二)RadioButton和CheckBox
  5. Python:构造函数和析构函数
  6. 拼数pascal程序
  7. 把c++语言转换为go的工具_V,新编程语言来袭!与Go类似,跟C一样快
  8. C语言程序判断计算机的CPU大小端
  9. 响应式方案调研及前端开发管理思考
  10. 苹果手机输入屏保后锁屏_修一块手机屏幕要7080元?
  11. 基于JAVA+SpringMVC+Mybatis+MYSQL的保险业务管理系统
  12. 了解java虚拟机mdash;垃圾回收算法(5)
  13. 使用GDAL库中的RPC校正问题
  14. 安卓从入门到进阶推荐学习方法与书籍整理(pdf)
  15. 怎样分析数据致提高产出?(一)
  16. zabbix报警 High swap space usage ( less than 50% free) 解决方案
  17. C 语言画一颗圣诞树
  18. linux装百度网盘不能运行,在Deepin系统中安装百度网盘的两种方法
  19. 软件工程实践者的思想
  20. 技能Get·PC及手机微信聊天记录的备份与还原

热门文章

  1. 佳能5d3右下角红灯一直闪_佳能700D右下角的红灯一直闪
  2. 2.1 数据库之序列,索引和同义词
  3. CloudStack常用API
  4. tinyint类型解释
  5. 宝塔安装数据库,连接报错Table‘ACT_GE_PROPERTY‘ not exist
  6. ArcTime 制作中英文字幕视频
  7. executeupdate mysql_executeUpdate(sql) 返回值是什么?
  8. Python实践教程--列表与元组
  9. list-style-type的样式和颜色
  10. 内部用户触达方式主要有哪些?