[C language] My locomotive is too slow ~ I made a sl command ~

【Overview】

I made a sl command that can move the locomotive in the terminal. However, the options are not implemented. Also, my locomotive is slower than the original locomotive (it's slower than the original locomotive after it was created, but I wonder if this is more interesting ...). The title is a homage to this article → My ls is too late

I created it by repeatedly looking at the execution result of the head family without looking at the code of the head family at all. (I don't know if the original code is on the net) [Addition] fujitanozomu commented and it was on GitHub below.   https://github.com/mtoyoda/sl

[Preparation]

You will need curses.h when you run the code. I referred to the following site for the installation method. Linux Error: curses.h: No such file or directory Problem Solution

【environment】

[vagrant@vagrant-centos65 sl_command]$ cat /etc/centos-release 
CentOS release 6.5 (Final)

【code】

When compiling gcc -o sl sl.c -l curses

sl.c


#include <stdio.h>
#include <curses.h>
#include <string.h>

void printSL();

int main(int argc, char *argv[])
{
	//Initialization
	initscr();
	//Don't show cursor
	curs_set(0);
	//Do not display keyed characters
	noecho();
	//Do not use buffer
	cbreak();
	//Locomotive output
	printSL();
	//End terminal control
	endwin();
	return 0;
}

void printSL()
{
	int count = 1;
	int slScrInCol = 0, slScrOutCol = 0;
	int lineNum = 0;
	int colNum = 0;

	//Get the current screen size
	// curses.LINES defined by h,With COLS, even if you change the size of the terminal
	//Get the current screen size because it will be the initial screen size
	//colNum is not needed and will no longer be used
	getmaxyx(stdscr, lineNum, colNum);
	//To display the locomotive in the center of the screen
	lineNum /= 4;
	char str[24][1000] = {'0'};


	int maxLen = strlen("  \\_/      \\__/  \\__/  \\__/  \\__/      \\_/               \\_/   \\_/    \\_/   \\_/");
	//Magic number 6 is for fine adjustment
	while(COLS + maxLen + 6 - slScrInCol != 0) {
		strncpy(str[0], "                        (  ) (@@) ( )  (@)  ()    @@    O     @     O     @      O", slScrInCol);
		strncpy(str[1], "                   (@@@)", slScrInCol);
		strncpy(str[2], "               (    )", slScrInCol);
		strncpy(str[3], "            (@@@@)", slScrInCol);
		strncpy(str[4], "          (   )", slScrInCol);

		strncpy(str[5], "                        (@@) (  ) ( )  ()  (@)    @@    @     O     @     O      @", slScrInCol);
		strncpy(str[6], "                   (   )", slScrInCol);
		strncpy(str[7], "               (@@@@)", slScrInCol);
		strncpy(str[8], "            (    )", slScrInCol);
		strncpy(str[9], "          (@@@)", slScrInCol);

		//Chimney smoke output
		//Smoke is displayed slower than the wheels because the speed at which the smoke and wheels move is different.
		if (count <= 4) {
			mvprintw(lineNum,   COLS-slScrInCol, str[0]);
			mvprintw(lineNum+1 ,COLS-slScrInCol, str[1]);
			mvprintw(lineNum+2 ,COLS-slScrInCol, str[2]);
			mvprintw(lineNum+3 ,COLS-slScrInCol, str[3]);
			mvprintw(lineNum+4 ,COLS-slScrInCol, "");
			mvprintw(lineNum+5 ,COLS-slScrInCol, str[4]);
		} else {
			mvprintw(lineNum,   COLS-slScrInCol, str[5]);
			mvprintw(lineNum+1 ,COLS-slScrInCol, str[6]);
			mvprintw(lineNum+2 ,COLS-slScrInCol, str[7]);
			mvprintw(lineNum+3 ,COLS-slScrInCol, str[8]);
			mvprintw(lineNum+4 ,COLS-slScrInCol, "");
			mvprintw(lineNum+5 ,COLS-slScrInCol, str[9]);
		}

		strncpy(str[10], "      ====        ________                ___________", slScrInCol);
		strncpy(str[11], "  _D _|  |_______/        \\__I_I_____===__|_________|", slScrInCol);
		strncpy(str[12], "   |(_)---  |   H\\________/ |   |        =|___ ___|      _________________", slScrInCol);
		strncpy(str[13], "   /     |  |   H  |  |     |   |         ||_| |_||     _|                \\_____A", slScrInCol);
		strncpy(str[14], "  |      |  |   H  |__--------------------| [___] |   =|                        |", slScrInCol);
		strncpy(str[15], "  | ________|___H__/__|_____/[][]~\\_______|       |   -|                        |", slScrInCol);
		strncpy(str[16], "  |/ |   |-----------i_____I [][] []  D   |=======|____|________________________|_", slScrInCol);

		//Output above the wheels
		mvprintw(lineNum+6 , COLS-slScrInCol, str[10]);
		mvprintw(lineNum+7 , COLS-slScrInCol, str[11]);
		mvprintw(lineNum+8 , COLS-slScrInCol, str[12]);
		mvprintw(lineNum+9 , COLS-slScrInCol, str[13]);
		mvprintw(lineNum+10, COLS-slScrInCol, str[14]);
		mvprintw(lineNum+11, COLS-slScrInCol, str[16]);
		mvprintw(lineNum+12, COLS-slScrInCol, str[10]);


		strncpy(str[17], "__/ =| o |=-O=====O=====O=====O\\  ____Y___________|__|__________________________|_", slScrInCol);
		strncpy(str[18], " |/-=|___|=    ||    ||    ||    |_____/\\___/          |_D__D__D_|  |_D__D__D_|", slScrInCol);
		strncpy(str[19], "  \\_/      \\__/  \\__/  \\__/  \\__/      \\_/               \\_/   \\_/    \\_/   \\_/", slScrInCol);
		strncpy(str[20], "__/ =| o |=-~~\\  /~~\\  /~~\\  /~~\\ ____Y___________|__|__________________________|_", slScrInCol);
		strncpy(str[21], " |/-=|___|=O=====O=====O=====O   |_____/\\___/          |_D__D__D_|  |_D__D__D_|", slScrInCol);
		strncpy(str[22], "  \\_/      \\_O=====O=====O=====O/      \\_/               \\_/   \\_/    \\_/   \\_/", slScrInCol);
		strncpy(str[23], " |/-=|___|=   O=====O=====O=====O|_____/\\___/          |_D__D__D_|  |_D__D__D_|", slScrInCol);


		//Wheel part output
		if (count % 4 == 1) {
			mvprintw(lineNum+13, COLS-slScrInCol, str[17]);
			mvprintw(lineNum+14, COLS-slScrInCol, str[18]);
			mvprintw(lineNum+15, COLS-slScrInCol, str[19]);			
		} else if (count % 4 == 2) {
			mvprintw(lineNum+13, COLS-slScrInCol, str[20]);
			mvprintw(lineNum+14, COLS-slScrInCol, str[21]);
			mvprintw(lineNum+15, COLS-slScrInCol, str[19]);
		} else if (count % 4 == 3) {
			mvprintw(lineNum+13, COLS-slScrInCol, str[20]);
			mvprintw(lineNum+14, COLS-slScrInCol, str[18]);
			mvprintw(lineNum+15, COLS-slScrInCol, str[22]);
		} else {
			mvprintw(lineNum+13, COLS-slScrInCol, str[20]);
			mvprintw(lineNum+14, COLS-slScrInCol, str[23]);
			mvprintw(lineNum+15, COLS-slScrInCol, str[19]);
		}

		//To avoid increasing the number of counts
		if (count / 8 == 1) {
			count = 1;
		} else {
			count++;
		}

		//Screen output when the head of the locomotive goes out from the left side of the screen
		if (COLS - slScrInCol <= 0) {
			if (count <= 4) {
				mvprintw(lineNum,   0, &str[0][slScrOutCol]);
				mvprintw(lineNum+1 ,0, &str[1][slScrOutCol]);
				mvprintw(lineNum+2 ,0, &str[2][slScrOutCol]);
				mvprintw(lineNum+3 ,0, &str[3][slScrOutCol]);
				mvprintw(lineNum+4 ,0, "");
				mvprintw(lineNum+5 ,0, &str[4][slScrOutCol]);
			} else {
				mvprintw(lineNum,   0, &str[5][slScrOutCol]);
				mvprintw(lineNum+1 ,0, &str[6][slScrOutCol]);
				mvprintw(lineNum+2 ,0, &str[7][slScrOutCol]);
				mvprintw(lineNum+3 ,0, &str[8][slScrOutCol]);
				mvprintw(lineNum+4 ,0, "");
				mvprintw(lineNum+5 ,0, &str[9][slScrOutCol]);
			}

			mvprintw(lineNum+6 , 0, &str[10][slScrOutCol]);
			mvprintw(lineNum+7 , 0, &str[11][slScrOutCol]);
			mvprintw(lineNum+8 , 0, &str[12][slScrOutCol]);
			mvprintw(lineNum+9 , 0, &str[13][slScrOutCol]);
			mvprintw(lineNum+10, 0, &str[14][slScrOutCol]);
			mvprintw(lineNum+11, 0, &str[16][slScrOutCol]);
			mvprintw(lineNum+12, 0, &str[10][slScrOutCol]);

			if (count % 4 == 1) {
				mvprintw(lineNum+13, 0, &str[17][slScrOutCol]);
				mvprintw(lineNum+14, 0, &str[18][slScrOutCol]);
				mvprintw(lineNum+15, 0, &str[19][slScrOutCol]);			
			} else if (count % 4 == 2) {
				mvprintw(lineNum+13, 0, &str[20][slScrOutCol]);
				mvprintw(lineNum+14, 0, &str[21][slScrOutCol]);
				mvprintw(lineNum+15, 0, &str[19][slScrOutCol]);
			} else if (count % 4 == 3) {
				mvprintw(lineNum+13, 0, &str[20][slScrOutCol]);
				mvprintw(lineNum+14, 0, &str[18][slScrOutCol]);
				mvprintw(lineNum+15, 0, &str[22][slScrOutCol]);
			} else {
				mvprintw(lineNum+13, 0, &str[20][slScrOutCol]);
				mvprintw(lineNum+14, 0, &str[23][slScrOutCol]);
				mvprintw(lineNum+15, 0, &str[19][slScrOutCol]);
			}
			slScrOutCol++;
		}

		slScrInCol++;
		//Wait 60ms
		usleep(6 * 10000);
		refresh();
		clear();
	}
}

【Execution result】

[My locomotive]

my_sl.gif

[Locomotive of the head family]

org_sl.gif

[Reference site]

I wrote a breakout that is too simple in C language Curses A super-explanatory explanation of frequently used functions

Recommended Posts

[C language] My locomotive is too slow ~ I made a sl command ~
I made my own language. (1)
I made my own language (2)
I made a C ++ learning site
I made a lo command that is more useful than ls
I made a module in C language to filter images loaded by Python
I made a simple RSS reader ~ C edition ~
I made a command to wait for Django to start until the DB is ready
I made a command to markdown the table clipboard
〇✕ I made a game
I made my goimports
I made a command to generate a table comment in Django
I made a surveillance camera with my first Raspberry PI.
When I made a Discord Bot, my classmates destroyed my computer
I made a script to say hello at my Koshien
Heapsort made in C language
Pipenv locking is too slow
I made a python text
I made my own AML
Made a command for FizzBuzz
I made a discord bot
[C language] [Linux] Try to create a simple Linux command * Just add! !!
Is go-template Turing complete? I made a brainf * ck processing system
Python> I made a test code for my own external file
I made a command to display a colorful calendar in the terminal