您好,登錄后才能下訂單哦!
這篇文章主要介紹“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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。