AllenYuan

colors and fast fall

May 24th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <time.h>
  3. using namespace sf;
  4.  
  5. const int M = 20;
  6. const int N = 10;
  7.  
  8. int field[M][N] = {0}; // tetris grid
  9.  
  10. struct Point
  11. { int x,y; } a[4], b[4]; // current/next tile = 4 points
  12.  
  13. int figures[7][4] = // encode blocks as 4 squares on a 2x4 rectangular grid indexed 0,...,7
  14. {
  15.   1,3,5,7, // I
  16.   2,4,5,7, // Z
  17.   3,5,4,6, // S
  18.   3,5,4,7, // T
  19.   2,3,5,7, // L
  20.   3,5,7,6, // J
  21.   2,3,4,5, // 0
  22. };
  23.  
  24. bool check() // collision detection
  25. {
  26.   for (int i = 0; i < 4; i++)
  27.     if (a[i].x < 0 || a[i].x >= N || a[i].y >= M) return 0;
  28.     else if (field[a[i].y][a[i].x]) return 0;
  29.  
  30.   return 1;
  31. };
  32.  
  33. int main()
  34. {
  35.   srand(time(0));
  36.   RenderWindow window(VideoMode(320, 480), "The Game!"); // window config
  37.  
  38.   Texture t;
  39.   t.loadFromFile("/Users/alleyuan/code-snippets/16_games/01 Tetris/images/tiles.png"); // load sprite. replace my absolute path
  40.  
  41.   Sprite s(t);
  42.   s.setTextureRect(IntRect(0,0,18,18));
  43.  
  44.   int dx = 0; bool rotate = 0; int colorNum=1;
  45.   float timer = 0, delay = 0.3; // tickrate
  46.  
  47.   Clock clock;
  48.  
  49.   while (window.isOpen()) // game loop
  50.   {
  51.     float time = clock.getElapsedTime().asSeconds();
  52.     clock.restart();
  53.     timer += time;
  54.  
  55.     Event e;
  56.     while (window.pollEvent(e)) // close window
  57.     {
  58.       if (e.type == Event::Closed)
  59.         window.close();
  60.  
  61.       if (e.type == Event::KeyPressed) //rotate/move tile
  62.         if (e.key.code == Keyboard::Up) rotate = true;
  63.         else if (e.key.code == Keyboard::Left) dx=-1;
  64.         else if (e.key.code == Keyboard::Right) dx=1;
  65.     }
  66.  
  67.     if (Keyboard::isKeyPressed(Keyboard::Down)) delay = 0.05;
  68.  
  69.     // MOVE
  70.  
  71.     for (int i =0; i < 4; i++) { b[i] = a[i]; a[i].x += dx; } //left/right movement
  72.     if (!check()) for (int i = 0; i < 4; i++) a[i] = b[i]; // reset a on invalid move
  73.  
  74.     if (rotate) // rotate clockwise
  75.     {
  76.       Point p = a[1]; // center of rotation
  77.       for (int i = 0; i < 4; i++) // shift each square
  78.       {
  79.         int x = a[i].y-p.y;
  80.         int y = a[i].x-p.x;
  81.         a[i].x = p.x - x;
  82.         a[i].y = p.y + y;
  83.       }
  84.       if (!check()) for (int i = 0; i < 4; i++) a[i] = b[i];
  85.     }
  86.  
  87.     if (timer > delay) // drop the block
  88.     {
  89.       for (int i = 0; i < 4; i++) { b[i] = a[i]; a[i].y += 1; }
  90.  
  91.       if (!check()) // hit bottom => paint block and load a random next block
  92.       {
  93.         for (int i = 0; i < 4; i++) field[b[i].y][b[i].x] = colorNum;
  94.  
  95.         colorNum = 1+rand()%7;
  96.         int n = rand()%7;
  97.         for (int i = 0; i < 4; i++)
  98.         {
  99.           a[i].x = figures[n][i] % 2;
  100.           a[i].y = figures[n][i] / 2;
  101.         }
  102.       }
  103.       timer = 0;
  104.     }
  105.  
  106.     dx = 0; rotate = 0; delay = 0.3;
  107.  
  108.     // DRAW
  109.  
  110.     window.clear(Color::White); // draw tiles
  111.  
  112.     for (int i = 0; i < M; i++) // paint all blocks
  113.       for (int j = 0; j < N; j++)
  114.       {
  115.         if (field[i][j] == 0) continue;
  116.         s.setTextureRect(IntRect(field[i][j] * 18,0,18,18));
  117.         s.setPosition(j*18,i*18);
  118.         window.draw(s);
  119.       }
  120.  
  121.     for (int i = 0; i < 4; i++) // paint current block
  122.     {
  123.       s.setTextureRect(IntRect(colorNum * 18,0,18,18));
  124.       s.setPosition(a[i].x*18,a[i].y*18);
  125.       window.draw(s);
  126.     }
  127.     window.display();
  128.   }
  129.  
  130.   return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment