Easyx项目小合集
生活随笔
收集整理的這篇文章主要介紹了
Easyx项目小合集
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
這好像是很早之前,在官網(wǎng)找的項目
目錄
- 項目名稱: 迷宮
- 項目名稱: 打雷風景
- 項目名稱: 煙花
項目名稱: 迷宮
#include <graphics.h> #include <time.h>// // 定義全局變量 // BYTE** g_aryMap = NULL; // 迷宮地圖 SIZE g_szMap; // 迷宮地圖的尺寸 IMAGE g_imgSight(360, 280); // 游戲的視野 RECT g_rtSight; // 游戲的視野的范圍 IMAGE g_imgItem(180, 20); // 地圖元素 IMAGE g_imgGPS; // 迷你地圖,用于顯示游戲者在地圖中的位置 POINT g_ptGPS; // 迷你地圖的顯示位置 SIZE g_szGPS; // 迷你地圖的尺寸 POINT g_ptPlayer; // 游戲者的位置// 枚舉地圖元素,兼做元素位置的 x 坐標 enum MAPITEM { MAP_WALL = 0, MAP_PLAYER = 20, MAP_GROUND = 40, MAP_MARKRED = 60, MAP_MARKGREEN = 80, MAP_MARKYELLOW = 100, MAP_ENTRANCE = 120, MAP_EXIT = 140, MAP_OUTSIDE = 160 }; // 枚舉用戶的控制命令 enum CMD { CMD_QUIT = 1, CMD_UP = 2, CMD_DOWN = 4, CMD_LEFT = 8, CMD_RIGHT = 16, CMD_MARKRED = 32, CMD_MARKGREEN = 64, CMD_MARKYELLOW = 128, CMD_CLEARMARK = 256 };// // 函數(shù)聲明 // void Welcome(); // 繪制游戲界面 void InitImage(); // 初始化游戲圖片 void InitGame(); // 初始化游戲數(shù)據(jù) void GetMazeSize(); // 提示用戶輸入迷宮大小 void MakeMaze(int width, int height); // 生成迷宮:初始化(注:寬高必須是奇數(shù)) void TravelMaze(int x, int y); // 生成迷宮:遍歷 (x, y) 四周 MAPITEM GetMazeItem(int x, int y); // 獲取指定坐標的迷宮元素 void Paint(); // 繪制視野范圍內(nèi)的迷宮 int GetCmd(); // 獲取用戶輸入的命令 void DispatchCmd(int cmd); // 處理用戶輸入的命令 void OnUp(); // 向上移動 void OnLeft(); // 向左移動 void OnRight(); // 向右移動 void OnDown(); // 向下移動 void OnMark(MAPITEM value); // 在地圖中做標記 bool CheckWin(); // 檢查是否到出口 bool Quit(); // 詢問用戶是否退出游戲// // 函數(shù)定義 //// 主程序 void main() {// 初始化initgraph(640, 480); // 創(chuàng)建繪圖窗口srand((unsigned)time(NULL)); // 設(shè)置隨機種子// 顯示主界面Welcome();// 初始化InitImage();InitGame();// 游戲過程int c;while( !(((c = GetCmd()) & CMD_QUIT) && Quit()) ){DispatchCmd(c);Paint();if (CheckWin())break;// 延時Sleep(100);}// 清理迷宮地圖占用的內(nèi)存for(int x = 0; x < g_szMap.cx + 2; x++)delete[] g_aryMap[x];delete [] g_aryMap;// 關(guān)閉圖形模式closegraph(); }// 繪制游戲界面 void Welcome() {// 繪制漸變色外框for(int i=0; i<128; i++){setlinecolor(RGB(0, 0, (127 - i) << 1));rectangle(149 - i, 109 - (i >> 1), 490 + i, 370 + (i >> 1));}// 設(shè)置字體樣式settextcolor(WHITE);setbkmode(TRANSPARENT);// 繪制標題settextstyle(36, 0, _T("宋體"));outtextxy(248, 40, _T("迷 宮"));// 繪制操作說明settextstyle(12, 0, _T("宋體"));outtextxy(50, 382, _T("控制說明:"));outtextxy(74, 400, _T("方向鍵或 A/S/D/W:移動"));outtextxy(74, 418, _T("空格、Y、G:在地圖上做紅、黃、綠色 M 標記"));outtextxy(74, 436, _T("C:清除地圖上的標記"));outtextxy(74, 454, _T("ESC:退出程序")); }// 初始化游戲圖片 void InitImage() {// 預(yù)繪制游戲圖片到 IMAGE 緩存(可以修改為加載圖片以獲得更好效果)SetWorkingImage(&g_imgItem);cleardevice();// 繪制 PLAYERsetorigin(MAP_PLAYER, 0);setfillcolor(YELLOW);setlinecolor(YELLOW);fillellipse(2, 2, 17, 17);setlinecolor(BLACK);line(7, 7, 7, 8);line(12, 7, 12, 8);arc(5, 6, 14, 14, 3.34, 6.08);// 繪制墻壁setorigin(MAP_WALL, 0);settextcolor(BROWN);setfillstyle((BYTE*)"\x20\x20\x20\xff\x04\x04\x04\xff");setlinecolor(BROWN);solidrectangle(1, 1, 18, 18);rectangle(0, 0, 19, 19);// 繪制紅色標記setorigin(MAP_MARKRED, 0);setlinecolor(RED);moveto(5, 15);linerel(0, -10); linerel(5, 5); linerel(5, -5); linerel(0, 10);// 繪制綠色標記setorigin(MAP_MARKGREEN, 0);setlinecolor(GREEN);moveto(5, 15);linerel(0, -10); linerel(5, 5); linerel(5, -5); linerel(0, 10);// 繪制黃色標記setorigin(MAP_MARKYELLOW, 0);setlinecolor(YELLOW);moveto(5, 15);linerel(0, -10); linerel(5, 5); linerel(5, -5); linerel(0, 10);// 繪制入口setorigin(MAP_ENTRANCE, 0);setlinecolor(GREEN);settextstyle(12, 0, _T("宋體"));outtextxy(4, 4, _T("入"));// 繪制出口setorigin(MAP_EXIT, 0);outtextxy(4, 4, _T("出"));// 繪制迷宮外面的空地setorigin(MAP_OUTSIDE, 0);settextcolor(GREEN);setfillstyle((BYTE*)"\x50\x55\x22\x20\x05\x55\x22\x02");solidrectangle(0, 0, 19, 19);// 恢復(fù)坐標系setorigin(0, 0);// 顯示作者SetWorkingImage();settextcolor(BLUE);TCHAR author[] = _T("Powered by zhaoh1987@qq.com");outtextxy(471, 4, author);settextcolor(LIGHTBLUE);outtextxy(470, 3, author); }// 初始化游戲數(shù)據(jù) void InitGame() {// 提示用戶輸入迷宮大小GetMazeSize();// 初始化參數(shù)if (g_aryMap != NULL){ // 清理迷宮地圖占用的內(nèi)存for(int x = 0; x < g_szMap.cx + 2; x++)delete[] g_aryMap[x];delete [] g_aryMap;}MakeMaze(g_szMap.cx, g_szMap.cy); // 創(chuàng)建迷宮g_ptPlayer.x = 2; // 設(shè)置游戲者的位置g_ptPlayer.y = 2;g_rtSight.left = 0; // 設(shè)置視野范圍g_rtSight.top = 0;g_rtSight.right = 17;g_rtSight.bottom= 13;// 設(shè)置 GPS 顯示區(qū)setfillcolor(BLUE);solidrectangle(522, 368, 637, 471);if (g_szMap.cx > g_szMap.cy) { g_szGPS.cx = 100; g_szGPS.cy = (int)(100.0 * g_szMap.cy / g_szMap.cx + 0.5); }else { g_szGPS.cy = 100; g_szGPS.cx = (int)(100.0 * g_szMap.cx / g_szMap.cy + 0.5); }Resize(&g_imgGPS, g_szGPS.cx, g_szGPS.cy);g_ptGPS.x = 530 + 50 - g_szGPS.cx / 2;g_ptGPS.y = 370 + 50 - g_szGPS.cy / 2;// 畫迷你地圖外框setlinecolor(RED);rectangle(g_ptGPS.x - 1, g_ptGPS.y - 1, g_ptGPS.x + g_szGPS.cx, g_ptGPS.y + g_szGPS.cy);// 畫迷你地圖入口和出口setlinecolor(YELLOW);moveto(g_ptGPS.x - 8, g_ptGPS.y + g_szGPS.cy / g_szMap.cy);linerel(7, 0); linerel(-3, -3); moverel(3, 3); linerel(-3, 3);moveto(g_ptGPS.x + g_szGPS.cx, g_ptGPS.y + g_szGPS.cy - g_szGPS.cy / g_szMap.cy);linerel(7, 0); linerel(-3, -3); moverel(3, 3); linerel(-3, 3);// 繪制游戲區(qū)Paint(); }// 提示用戶輸入迷宮大小 void GetMazeSize() {g_szMap.cx = g_szMap.cy = 0;// 獲取用戶輸入的寬高TCHAR s[4];while(g_szMap.cx < 20 || g_szMap.cx > 200){InputBox(s, 4, _T("請輸入迷宮的寬度\n范圍:20~200"), _T("輸入"), _T("25"));g_szMap.cx = _ttoi(s);}while(g_szMap.cy < 20 || g_szMap.cx > 200){InputBox(s, 4, _T("請輸入迷宮的高度\n范圍:20~200"), _T("輸入"), _T("25"));g_szMap.cy = _ttoi(s);}// 確保寬高為奇數(shù)if (g_szMap.cx % 2 != 1) g_szMap.cx++;if (g_szMap.cy % 2 != 1) g_szMap.cy++; }// 生成迷宮:初始化(注:寬高必須是奇數(shù)) void MakeMaze(int width, int height) {if (width % 2 != 1 || height % 2 != 1)return;int x, y;// 定義迷宮尺寸,并分配迷宮內(nèi)存g_aryMap = new BYTE*[width + 2];for(x = 0; x < width + 2; x++){g_aryMap[x] = new BYTE[height + 2];memset(g_aryMap[x], MAP_WALL, height + 2);}// 定義邊界for (x = 0; x <= width + 1; x++)g_aryMap[x][0] = g_aryMap[x][height + 1] = MAP_GROUND;for (y = 1; y <= height; y++)g_aryMap[0][y] = g_aryMap[width + 1][y] = MAP_GROUND;// 定義入口和出口g_aryMap[1][2] = MAP_ENTRANCE;g_aryMap[width][height - 1] = MAP_EXIT;// 從任意點開始遍歷生成迷宮TravelMaze(((rand() % (width - 1)) & 0xfffe) + 2, ((rand() % (height - 1)) & 0xfffe) + 2);// 將邊界標記為迷宮外for (x = 0; x <= width + 1; x++)g_aryMap[x][0] = g_aryMap[x][height + 1] = MAP_OUTSIDE;for (y = 1; y <= height; y++)g_aryMap[0][y] = g_aryMap[width + 1][y] = MAP_OUTSIDE; }// 生成迷宮:遍歷 (x, y) 四周 void TravelMaze(int x, int y) {// 定義遍歷方向int d[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};// 將遍歷方向亂序int n, t, i;for(i = 0; i < 4; i++){n = rand() % 4;t = d[i][0], d[i][0] = d[n][0], d[n][0] = t;t = d[i][1], d[i][1] = d[n][1], d[n][1] = t;}// 嘗試周圍四個方向g_aryMap[x][y] = MAP_GROUND;for(i = 0; i < 4; i++)if (g_aryMap[x + 2 * d[i][0]][y + 2 * d[i][1]] == MAP_WALL){g_aryMap[x + d[i][0]][y + d[i][1]] = MAP_GROUND;TravelMaze(x + d[i][0] * 2, y + d[i][1] * 2); // 遞歸} }// 獲取指定坐標的迷宮元素 MAPITEM GetMazeItem(int x, int y) {return (MAPITEM)g_aryMap[x][y]; }// 繪制視野范圍內(nèi)的迷宮 void Paint() {int x1, y1;// 繪制視野內(nèi)的迷宮SetWorkingImage(&g_imgSight);for(int x = g_rtSight.left; x <= g_rtSight.right; x++)for(int y = g_rtSight.top; y <= g_rtSight.bottom; y++){x1 = (x - g_rtSight.left) * 20;y1 = (y - g_rtSight.top) * 20;putimage(x1, y1, 20, 20, &g_imgItem, GetMazeItem(x, y), 0);}// 繪制游戲者x1 = (g_ptPlayer.x - g_rtSight.left) * 20;y1 = (g_ptPlayer.y - g_rtSight.top) * 20;putimage(x1, y1, 20, 20, &g_imgItem, MAP_PLAYER, 0);// 繪制迷你地圖SetWorkingImage(&g_imgGPS);cleardevice();int tx = (int)((g_ptPlayer.x - 1) * g_szGPS.cx / (double)(g_szMap.cx - 1) + 0.5);int ty = (int)((g_ptPlayer.y - 1) * g_szGPS.cy / (double)(g_szMap.cy - 1) + 0.5);setlinecolor(YELLOW);circle(tx, ty, 1);// 更新到繪圖窗口SetWorkingImage();putimage(150, 110, 340, 260, &g_imgSight, 10, 10);putimage(g_ptGPS.x, g_ptGPS.y, &g_imgGPS); }// 獲取用戶輸入的命令 int GetCmd() {int c = 0;if (GetAsyncKeyState(VK_LEFT) & 0x8000) c |= CMD_LEFT;if (GetAsyncKeyState(VK_RIGHT) & 0x8000) c |= CMD_RIGHT;if (GetAsyncKeyState(VK_UP) & 0x8000) c |= CMD_UP;if (GetAsyncKeyState(VK_DOWN) & 0x8000) c |= CMD_DOWN;if (GetAsyncKeyState('A') & 0x8000) c |= CMD_LEFT;if (GetAsyncKeyState('D') & 0x8000) c |= CMD_RIGHT;if (GetAsyncKeyState('W') & 0x8000) c |= CMD_UP;if (GetAsyncKeyState('S') & 0x8000) c |= CMD_DOWN;if (GetAsyncKeyState(' ') & 0x8000) c |= CMD_MARKRED;if (GetAsyncKeyState('G') & 0x8000) c |= CMD_MARKGREEN;if (GetAsyncKeyState('Y') & 0x8000) c |= CMD_MARKYELLOW;if (GetAsyncKeyState('C') & 0x8000) c |= CMD_CLEARMARK;if (GetAsyncKeyState(VK_ESCAPE) & 0x8000) c |= CMD_QUIT;return c; }// 處理用戶輸入的命令 void DispatchCmd(int cmd) {if (cmd & CMD_UP) OnUp();if (cmd & CMD_DOWN) OnDown();if (cmd & CMD_LEFT) OnLeft();if (cmd & CMD_RIGHT) OnRight();if (cmd & CMD_MARKRED) OnMark(MAP_MARKRED);if (cmd & CMD_MARKGREEN) OnMark(MAP_MARKGREEN);if (cmd & CMD_MARKYELLOW) OnMark(MAP_MARKYELLOW);if (cmd & CMD_CLEARMARK) OnMark(MAP_GROUND); }// 向上移動 void OnUp() {if (g_ptPlayer.y > 1 && GetMazeItem(g_ptPlayer.x, g_ptPlayer.y - 1) != MAP_WALL){g_ptPlayer.y--;if (g_ptPlayer.y - g_rtSight.top < 4 && g_rtSight.top > 0){g_rtSight.top--;g_rtSight.bottom--;}} }// 向左移動 void OnLeft() {if (g_ptPlayer.x > 1 && GetMazeItem(g_ptPlayer.x - 1, g_ptPlayer.y) != MAP_WALL && GetMazeItem(g_ptPlayer.x - 1, g_ptPlayer.y) != MAP_ENTRANCE){g_ptPlayer.x--;if (g_ptPlayer.x - g_rtSight.left < 5 && g_rtSight.left > 0){g_rtSight.left--;g_rtSight.right--;}} }// 向右移動 void OnRight() {if (g_ptPlayer.x < g_szMap.cx && GetMazeItem(g_ptPlayer.x + 1, g_ptPlayer.y) != MAP_WALL){g_ptPlayer.x++;if (g_rtSight.right - g_ptPlayer.x < 5 && g_rtSight.right <= g_szMap.cx){g_rtSight.left++;g_rtSight.right++;}} }// 向下移動 void OnDown() {if (g_ptPlayer.y < g_szMap.cy && GetMazeItem(g_ptPlayer.x, g_ptPlayer.y + 1) != MAP_WALL){g_ptPlayer.y++;if (g_rtSight.bottom - g_ptPlayer.y < 4 && g_rtSight.bottom <= g_szMap.cy){g_rtSight.top++;g_rtSight.bottom++;}} }// 在地圖中做標記 void OnMark(MAPITEM value) {g_aryMap[g_ptPlayer.x][g_ptPlayer.y] = value; }// 檢查是否到出口 bool CheckWin() {if (g_ptPlayer.x == g_szMap.cx && g_ptPlayer.y == g_szMap.cy - 1){HWND hwnd = GetHWnd();if (MessageBox(hwnd, _T("恭喜你走出來了!\n您想再來一局嗎?"), _T("恭喜"), MB_YESNO | MB_ICONQUESTION) == IDYES){InitGame();return false;}elsereturn true;}return false; }// 詢問用戶是否退出游戲 bool Quit() {HWND hwnd = GetHWnd();return (MessageBox(hwnd, _T("您確定要退出游戲嗎?"), _T("詢問"), MB_OKCANCEL | MB_ICONQUESTION) == IDOK); }項目名稱: 打雷風景
#include <stdio.h> #include <graphics.h> #include <time.h> #include <math.h>#define PI 3.1415926 // 定義圓周率 #define BRIGHT 5 // 閃電顏色亮度(可以隨意調(diào)節(jié))// 街道建筑物的結(jié)構(gòu)體 struct Window {int length; // 窗戶的長int width; // 窗戶的寬 }; // 房子類型1 struct House1 {int length; // 房子的長int width; // 房子的寬Window win; // 房子上的窗戶 }; // 房子類型2 struct House2 {int length; // 房子的長int width; // 房子墻壁的高度int top; // 房子頂部尖錐的高 };IMAGE temp; // 用于保存閃電前的街道圖像// 函數(shù)聲明 int CirJudgeZero(int index1, int index2); // 得到一個非零的隨機數(shù),index1、index2表示隨機范圍的指數(shù) void Light(int x); // 繪制閃電 void Building(); // 繪制街道(部分隨機) int DrawHou1(int Sumbor); // 畫房子類型1 int DrawHou2(int Sumbor); // 畫房子類型2 void LightChange(); // 繪制高亮街道// 入口函數(shù) int main() {srand((unsigned)time(NULL)); // 設(shè)置隨機種子initgraph(640, 480);setorigin(0, 480); // 設(shè)置坐標原點在左下角setaspectratio(1, -1); // 設(shè)置坐標y軸反向setbkcolor(RGB(20, 20, 20)); // 重置背景色cleardevice();Building(); // 先繪制街道并保存imageint seedtime,time,Xpoint;while (true) // 鼠標右鍵退出程序{do {seedtime = rand() % 500;} while (seedtime <= 150);time = rand() % 3000 + seedtime;Xpoint = rand() % 590 + 50; // 隨機閃電出現(xiàn)的橫坐標Sleep(time); // 等待一段隨機時間,出現(xiàn)閃電Light(Xpoint); // 畫閃電LightChange(); // 高光圖像算法以及恢復(fù)}closegraph(); }// 得到一個非零的隨機數(shù),index1、index2表示隨機范圍的指數(shù) int CirJudgeZero(int index1,int index2) // 循環(huán)隨機數(shù),得到一個非零的隨機數(shù),index1、index2表示隨機范圍的指數(shù) {int rec = 0; // 用于傳遞的非零參數(shù),初始化為0while (rec == 0){rec = rand() % index1 + index2;}return rec; }// 繪制閃電 void Light(int x) {int RanTimes = CirJudgeZero(3, 0); // 隨機是否出現(xiàn)雙重閃電int EndPoi; // 用于記錄閃電的終點for (int i = 0; i < RanTimes; i++){int Xpoint = x, Ypoint = 480;EndPoi = Ypoint - rand() % 120 - 120; // 隨機閃電的終點while (Ypoint >= EndPoi) // 判斷是否到達終點 && 繪制每個小線段{int Dis = rand() % 20; // 隨機閃電每段的長度int Ang1 = rand() % 120 - 60; // 隨機閃電的角度(x軸)int Ang2 = rand() % 120 - 60; // 隨機閃電的角度(y軸)int NextX = Xpoint + (int)(Dis * sin(Ang1 * PI / 180)); // 計算閃電短點的下一點(x軸)int NextY = Ypoint - (int)(Dis * cos(Ang2 * PI / 180)); // 計算閃電短點的下一點(y軸)setlinecolor(RGB(125, 250, 255)); // 設(shè)置閃電顏色setlinestyle(PS_ENDCAP_ROUND | PS_SOLID, 2); // 設(shè)置閃電樣式(線段斷點為平、實線、寬度)line(Xpoint, Ypoint, NextX, NextY); // 繪制閃電Xpoint = NextX; // 將端點賦值給初始點(x軸)Ypoint = NextY; // 將端點賦值給初始點(y軸)Sleep((rand() % 100) > 66 ? 1 : 0); // 設(shè)置停滯時間}} }// 畫房子類型1 int DrawHou1(int Sumbor) {House1 hou1;hou1.length = CirJudgeZero(40, 60); // 隨機房子的長hou1.width = CirJudgeZero(160, 80); // 隨機房子的寬hou1.win.length = 10; // 窗戶的長hou1.win.width = 8; // 窗戶的寬int point1[8] = { Sumbor,0,Sumbor + hou1.length,0,Sumbor + hou1.length,hou1.width,Sumbor,hou1.width }; // 定義房子的輪廓坐標setfillcolor(RGB(30, 30, 60)); // 設(shè)置房子的填充色solidpolygon((POINT*)point1, 4); // 繪制填充房子(四邊形)// 繪制房子上的窗戶int WinLefDis = CirJudgeZero(10, 18); // 隨機窗戶離左邊的距離int WinTopDis = CirJudgeZero(10, 10); // 隨機窗戶離頂部的距離int WinInvDis = CirJudgeZero(10, 10); // 隨機窗戶與窗戶之間的間隔(上下與左右都是這個值)for (int Ypoint = hou1.width - WinTopDis; Ypoint > hou1.width / 3;) // 控制窗戶的行數(shù){for (int Xpoint = Sumbor + WinLefDis; Xpoint < Sumbor + hou1.length - 19;) // 控制每行畫幾個窗戶{int WinPoint[8] = { Xpoint ,Ypoint ,Xpoint + hou1.win.length ,Ypoint ,Xpoint + hou1.win.length ,Ypoint - hou1.win.width,Xpoint ,Ypoint - hou1.win.width }; // 定義窗戶坐標數(shù)組int ran = CirJudgeZero(3, 0); // 隨機值用于判斷顏色(亮著的和暗著的)if (ran == 1)setfillcolor(RGB(240, 240, 150)); // 設(shè)置窗戶亮著else if (ran == 2)setfillcolor(RGB(30, 44, 40)); // 設(shè)置窗戶暗著solidpolygon((POINT*)WinPoint, 4); // 畫無邊框的填充窗(四邊形)Xpoint += (hou1.win.length + WinInvDis); // 窗左上角的x值每次畫完都增加,方便畫下一個窗戶}Ypoint -= (hou1.win.width + WinInvDis); // 窗左上角的y值每次畫完都增加,方便畫下一個窗戶}Sumbor += hou1.length; // x坐標加上整個房子的長,用于判斷是否畫出屏幕范圍return Sumbor; // 返回x坐標 }// 畫房子類型2 int DrawHou2(int Sumbor) {House2 hou2;hou2.length = CirJudgeZero(20, 80); // 隨機房子的長hou2.width = CirJudgeZero(40, 60); // 隨機房子的寬hou2.top = CirJudgeZero(10, 100); // 隨機房子的頂部的y坐標int point2[10] = { Sumbor,0,Sumbor ,hou2.width ,Sumbor + (hou2.length / 2),hou2.top ,hou2.length + Sumbor,hou2.width,hou2.length + Sumbor,0 }; // 定義房子的坐標setfillcolor(RGB(30, 44, 83)); // 設(shè)置房子的填充色solidpolygon((POINT*)point2, 5); // 畫填充的房子(5邊形)// 畫房子2的裝飾int ran = CirJudgeZero(3, 0); // 定義一個隨機數(shù),用于判斷畫什么樣的窗戶if (ran == 1) // 畫圓形窗戶{int ranlight = CirJudgeZero(3, 0); // 定義一個隨機數(shù),用于判斷窗戶是否亮著if (ranlight == 1) // 暗著的窗戶setfillcolor(RGB(30, 44, 50));else if(ranlight == 2) // 亮著的窗戶setfillcolor(RGB(150, 200, 130));int radius = rand() % 10 + (hou2.width / 6); // 定義半徑solidcircle(Sumbor + (hou2.length / 2), hou2.width * 2 / 3, radius); // 畫填充無邊框圓形窗戶}else if (ran == 2) // 畫拱形窗戶{int ranlight = CirJudgeZero(3, 0); // 定義一個隨機數(shù),用于判斷窗戶是否亮著if (ranlight == 1) // 暗著的窗戶setfillcolor(RGB(30, 44, 50));else if (ranlight == 2) // 亮著的窗戶setfillcolor(RGB(150, 200, 130));int radius = rand() % 10 + (hou2.width / 6); // 定義半徑solidcircle(Sumbor + (hou2.length / 2), hou2.width * 3 / 5, radius); // 拱形是由圓形和四邊形嵌合組成solidrectangle(Sumbor + (hou2.length / 2) - radius, hou2.width * 3 / 5, Sumbor + (hou2.length / 2) + radius, hou2.width * 3 / 5 - radius);}Sumbor += hou2.length; // x坐標加上整個房子的長,用于判斷是否畫出屏幕范圍return Sumbor; }// 畫建筑物 void Building() {int index = 0; // 隨機值,來判斷繪制什么房子// 隨機房子類型for (int Sumbor = 0; Sumbor < 640; ) // 記錄建筑物的右邊界,別讓程序停不下來了{index = CirJudgeZero(3, 0);switch (index){case 1: // 畫房子類型1Sumbor = DrawHou1(Sumbor);break;case 2: // 畫房子類型2Sumbor = DrawHou2(Sumbor);break;}}getimage(&temp, 0, 0, 640, 480); // 記錄此時的圖像,為了閃電后恢復(fù)圖像 }// 閃電出現(xiàn)時的???光 void LightChange() {// 隨機閃電(屏幕)的亮度float Lightness = (float)CirJudgeZero(10,(CirJudgeZero(15, 10) / 10));IMAGE image; // 定義一個圖像對象,用于繪制高亮的圖像getimage(&image, 0, 0, 640, 480); // 獲取圖像范圍DWORD *pMem = GetImageBuffer(&image); // 獲取指向顯存的指針int r, g, b; // 定義分別獲取點的RGB顏色值for (int i = 0; i < (640 * 480); i++) // 循環(huán)獲取每個點的RGB值并判斷是否大于255{r = min((int)(GetRValue(pMem[i])*Lightness), 255); // 這里所有像素點都乘以一個大于1的數(shù)值,點的顏色就變亮了b = min((int)(GetBValue(pMem[i])*Lightness), 255);g = min((int)(GetGValue(pMem[i])*Lightness), 255);pMem[i] = RGB(r, g, b);}putimage(0, 0, &image); // 輸出高光圖像Sleep(100); // 停滯時間cleardevice(); // 清除屏幕圖像(好像putimage輸出圖像在頂部是有一段空隙的,因此直接清屏,就看不到空隙了)putimage(0, 0, &temp); // 重新輸出原來街道的圖像 }項目名稱: 煙花
#include<graphics.h> #include<cmath> #include<conio.h> #define PI 3.1415926 void star(int x,int y); //畫星星 void drawmoon(); //畫月亮 void drawstar(); //畫星空 void starflower1(); //煙花綻放1 void starflower2(); //煙花綻放2int main() { int i; initgraph(640, 480);// 初始化繪圖窗口 line(100, 421, 540, 421);// 畫地平線 drawstar(); //畫星空 while(!kbhit()) { starflower1();//煙花綻放函數(shù)1 starflower2();//煙花綻放函數(shù)2 Sleep(10); } getch(); closegraph(); // 關(guān)閉繪圖窗口 return 0; }void star(int x,int y) //畫星星函數(shù) { int i,a; int n=5; int x1[5],y1[5],x2[5],y2[5]; setcolor(YELLOW); for(i=0;i<5;i++) { x1[i]=(int)(x+n*cos(i*72*PI/180) + 1); y1[i]=(int)(y+n*sin(i*72*PI/180) + 1); x2[i]=(int)(x+n/2*cos(i*72*PI/180+PI/5) + 1); y2[i]=(int)(y+n/2*sin(i*72*PI/180+PI/5) + 1); } for(i=0;i<5;i++) { a=i+1; if(a>4) a=0; line(x1[i],y1[i],x2[i],y2[i]);//兩點間畫直線 line(x2[i],y2[i],x1[a],y1[a]); } }void drawmoon() //畫月亮 { setfillcolor(WHITE); //fillcircle(550,80,40);//畫圓(有邊框) solidcircle(550,80,40);//畫圓(無邊框) }void drawstar() //畫星空 { int a[]={40,250,140,140,90,350,300}; int b[]={40,25,99,100,98,60,78},i; //setfillstyle(1,14); for(i=0;i<10;i++) { star(a[i],b[i]); floodfill(a[i],b[i],YELLOW); } drawmoon(); }void starflower1() //煙花綻放函數(shù)1 {double h,v,dv; // 高度, 速度(方向向下),加速度(每 1/50 秒) { h=470,v= 54,dv= 9.8 / 10; while(h>=200) { h-=(v - dv / 2); v = v * 0.9; setcolor(GREEN); setfillcolor(RED);//填充顏色為藍色 fillcircle(300,int(h), 5);Sleep(20);// 延時 // 擦掉球?setcolor(BLACK);setfillcolor(BLACK);//填充顏色為黑色fillcircle(300,int(h), 5); Sleep(10); } } }void starflower2() //煙花綻放函數(shù)2 { int i=0,j,n=60, x=300,y=200, px,py; while(1) { if(i<100) { for(j=0;j<n;j++) { px=(int)(x+i*cos(j*360/n*PI/180) + 1); py=(int)(y+i*sin(j*360/n*PI/180) + 1); putpixel(px-1,py,BLUE); putpixel(px,py+1,BLUE); putpixel(px+1,py-1,YELLOW); putpixel(px,py-1,YELLOW); putpixel(px+1,py,RED); putpixel(px+1,py+1,RED); } //畫圓擦掉 Sleep(10); setfillcolor(BLACK); solidcircle(300,200,101); } i+=2; if(i>=100) break; } }總結(jié)
以上是生活随笔為你收集整理的Easyx项目小合集的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: EasyX库的安装
- 下一篇: 项目: 推箱子游戏【c/c++】