C++入门 |
您所在的位置:网站首页 › 地铁跑酷源代码 › C++入门 |
参考
《C和C++游戏趣味编程》 童晶
火柴人跑酷
游戏的思路是,玩家通过键盘控制火柴人的奔跑和跳跃,躲避蝙蝠到达终点。游戏地图随机生成,随着关卡数增加,游戏难度越来越大 定义Player类 class Player { public: IMAGE im_show; // 当前时刻要显示的图像 float x_left, y_bottom; // 左下角位置 float vx, vy; // 速度 float width, height; // 图片的宽度、高度 void draw() { putimagePng(x_left, y_bottom - height, &im_show); } void initialize() { loadimage(&im_show, _T("standright.png")); width = im_show.getwidth(); height = im_show.getheight(); updateXY(WIDTH / 2, HEIGHT / 2); vx = 10; vy = 10; } void updateXY(float mx, float my) { x_left = mx; y_bottom = my; } };定义全局变量,初始化并显示: IMAGE im_land; IMAGE im_bk; Player player; void startup() { player.initialize(); loadimage(&im_land, _T("land.png")); loadimage(&im_bk, _T("landscape1.png")); initgraph(WIDTH, HEIGHT); BeginBatchDraw(); } void show() { putimage(-100, -100, &im_bk); putimage(WIDTH / 2, HEIGHT / 2, &im_land); player.draw(); FlushBatchDraw(); }通过按键控制角色移动: void updateWithInput() { if (_kbhit()) { char input = getch(); if (input == 'A') { player.x_left -= player.vx; } if (input == 'D') { player.x_left += player.vx; } if (input == 'W') { player.y_bottom -= player.vy; } if (input == 'S') { player.y_bottom += player.vy; } } } 异步输入利用异步输入函数GetAsyncKeyState(),可以同时识别两个按键被按下的情况,玩家可以用A、S、D、W或左、右、上、下方向键控制火柴人移动: void updateWithInput() { if (_kbhit()) { if (GetAsyncKeyState(VK_LEFT) || GetAsyncKeyState('A')) { player.x_left -= player.vx; } if (GetAsyncKeyState(VK_RIGHT) || GetAsyncKeyState('D')) { player.x_left += player.vx; } if (GetAsyncKeyState(VK_UP) || GetAsyncKeyState('W')) { player.y_bottom -= player.vy; } if (GetAsyncKeyState(VK_DOWN) || GetAsyncKeyState('S')) { player.y_bottom += player.vy; } } } 枚举类型状态切换火柴人会有向左站立、向右站立、向左奔跑、向右奔跑、向左跳跃、向右跳跃、死亡这7种状态: 在Player类中新增成员变量存储向右站立图像、向左站立图像和角色状态: IMAGE im_standright; IMAGE im_standleft; PlayerStatus playerStatus; // 当前状态在initialize()中对新增成员变量初始化: loadimage(&im_standright, _T("standright.png")); loadimage(&im_standleft, _T("standleft.png")); playerStatus = standright; im_show = im_standright;在updateWithInput()中,当键盘控制角色移动时,角色站立方向也应变化: if (GetAsyncKeyState(VK_LEFT) || GetAsyncKeyState('A')) { player.x_left -= player.vx; player.playerStatus = standleft; } if (GetAsyncKeyState(VK_RIGHT) || GetAsyncKeyState('D')) { player.x_left += player.vx; player.playerStatus = standright; } 添加奔跑动画在Player类中添加成员变量ims_runright存储向右奔跑的8张图片,并初始化: vector ims_runright; int animId; void initialize() { loadimage(&im_standright, _T("standright.png")); loadimage(&im_standleft, _T("standleft.png")); playerStatus = standright; im_show = im_standright; width = im_standright.getwidth(); height = im_standright.getheight(); TCHAR filename[80]; for (int i = 0; i = ims_runright.size()) { animId = 0; } } im_show = ims_runright[animId]; } 实现跳跃在Player类中添加成员变量存储向右、向左跳跃的图片和重力加速度,并初始化: IMAGE im_jumpright; IMAGE im_jumpleft; float gravity; void initialize() { loadimage(&im_jumpright, _T("jumpright.png")); loadimage(&im_jumpleft, _T("jumpleft.png")); vx = 10; vy = 0; gravity = 3; }添加beginJump(),控制动画切换并给角色一个向上初速度: void beginJump() { if (playerStatus != jumpleft && playerStatus != jumpright) // 已在空中不能起跳 { if (playerStatus == runleft || playerStatus == standleft) { im_show = im_jumpleft; playerStatus = jumpleft; } else if (playerStatus == runright || playerStatus == standright) { im_show = im_jumpright; playerStatus = jumpright; } vy = -30; } }新增成员函数updateYcoordinate(),自动完成自由落体运动: void updateYcoordinate() { if (playerStatus == jumpleft || playerStatus == jumpright) { vy += gravity; y_bottom += vy; if (y_bottom > HEIGHT / 2) { y_bottom = HEIGHT / 2; // 保证正好落在地面上 if (playerStatus == jumpleft) { playerStatus = standleft; } if (playerStatus == jumpright) { playerStatus = standright; } } } } 地面类 class Land { public: IMAGE im_land; float left_x, right_x, top_y; float land_width, land_height; void initialize() { loadimage(&im_land, _T("land.png")); land_width = im_land.getwidth(); land_height = im_land.getheight(); left_x = WIDTH / 2; right_x = left_x + land_width; top_y = HEIGHT / 2; } void draw() { putimage(left_x, top_y, &im_land); } }; 场景类场景类包括背景图片和多个地面对象 class Scene { public: IMAGE im_bk; vector lands; void draw() { putimage(-100, -100, &im_bk); for (int i = 0; i |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |