From 841685787891c76c9fbe9507d8ae18f13907484a Mon Sep 17 00:00:00 2001 From: coderakstar <64554764+coderakstar@users.noreply.github.com> Date: Sun, 4 Oct 2020 18:14:49 +0530 Subject: [PATCH] Added a heading of the tetris program I have added a heading for your tetris program. Please accept my pull request as I did it for hacktober fest. --- 01 Tetris/main.cpp | 112 +++++++++++++++++++++++---------------------- 1 file changed, 57 insertions(+), 55 deletions(-) diff --git a/01 Tetris/main.cpp b/01 Tetris/main.cpp index e773e9c..1f6ffc7 100644 --- a/01 Tetris/main.cpp +++ b/01 Tetris/main.cpp @@ -1,7 +1,9 @@ -#include -#include -using namespace sf; - + +// C++ program for tetris game +#include +#include +using namespace sf; + const int M = 20; const int N = 10; @@ -19,8 +21,8 @@ int figures[7][4] = 2,3,5,7, // L 3,5,7,6, // J 2,3,4,5, // O -}; - +}; + bool check() { for (int i=0;i<4;i++) @@ -28,47 +30,47 @@ bool check() else if (field[a[i].y][a[i].x]) return 0; return 1; -}; - - -int main() -{ - srand(time(0)); - - RenderWindow window(VideoMode(320, 480), "The Game!"); - - Texture t1,t2,t3; +}; + + +int main() +{ + srand(time(0)); + + RenderWindow window(VideoMode(320, 480), "The Game!"); + + Texture t1,t2,t3; t1.loadFromFile("images/tiles.png"); t2.loadFromFile("images/background.png"); t3.loadFromFile("images/frame.png"); Sprite s(t1), background(t2), frame(t3); - - int dx=0; bool rotate=0; int colorNum=1; - float timer=0,delay=0.3; - - Clock clock; - - while (window.isOpen()) - { + + int dx=0; bool rotate=0; int colorNum=1; + float timer=0,delay=0.3; + + Clock clock; + + while (window.isOpen()) + { float time = clock.getElapsedTime().asSeconds(); clock.restart(); - timer+=time; - - Event e; - while (window.pollEvent(e)) - { - if (e.type == Event::Closed) - window.close(); - + timer+=time; + + Event e; + while (window.pollEvent(e)) + { + if (e.type == Event::Closed) + window.close(); + if (e.type == Event::KeyPressed) if (e.key.code==Keyboard::Up) rotate=true; else if (e.key.code==Keyboard::Left) dx=-1; - else if (e.key.code==Keyboard::Right) dx=1; - } - + else if (e.key.code==Keyboard::Right) dx=1; + } + if (Keyboard::isKeyPressed(Keyboard::Down)) delay=0.05; - + //// <- Move -> /// for (int i=0;i<4;i++) { b[i]=a[i]; a[i].x+=dx; } if (!check()) for (int i=0;i<4;i++) a[i]=b[i]; @@ -86,7 +88,7 @@ int main() } if (!check()) for (int i=0;i<4;i++) a[i]=b[i]; } - + ///////Tick////// if (timer>delay) { @@ -105,9 +107,9 @@ int main() } } - timer=0; - } - + timer=0; + } + ///////check lines////////// int k=M-1; for (int i=M-1;i>0;i--) @@ -119,13 +121,13 @@ int main() field[k][j]=field[i][j]; } if (count