Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int Score(int score) { //FUNCTION TO CALCULATE SCORE
- score = score + 100;
- return score;
- }
- int main() {
- // Two bucket arrays, one shown on the screen, the other with the permanent contents of the buckets (walls and any non-moving shapes)
- char bucket[12][25];
- char _bucket[12][25];
- int score = 0; //DECLARING SCORE VARIABLE FOR HOLDING SCORE
- int shapes[256] = {};
- int shapeIndex = 0;
- bool gameOver = false;
- generateBucket(bucket);
- generateBucket(_bucket);
- generateShapeStream();
- drawBucket(bucket);
- while (!gameOver) {
- gameOver = gameTick(bucket, _bucket);
- Sleep(50);
- cout << "Your Score: " << score << endl; //THIS IS THE SCORE VARIABLE NEEDING UPDATING
- currentTick++;
- }
- setCursorTo(25, 6);
- cout << "GAME OVER";
- system("pause");
- }
- void setCursorTo(int x, int y)
- {
- HANDLE handle;
- COORD position;
- handle = GetStdHandle(STD_OUTPUT_HANDLE);
- position.X = x;
- position.Y = y;
- SetConsoleCursorPosition(handle, position);
- }
- /* generateBucket - takes a bucket array of any size and
- * creates a semi-empty bucket, with a
- * few random shapes in the bottom few lines. */
- template <size_t rows, size_t cols>
- void generateBucket(char(&bucket)[rows][cols]) {
- for (int w = 0; w < 12; w++) {
- for (int z = 0; z < 25; z++) {
- if (((w == 0) || (w == 11)) && (z == 0)) { bucket[w][z] = '.'; }
- else if (((w % 12 == 0) || (w % 12 == 11)) && ((z > 0) && (z < 24))) { bucket[w][z] = '|'; }
- else if (((w == 0) || (w == 11)) && (z == 24)) { bucket[w][z] = '+'; }
- else if (z == 24) { bucket[w][z] = '-'; }
- else { bucket[w][z] = ' '; }
- }
- }
- }
- /* generateShapeStream - generates a pre-determined list of
- * shapes that will fall from the sky. */
- void generateShapeStream() {
- // Initialize the random number generator
- srand(time(NULL));
- for (int i = 0; i < 256; i++) {
- shapes[i] = rand() % 6 + 1;
- }
- //cout << "In generate shape..." << endl;
- }
- /* drawBucket - draws the actual bucket on the screen
- * including the currently dropping shape. */
- template <size_t rows, size_t cols>
- void drawBucket(char(&bucket)[rows][cols]) {
- setCursorTo(0, 0);
- for (int w = 0; w < 25; w++) {
- for (int z = 0; z < 12; z++) {
- cout << bucket[z][w];
- }
- cout << endl;
- }
- }
- /* gameTick - this function does all of the different
- * processessing that happens throughout
- * the game. This also returns false to
- * stop the main loop once gameover has
- * been reached */
- template <size_t rows, size_t cols>
- bool gameTick(char(&bucket)[rows][cols], char(&perm_bucket)[rows][cols]) {
- drawBucket(bucket);
- if (!isDropping) {
- currentShape++;
- activeShape = TetrisShape(shapes[currentShape]);
- if (!canEnter(DIR_DOWN, perm_bucket)) {
- return true;
- }
- else {
- isDropping = true;
- updateBucket(bucket, false);
- }
- }
- else {
- if (currentTick % GAME_INTERVAL == 1) {
- // we are on a drop interval.
- if (canEnter(DIR_DOWN, perm_bucket)) {
- updateBucket(bucket, moveShape(DIR_DOWN));
- }
- else {
- landShape(perm_bucket);
- }
- }
- }
- int direction = getUserInput();
- if (canEnter(direction, perm_bucket)) {
- updateBucket(bucket, moveShape(direction));
- }
- if (!canEnter(DIR_DOWN, perm_bucket)) {
- landShape(perm_bucket);
- set_bucket(bucket, perm_bucket);
- }
- return false;
- }
- /* moveShape - Handles moving the shape in the bucket. */
- bool moveShape(int direction) {
- previousX = activeShape.shapeTopLeftX;
- previousY = activeShape.shapeTopLeftY;
- switch (direction) {
- case DIR_DOWN:
- activeShape.shapeTopLeftY++;
- return false;
- break;
- case DIR_RIGHT:
- activeShape.shapeTopLeftX++;
- return false;
- break;
- case DIR_LEFT:
- activeShape.shapeTopLeftX--;
- return false;
- break;
- case DIR_ROTATE:
- activeShape.rotate();
- return true;
- break;
- }
- }
- /* updateBucket - place the cureret shape in the bucket, remove the old shape*/
- template <size_t rows, size_t cols>
- void updateBucket(char(&bucket)[rows][cols], bool isRotation) {
- for (int _x = 0; _x < 4; _x++) {
- for (int _y = 0; _y < 4; _y++) {
- if (!isRotation) {
- if ((activeShape.shapeArray[_x][_y] != ' ') && (bucket[_x + previousX][_y + previousY] != '|') && (bucket[_x + previousX][_y + previousY] != '-')) {
- bucket[_x + previousX][_y + previousY] = ' ';
- }
- }
- else {
- if ((bucket[_x + previousX][_y + previousY] != '|') && (bucket[_x + previousX][_y + previousY] != '-')) {
- bucket[_x + previousX][_y + previousY] = ' ';
- }
- }
- }
- }
- for (int _x = 0; _x < 4; _x++) {
- for (int _y = 0; _y < 4; _y++) {
- if (activeShape.shapeArray[_x][_y] != ' ') {
- bucket[_x + activeShape.shapeTopLeftX][_y + activeShape.shapeTopLeftY] = activeShape.shapeArray[_x][_y];
- }
- }
- }
- }
- /* landShape - Sets the shape in place once it hits the
- bottom of the bucket. Moves the shape to the permanent bucket (_bucket) */
- template <size_t rows, size_t cols>
- void landShape(char(&bucket)[rows][cols]) {
- updateBucket(bucket, false);
- previousX = 6; previousY = 0;
- check_bucket(bucket);
- isDropping = false;
- }
- /* getUserInput - Reads the user input from the player*/
- int getUserInput() {
- setCursorTo(35, 9);
- if ((GetKeyState(VK_DOWN) != 0) && (GetKeyState(VK_DOWN) != 1)) { return DIR_DOWN; }
- if ((GetKeyState(VK_RIGHT) != 0) && (GetKeyState(VK_RIGHT) != 1)) { return DIR_RIGHT; }
- if ((GetKeyState(VK_LEFT) != 0) && (GetKeyState(VK_LEFT) != 1)) { return DIR_LEFT; }
- if ((GetKeyState(VK_UP) != 0) && (GetKeyState(VK_UP) != 1)) { return DIR_ROTATE; }
- return 0;
- }
- /* canRotate - if we are adjacent to another shape, then we CANNOT rotate */
- template <size_t rows, size_t cols>
- bool canRotate(char(&bucket)[rows][cols]) {
- // The only real way to do this is to create a copy of the shape, rotate it, then try to determine where it will be in the bucket.
- TetrisShape _tmp = TetrisShape(activeShape);
- _tmp.rotate();
- for (int _x = 0; _x < 4; _x++) {
- for (int _y = 0; _y < 4; _y++) {
- if (_tmp.shapeArray[_x][_y] != ' ') {
- if (bucket[_tmp.shapeTopLeftX + _x][_tmp.shapeTopLeftY + _y] != ' ') {
- return false;
- }
- }
- }
- }
- return true;
- }
- /* canEnter - Tests to see if the falling blocks can
- enter in any direction. */
- template <size_t rows, size_t cols>
- bool canEnter(int dir, char(&bucket)[rows][cols]) {
- // Check for collision with any elements of the shape array, and any elements of the bucket.
- // Determine which direction we are moving.
- int delta_x = 0, delta_y = 0;
- switch (dir) {
- case DIR_DOWN:
- delta_y++;
- break;
- case DIR_LEFT:
- delta_x--;
- break;
- case DIR_RIGHT:
- delta_x++;
- break;
- case DIR_ROTATE:
- return canRotate(bucket);
- break;
- }
- // Create the starting {x, y} position to test for collsion
- int test_x = activeShape.shapeTopLeftX + delta_x;
- int test_y = activeShape.shapeTopLeftY + delta_y;
- for (int _x = 0; _x < 4; _x++) {
- for (int _y = 0; _y < 4; _y++) {
- if (activeShape.shapeArray[_x][_y] != ' ') {
- if (bucket[test_x + _x][test_y + _y] != ' ') {
- return false;
- }
- }
- }
- }
- return true;
- }
- template <size_t rows, size_t cols>
- void check_bucket(char(&bucket)[rows][cols]) {
- for (int y = 0; y < 25; y++) {
- int tmp_count = 0;
- for (int x = 0; x < 13; x++) {
- if (bucket[x][y] == 'X') {
- tmp_count++;
- }
- }
- if (tmp_count == 10) {
- Score(score); // CALLING SCORE TO UPDATE THE SCORE FOR GETTING A FULL LINE
- //UNDECLARED IDENTIFIER?
- for (int x = 1; x < 11; x++) {
- for (int _y = y; _y > 0; _y--) {
- bucket[x][_y] = bucket[x][_y - 1];
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement