├── Forwa_font.TTF ├── sounds ├── fall.wav ├── line.wav └── tetris.wav ├── images └── tetris_tile.png ├── LICENSE ├── README.md ├── .gitignore └── Main.cpp /Forwa_font.TTF: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThomasGITH/Tetris-SFML/HEAD/Forwa_font.TTF -------------------------------------------------------------------------------- /sounds/fall.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThomasGITH/Tetris-SFML/HEAD/sounds/fall.wav -------------------------------------------------------------------------------- /sounds/line.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThomasGITH/Tetris-SFML/HEAD/sounds/line.wav -------------------------------------------------------------------------------- /sounds/tetris.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThomasGITH/Tetris-SFML/HEAD/sounds/tetris.wav -------------------------------------------------------------------------------- /images/tetris_tile.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ThomasGITH/Tetris-SFML/HEAD/images/tetris_tile.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Thomas Linssen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Tetris-SFML- 2 | A Tetris game made in C++ using SFML library. 3 | 4 | ![tetris_scrnshot](https://user-images.githubusercontent.com/31830553/69095053-d88d7780-0a51-11ea-931b-ee0abbd12080.png) 5 | 6 | ## Features 7 | It's Tetris. 8 | 9 | Use the left/right arrow keys to adjust the horizontal position of a falling piece. 10 | Use the 'up' arrow to rotate a falling piece. 11 | Use the 'down' arrow key to increase the falling-speed of a piece. 12 | 13 | Build horizontal lines of blocks/tiles for the lines to 'collapse' and to gain points from them. 14 | The game is over when the blocks/tiles (attempt to) reach outside of the window on the y-axis. 15 | 16 | ## Software Analysis 17 | It's made using C++ and the SFML library. I'm using these as C++ is my preferred language and SFML is a reasonably lightweight graphics library that fits a task like this. 18 | 19 | My second pick would have been Javascript. It was quite hard for me to decide between the two. Tetris is a game perfect to run in the browser, and Javascript itself is very lightweight and does not require any additional (graphics) libraries to display the game since you can just use its built-in canvas. I ended up choosing C++/SFML anyway since I liked the idea of having my own desktop-version of the game and I didn't want to make it myself too easy. 20 | 21 | I also considered using Java, but quickly dismissed that idea as I did not see any specific advantage of doing so except that it might be more portable. If I had decided to use it, then I'd probably have used JavaFX to display the game's visuals. 22 | 23 | The thoughts of using either the Unity engine or the 'Phaser' framework also crossed my mind, but were both quickly dismissed as using Unity would have been overkill, and using Phaser would just have been unnecessary since native Javascript already has everything that you might need for a task like this. 24 | 25 | Usually when making games, I directly manipulate the objects that get visibly drawn to the screen, but for this project I'll be manipulating a 2D array of characters. I'll then use SFML to 'read' my array of characters and represent them on the screen using a specific texture for each character. 26 | 27 | ## Learning purpose 28 | Tetris has been on my bucket list for quite a while. It's more complicated than Snake mainly due to the math required to rotate the falling pieces, but also due to the collision physics involved in the game. Still, I already have quite a good grasp on both of these subjects so I think the task will be easy enough for me to complete within a week. 29 | 30 | I think making your own version of a 'simple-but-classic' game such as Tetris is usually a nice little programming challenge since they all have a certain unique mechanic that serves as a new fresh-feeling problem. In 'Tetris' it's the rotating pieces and collision, in 'Snake' it's the player's movement behaviour, in 'Pacman' it's the ghost AI's, etc. 31 | 32 | ## Planning 33 | I will approach this task by doing the complicated work first; meaning that I won't be touching the game's window and outer visuals until all the inner logic of it is (as good as) finished. 34 | 35 | As I said earlier, I think the task is easy enough to finish within a week. When I'm done, I'll make a desktop shortcut linking to the executable, and give it a thumbnail; just for the satisfaction =) 36 | 37 | | | Monday | Tuesday | Wednesday | Thursday | Friday | 38 | | --- | --- | --- | --- | --- | --- | 39 | ||Setup SFML. Create a window, test the input, and make the array. Also make a 'piece' class/struct and a Vector struct.|Start working on making the pieces rotate. Use the console as a temporary visual aspect. Also setup a 'delta-time' variable and start letting pieces fall down tick by tick.|Implement collision. Next is to visualize everything with textures in the SFML window. Make some textures in paint, or get some from the internet.|Continue visualizing the game.|Final day. Optimize the code and get rid of any potential bugs. 40 | 41 | ## Sources 42 | I'm using older projects for reference. 43 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.suo 8 | *.user 9 | *.userosscache 10 | *.sln.docstates 11 | 12 | # User-specific files (MonoDevelop/Xamarin Studio) 13 | *.userprefs 14 | 15 | # Build results 16 | [Dd]ebug/ 17 | [Dd]ebugPublic/ 18 | [Rr]elease/ 19 | [Rr]eleases/ 20 | x64/ 21 | x86/ 22 | bld/ 23 | [Bb]in/ 24 | [Oo]bj/ 25 | [Ll]og/ 26 | 27 | # Visual Studio 2015/2017 cache/options directory 28 | .vs/ 29 | # Uncomment if you have tasks that create the project's static files in wwwroot 30 | #wwwroot/ 31 | 32 | # Visual Studio 2017 auto generated files 33 | Generated\ Files/ 34 | 35 | # MSTest test Results 36 | [Tt]est[Rr]esult*/ 37 | [Bb]uild[Ll]og.* 38 | 39 | # NUNIT 40 | *.VisualState.xml 41 | TestResult.xml 42 | 43 | # Build Results of an ATL Project 44 | [Dd]ebugPS/ 45 | [Rr]eleasePS/ 46 | dlldata.c 47 | 48 | # Benchmark Results 49 | BenchmarkDotNet.Artifacts/ 50 | 51 | # .NET Core 52 | project.lock.json 53 | project.fragment.lock.json 54 | artifacts/ 55 | **/Properties/launchSettings.json 56 | 57 | # StyleCop 58 | StyleCopReport.xml 59 | 60 | # Files built by Visual Studio 61 | *_i.c 62 | *_p.c 63 | *_i.h 64 | *.ilk 65 | *.meta 66 | *.obj 67 | *.iobj 68 | *.pch 69 | *.pdb 70 | *.ipdb 71 | *.pgc 72 | *.pgd 73 | *.rsp 74 | *.sbr 75 | *.tlb 76 | *.tli 77 | *.tlh 78 | *.tmp 79 | *.tmp_proj 80 | *.log 81 | *.vspscc 82 | *.vssscc 83 | .builds 84 | *.pidb 85 | *.svclog 86 | *.scc 87 | 88 | # Chutzpah Test files 89 | _Chutzpah* 90 | 91 | # Visual C++ cache files 92 | ipch/ 93 | *.aps 94 | *.ncb 95 | *.opendb 96 | *.opensdf 97 | *.sdf 98 | *.cachefile 99 | *.VC.db 100 | *.VC.VC.opendb 101 | 102 | # Visual Studio profiler 103 | *.psess 104 | *.vsp 105 | *.vspx 106 | *.sap 107 | 108 | # Visual Studio Trace Files 109 | *.e2e 110 | 111 | # TFS 2012 Local Workspace 112 | $tf/ 113 | 114 | # Guidance Automation Toolkit 115 | *.gpState 116 | 117 | # ReSharper is a .NET coding add-in 118 | _ReSharper*/ 119 | *.[Rr]e[Ss]harper 120 | *.DotSettings.user 121 | 122 | # JustCode is a .NET coding add-in 123 | .JustCode 124 | 125 | # TeamCity is a build add-in 126 | _TeamCity* 127 | 128 | # DotCover is a Code Coverage Tool 129 | *.dotCover 130 | 131 | # AxoCover is a Code Coverage Tool 132 | .axoCover/* 133 | !.axoCover/settings.json 134 | 135 | # Visual Studio code coverage results 136 | *.coverage 137 | *.coveragexml 138 | 139 | # NCrunch 140 | _NCrunch_* 141 | .*crunch*.local.xml 142 | nCrunchTemp_* 143 | 144 | # MightyMoose 145 | *.mm.* 146 | AutoTest.Net/ 147 | 148 | # Web workbench (sass) 149 | .sass-cache/ 150 | 151 | # Installshield output folder 152 | [Ee]xpress/ 153 | 154 | # DocProject is a documentation generator add-in 155 | DocProject/buildhelp/ 156 | DocProject/Help/*.HxT 157 | DocProject/Help/*.HxC 158 | DocProject/Help/*.hhc 159 | DocProject/Help/*.hhk 160 | DocProject/Help/*.hhp 161 | DocProject/Help/Html2 162 | DocProject/Help/html 163 | 164 | # Click-Once directory 165 | publish/ 166 | 167 | # Publish Web Output 168 | *.[Pp]ublish.xml 169 | *.azurePubxml 170 | # Note: Comment the next line if you want to checkin your web deploy settings, 171 | # but database connection strings (with potential passwords) will be unencrypted 172 | *.pubxml 173 | *.publishproj 174 | 175 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 176 | # checkin your Azure Web App publish settings, but sensitive information contained 177 | # in these scripts will be unencrypted 178 | PublishScripts/ 179 | 180 | # NuGet Packages 181 | *.nupkg 182 | # The packages folder can be ignored because of Package Restore 183 | **/[Pp]ackages/* 184 | # except build/, which is used as an MSBuild target. 185 | !**/[Pp]ackages/build/ 186 | # Uncomment if necessary however generally it will be regenerated when needed 187 | #!**/[Pp]ackages/repositories.config 188 | # NuGet v3's project.json files produces more ignorable files 189 | *.nuget.props 190 | *.nuget.targets 191 | 192 | # Microsoft Azure Build Output 193 | csx/ 194 | *.build.csdef 195 | 196 | # Microsoft Azure Emulator 197 | ecf/ 198 | rcf/ 199 | 200 | # Windows Store app package directories and files 201 | AppPackages/ 202 | BundleArtifacts/ 203 | Package.StoreAssociation.xml 204 | _pkginfo.txt 205 | *.appx 206 | 207 | # Visual Studio cache files 208 | # files ending in .cache can be ignored 209 | *.[Cc]ache 210 | # but keep track of directories ending in .cache 211 | !*.[Cc]ache/ 212 | 213 | # Others 214 | ClientBin/ 215 | ~$* 216 | *~ 217 | *.dbmdl 218 | *.dbproj.schemaview 219 | *.jfm 220 | *.pfx 221 | *.publishsettings 222 | orleans.codegen.cs 223 | 224 | # Including strong name files can present a security risk 225 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 226 | #*.snk 227 | 228 | # Since there are multiple workflows, uncomment next line to ignore bower_components 229 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 230 | #bower_components/ 231 | 232 | # RIA/Silverlight projects 233 | Generated_Code/ 234 | 235 | # Backup & report files from converting an old project file 236 | # to a newer Visual Studio version. Backup files are not needed, 237 | # because we have git ;-) 238 | _UpgradeReport_Files/ 239 | Backup*/ 240 | UpgradeLog*.XML 241 | UpgradeLog*.htm 242 | ServiceFabricBackup/ 243 | *.rptproj.bak 244 | 245 | # SQL Server files 246 | *.mdf 247 | *.ldf 248 | *.ndf 249 | 250 | # Business Intelligence projects 251 | *.rdl.data 252 | *.bim.layout 253 | *.bim_*.settings 254 | *.rptproj.rsuser 255 | 256 | # Microsoft Fakes 257 | FakesAssemblies/ 258 | 259 | # GhostDoc plugin setting file 260 | *.GhostDoc.xml 261 | 262 | # Node.js Tools for Visual Studio 263 | .ntvs_analysis.dat 264 | node_modules/ 265 | 266 | # Visual Studio 6 build log 267 | *.plg 268 | 269 | # Visual Studio 6 workspace options file 270 | *.opt 271 | 272 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 273 | *.vbw 274 | 275 | # Visual Studio LightSwitch build output 276 | **/*.HTMLClient/GeneratedArtifacts 277 | **/*.DesktopClient/GeneratedArtifacts 278 | **/*.DesktopClient/ModelManifest.xml 279 | **/*.Server/GeneratedArtifacts 280 | **/*.Server/ModelManifest.xml 281 | _Pvt_Extensions 282 | 283 | # Paket dependency manager 284 | .paket/paket.exe 285 | paket-files/ 286 | 287 | # FAKE - F# Make 288 | .fake/ 289 | 290 | # JetBrains Rider 291 | .idea/ 292 | *.sln.iml 293 | 294 | # CodeRush 295 | .cr/ 296 | 297 | # Python Tools for Visual Studio (PTVS) 298 | __pycache__/ 299 | *.pyc 300 | 301 | # Cake - Uncomment if you are using it 302 | # tools/** 303 | # !tools/packages.config 304 | 305 | # Tabs Studio 306 | *.tss 307 | 308 | # Telerik's JustMock configuration file 309 | *.jmconfig 310 | 311 | # BizTalk build output 312 | *.btp.cs 313 | *.btm.cs 314 | *.odx.cs 315 | *.xsd.cs 316 | 317 | # OpenCover UI analysis results 318 | OpenCover/ 319 | 320 | # Azure Stream Analytics local run output 321 | ASALocalRun/ 322 | 323 | # MSBuild Binary and Structured Log 324 | *.binlog 325 | 326 | # NVidia Nsight GPU debugger configuration file 327 | *.nvuser 328 | 329 | # MFractors (Xamarin productivity tool) working folder 330 | .mfractor/ 331 | -------------------------------------------------------------------------------- /Main.cpp: -------------------------------------------------------------------------------- 1 | #pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup") //prevents the console from opening 2 | #include 3 | #include 4 | 5 | #define PI 3.14159265359 6 | 7 | typedef uint8_t byte; 8 | 9 | byte grid[20][12]; 10 | byte colliders[20][12]; 11 | 12 | struct Vec2 13 | { 14 | byte y, x; 15 | Vec2(){} 16 | Vec2(byte dy, byte dx) : x(dx), y(dy){} 17 | }; 18 | 19 | struct Piece 20 | { 21 | Vec2 a, b, c, d; 22 | Piece(){} 23 | Piece(byte ax, byte ay, byte bx, byte by, byte cx, byte cy, byte dx, byte dy) 24 | : a(Vec2(ax,ay)), b(Vec2(bx, by)), c(Vec2(cx, cy)), d(Vec2(dx, dy)) {} 25 | Piece(Vec2 da, Vec2 db, Vec2 dc, Vec2 dd) 26 | : a(da), b(db), c(dc), d(dd){} 27 | }; 28 | 29 | enum PIECE_TYPE 30 | { 31 | S, Z, L, J, SQR, I, T 32 | }; 33 | 34 | Piece CreatePiece(PIECE_TYPE type) 35 | { 36 | switch (type) 37 | { 38 | case S : return Piece(Vec2(1, 5), Vec2(1, 4), Vec2(0, 4), Vec2(2, 5)); 39 | break; 40 | case Z: return Piece(Vec2(1, 4), Vec2(1, 5), Vec2(0, 5), Vec2(2, 4)); 41 | break; 42 | case L: return Piece(Vec2(1, 5), Vec2(1, 4), Vec2(1, 6), Vec2(0, 6)); 43 | break; 44 | case J: return Piece(Vec2(1, 5), Vec2(1, 4), Vec2(1, 6), Vec2(0, 4)); 45 | break; 46 | case SQR: return Piece(Vec2(1, 5), Vec2(1, 4), Vec2(0, 5), Vec2(0, 4)); 47 | break; 48 | case I: return Piece(Vec2(1, 5), Vec2(1, 4), Vec2(1, 6), Vec2(1, 7)); 49 | break; 50 | case T: return Piece(Vec2(1, 5), Vec2(1, 4), Vec2(1, 6), Vec2(0, 5)); 51 | break; 52 | default: 53 | break; 54 | } 55 | } 56 | 57 | void rotate(Piece& piece) 58 | { 59 | float angle = 90 * (PI / 180); 60 | 61 | float b_offset_x = piece.b.x - piece.a.x; 62 | float c_offset_x = piece.c.x - piece.a.x; 63 | float d_offset_x = piece.d.x - piece.a.x; 64 | 65 | float b_offset_y = piece.b.y - piece.a.y; 66 | float c_offset_y = piece.c.y - piece.a.y; 67 | float d_offset_y = piece.d.y - piece.a.y; 68 | 69 | float pbx = piece.a.x + (b_offset_x * cosf(angle) - b_offset_y * sinf(angle)); 70 | float pby = piece.a.y + (b_offset_x * sinf(angle) + b_offset_y * cosf(angle)); 71 | 72 | float pcx = piece.a.x + (c_offset_x * cosf(angle) - c_offset_y * sinf(angle)); 73 | float pcy = piece.a.y + (c_offset_x * sinf(angle) + c_offset_y * cosf(angle)); 74 | 75 | float pdx = piece.a.x + (d_offset_x * cosf(angle) - d_offset_y * sinf(angle)); 76 | float pdy = piece.a.y + (d_offset_x * sinf(angle) + d_offset_y * cosf(angle)); 77 | 78 | //Prevents rotating into objects and walls 79 | if (colliders[(byte)pby][(byte)pbx] != 2 && 80 | colliders[(byte)pcy][(byte)pcx] != 2 && 81 | colliders[(byte)pdy][(byte)pdx] != 2 && 82 | piece.a.x != 1 && piece.a.y != 1) 83 | { 84 | piece.b.x = pbx; 85 | piece.b.y = pby; 86 | 87 | piece.c.x = pcx; 88 | piece.c.y = pcy; 89 | 90 | piece.d.x = pdx; 91 | piece.d.y = pdy; 92 | } 93 | } 94 | 95 | int main() 96 | { 97 | //16x16 tiles 98 | sf::Texture tile_tex; 99 | tile_tex.loadFromFile("images/tetris_tile.png"); 100 | 101 | sf::Sprite tile(tile_tex); 102 | tile.setScale(2.83, 2.83); 103 | 104 | float tile_size = tile_tex.getSize().x * tile.getScale().x; 105 | size_t width = tile_size * 12, height = tile_size * 20; 106 | 107 | //Music 108 | sf::Music track; 109 | track.openFromFile("sounds/tetris_theme.wav"); 110 | track.play(); 111 | track.setLoop(true); 112 | 113 | //Sound effects 114 | sf::SoundBuffer buffer; 115 | buffer.loadFromFile("sounds/fall.wav"); 116 | sf::Sound fall; 117 | fall.setBuffer(buffer); 118 | 119 | sf::SoundBuffer bufferTwo; 120 | bufferTwo.loadFromFile("sounds/line.wav"); 121 | sf::Sound line; 122 | line.setBuffer(bufferTwo); 123 | 124 | sf::SoundBuffer bufferThree; 125 | bufferThree.loadFromFile("sounds/tetris.wav"); 126 | sf::Sound tetris; 127 | tetris.setBuffer(bufferThree); 128 | 129 | //Setup score counter 130 | sf::Text score; 131 | sf::Font font; 132 | font.loadFromFile("Forwa_font.TTF"); 133 | score.setFont(font); 134 | score.setCharacterSize(15); 135 | score.setFillColor(sf::Color::Blue); 136 | score.setPosition(10,25); 137 | sf::Vector2 score_scale(1.5f,1.5f); 138 | score.setScale(score_scale); 139 | score.setString("Lines: 0"); 140 | 141 | //Setup window & create first piece 142 | sf::RenderWindow window(sf::VideoMode(width, height), "Tetris", sf::Style::Titlebar | sf::Style::Close); 143 | window.setKeyRepeatEnabled(true); 144 | window.setFramerateLimit(60); 145 | 146 | Piece piece = CreatePiece(static_cast((rand() % 7))); 147 | 148 | unsigned int timer = 0, gamespeed = 10, scoreCounter = 0; 149 | 150 | while (window.isOpen()) 151 | { 152 | srand(time(NULL)); 153 | 154 | sf::Event event; 155 | while (window.pollEvent(event)) 156 | { 157 | if (event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) 158 | { 159 | window.close(); 160 | } 161 | if (event.type == sf::Event::KeyPressed) 162 | { 163 | //Movement 164 | if (event.key.code == sf::Keyboard::Up) 165 | { 166 | rotate(piece); 167 | } 168 | else if (event.key.code == sf::Keyboard::Left && 169 | piece.a.x != 0 && piece.b.x != 0 && piece.c.x != 0 && piece.d.x != 0 && 170 | (colliders[piece.a.y][piece.a.x - 1]) != 2 && (colliders[piece.b.y][piece.b.x - 1]) != 2 && 171 | (colliders[piece.c.y][piece.c.x - 1]) != 2 && (colliders[piece.d.y][piece.d.x - 1]) != 2) 172 | { 173 | piece.a.x--; 174 | piece.b.x--; 175 | piece.c.x--; 176 | piece.d.x--; 177 | } 178 | else if (event.key.code == sf::Keyboard::Right && 179 | piece.a.x != 11 && piece.b.x != 11 && piece.c.x != 11 && piece.d.x != 11 && 180 | (colliders[piece.a.y][piece.a.x + 1]) != 2 && (colliders[piece.b.y][piece.b.x + 1]) != 2 && 181 | (colliders[piece.c.y][piece.c.x + 1]) != 2 && (colliders[piece.d.y][piece.d.x + 1]) != 2) 182 | { 183 | piece.a.x++; 184 | piece.b.x++; 185 | piece.c.x++; 186 | piece.d.x++; 187 | } 188 | } 189 | if (event.type == sf::Event::KeyReleased) 190 | { 191 | gamespeed = 10; 192 | } 193 | } 194 | 195 | if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down)) 196 | { 197 | gamespeed = 1; 198 | } 199 | 200 | window.clear(sf::Color(0, 220, 255, 1)); 201 | 202 | 203 | //Draw the current falling piece 204 | sf::Sprite piece_tile = tile; 205 | 206 | piece_tile.setPosition(tile_size * piece.a.x, tile_size * piece.a.y); 207 | window.draw(piece_tile); 208 | 209 | piece_tile.setPosition(tile_size * piece.b.x, tile_size * piece.b.y); 210 | window.draw(piece_tile); 211 | 212 | piece_tile.setPosition(tile_size * piece.c.x, tile_size * piece.c.y); 213 | window.draw(piece_tile); 214 | 215 | piece_tile.setPosition(tile_size * piece.d.x, tile_size * piece.d.y); 216 | window.draw(piece_tile); 217 | 218 | //Refresh the grid-array 219 | for (size_t i = 0; i < 20; i++) 220 | { 221 | for (size_t j = 0; j < 12; j++) 222 | { 223 | if (colliders[i][j] == 2) 224 | { 225 | grid[i][j] = 2; 226 | } 227 | else 228 | { 229 | grid[i][j] = 0; 230 | } 231 | } 232 | } 233 | 234 | //Clock 235 | if (timer > gamespeed) 236 | { 237 | //Collission checks 238 | if (grid[piece.a.y + 1][piece.a.x] == 2 || 239 | grid[piece.b.y + 1][piece.b.x] == 2 || 240 | grid[piece.c.y + 1][piece.c.x] == 2 || 241 | grid[piece.d.y + 1][piece.d.x] == 2 || 242 | piece.a.y == 19 || piece.b.y == 19 || piece.c.y == 19 || piece.d.y == 19 243 | ) 244 | { 245 | fall.play(); 246 | 247 | grid[piece.a.y][piece.a.x] = 2; 248 | grid[piece.b.y][piece.b.x] = 2; 249 | grid[piece.c.y][piece.c.x] = 2; 250 | grid[piece.d.y][piece.d.x] = 2; 251 | 252 | colliders[piece.a.y][piece.a.x] = 2; 253 | colliders[piece.b.y][piece.b.x] = 2; 254 | colliders[piece.c.y][piece.c.x] = 2; 255 | colliders[piece.d.y][piece.d.x] = 2; 256 | 257 | piece = CreatePiece(static_cast((rand() % 7))); 258 | } 259 | else 260 | { 261 | grid[piece.a.y + 1][piece.a.x] = 1; 262 | grid[piece.b.y + 1][piece.b.x] = 1; 263 | grid[piece.c.y + 1][piece.c.x] = 1; 264 | grid[piece.d.y + 1][piece.d.x] = 1; 265 | 266 | piece.a.y++; 267 | piece.b.y++; 268 | piece.c.y++; 269 | piece.d.y++; 270 | } 271 | 272 | //Check if the player has a line or 'tetris' 273 | byte tetris_row = 0; 274 | for (size_t i = 0; i < 20; i++) 275 | { 276 | byte blocks_in_a_row = 0; 277 | for (size_t j = 0; j < 12; j++) 278 | { 279 | if (colliders[i][j] == 2) 280 | { 281 | blocks_in_a_row++; 282 | } 283 | } 284 | if (blocks_in_a_row == 12) 285 | { 286 | tetris_row++; 287 | if (tetris_row >= 4) 288 | { 289 | tetris.play(); 290 | } 291 | else 292 | { 293 | line.play(); 294 | } 295 | for (size_t k = i; k > 0; k--) 296 | { 297 | for (size_t l = 0; l < 12; l++) 298 | { 299 | colliders[k][l] = colliders[k - 1][l]; 300 | } 301 | } 302 | scoreCounter++; 303 | char temp[256]; 304 | sprintf_s(temp, "Lines: %i", scoreCounter); 305 | score.setString(temp); 306 | } 307 | } 308 | 309 | //If game over, then close application 310 | for (size_t i = 0; i < 12; i++) 311 | { 312 | if (colliders[0][i] == 2) 313 | { 314 | window.close(); 315 | } 316 | } 317 | 318 | timer = 0; 319 | 320 | //Prints out the grid in the console (if enabled) for testing purposes 321 | /* 322 | for (size_t i = 0; i < 20; i++) 323 | { 324 | for (size_t j = 0; j < 12; j++) 325 | { 326 | printf("%i", grid[i][j]); 327 | } 328 | printf("\n"); 329 | } 330 | */ 331 | } 332 | else 333 | { 334 | timer++; 335 | } 336 | 337 | //Draws all the tiles 338 | for (size_t i = 0; i < 20; i++) 339 | { 340 | for (size_t j = 0; j < 12; j++) 341 | { 342 | if (grid[i][j] == 1) 343 | { 344 | sf::Sprite t = tile; 345 | t.setPosition(tile_size * j, tile_size * i); 346 | window.draw(t); 347 | } 348 | if (colliders[i][j] == 2) 349 | { 350 | sf::Sprite t = tile; 351 | t.setPosition(tile_size * j, tile_size * i); 352 | window.draw(t); 353 | } 354 | } 355 | } 356 | 357 | //Draw the score and finally update the window 358 | window.draw(score); 359 | window.display(); 360 | } 361 | 362 | return 0; 363 | } 364 | --------------------------------------------------------------------------------