c++实现俄罗斯方块游戏代码
程序员文章站
2022-06-15 13:02:40
俄罗斯方块c++1.创建项目2.总共需要创建两个文件,一个main.cpp,一个是elsfk2.h。本人使用的编译器是vs2019.3.在项目的源文件夹下创建一个文件夹image4.把下面两张图片重命...
俄罗斯方块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++实现俄罗斯方块游戏代码的文章就介绍到这了,更多相关c++俄罗斯方块内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
下一篇: Linux设备之网络驱动介绍