screen-size - 如何使屏幕尺寸成为自变量
问题描述
我正在做一个学校项目,我正在制作俄罗斯方块游戏。我还没有编写整个游戏,我现在只是想修复这个领域。在我的程序中,我必须将屏幕宽度和高度设置为特定的整数值,并且只有当输出屏幕设置为相同的整数值时,游戏字段才会正确对齐。我想让字段对齐独立于屏幕大小。我设置屏幕宽度和高度的代码是第 11/12 行,我在程序末尾使用变量。这是我的代码,
#include "pch.h"
#include <iostream>
#include <Windows.h>
using namespace std;
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)
int Rotate(int px, int py, int r)
{
switch (r % 4)
{
case 0: return py * 4 + px; // 0 degrees
case 1: return 12 + py - (px * 4); // 90 degrees
case 2: return 15 - (py * 4) - px; // 180 degrees
case 3: return 3 - py + (px * 4); // 270 degrees
}
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 buffer
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;
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;
}
解决方案
推荐阅读
- python - 切换轴刻度标签的可见性会导致它们完全被破坏
- python - Django 本地运行服务器错误(不会启动)
- amazon-web-services - AWS CodeDeploy 每次都在 EC2 实例上失败
- android - 如何在 React Native android 中实现谷歌分析的谷歌营销活动测量?
- java - Hibernate 问题,以对象为键、整数为值的映射
- rust - Rust 中 if 和 if-let 的主要区别
- java - 如何在编译前修改 Gradle 项目中的 Java 源代码而不修改磁盘上的文件?
- python - 我想将我的 csv 文件连接到 sqlite,但是通过 dcc.upload python 获取用户输入,并且对于每个文件,我希望在一个数据库中有一个单独的表
- python - 评估基于内容的推荐系统(没有“用户”)
- php - 如何在 PHP 中验证用户名