Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <chrono>
  3. using namespace std;
  4. #include <Windows.h>
  5.  
  6. int nScreenWidth = 120;
  7. int nScreenHeight = 40;
  8.  
  9. float fPlayerX = 8.0f;
  10. float fPlayerY = 8.0f;
  11. float fPlayerA = 0.0f;
  12.  
  13. int nMapHeight = 16;
  14. int nMapWidth = 16;
  15.  
  16. float fFOV = 3.14159 / 4.0;
  17. float fDepth = 16.0f;
  18.  
  19. int main()
  20. {
  21.     wchar_t *screen = new wchar_t[nScreenWidth*nScreenHeight];
  22.     HANDLE hConsole = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
  23.     SetConsoleActiveScreenBuffer(hConsole);
  24.     DWORD dwBytesWritten = 0;
  25.  
  26.     wstring map;
  27.  
  28.     map += L"################";
  29.     map += L"#..............#";
  30.     map += L"#..............#";
  31.     map += L"#..............#";
  32.     map += L"#..............#";
  33.     map += L"#..............#";
  34.     map += L"#..............#";
  35.     map += L"#..............#";
  36.     map += L"#..............#";
  37.     map += L"#..............#";
  38.     map += L"#..............#";
  39.     map += L"#..............#";
  40.     map += L"#..............#";
  41.     map += L"#..............#";
  42.     map += L"#..............#";
  43.     map += L"################";
  44.    
  45.     bool bHitWallActually = false;
  46.     if (fPlayerX < 2.0f || fPlayerX > 15.0f || fPlayerY < 2.0f || fPlayerY > 15.0f) {
  47.         bHitWallActually = true;
  48.     }
  49.  
  50.    
  51.     while (1)
  52.     {
  53.        
  54.  
  55.         if (GetAsyncKeyState((unsigned short)'J') & 0x8000)
  56.             fPlayerA -= (0.005f);
  57.         if (GetAsyncKeyState((unsigned short)'K') & 0x8000)
  58.             fPlayerA += (0.005f);
  59.         if (GetAsyncKeyState((unsigned short)'W') & 0x8000) {
  60.             fPlayerX += sinf(fPlayerA) * 0.005f;
  61.             fPlayerY += cosf(fPlayerA) * 0.005f;
  62.  
  63.             if (bHitWallActually == true)
  64.             {
  65.                 fPlayerX -= sinf(fPlayerA) * 0.005f;
  66.                 fPlayerY -= cosf(fPlayerA) * 0.005f;
  67.             }
  68.  
  69.         }
  70.  
  71.         if (GetAsyncKeyState((unsigned short)'S') & 0x8000) {
  72.             fPlayerX -= sinf(fPlayerA) * 0.005f;
  73.             fPlayerY -= cosf(fPlayerA) * 0.005f;
  74.  
  75.         }
  76.         if (GetAsyncKeyState((unsigned short)'A') & 0x8000) {
  77.             fPlayerX -= cosf(fPlayerA) * 0.005f;
  78.             fPlayerY += sinf(fPlayerA) * 0.005f;
  79.         }
  80.         if (GetAsyncKeyState((unsigned short)'D') & 0x8000) {
  81.             fPlayerX += cosf(fPlayerA) * 0.005f;
  82.             fPlayerY -= sinf(fPlayerA) * 0.005f;
  83.         }
  84.  
  85.  
  86.         for (int x = 0; x < nScreenWidth; x++)
  87.         {
  88.             float fRayAngle = (fPlayerA - fFOV / 2.0f) + ((float)x / (float)nScreenWidth) * fFOV;
  89.  
  90.             float fDistanceToWall = 0;
  91.             bool bHitWall = false;
  92.             float fEyeX = sinf(fRayAngle);
  93.             float fEyeY = cosf(fRayAngle);
  94.  
  95.             while (!bHitWall && fDistanceToWall < fDepth)
  96.             {
  97.                 fDistanceToWall += 0.1f;
  98.  
  99.  
  100.                 int nTestX = (int)(fPlayerX + fEyeX * fDistanceToWall);
  101.                 int nTestY = (int)(fPlayerY + fEyeY * fDistanceToWall);
  102.  
  103.                 if (nTestX < 0 || nTestX >= nMapWidth || nTestY < 0 || nTestY >= nMapHeight)
  104.                 {
  105.                     bHitWall = true;
  106.                     fDistanceToWall = fDepth;
  107.                 }
  108.                 else
  109.                 {
  110.                     if (map[nTestY * nMapWidth + nTestX] == '#')
  111.                     {
  112.                         bHitWall = true;
  113.                     }
  114.                 }
  115.  
  116.  
  117.             }
  118.  
  119.  
  120.             int nCeiling = (float)(nScreenHeight / 2.0) - nScreenHeight / ((float)fDistanceToWall);
  121.             int nFloor = nScreenHeight - nCeiling;
  122.  
  123.             for (int y = 0; y < nScreenHeight; y++)
  124.             {
  125.                 if (y < nCeiling)
  126.                     screen[y*nScreenWidth + x] = ' ';
  127.                 else if (y > nCeiling && y <= nFloor)
  128.                     screen[y*nScreenWidth + x] = '#';
  129.                 else
  130.                     screen[y*nScreenWidth + x] = '.';
  131.             }
  132.         }
  133.  
  134.  
  135.            
  136.  
  137.         screen[nScreenWidth * nScreenHeight - 1] = '\0';
  138.         WriteConsoleOutputCharacter(hConsole, screen, nScreenWidth * nScreenHeight, { 0,0 }, &dwBytesWritten);
  139.     }
  140.  
  141.     system("pause");
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement