Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <time.h>
- using namespace sf;
- int ts = 63; //tile size
- // Sprite offsets( for matching the mini boxes
- Vector2i offset(43, 22);
- struct Piece
- {
- int x, y, col, row, kind, match, alpha; // Coordinates(x,y) (column, row), type of piece, number of matches, transparency
- // Called when creating grid[9][9]
- Piece() {
- match = 0; // How much pieces have matched
- alpha = 255; // Transparency
- }
- } grid[9][9]; // Global grid for the game
- // Helper function to help swapping two pieces' places
- void swap(Piece p1, Piece p2)
- {
- // Swaps the values of the pieces(column, row)
- std::swap(p1.col, p2.col);
- std::swap(p1.row, p2.row);
- // Moves every piece to its' new coordinates(column, row)
- grid[p1.row][p1.col] = p1;
- grid[p2.row][p2.col] = p2;
- }
- int main()
- {
- // Random Number Seeder by current time { time(null) or time(0) }
- srand(time(0));
- // Create a window to show the application( Graphic User Interface Window )
- RenderWindow app(VideoMode(740, 480), "Gem Crush!");
- // Set maximum frames per second
- app.setFramerateLimit(60);
- // Textures from images
- Texture t1, t2, t3;
- // Load textures from files
- t1.loadFromFile("images/background.png");
- t2.loadFromFile("images/gems.png");
- t3.loadFromFile("images/cursor.png");
- // Create sprites from textures
- Sprite background(t1), gems(t2), cursor(t3);
- // Create the board grid itself
- for (int i = 1; i <= 7; i++)
- for (int j = 1; j <= 7; j++)
- {
- grid[i][j].kind = rand() % 3; // Get random type
- grid[i][j].col = j; // Set the column
- grid[i][j].row = i; // Set current row
- grid[i][j].x = j * ts; // Get box tile size and multiply by the column to match the size of the sprite
- grid[i][j].y = i * ts; // Get box tile size and multiply by the row to match the size of the sprite
- }
- /*! Helper variables for coordinates, mouse clicks and position of click plus animations and swaps */
- int x0, y0, x, y; int click = 0; Vector2i pos;
- bool isSwap = false, isMoving = false;
- /*! While the application is open */
- while (app.isOpen())
- {
- // Detect new event
- Event e;
- // While new event has come and is processing
- while (app.pollEvent(e))
- {
- // Check if the event is for closing the window
- if (e.type == Event::Closed)
- // If yes, close
- app.close();
- // Check if the event is for mouse clicking
- if (e.type == Event::MouseButtonPressed)
- // Check if the mouse click is by left mouse button
- if (e.key.code == Mouse::Left) {
- // If it is and nothing is moving or swapping right now
- if (!isSwap && !isMoving)
- // Increment clicks
- click++;
- // Get mouse click position minus the offset of the sprites
- pos = Mouse::getPosition(app) - offset;
- }
- }
- // if mouse is clicked only once
- if (click == 1)
- {
- // get that position of the click and divide it by the tilesize square + 1
- x0 = pos.x / ts + 1;
- y0 = pos.y / ts + 1;
- // Set the bounding box(limit box), where 300 is coordinate of x, 100 is y, 48 is width and 52 is height
- cursor.setTextureRect(IntRect(300, 100, 48, 52));
- // Set current position
- cursor.setPosition(pos.x, pos.y);
- // Set visible colour to 255 transparency(full visibility)
- cursor.setColor(Color(255, 255, 255, 255));
- // Draw the cursor
- app.draw(cursor);
- }
- // If mouse is clicked second time
- if (click == 2)
- {
- // get that position of the click and divide it by the tilesize square + 1
- x = pos.x / ts + 1;
- y = pos.y / ts + 1;
- // Set the bounding box(limit box), where 0 is coordinate of x, 0 is y, 48 is width and 52 is height
- cursor.setTextureRect(IntRect(0, 0, 48, 52));
- // Set current position
- cursor.setPosition(x, y);
- // Set visible colour to 255 transparency(full visibility)
- cursor.setColor(Color(255, 255, 255, 255));
- // Set the texture
- cursor.setTexture(t3);
- // Draw the cursor
- app.draw(cursor);
- // Check if the absolute value of the summary of x - x0 and y - y0 is equal to 1
- if (abs(x - x0) + abs(y - y0) == 1)
- {
- // if yes, swap the two pieces
- swap(grid[y0][x0], grid[y][x]);
- // Set the swap flag
- isSwap = 1;
- // Reset clicks count
- click = 0;
- }
- else
- // Set the click to one, because we pressed the same position
- click = 1;
- }
- // Match finding
- // Go through the grid of the game
- for (int i = 1; i <= 8; i++)
- for (int j = 1; j <= 8; j++)
- {
- // If the grid matches the one on the same column with him(row, row + 1, row - 1)
- if (grid[i][j].kind == grid[i + 1][j].kind)
- if (grid[i][j].kind == grid[i - 1][j].kind)
- // Go through the possibilities
- for (int n = -1; n <= 1; n++)
- // Increment matches for this piece
- grid[i + n][j].match++;
- // If the grid matches the one on the same row with him(column, column + 1, column - 1)
- if (grid[i][j].kind == grid[i][j + 1].kind)
- if (grid[i][j].kind == grid[i][j - 1].kind)
- // Go through the possibilities
- for (int n = -1; n <= 1; n++)
- // Increment matches for this piece
- grid[i][j + n].match++;
- }
- // Moving animation
- // Sets the flag for the movement animation
- isMoving = false;
- // Go through the game grid
- for (int i = 1; i <= 7; i++)
- for (int j = 1; j <= 7; j++)
- {
- // Get the current piece
- Piece &p = grid[i][j];
- // Will be used for new coordinates
- int dx, dy;
- // Set the speed in a for loop
- for (int n = 0; n < 4; n++) // 4 - speed
- {
- // Get the projection of the new points(dx, dy)
- dx = p.x - p.col*ts;
- dy = p.y - p.row*ts;
- // Check if they are legit values
- if (dx)
- // Set the new position of the piece
- p.x -= dx / abs(dx);
- // Check if they are legit values
- if (dy)
- // Set the new position of the piece
- p.y -= dy / abs(dy);
- }
- // If both are legit, set the flag to return moving
- if (dx || dy)
- isMoving = 1;
- }
- // Deleting amimation
- // If they are not moving
- if (!isMoving)
- // Go through the game grid
- for (int i = 1; i <= 8; i++)
- for (int j = 1; j <= 8; j++)
- // If the grid matches when placing
- if (grid[i][j].match)
- // And if the piece's transparency is higher than 10
- if (grid[i][j].alpha > 10) {
- /*! Lower the value by 10 to hide*/
- grid[i][j].alpha -= 10;
- // Move out the board
- isMoving = true;
- }
- // Get score
- int score = 0;
- // Go through the grid
- for (int i = 1; i <= 8; i++)
- for (int j = 1; j <= 8; j++)
- // For every match make a point to the score;
- score += grid[i][j].match;
- // Second swap if no match is found
- if (isSwap && !isMoving)
- {
- // If the score is not a legitimate value
- if (!score)
- // Swap back
- swap(grid[y0][x0], grid[y][x]);
- // Reset the swap flag
- isSwap = 0;
- }
- // Update grid
- if (!isMoving)
- {
- // Go through the grid both ways
- for (int i = 8; i > 0; i--)
- for (int j = 1; j <= 8; j++)
- // if a match is found
- if (grid[i][j].match)
- // Check that match
- for (int n = i; n > 0; n--)
- // if it is not a legit value
- if (!grid[n][j].match) {
- // Swap back and update
- swap(grid[n][j], grid[i][j]);
- // End
- break;
- }
- // Go through the grid both ways
- for (int j = 1; j <= 8; j++)
- for (int i = 8, n = 0; i > 0; i--)
- // if a match is found
- if (grid[i][j].match)
- {
- // Set new random kind
- grid[i][j].kind = rand() % 7;
- // Set new y value and move it in the grid, then increment the grid number
- grid[i][j].y = -ts * n++;
- // Set that no match is found
- grid[i][j].match = 0;
- // Set transparency to full visibility
- grid[i][j].alpha = 255;
- }
- }
- //////draw///////
- // Draw the background image
- app.draw(background);
- // Draw the grid
- for (int i = 1; i <= 8; i++)
- for (int j = 1; j <= 8; j++)
- {
- // Every new piece
- Piece p = grid[i][j];
- // There is a rectangular texture with the kind's size multiplied by the offset
- gems.setTextureRect(IntRect(p.kind * 49, 0, 49, 49));
- // Has a color, set by default to full visibility
- gems.setColor(Color(255, 255, 255, p.alpha));
- // Has a position
- gems.setPosition(p.x, p.y);
- // Moves with animation towards its' own position
- gems.move(offset.x - ts, offset.y - ts);
- // Is drawing
- app.draw(gems);
- }
- // Show the application itself
- app.display();
- }
- // Carriage return
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement