Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.77 KB | None | 0 0
  1. // Tetris.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include "pch.h"
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. #include <Windows.h>
  9.  
  10. wstring tetromino[7];
  11. int nFieldWidth = 12;
  12. int nFieldHeight = 18;
  13. unsigned char *pField = nullptr;
  14.  
  15. int nScreenWidth = 80; // Console screen size X (columns)
  16. int nScreenHeight = 30; // Console screen size Y (rows)
  17.  
  18. // Rotation function. Takes in an x and y coordinate for inputs and r is the amount you want to rotate it by
  19. // Returns the appropriate index for the pieces
  20. int Rotate(int px, int py, int r)
  21. {
  22.     switch (r % 4)
  23.     {
  24.     case 0: return py * 4 + px; // 0 degree rotation
  25.     case 1: return 12 + py - (px * 4); // 90 degree rotation
  26.     case 2: return 15 - (py * 4) - px; // 180 degree rotation
  27.     case 3: return 3 - py + (px * 4); // 270 degree rotation
  28.     }
  29.     return 0;
  30. }
  31.  
  32. int main()
  33. {
  34.     // Create assets
  35.     tetromino[0].append(L"..X.");
  36.     tetromino[0].append(L"..X.");
  37.     tetromino[0].append(L"..X.");
  38.     tetromino[0].append(L"..X.");
  39.  
  40.     tetromino[1].append(L"..X.");
  41.     tetromino[1].append(L".XX.");
  42.     tetromino[1].append(L".X..");
  43.     tetromino[1].append(L"....");
  44.  
  45.     tetromino[2].append(L".X..");
  46.     tetromino[2].append(L".XX.");
  47.     tetromino[2].append(L"..X.");
  48.     tetromino[2].append(L"....");
  49.  
  50.     tetromino[3].append(L"....");
  51.     tetromino[3].append(L".XX.");
  52.     tetromino[3].append(L".XX.");
  53.     tetromino[3].append(L"....");
  54.  
  55.     tetromino[4].append(L"..X.");
  56.     tetromino[4].append(L".XX.");
  57.     tetromino[4].append(L"..X.");
  58.     tetromino[4].append(L"....");
  59.  
  60.     tetromino[5].append(L"....");
  61.     tetromino[5].append(L".XX.");
  62.     tetromino[5].append(L"..X.");
  63.     tetromino[5].append(L"..X.");
  64.  
  65.     tetromino[6].append(L"....");
  66.     tetromino[6].append(L".XX.");
  67.     tetromino[6].append(L".X..");
  68.     tetromino[6].append(L".X..");
  69.  
  70.     pField = new unsigned char[nFieldWidth*nFieldHeight]; // create play field
  71.     for (int x = 0; x < nFieldWidth; x++) // Board boundary
  72.         for (int y = 0; y < nFieldHeight; y++)
  73.             pField[y*nFieldWidth + x] = (x == 0 || x == nFieldWidth - 1 || y == nFieldHeight - 1) ? 9 : 0;
  74.  
  75.     // Screen Buffer
  76.     wchar_t *screen = new wchar_t[nScreenWidth*nScreenHeight];
  77.     for (int i = 0; i < nScreenWidth*nScreenHeight; i++) screen[i] = L' ';
  78.     HANDLE hConsole = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
  79.     SetConsoleActiveScreenBuffer(hConsole);
  80.     DWORD dwBytesWritten = 0;
  81.  
  82.     bool bGameOver = false;
  83.     while (!bGameOver)
  84.     {
  85.         //Draw Field
  86.         for (int x = 0; x < nFieldWidth; x++)
  87.             for (int y = 0; y < nFieldHeight; y++)
  88.                 screen[(y + 2)*nScreenWidth + (x + 2)] = L" ABCDEFG=#"[pField[y*nFieldWidth + x]];
  89.         // Display frame
  90.         WriteConsoleOutputCharacter(hConsole, screen, nScreenWidth * nScreenHeight, { 0,0 }, &dwBytesWritten);
  91.  
  92.     }
  93.  
  94.    
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement