見出し画像

【C言語プログラミング3】ルフィを描く!

コンソールに何か表示するとして、何ができるかなぁと考えて思い付いたのがタイトルの通りです。ワンピースのモンキー・D・ルフィをC言語で描きます。いや、ルフィを描くプログラムを書きたいと思います。

・・・・・
・・・・
製作中
・・

できた。

ルフィを描くソースコード

以下がルフィを描くソースコードです。一気にプログラムっぽくなりましたね。とは言えトータル100行程度の小さなプログラムです。

#include <stdio.h>

#include <windows.h>

#define IMAGE_HEIGHT	100

#define IMAGE_WIDTH		100

unsigned char gucImage[IMAGE_HEIGHT][IMAGE_WIDTH];
unsigned char gucImageData[1250] = {
	0x00,0x00,0x00,0x41,0x08,0x07,0x00,0x00,0x00,0x00,0x9f,0x0f,0x00,0x00,0x00,0x08,0xde,0x0f,0x00,0x00,0x00,0x00,0x00,0x6f,0x02,0x00,
	0x00,0x40,0x58,0x3d,0x00,0x00,0x00,0x00,0x00,0x80,0xc7,0x00,0x00,0x00,0xc6,0xfd,0x00,0x00,0x00,0x00,0x00,0x00,0xe0,0x0d,0x00,0x00,
	0x20,0xf0,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,0x04,0x00,0x00,0xf3,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xdc,0x00,0x00,0xf0,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0f,0x00,0xc0,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x80,0x07,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0xc0,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x03,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x1c,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x61,0x00,0x1e,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x10,0x01,0xe0,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0d,0x00,0xfe,0x03,0x00,0xfc,0xff,0x03,
	0x00,0x00,0x00,0x01,0x70,0x00,0xc0,0xff,0x01,0xfc,0xff,0xff,0x07,0x00,0x00,0x34,0x00,0x03,0x00,0xf8,0xff,0xff,0xff,0xff,0xff,0x1f,
	0x00,0xa1,0x05,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0x60,0x2f,0x00,0x00,0x00,0xe0,0xff,0xff,0xff,0xff,0xff,0xff,0x87,
	0xc9,0x00,0x00,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0x6d,0xcb,0x02,0x00,0x00,0x80,0xff,0xff,0xff,0xff,0xff,0xff,0x3f,0x09,
	0x52,0x00,0x00,0x00,0xf0,0xff,0xdf,0xff,0xff,0xff,0xff,0x6f,0x50,0x02,0x00,0x00,0x00,0xff,0xff,0xfd,0xff,0xff,0xff,0xff,0x53,0x4b,
	0x00,0x00,0x00,0xf8,0xff,0x8f,0xff,0xff,0xff,0xff,0x7f,0x6a,0x00,0x00,0x00,0xe0,0xff,0xdf,0xf8,0xff,0xff,0xff,0xff,0x6f,0x01,0x00,
	0x00,0x80,0xff,0xff,0x0c,0xff,0xff,0xff,0xff,0xff,0x2f,0x02,0x00,0x00,0xfc,0xff,0x4f,0xf0,0xff,0xff,0xff,0xff,0xff,0xa9,0x02,0x00,
	0xf0,0xff,0x7f,0x04,0xff,0xff,0xff,0xff,0xff,0x0f,0x75,0x00,0x80,0xff,0xff,0x63,0xe0,0xff,0xff,0xff,0xff,0xff,0xaf,0x06,0x00,0xfe,
	0xff,0x3f,0x06,0xfe,0xbf,0xff,0xff,0xff,0xbf,0x28,0x00,0xf0,0xff,0xff,0x63,0xc0,0xdf,0xfb,0xff,0xff,0xff,0x15,0x00,0xe0,0xff,0xff,
	0x1f,0x06,0xfc,0xb9,0xff,0xff,0xff,0xbf,0x50,0x80,0x8f,0xff,0xff,0x61,0xc0,0x9f,0xf3,0xff,0xff,0xff,0x4b,0x05,0x00,0xf8,0xff,0x0f,
	0x06,0xf8,0x39,0xfe,0xff,0xff,0x7f,0xae,0x00,0xc0,0xff,0xff,0x60,0x80,0xbf,0xe3,0xff,0xff,0xff,0x97,0x0a,0x00,0xfe,0xff,0x07,0x06,
	0xf0,0x33,0xfc,0xff,0xff,0x7f,0x4d,0x00,0xe0,0xff,0x7f,0x40,0x00,0x3f,0xc3,0xff,0xff,0xff,0x5f,0x01,0x00,0xfe,0xff,0x07,0x04,0xf0,
	0x73,0xf8,0xff,0xff,0xff,0x16,0x00,0xf0,0xff,0x3f,0x00,0x00,0x3e,0x87,0xff,0xff,0xff,0xaf,0x00,0x00,0xff,0xff,0x03,0x00,0xe0,0x63,
	0xf8,0xff,0xff,0xff,0x63,0x00,0xf8,0xff,0x1f,0x00,0x00,0x3c,0x06,0xff,0xff,0xff,0x1f,0x86,0x82,0xff,0xff,0x01,0x00,0xc0,0x63,0xf0,
	0xff,0xff,0xff,0xa1,0x28,0xfc,0xff,0x0f,0x00,0x00,0x3c,0x04,0xfe,0xff,0xff,0x5f,0x2a,0xca,0xff,0xff,0x00,0x00,0x80,0x43,0xe0,0xff,
	0xff,0xff,0x85,0xaa,0xfc,0xff,0x0f,0x00,0x00,0x38,0x80,0xff,0xff,0xff,0x5f,0xa9,0xea,0xff,0x7f,0x00,0x00,0x80,0x03,0xcf,0xff,0xff,
	0xff,0xd7,0xac,0xfe,0xff,0x07,0x00,0x00,0x30,0x1e,0xfc,0xff,0xff,0x3f,0x0d,0xf0,0xff,0x7f,0x00,0x00,0x00,0x3b,0xc0,0xfb,0xff,0xff,
	0x03,0x80,0xdf,0xff,0x07,0x00,0x00,0xf0,0xf8,0xbb,0xff,0xff,0x3f,0x04,0xfc,0xfe,0xff,0x03,0x00,0x00,0xe1,0xe0,0xfb,0xff,0xff,0x4b,
	0xe0,0xe3,0xff,0xf3,0x01,0x00,0x00,0x03,0xb8,0xff,0xff,0x3f,0x84,0x5f,0xff,0x3f,0xf0,0x03,0x00,0x88,0x81,0xfb,0xff,0xff,0x53,0xfc,
	0xe6,0xff,0x83,0x77,0x00,0xc0,0x3c,0xb0,0xff,0xff,0x3f,0xe5,0xa5,0xfe,0x3f,0x8e,0x0d,0x00,0xc4,0x07,0xfb,0xff,0xff,0x43,0xf2,0xf4,
	0xff,0x13,0xe0,0x00,0x40,0x7c,0x30,0xff,0xff,0x3f,0x20,0x62,0xff,0xb7,0x39,0x0e,0x00,0x84,0x03,0xf1,0xff,0xff,0x0b,0x86,0xfa,0x7f,
	0xcb,0xc7,0x00,0x40,0x00,0x10,0xf9,0xff,0x3f,0x10,0x5a,0xff,0xf7,0x7c,0x08,0x00,0x00,0x00,0x90,0xff,0xff,0x53,0xb4,0xfb,0x7f,0x87,
	0x83,0x00,0x00,0x00,0x00,0xf9,0xff,0x7f,0xa5,0x55,0xfe,0x67,0x10,0x08,0x00,0x00,0x00,0x80,0xff,0xff,0x57,0xaa,0xee,0x7f,0x00,0x40,
	0x00,0x00,0x01,0x04,0xd8,0xff,0x7f,0x55,0x15,0xff,0x0f,0x00,0x04,0x00,0x60,0x30,0x80,0xfd,0xe7,0x3b,0x1b,0xfa,0xfb,0x00,0x40,0x00,
	0x00,0xf8,0x61,0xc8,0xff,0x7d,0xa8,0x52,0x3b,0x0f,0x03,0x06,0x00,0x00,0xa0,0x83,0xfc,0xd3,0x07,0xa5,0xd5,0xf1,0x60,0x78,0x00,0x00,
	0x00,0x1e,0xc8,0x1f,0xed,0xa4,0xaa,0xde,0x0e,0x7c,0x06,0x00,0x1c,0x5f,0x00,0xbc,0xd0,0x8e,0x50,0xd5,0x9a,0x00,0x40,0x04,0x00,0x3f,
	0x00,0xc0,0x0d,0xdd,0x5b,0xab,0x5e,0x1a,0x00,0x58,0x00,0x00,0x00,0x00,0x5c,0xd8,0x65,0x2d,0xe5,0x5b,0x01,0x00,0x01,0x00,0x00,0x00,
	0xc0,0xec,0x2e,0xa5,0x2c,0xd7,0x12,0x00,0x00,0x00,0x00,0x00,0x00,0x4c,0xe1,0xb2,0x94,0x94,0x96,0x01,0x00,0xf0,0xff,0xff,0x00,0x40,
	0x0e,0x9e,0x9b,0x86,0xd2,0x34,0x00,0xf8,0x01,0xc0,0x7d,0x00,0xe4,0xf3,0x50,0x92,0x56,0x82,0x03,0xf0,0x00,0x00,0x8c,0x04,0x20,0x22,
	0x1f,0x00,0x56,0x8a,0x37,0xc0,0x01,0x00,0x40,0x78,0x00,0xbe,0xf9,0x00,0x40,0xdb,0xb1,0x02,0x0f,0x00,0x00,0x80,0x07,0xe0,0x8c,0x2e,
	0x00,0x68,0x82,0x34,0xf0,0x00,0x00,0x00,0x68,0x00,0x72,0xdc,0x0a,0x02,0x09,0x3e,0x06,0x01,0xf0,0x3f,0x00,0x02,0xe0,0xe1,0x5a,0xa5,
	0x20,0xe8,0x44,0x10,0xf0,0xff,0x3f,0x20,0x00,0x87,0x9f,0x6a,0xd4,0x00,0xbd,0x0e,0xc3,0xff,0xff,0x0f,0x03,0x30,0xec,0x54,0x9a,0x82,
	0xa6,0xfd,0x30,0xff,0xff,0xff,0x13,0x00,0xf1,0xa7,0x52,0x17,0xd8,0x64,0x1a,0xfa,0xff,0xdf,0xff,0x01,0xf8,0xdf,0xa0,0x0a,0x80,0x33,
	0x5e,0xe1,0xfe,0xff,0xb7,0x0f,0x80,0x7f,0x02,0x80,0x98,0xf0,0xf0,0x30,0xfe,0xbf,0xee,0xfe,0x00,0x7c,0xda,0x02,0x00,0x13,0xbe,0x1c,
	0xc6,0x3f,0x08,0x70,0x07,0xe0,0xc7,0x92,0x10,0xc0,0x07,0x36,0x50,0xfc,0x01,0x00,0x20,0x00,0x7f,0x92,0xa4,0x01,0xf0,0xd0,0xc6,0x88,
	0x0f,0x00,0x00,0x03,0xf8,0x86,0x34,0x31,0x04,0x38,0xb3,0x86,0xf1,0xf8,0xff,0x1b,0xe0,0x3f,0xac,0x8c,0x00,0x00,0x44,0x30,0x3a,0x7e,
	0x00,0xe0,0x80,0x7f,0x8e,0x35,0x91,0x00,0xc0,0xf2,0x5e,0xc6,0x00,0x00,0x06,0xfc,0xdf,0xe4,0x8c,0x0c,0x00,0x50,0x0e,0xcb,0x18,0x00,
	0x38,0xf0,0xff,0xb0,0x17,0x3b,0x00,0x00,0x38,0x44,0x1a,0x0f,0xe0,0xc0,0xff,0x3f,0x43,0xcc,0x00,0x00,0x00,0x8c,0xc1,0x83,0xff,0x01,
	0xff,0xbf,0x0b,0x83,0x07,0x00,0x00,0x00,0xcf,0x66,0x00,0x00,0xf8,0xff,0x7f,0x80,0x6f,0x00,0x00,0x00,0x00,0x0f,0x0c,0x00,0xe0,0xff,
	0x6f,0xc0,0x0f,0x03,0x00,0x00,0x00,0x80,0x33,0x03,0x80,0xff,0x7f,0xfe,0x03,0x30,0x00,0x00,0x00,0x00,0xc0,0xe3,0x00,0xff,0x7d,0x27,
	0x00,0x00,0x03,0x00,0x00,0x00,0x00,0xe0,0x7f,0xf8,0xff,0x2f,0x02,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0xc0,0xff,0xff,0xee,0x20,0x00,
	0x80,0x01
};

void DecompressLuffyData(void);
void PrintLuffy(void);

void main(void)
{
	DecompressLuffyData();		/*	ルフィの画像データを解凍	*/
	PrintLuffy();				/*	ルフィを表示する		*/
}

void DecompressLuffyData(void)
{
	int Hcount;
	int Wcount;
	int bytePos = 0;
	int bitsift = 0;

	for (Hcount = 0; Hcount < IMAGE_HEIGHT; Hcount++) {
		for (Wcount = 0; Wcount < IMAGE_WIDTH; Wcount++) {
			gucImage[Hcount][Wcount] = (gucImageData[bytePos] >> bitsift) & 0x01;

			if (bitsift == 7) {
				bitsift = 0;
				bytePos++;
			}
			else {
				bitsift++;
			}
		}
	}
}

void PrintLuffy(void)
{
	int Hcount;
	int Wcount;

	for (Hcount = 0; Hcount < IMAGE_HEIGHT; Hcount++) {
		for (Wcount = 0; Wcount < IMAGE_WIDTH; Wcount++) {
			Sleep(1);

			if (gucImage[Hcount][Wcount] == 0) {
				printf("■");
			}
			else {
				printf("□");
			}
		}
		printf("\n");
	}
}

このソースコードをコピペしてビルド後に実行すると、麦わらのルフィがコンソール上に描かれるはずです。コンソールの画面サイズやフォントサイズを調整しないと綺麗に見えない可能性があるので、その場合は以下の手順でコンソールのプロパティを調整して下さい。

赤丸の部分にマウスポインタを合わせて右クリックし、プロパティ(P)をクリックする。

コンソールのプロティ

プロパティが開くとフォントタブを選択しサイズ(S)を12以下に設定する。

フォント調整

後はコンソールの画面を最大化すれば綺麗に見えると思います。

ルフィが描かれる様子

私の実行環境で実行した時はこんな感じでした。動画ファイルにしたので参考にどうぞ。

おぉー!ルフィやんってなりましたかね?

これはどうやってんの?

じゃー、これはどうやってんのかって話になりますが、それはソースコードに書いてある通りです。以上。


と言うのは冗談で、一つずつ解説をして行きたいと思います。ほんの100行程度のソースコードですが、これを理解するにはC言語の文法を理解する必要があります。今回は詳細な解説には入りませんが、コードを読み解く上で必要なC文法のリストをまとめておきます。

1)define定義
2)一次元配列、二次元配列
3)自作関数
4)forループ
5)if条件式
6)算術演算子、論理演算子、ビット演算子
7)16進数&2進数

大雑把にこれだけのことを知っていれば、今回のプログラムは書けます。解説は次回以降で。つづく。

この記事が気に入ったらサポートをしてみませんか?