Download Source code Link is also in End of Page !!!!
Tetris Game
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | #include <SFML/Graphics.hpp> #include <time.h> using namespace sf; const int M = 20; const int N = 10; int field[M][N] = {0}; struct Point {int x,y;} a[4], b[4]; int figures[7][4] = { 1,3,5,7, // I 2,4,5,7, // Z 3,5,4,6, // S 3,5,4,7, // T 2,3,5,7, // L 3,5,7,6, // J 2,3,4,5, // O }; bool check() { for (int i=0;i<4;i++) if (a[i].x<0 || a[i].x>=N || a[i].y>=M) return 0; 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; 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()) { float time = clock.getElapsedTime().asSeconds(); clock.restart(); 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; } 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]; //////Rotate////// if (rotate) { Point p = a[1]; //center of rotation for (int i=0;i<4;i++) { int x = a[i].y-p.y; int y = a[i].x-p.x; a[i].x = p.x - x; a[i].y = p.y + y; } if (!check()) for (int i=0;i<4;i++) a[i]=b[i]; } ///////Tick////// if (timer>delay) { for (int i=0;i<4;i++) { b[i]=a[i]; a[i].y+=1; } if (!check()) { for (int i=0;i<4;i++) field[b[i].y][b[i].x]=colorNum; colorNum=1+rand()%7; int n=rand()%7; for (int i=0;i<4;i++) { a[i].x = figures[n][i] % 2; a[i].y = figures[n][i] / 2; } } timer=0; } ///////check lines////////// int k=M-1; for (int i=M-1;i>0;i--) { int count=0; for (int j=0;j<N;j++) { if (field[i][j]) count++; field[k][j]=field[i][j]; } if (count<N) k--; } dx=0; rotate=0; delay=0.3; /////////draw////////// window.clear(Color::White); window.draw(background); for (int i=0;i<M;i++) for (int j=0;j<N;j++) { if (field[i][j]==0) continue; s.setTextureRect(IntRect(field[i][j]*18,0,18,18)); s.setPosition(j*18,i*18); s.move(28,31); //offset window.draw(s); } for (int i=0;i<4;i++) { s.setTextureRect(IntRect(colorNum*18,0,18,18)); s.setPosition(a[i].x*18,a[i].y*18); s.move(28,31); //offset window.draw(s); } window.draw(frame); window.display(); } return 0; } |