English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
First, Experimental Introduction
1.1 Experimental content
In this section, we will analyze the design ideas before the Tetris game and introduce the usage method of the ncurses library.
1.2 Experimental knowledge points
C++ Basic programming
Usage of ncurses library
Tetris logic design
1.3 Experimental environment
xface terminal
g++ Compiler
ncurses library
1.4 Target audience
This course is of general difficulty and suitable for those with C++ Basic programming knowledge, interested in game design and logic analysis.
1.5 Code acquisition
git clone https://github.com/Gamerchen/game_zero.git
Second, Development Preparation
2.1 Install ncurses library
sudo apt-get update sudo apt-get install libncurses5-dev
2.2 Compile the program
The compilation command needs to be added -l Options to introduce the ncurses library:
g++ main.c -l ncurses
Three, Experimental Principle
3.1 Analysis before design
Before starting to write the program, we need to analyze first what functions the program design needs to implement and how to divide it into modules. In Tetris, the first thing we should think of is displaying the blocks, followed by the falling, left and right movement, rotation, and finally clearing the rows when the layer is full. Another basic function of a Tetris game should be the prompt function for the next block shape.
Therefore, the problems we need to solve in programming are:
Display the block
Implement the movement of the block
Block rotation
Clear rows when the layer is full
Prompt the next block shape
3.2 Basic graphics
Each block is composed of four boxes, which fall from the center of the game box and can be rotated without colliding with the boundaries of the box or other blocks inside.
3.3 Usage of NCURSES library
In simple terms, NCURSES is a library derived from System V Release 4.0 (SVr4is a clone of CURSES, a freely configurable library that is fully compatible with the old version of CURSES and allows applications to directly control the display of terminal screens. NCURSES encapsulates the underlying terminal functions, includes some functions for creating windows, and has extensions such as Menu, Panel, and Form for the CURSES basic library. We can build an application that includes multiple windows (multiple windows), menus (menus), panels (panels), and forms (forms). Windows can be managed independently, such as allowing them to scroll (scrollability) or hide. Menus (Menus) allow users to establish command options for easy execution of commands. Forms allow users to establish simple data input and display windows. Panels are an extension of the NCURSES window management function, which can be used to overlay or stack windows.
3.3.1 NCURSES--Start from the Hello World program
If you call functions from the NCURSES library, you must load the ncurses.h file in the code (ncurses.h already includes stdio.h)
Example:
#include <ncurses.h> int main() { initscr(); //Initialization, enter NCURSES mode printw("Hello World!"); //Print "Hello World!" on the virtual screen refresh(); //Write the content on the virtual screen to the display and refresh getch(); //Wait for user input endwin(); //Exit NCURSES mode return 0; }
In the above examples, we have introduced the usage of the most basic functions in the NCURSES library, the functions' functions are described in the comments, and will not be repeated here.
3.3.2 Window Mechanism
When NCURSES initializes, it will create a window named stdscr by default, the size is generally 80 columns,25 Line (the size may vary depending on the monitor or graphics card), in addition, you can also create your own windows through the window system functions.
For example, if you call the following function:
printw("Hi!"); refresh();
It will output "Hi!" at the current cursor position of stdscr, and call the refresh() function to only update the buffer on stdscr.
If you have already created a window named win and want to output content to it, you can add 'w' before the normal function, and the parameters also need to change.
printw(string) //Print the string string at the current cursor position of stdscr
mvprintw(y,x,string) //Print the string string at the coordinates (y,x)
wprintw(win,string) //Print the string string at the current cursor position of the window win
mvwprintw(win,y,x,string) //Move the cursor to the (y,x) position of the window win and print the string string
I believe that after reading the above examples, you have been able to see the functional differences of each function through the naming rules of the functions.
3.3.3 newwin and box functions
The establishment of a window starts with the newwin() function, which returns a pointer to the window structure, which can be passed to some functions that require window parameters, such as wprintw().
However, we created a window but could not see it, and we need to use the box() function to draw a border around the window that has already been defined.
Example:
WINDOW *create_newin(int height, int width, int starty, int startx) { WINDOW *local_win; local_win = newin(height, width, starty, startx); box(local_win, 0, 0); wrefresh(local_win); return local_win; }
That's all for the basic usage of the NUCRSES library. You still need to consult relevant materials when encountering problems in specific use.
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)