AllenYuan

draw tile

May 24th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 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. int main()
  25. {
  26.   RenderWindow window(VideoMode(320, 480), "The Game!"); // window config
  27.  
  28.   Texture t;
  29.   t.loadFromFile("/Users/alleyuan/code-snippets/16_games/01 Tetris/images/tiles.png"); // load sprite. replace my absolute path
  30.  
  31.   Sprite s(t);
  32.   s.setTextureRect(IntRect(0,0,18,18));
  33.  
  34.   while (window.isOpen()) // game loop
  35.   {
  36.     Event e;
  37.     while (window.pollEvent(e)) // close window
  38.     {
  39.       if (e.type == Event::Closed)
  40.         window.close();
  41.     }
  42.  
  43.     int n = 3;
  44.     for (int i = 0; i < 4; i++) // load T tile into a
  45.     {
  46.       a[i].x = figures[n][i] % 2;
  47.       a[i].y = figures[n][i] / 2;
  48.     }
  49.  
  50.     window.clear(Color::White); // draw tiles
  51.     for (int i = 0; i < 4; i++)
  52.     {
  53.       s.setPosition(a[i].x*18,a[i].y*18);
  54.       window.draw(s);
  55.     }
  56.     window.display();
  57.   }
  58.  
  59.   return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment