AllenYuan

collision

May 24th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 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.     // MOVE
  68.  
  69.     for (int i =0; i < 4; i++) { b[i] = a[i]; a[i].x += dx; } //left/right movement
  70.     if (!check()) for (int i = 0; i < 4; i++) a[i] = b[i]; // reset a on invalid move
  71.  
  72.     if (rotate) // rotate clockwise
  73.     {
  74.       Point p = a[1]; // center of rotation
  75.       for (int i = 0; i < 4; i++) // shift each square
  76.       {
  77.         int x = a[i].y-p.y;
  78.         int y = a[i].x-p.x;
  79.         a[i].x = p.x - x;
  80.         a[i].y = p.y + y;
  81.       }
  82.       if (!check()) for (int i = 0; i < 4; i++) a[i] = b[i];
  83.     }
  84.  
  85.     if (timer > delay) // drop the block
  86.     {
  87.       for (int i = 0; i < 4; i++) { b[i] = a[i]; a[i].y += 1; }
  88.  
  89.       if (!check()) // hit bottom => paint block and load a random next block
  90.       {
  91.         for (int i = 0; i < 4; i++) field[b[i].y][b[i].x] = colorNum;
  92.  
  93.         colorNum = 1+rand()%7;
  94.         int n = rand()%7;
  95.         for (int i = 0; i < 4; i++)
  96.         {
  97.           a[i].x = figures[n][i] % 2;
  98.           a[i].y = figures[n][i] / 2;
  99.         }
  100.       }
  101.       timer = 0;
  102.     }
  103.  
  104.     dx = 0; rotate = 0;
  105.  
  106.     // DRAW
  107.  
  108.     window.clear(Color::White); // draw tiles
  109.  
  110.     for (int i = 0; i < M; i++) // paint all blocks
  111.       for (int j = 0; j < N; j++)
  112.       {
  113.         if (field[i][j] == 0) continue;
  114.         s.setPosition(j*18,i*18);
  115.         window.draw(s);
  116.       }
  117.     for (int i = 0; i < 4; i++) // paint current block
  118.     {
  119.       s.setPosition(a[i].x*18,a[i].y*18);
  120.       window.draw(s);
  121.     }
  122.     window.display();
  123.   }
  124.  
  125.   return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment