简体   繁体   English

如何使用 C 以编程方式更改设置以使 Linux 终端中字符的宽度和高度相同而没有任何间距?

[英]How do I change the settings programmatically with C to make the width and height of characters in a Linux terminal the same without any spacing?

I'm trying to implement the snake game for Linux terminal with C programming language.我正在尝试使用 C 编程语言为 Linux 终端实现蛇游戏。

Here is the code for the background which is simply a set of "walls".这是背景的代码,它只是一组“墙”。

#include <stdio.h>
int main()
{
    printf("\033[H");
    printf("\033[J");
    for (int j = 0; j < 22; j++)
        printf("**");
    printf("\n");
    for (int i = 0; i < 20; i++)
    {
        printf("**");
        for (int j = 0; j < 20; j++)
            printf("  ");
        printf("**");
        printf("\n");
    }
    for (int j = 0; j < 22; j++)
        printf("**");
    printf("\n");
    printf("done\n");
    return 0;
}

Here is what I got in the terminal这是我在终端中得到的

在此处输入图片说明

and this when replacing " " with "00"这当用“00”替换“”时

在此处输入图片说明

I'm not sure if it's some kind of spacing between lines of the terminal window.我不确定终端窗口的行之间是否存在某种间距。 It's necessary for normal use, but not for a console game.它是正常使用所必需的,但不适用于主机游戏。

How do I change the settings programmatically with C to make the width and height of characters the same without any spacing, like the one shown below?如何使用 C 以编程方式更改设置以使字符的宽度和高度相同而没有任何间距,如下所示?

在此处输入图片说明

In other words, how do I make 20 rows by 20 columns of characters giving a square area with C?换句话说,我如何制作 20 行 x 20 列的字符,给出一个带有 C 的正方形区域?

Use ncursesw, and you can use Unicode Block Elements (█ ▌ ▐ ▀ ▄ ▖▗ ▘ ▙ ▚ ▛ ▜▝ ▞ ▟ and ░ ▒ ▓) and Box Drawing characters (─ ┐┌ │└ ┘├ ┤┬ ┴ ┼ ═ ╗ ╔ ║╚ ╝╠ ╣ ╦ ╩ ╬ ╒╓ ╕╖╘╙ ╛╜╞╟╡╢╤ ╥ ╧ ╨ ╪ ╫) and other Unicode characters like Geometric Shapes (■□▣▪▫◤◣◢◥ ▤ ▥ ▦ ▧ ▨ ▩ ● ◎ ◉ ◌ ○ ◯), depending on the glyphs provided by the font you use in your terminal.使用ncursesw,你可以使用Unicode块元素(█ ▌ ▐ ▀ ▄ ▖▗ ▘ ▙ ▚ ▛ ▜▝ ▞ ▟ and ░ ▒ ▓)和方框绘图字符(─ ▌ ▐ ☔☔☔☔☔ ║╚╝╠╣╦╩╬╒╓╕╖╘╙╛╜╞╟╡╢╤╥╧╨╪╫)和其他Unicode字符等几何形状(■□▣▪▫◤◣◢◥▤▥▦▧▨▩ ● ◎ ◉ ◌ ○ ◯),取决于您在终端中使用的字体所提供的字形。

(If you want a more square glyphs, just pick a more square font, like Courier New. Most monospaced fonts are rather rectangular, taller than they are wide, though.) (如果您想要更方形的字形,只需选择更方形的字体,例如 Courier New。不过,大多数等宽字体都是矩形的,但高度大于宽度。)

For wide character constants and wide strings, you prepend an L before the quotes.对于宽字符常量和宽字符串,在引号前加上L For example, L'▒' is the wide character constant specifying medium fill, and L"╔═╤═══╤══╗" is a wide string containing ten wide characters.例如, L'▒'是指定中等填充的宽字符常量,而L"╔═╤═══╤══╗"是包含十个宽字符的宽字符串。

(Wide does not mean visually wide, it is just what the C standard calls these facilities. Normal and wide I/O cannot be mixed in the same stream. For ncurses, ncurses is the standard non-wide terminal curses library, and ncursesw is the wide version.) (宽并不意味着视觉上的宽,只是C标准对这些设施的称呼。普通I/O和宽I/O不能混在同一个流中。对于ncurses,ncurses是标准的非宽终端curses库,ncursesw是宽版。)

As a bonus, you get nonblocking keyboard input support, for making your own terminal games (like rogue or nethack ).作为奖励,您可以获得非阻塞键盘输入支持,用于制作您自己的终端游戏(如roguenethack )。

Note that I don't use Windows, so I don't know how well WSL/WSL2 supports wide I/O yet.请注意,我不使用 Windows,所以我不知道 WSL/WSL2 对宽 I/O 的支持情况如何。 In the past, native windows programs needed some additional calls for wide I/O, but ncurses 6.2 INSTALL file says it now has code to support Windows command line too.过去,原生 Windows 程序需要一些额外的宽 I/O 调用,但是 ncurses 6.2 INSTALL 文件说它现在也有代码来支持 Windows 命令行。 The question is tagged , and ncursesw works fine in basically all terminal types in Linux, Mac OS, OpenBSD, FreeBSD, etc.问题被标记为 ,并且 ncursesw 基本上适用于 Linux、Mac OS、OpenBSD、FreeBSD 等中的所有终端类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM