c語言顯示心型程式碼
阿新 • • 發佈:2022-04-19
程式碼來源來自網上,本菜鳥只是剪接了一下
字首和常量
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> #include<windows.h> #include <graphics.h> #include <time.h> #include <conio.h> #define U 0.1 #define V 0.053//U、V用於控制心的形狀 #define High 500 // 遊戲畫面尺寸 #define Width 1000 #defineCharSize 25 // 每個字元顯示的大小
心型的字型顏色設定
void SetColor(unsigned short ForeColor, unsigned short BackGroundColor) { HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hCon, (ForeColor) | (BackGroundColor)); // SetConsoleTextAttribute函式:是系統設定控制檯背景色和前景色的函式 ,在標頭檔案windows.h中// Set Console TextAttribute 漢意:設定控制檯的文字屬性 /* 原型為: BOOL SetConsoleTextAttribute(HANDLE hConsoleOutput, WORD wAttributes) */ //hCon:是一個 HANDLE 型別的變數,控制代碼,某種結構的唯一標識(其值是一個整型數) //ForeColor:前景色(字元顏色);BackGroundColor:背景色,用十六進位制數表示 }
一個特別裝的滾動字元程式碼
void display() { int highNum = High / CharSize;View Codeint widthNum = Width / CharSize; // 儲存對應字元矩陣中需要輸出字元的ASCII碼 int CharRain[Width / CharSize][High / CharSize]; int CNum[Width / CharSize]; // 每一列的有效字元個數 int ColorG[Width / CharSize]; // 每一列字元的顏色 int i, j, x, y; srand((unsigned)time(NULL)); // 設定隨機函式種子 for (i = 0; i < widthNum; i++) // 初始化字元矩陣 { CNum[i] = (rand() % (highNum * 9 / 10)) + highNum / 10; // 這一列的有效字元個數 ColorG[i] = 255; for (j = 0; j < CNum[i]; j++) CharRain[j][i] = (rand() % 26) + 65; // 產生A-Z的隨機ASCII碼 } initgraph(Width, High); BeginBatchDraw(); settextstyle(25, 10, _T("Courier")); // 設定字型 // 下面每一幀,讓字元向下移動,然後最上面產生新的字元 int num = 0; while (1) { num++; for (i = 0; i < widthNum; i++) { if (CNum[i] < highNum - 1) // 當這一列字元沒有填滿時 { for (j = CNum[i] - 1; j >= 0; j--) // 向下移動一格 CharRain[j + 1][i] = CharRain[j][i]; CharRain[0][i] = (rand() % 26) + 65; // 最上面的產生A-Z的隨機ASCII碼 CNum[i] = CNum[i] + 1; // 這一列的有效字元的個數+1 } else // 這一列字元已經填滿 { if (ColorG[i] > 40) ColorG[i] = ColorG[i] - 20; // 讓滿的這一列逐漸變暗 else { CNum[i] = (rand() % (highNum / 3)) + highNum / 10; // 這一列字元的個數 ColorG[i] = (rand() % 75) + 180; // 這一列字元的顏色 for (j = 0; j < CNum[i]; j++) // 重新初始化這一列字元 CharRain[j][i] = (rand() % 26) + 65; // 產生A-Z的隨機ASCII碼 } } } // 輸出整個字元矩陣 for (i = 0; i < widthNum; i++) { x = i * CharSize; // 當前字元的x座標 for (j = 0; j < CNum[i]; j++) { y = j * CharSize; // 當前字元的y座標 setcolor(RGB(0, ColorG[i], 0)); outtextxy(x, y, CharRain[j][i]); // 輸出當前字元 } } FlushBatchDraw(); Sleep(100); clearrectangle(0, 0, Width - 1, High - 1); // 清空畫面全部矩形區域 if (num >= 20) { break; return; } } EndBatchDraw(); /*_getch();*/ closegraph(); }
彩色心型程式碼
void xin() { int i, s = 0, t, a = 10, b = 11, c = 12, d = 13, e = 14; int z[] = { -49,-78,-45,-83,-74,-2,-54,-82,-76,-13,-93,-95 }; //ASCII碼 float x, y, m; for (y = 1.3; y >= -1.1; y -= U) { for (x = -2; x < 1.4; x += V) { if ((((x * x + y * y - 1) * (x * x + y * y - 1) * (x * x + y * y - 1) - x * x * y * y * y) <= 0))//心形線的公式 { if (y >= 1.3 - 10 * U || y <= 1.3 - 11 * U) { s++; if (s % 4 == 1) { SetColor(a, 0); printf("1"); } if (s % 4 == 2) { SetColor(e, 0); printf("0"); } if (s % 4 == 3) { SetColor(c, 0); printf("2"); } if (s % 4 == 0) { SetColor(d, 0); printf("4"); } } else { for (i = 0; i < 42; i++) { if (i <= 14 || i >= 28) { s++; if (s % 4 == 1) { SetColor(a, 0); printf("1"); } if (s % 4 == 2) { SetColor(e, 0); printf("0"); } if (s % 4 == 3) { SetColor(c, 0); printf("2"); } if (s % 4 == 0) { SetColor(d, 0); printf("4"); } } else { SetColor(b, 0); printf("%c", z[i - 15]); Sleep(50); } } break; } } else printf(" "); Sleep(1); } printf("\n"); } printf("請按任意鍵繼續!"); getchar(); while (1) { system("cls"); t = a; a = b; b = c; c = d; d = e; e = t; for (y = 1.3; y >= -1.1; y -= U) { for (x = -2; x < 1.4; x += V) { if ((((x * x + y * y - 1) * (x * x + y * y - 1) * (x * x + y * y - 1) - x * x * y * y * y) <= 0)) { if (y >= 1.3 - 10 * U || y <= 1.3 - 11 * U) { s++; if (s % 4 == 1) { SetColor(a, 0); printf("1"); } if (s % 4 == 2) { SetColor(b, 0); printf("0"); } if (s % 4 == 3) { SetColor(c, 0); printf("2"); } if (s % 4 == 0) { SetColor(d, 0); printf("4"); } } else { for (i = 0; i < 42; i++) { if (i <= 14 || i >= 28) { s++; if (s % 4 == 1) { SetColor(a, 0); printf("1"); } if (s % 4 == 2) { SetColor(b, 0); printf("0"); } if (s % 4 == 3) { SetColor(c, 0); printf("2"); } if (s % 4 == 0) { SetColor(d, 0); printf("4"); } } else { SetColor(e, 0); printf("%c", z[i - 15]); } } break; } } else printf(" "); } printf("\n"); } Sleep(1000); } }View Code
int main() { display(); xin(); }