本文主要是介绍本人菜鸟学习c++并用sfml库制作简单的俄罗斯方块,还是个雏形, 谢谢各位大佬指教,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
效果图
方块类
#ifndef BLOCK_H_
#define BLOCK_H_
#include<SFML/Audio.hpp>
#include<SFML/Graphics.hpp>
#include<Windows.h>
namespace WT
{class Block{private:int mode_block[7][4] ={{1,3,5,7},{0,2,3,4},{1,2,3,4},{0,2,4,5},{1,3,5,4},{1,3,5,2},{0,1,2,4},};COORD curcoord[4] = { 0 };bool start;bool rotate;sf::Texture blocks;sf::Sprite spblock;int mode;int map[31][20] = {0};int map_mode[30][20] = {0};sf::SoundBuffer xiaochu;sf::Sound sxiaochu;public://显示默认构造函数Block(){start = rotate = false;blocks.loadFromFile("f:\\方块资源\\block.png");spblock.setTexture(blocks);mode = 0;xiaochu.loadFromFile("F:\\方块资源\\xiaochu.wav");sxiaochu.setBuffer(xiaochu);}//析构函数~Block(){}//数据更新void updata(){int count = 0;for (int index = 0; index < 20; index++){if (map[29][index]){count++;}}if (count > 17){for (int x = 29; x > 0; x--){for (int j = 0; j < 20; j++){map[x][j] = map[x - 1][j];}}sxiaochu.play();}if (!start){start = true;srand(time(nullptr));mode = rand() % 7;for (int index = 0; index < 4; index++){curcoord[index].X= (mode_block[mode][index] % 2+10)*20;curcoord[index].Y = (mode_block[mode][index] /2 + 2) * 20;}}}//绘制屏幕void draw(sf::RenderWindow& window){for (int x = 0; x < 30; x++){for (int y = 0; y < 20; y++){if (map[x][y]){spblock.setTextureRect(sf::IntRect(map_mode[x][y] * 20, 0, 20, 20));spblock.setPosition(y * 20, x * 20);window.draw(spblock);}}}spblock.setTextureRect(sf::IntRect(mode * 20, 0, 20, 20));for (int index = 0; index < 4; index++){spblock.setPosition(curcoord[index].X, curcoord[index].Y);window.draw(spblock);}}//自动下移void autoaddy(){bool flag = true;for (int index = 0; index < 4; index++){if (curcoord[index].Y == 580||map[curcoord[index].Y/20+1][curcoord[index].X/20]){draw_map();flag = false;start = false;}}if (flag){for (int index = 0; index < 4; index++){curcoord[index].Y+=2;}}}//坐标右移void addx(){bool flag = true;for (int index = 0; index < 4; index++){if (curcoord[index].X == 380){flag = false;}}if (flag){for (int index = 0; index < 4; index++){curcoord[index].X += 20;}}}//坐标左移void subx(){bool flag = true;for (int index = 0; index < 4; index++){if (curcoord[index].X == 0){flag = false;}}if (flag){for (int index = 0; index < 4; index++){curcoord[index].X -= 20;}}}//旋转图形void Rotate(){COORD temp = curcoord[1];for (int index = 0; index < 4; index++){int X = temp.Y - curcoord[index].Y;int Y = temp.X - curcoord[index].X;curcoord[index].X = temp.X - X;curcoord[index].Y = temp.Y + Y;}}//坐标下移void addy(){bool flag = true;for (int index = 0; index < 4; index++){if (curcoord[index].Y == 580 || map[curcoord[index].Y / 20 + 1][curcoord[index].X / 20]){draw_map();flag = false;start = false;}}if (flag){for (int index = 0; index < 4; index++){curcoord[index].Y += (curcoord[index].Y / 20+1) * 20-curcoord[index].Y;}}}//设置地图void draw_map(){for (int index = 0; index < 4; index++){map[curcoord[index].Y / 20][curcoord[index].X / 20] = 1;map_mode[curcoord[index].Y / 20][curcoord[index].X / 20] = mode;}}};
}
#endif // !BLOCK_H_
主函数
#include"blocks.h"
int main()
{WT::Block block;sf::RenderWindow window(sf::VideoMode(600, 600), "the_game");sf::Event event;sf::SoundBuffer sbuffer;sbuffer.loadFromFile("F:\\bgm.wav");sf::Sound sound(sbuffer);sound.setLoop(true); sf::SoundBuffer srotatebuff;srotatebuff.loadFromFile("F:\\方块资源\\rotate.wav");sf::Sound srotate(srotatebuff);sound.play();while (window.isOpen()){window.pollEvent(event);switch (event.type){case sf::Event::Closed:window.close();case sf::Event::KeyPressed:switch (event.key.code){case sf::Keyboard::D:block.addx();break;case sf::Keyboard::A:block.subx();break;case sf::Keyboard::Space:srotate.play();block.Rotate();break;case sf::Keyboard::S:block.addy();break;}default:break;}window.clear();block.updata();block.draw(window);window.display();Sleep(20);block.autoaddy();}return 0;
}
这篇关于本人菜鸟学习c++并用sfml库制作简单的俄罗斯方块,还是个雏形, 谢谢各位大佬指教的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!