概述
俄罗斯方块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++俄罗斯方块内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!
最后
以上就是友好高山为你收集整理的c++实现俄罗斯方块游戏代码的全部内容,希望文章能够帮你解决c++实现俄罗斯方块游戏代码所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复