Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <time.h>
- using namespace sf;
- const int M = 20;
- const int N = 10;
- int field[M][N] = {0}; // tetris grid
- struct Point
- { int x,y; } a[4], b[4]; // current/next tile = 4 points
- int figures[7][4] = // encode blocks as 4 squares on a 2x4 rectangular grid indexed 0,...,7
- {
- 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, // 0
- };
- bool check() // collision detection
- {
- 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!"); // window config
- Texture t;
- t.loadFromFile("/Users/alleyuan/code-snippets/16_games/01 Tetris/images/tiles.png"); // load sprite. replace my absolute path
- Sprite s(t);
- s.setTextureRect(IntRect(0,0,18,18));
- int dx = 0; bool rotate = 0; int colorNum=1;
- float timer = 0, delay = 0.3; // tickrate
- Clock clock;
- while (window.isOpen()) // game loop
- {
- float time = clock.getElapsedTime().asSeconds();
- clock.restart();
- timer += time;
- Event e;
- while (window.pollEvent(e)) // close window
- {
- if (e.type == Event::Closed)
- window.close();
- if (e.type == Event::KeyPressed) //rotate/move tile
- 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; } //left/right movement
- if (!check()) for (int i = 0; i < 4; i++) a[i] = b[i]; // reset a on invalid move
- if (rotate) // rotate clockwise
- {
- Point p = a[1]; // center of rotation
- for (int i = 0; i < 4; i++) // shift each square
- {
- 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];
- }
- if (timer > delay) // drop the block
- {
- for (int i = 0; i < 4; i++) { b[i] = a[i]; a[i].y += 1; }
- if (!check()) // hit bottom => paint block and load a random next block
- {
- 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;
- }
- dx = 0; rotate = 0; delay = 0.3;
- // DRAW
- window.clear(Color::White); // draw tiles
- for (int i = 0; i < M; i++) // paint all blocks
- 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);
- window.draw(s);
- }
- for (int i = 0; i < 4; i++) // paint current block
- {
- s.setTextureRect(IntRect(colorNum * 18,0,18,18));
- s.setPosition(a[i].x*18,a[i].y*18);
- window.draw(s);
- }
- window.display();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment