Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Tetris.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- #include "pch.h"
- #include <iostream>
- using namespace std;
- #include <Windows.h>
- wstring tetromino[7];
- int nFieldWidth = 12;
- int nFieldHeight = 18;
- unsigned char *pField = nullptr;
- int nScreenWidth = 80; // Console screen size X (columns)
- int nScreenHeight = 30; // Console screen size Y (rows)
- // Rotation function. Takes in an x and y coordinate for inputs and r is the amount you want to rotate it by
- // Returns the appropriate index for the pieces
- int Rotate(int px, int py, int r)
- {
- switch (r % 4)
- {
- case 0: return py * 4 + px; // 0 degree rotation
- case 1: return 12 + py - (px * 4); // 90 degree rotation
- case 2: return 15 - (py * 4) - px; // 180 degree rotation
- case 3: return 3 - py + (px * 4); // 270 degree rotation
- }
- return 0;
- }
- int main()
- {
- // Create assets
- tetromino[0].append(L"..X.");
- tetromino[0].append(L"..X.");
- tetromino[0].append(L"..X.");
- tetromino[0].append(L"..X.");
- tetromino[1].append(L"..X.");
- tetromino[1].append(L".XX.");
- tetromino[1].append(L".X..");
- tetromino[1].append(L"....");
- tetromino[2].append(L".X..");
- tetromino[2].append(L".XX.");
- tetromino[2].append(L"..X.");
- tetromino[2].append(L"....");
- tetromino[3].append(L"....");
- tetromino[3].append(L".XX.");
- tetromino[3].append(L".XX.");
- tetromino[3].append(L"....");
- tetromino[4].append(L"..X.");
- tetromino[4].append(L".XX.");
- tetromino[4].append(L"..X.");
- tetromino[4].append(L"....");
- tetromino[5].append(L"....");
- tetromino[5].append(L".XX.");
- tetromino[5].append(L"..X.");
- tetromino[5].append(L"..X.");
- tetromino[6].append(L"....");
- tetromino[6].append(L".XX.");
- tetromino[6].append(L".X..");
- tetromino[6].append(L".X..");
- pField = new unsigned char[nFieldWidth*nFieldHeight]; // create play field
- for (int x = 0; x < nFieldWidth; x++) // Board boundary
- for (int y = 0; y < nFieldHeight; y++)
- pField[y*nFieldWidth + x] = (x == 0 || x == nFieldWidth - 1 || y == nFieldHeight - 1) ? 9 : 0;
- // Screen Buffer
- wchar_t *screen = new wchar_t[nScreenWidth*nScreenHeight];
- for (int i = 0; i < nScreenWidth*nScreenHeight; i++) screen[i] = L' ';
- HANDLE hConsole = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
- SetConsoleActiveScreenBuffer(hConsole);
- DWORD dwBytesWritten = 0;
- bool bGameOver = false;
- while (!bGameOver)
- {
- //Draw Field
- for (int x = 0; x < nFieldWidth; x++)
- for (int y = 0; y < nFieldHeight; y++)
- screen[(y + 2)*nScreenWidth + (x + 2)] = L" ABCDEFG=#"[pField[y*nFieldWidth + x]];
- // Display frame
- WriteConsoleOutputCharacter(hConsole, screen, nScreenWidth * nScreenHeight, { 0,0 }, &dwBytesWritten);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement