您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍“c++如何实现俄罗斯方块游戏”,在日常操作中,相信很多人在c++如何实现俄罗斯方块游戏问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”c++如何实现俄罗斯方块游戏”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
俄罗斯方块c++
1.创建项目
2.总共需要创建两个文件,一个main.cpp,一个是elsfk2.h。本人使用的编译器是vs2019.
3.在项目的源文件夹下创建一个文件夹image
4.把下面两张图片重命名好放进刚创建的文件夹
elsfk.jpg
elsfk_block.jpg
5.把下列代码分别复制到对应文件中
----------------------------------------------------------------------------------- 以下是main.cpp 文件的内容 #include"elsfk2.h" int main() { srand((int)time(0)); //创建游戏窗口 RenderWindow window(VideoMode(COL*18/*+100*/, ROW*18-36),"ELSFK"); //添加游戏背景 Texture t1,t2; t1.loadFromFile("image/elsfk.jpg"); t2.loadFromFile("image/elsfk_block.jpg"); Sprite sprite_Bg(t1); Sprite sprite_block(t2); NewBlock(); Clock begin; float time1 = 0, time2 = 0; while (window.isOpen()) { time2 = begin.getElapsedTime().asSeconds(); begin.restart(); time1 += time2; //等待用户按下按键 keyEvent(&window); if (time1 > delay) { time1 = 0; blockDrop(); } delay = SPEED_NOM; window.draw(sprite_Bg); drawBlock(&sprite_block, &window); window.display(); } } -------------------------------------------------------------------------------- 以下为elsfk2.cpp #pragma once #include<SFML/Graphics.hpp> //图像处理头文件 //#include<SFML/Audio.hpp> #include<time.h> using namespace sf; #define ROW 22 //行 #define COL 10 //列 #define SPEED_NOM 0.5 //不加速运动的时间间隔 #define SPEED_QIK 0.05 //加速运动的时间间隔 int map[ROW][COL] = { 0 }; //游戏区域大小 int blocktype; //方块类型 int Delete=0; //删除的行数 float delay = SPEED_NOM; //时间间隔 void Move_x(int); void Rotate(); void ClearBlock(); void NewBlock(); void blockDrop(); void keyEvent(RenderWindow*); void drawBlock(Sprite*, RenderWindow*); bool check(); //存放7种方块的二维数组 int a[7][4] = { 1,3,5,7, 2,4,5,7, 3,5,4,6, 3,5,4,7, 2,3,5,7, 3,5,7,6, 2,3,4,5, }; //点的结构体 struct point { int x, y; }; //当前方块 point block[4]; //方块的备份 point bakblock[4]; //处理按键 void keyEvent(RenderWindow *w) { Event e; bool rotate = 0; int x = 0; while (w->pollEvent(e)) { if (e.type == Event::Closed) { w->close(); } if (e.type == Event::KeyPressed) { switch (e.key.code) { case Keyboard::Up: rotate = 1; break; case Keyboard::Left: x = -1; break; case Keyboard::Right: x = 1; break; default: break; } } if (Keyboard::isKeyPressed(Keyboard::Down)) { delay = SPEED_QIK; } if (x) { Move_x(x); } if (rotate) { Rotate(); } } } //消除完成的行 void ClearBlock() { int k = ROW - 1; for (int i = ROW - 1; i > 0; i--) { int count = 0; for (int j = 0; j < COL; j++) { if (map[i][j]) { count++; } map[k][j] = map[i][j]; } if (count < COL) { k--; } } } //检查移动合理性 bool check() { for (int i = 0; i < 4; i++){ if( block[i].x<0||block[i].x>=COL||block[i].y>=ROW|| map[block[i].y][block[i].x]){ return 0; } } return 1; } //方块降落 void blockDrop() { for (int i = 0; i < 4; i++) { bakblock[i] = block[i]; block[i].y++; } if (!check()) { for (int j = 0; j < 4; j++){ map[bakblock[j].y][bakblock[j].x] = blocktype; } NewBlock(); ClearBlock(); } } //左右移动 void Move_x(int x) { for (int i = 0; i < 4; i++){ bakblock[i] = block[i]; block[i].x += x; } if (!check()) { for (int i = 0; i < 4; i++) { block[i] = bakblock[i]; } } } //旋转****************************** important void Rotate() { if (blocktype == 7) { return; } point p = block[1]; for (int i = 0; i < 4; i++) { bakblock[i] = block[i]; block[i].x = p.x - bakblock[i].y + p.y; block[i].y = bakblock[i].x - p.x + p.y; } if (!check()) { for (int i = 0; i < 4; i++) { block[i] = bakblock[i]; } } } //生成方块 void NewBlock() { blocktype = 1 + rand() % 7; for (int i = 0; i < 4; i++) { block[i].x = a[blocktype-1][i] % 2+4; block[i].y = a[blocktype-1][i] / 2; } } //绘制方块 void drawBlock(Sprite *b, RenderWindow *w) { //完成的方块 for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { if (map[i][j] != 0) { b->setTextureRect(IntRect(map[i][j]*18,0,18,18)); b->setPosition(j * 18, i * 18); b->move(0, -36); w->draw(*b); } } } for (int j = 0; j < 4; j++) { b->setTextureRect(IntRect(blocktype * 18, 0, 18, 18)); b->setPosition(block[j].x * 18, block[j].y * 18); b->move(0, -36); w->draw(*b); } }
到此,关于“c++如何实现俄罗斯方块游戏”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。