導入兩個頭文件
#include <graphics.h> // 引入 EasyX 的圖形庫頭文件
#include <conio.h> // 引入 conio.h 以使用 getch()
窗口創建函數:小黑屏
initgraph(640, 480, SHOWCONSOLE);
closegraph(); //關閉一個窗口
設置背景顏色:這里設置完顏色之后,還需要清除一下設備,不然還是黑色的
setbkcolor(WHITE); //設置背景顏色
cleardevice(); //清屏,把黑色清楚掉......每畫一個東西,都需要清除一次
繪圖函數:(這里以畫圓為例),前綴是需要繪圖的類型,后綴是種類
circle()無填充
fillcircle()有邊框填充
solidcircle()無邊框填充
下面是使用方式:
circle(50, 50, 50);
fillcircle(50, 150, 50);
solidcircle(50, 250, 50);
設置類:這個是窗口下的形狀
setlinestyle(PS_SOLID, 5); //設置線條樣式 setlinestyle(樣式,寬度)
setfillcolor(YELLOW); //設置填充顏色 setfillcolor(顏色)
setlinecolor(BLUE); //設置線條邊框顏色 setlinecolor
設置文字的函數:
//設置文字顏色
settextcolor(RED);
//設置文字樣式
settextstyle(20, 0, "楷體");
//設置背景模式
//變成透明TRANSPARENT
setbkmode(TRANSPARENT);
輸出文字:
outtextxy(50, 50, "azlml");
文字居中的方法:
//把文字居中的操作,畫圖理解
fillrectangle(200, 50, 500, 100); //畫一個矩形
settextcolor(RGB(0, 0, 14));
char arr[] = "居中顯示";
int width = 300 / 2 - textwidth(arr) / 2;
int hight = 50 / 2 - textheight(arr) / 2;
outtextxy(width + 200, hight + 50, arr);
有關圖片的函數:
IMAGE img; //定義一個對象
//加載圖片
// 用的是相對路徑 ./表示當前文件夾下 ../當前文件夾的上一級目錄
//絕對路徑 C:\Users\傅\source\repos\項目\項目\小鳥游六花.jpg
//jpg支持背景透明,png不支持背景透明
loadimage(&img, "./小鳥游六花.jpg", 250, 250); //可以調整圖片的大小,不是在輸出的時候改
//輸出圖片
putimage(0, 0, &img);
有關鍵盤的函數:
kbhit() //判斷是有鍵盤有被按下
char key = _getch(); //讀鍵盤的消息
鍵盤的操作:
if (kbhit()) //判斷是否有鍵盤按下
{//鍵盤消息char key = _getch();printf("%d%c\n", key, key);switch (key){//上鍵case 'w':case 'W':yy--;printf("上鍵\n");break;case 's':case 'S':yy++;printf("下鍵\n");break;case 'a':case 'A':xx--;printf("左鍵\n");break;case 'd':case 'D':xx++;printf("右鍵\n");break;}
}
鼠標操作:
//鼠標消息
if (MouseHit())
{MOUSEMSG msg = GetMouseMsg();// 消息分發switch (msg.uMsg){case WM_LBUTTONDOWN:outtextxy(400, 400, "鼠標左鍵按下");if (msg.x > 200 && msg.x < 500 && msg.y>50 && msg.y < 200) //在框框內{printf("在框框內\n");//輸出鼠標點擊的坐標printf("坐標(%d,%d)\n", msg.x, msg.y);}break;case WM_RBUTTONDOWN:outtextxy(400, 400, "鼠標右鍵按下");if (msg.x > 200 && msg.x < 500 && msg.y>50 && msg.y < 200) //在框框內{printf("在框框內\n");//輸出鼠標點擊的坐標printf("坐標(%d,%d)\n", msg.x, msg.y);}break;}
}/*case WM_MOUSEMOVE: ……break;//鼠標移動消息case WM_MOUSEWHEEL: ……break;// 鼠標滾輪撥動消息case WM_LBUTTONDOWN: ……break;// 左鍵按下消息case WM_MBUTTONDOWN: ……break;// 中鍵按下消息case WM_RBUTTONDOWN: ……break;// 右鍵按下消息
*/
2048項目
#include <graphics.h> // 引入 EasyX 的圖形庫頭文件
#include <conio.h> // 引入 conio.h 以使用 getch()
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<math.h>
using namespace std;#define GRID_W 100//格子的寬度 100
#define MAX_SIZE 4//每行列,格子的數量
#define INTERVAL 15//格子的間距
#define WIN_SIZE MAX_SIZE * GRID_W + INTERVAL*5 // 矩形窗口
//定義一個數組,用來存儲數據
int map[MAX_SIZE][MAX_SIZE];
int flag = 0;
enum Color {zero = RGB(205, 193, 180),two1 = RGB(238, 228, 218),two2 = RGB(237, 224, 200),two3 = RGB(242, 177, 121),two4 = RGB(245, 149, 99),two5 = RGB(246, 124, 95),two6 = RGB(246, 94, 59),two7 = RGB(242, 177, 121),two8 = RGB(237, 204, 97),two9 = RGB(255, 0, 128),two10 = RGB(145, 0, 72),two11 = RGB(242, 17, 158),back = RGB(187, 173, 160),
};
Color arr[13] = { zero,two1,two2, two3, two4, two5, two6, two7, two8, two9, two10,two11,back };
//隨機產生一個整數2 or 4 ,產生2的概率大于4
int creatnum()
{//隨機數 rand 需要加上頭文件//直接用rand是不會產生隨機數的,在此之前,需要設置隨機數種子//需要獲取時間,需要加上time.h頭文件srand((unsigned)time(NULL) + clock());printf("%d",rand());if (rand()%6 ==0){return 4;}else{return 2;}
}
//初始化數據,隨機產生兩個數
void gameinit()
{for (int i=0; i<2 ;){//隨機生成兩個下標int r = rand() % 4;int c = rand() % 4; if (!map[r][c]){map[r][c] = creatnum();++i;}}
}
void gamenew()
{for (int i = 0; i < 1;){//隨機生成兩個下標int r = rand() % 4;int c = rand() % 4;if (!map[r][c]){map[r][c] = creatnum();++i;}}
}
//繪制主界面
void gamedraw()
{//設置背景顏色setbkcolor(RGB(186, 173, 160));cleardevice(); //設置顏色的時候需要清除背景for (int i = 0; i < MAX_SIZE; ++i){for (int j = 0; j < MAX_SIZE; ++j){//找到對應格子的左上角坐標int x1 = INTERVAL * (j+1) + GRID_W * j, y1=INTERVAL* (i + 1) + GRID_W * i;int x2 = x1 + GRID_W, y2 = y1 + GRID_W;int index = log2(map[i][j]);//設置格子填充顏色COLORREF tcolor = arr[index] ;setfillcolor(tcolor);//繪制格子solidroundrect(x1, y1, x2, y2,10,10);//把數組里面的數據繪制到圖形窗口上面if (map[i][j] != 0) //去0{//設置文字樣式settextstyle(50, 0, "黑體");//設置文字背景透明setbkmode(TRANSPARENT);settextcolor(RGB(119, 110, 101));//把整形轉化為字符串char str[10] = "";sprintf_s(str, "%d", map[i][j]);//居中int width = textwidth(str);int height = textheight(str);int xx = x1 + (GRID_W - width) / 2;int yy = y1 + (GRID_W - height) / 2;outtextxy(xx, yy, str);}}}
}//移動方格,移動數組里面的數
void moveup()
{for (int i = 0; i < MAX_SIZE; ++i){int temp = 0;for (int j = 1; j < MAX_SIZE; ++j){if (map[j][i]){if (!map[temp][i]) //上方元素為0{map[temp][i] = map[j][i];map[j][i] = 0;}else if (map[temp][i] == map[j][i]) //上方元素與當前元素相同{map[temp][i] *= 2;map[j][i] = 0;temp++ ;}else //有距離{map[temp + 1][i] = map[j][i];if (temp + 1 != j){map[j][i] = 0;}temp++;}flag = 1;}}}
}void movedown()
{for (int i = MAX_SIZE-1; i >=0; --i){int temp = MAX_SIZE - 1;for (int j = MAX_SIZE - 2; j >=0; --j){if (map[j][i]){if (!map[temp][i]){map[temp][i] = map[j][i];map[j][i] = 0;}else if (map[temp][i] == map[j][i]){map[temp][i] *= 2;map[j][i] = 0;temp--;}else{map[temp - 1][i] = map[j][i];if (temp - 1 != j){map[j][i] = 0;}temp--;}flag = 1;}}}
}void moveleft()
{for (int i = 0 ; i < MAX_SIZE ; ++i){int temp = 0;for (int j = 1; j <MAX_SIZE; ++j){if (map[i][j]){if (!map[i][temp]){map[i][temp] = map[i][j];map[i][j] = 0;}else if (map[i][temp] == map[i][j]){map[i][temp] *= 2;map[i][j] = 0;temp++;}else{map[i][temp +1] = map[i][j];if (temp + 1 != j){map[i][j] = 0;}temp++;}flag = 1;}}}
}void moveright()
{for (int i = MAX_SIZE-1; i >=0; --i){int temp = MAX_SIZE-1;for (int j = MAX_SIZE - 2; j >=0; --j){if (map[i][j]){if (!map[i][temp]){map[i][temp] = map[i][j];map[i][j] = 0;}else if (map[i][temp] == map[i][j]){map[i][temp] *= 2;map[i][j] = 0;temp--;}else{map[i][temp -1] = map[i][j];if (temp - 1 != j){map[i][j] = 0;}temp--;}flag = 1;}}}
} //按鍵處理
void keydeal()
{char key = _getch();switch (key){case 'w':case 'W':moveup();break;case 's':case 'S':movedown();break;case 'a':case 'A':moveleft();break;case 'd':case 'D':moveright();break;}
}int main()
{//創建窗口initgraph(WIN_SIZE, WIN_SIZE);gameinit();while (1){gamedraw();keydeal();if (flag){gamenew();flag = 0;}}_getch();closegraph(); //關閉窗口return 0;
}
球球大作戰:
#include <graphics.h> // 引入 EasyX 的圖形庫頭文件
#include <conio.h> // 引入 conio.h 以使用 getch()
#include<time.h>
#include<stdlib.h>
#include<math.h>
#define FOOD_NUM 100
#define MAX_WIDTH 640
#define MAX_HIGHT 480
#define eatting(x1,y1,x2,y2) sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))struct vector2d
{int x;int y;
};struct player
{vector2d pos; //球的位置int r; //球的半徑vector2d v = { 0 };COLORREF color; //角色球的顏色
};struct Food
{vector2d pos; //球的位置int r; //球的半徑COLORREF color; //球的顏色
};
//初始化玩家
void play_init(player *players)
{players->pos.x = rand() % MAX_HIGHT;players->pos.y = rand() % MAX_WIDTH;players->r = rand() % 10+5;players->v = { 0 };players->color = RGB(rand() % 256, rand() % 256, rand() % 256);
}
//繪制玩家
void player_drew(player* players)
{setfillcolor(players->color);//創建一個球solidcircle(players->pos.x, players->pos.y, players->r);
}void food_init(Food* food)
{food->pos.x = rand() % MAX_HIGHT;food->pos.y = rand() % MAX_WIDTH;food->r = rand() % 10 + 1;food->color = RGB(rand() % 256, rand() % 256, rand() % 256);
}void Food_drew(Food* food)
{setfillcolor(food->color);//創建一個球solidcircle(food->pos.x, food->pos.y, food->r);
}void move(player* players,ExMessage* msg)
{//按下消息if (msg->message == WM_KEYDOWN){switch (msg->vkcode){case VK_UP:players->v.y = -1;break;case VK_DOWN:players->v.y = 1;break;case VK_LEFT:players->v.x = -1;break;case VK_RIGHT:players->v.x = 1;break;}}else if (msg->message == WM_KEYUP) //放開消息{switch (msg->vkcode){case VK_UP:players->v.y = 0;break;case VK_DOWN:players->v.y = 0;break;case VK_LEFT:players->v.x = 0;break;case VK_RIGHT:players->v.x = 0;break;}}
}void update(player* players)
{int tx = players->pos.x;int ty = players->pos.y;players->pos.x += players->v.x, players->pos.y += players->v.y;if (players->pos.x<0 || players->pos.x>MAX_WIDTH){players->pos.x = tx;}if (players->pos.y<0 || players->pos.y>MAX_HIGHT){players->pos.y = ty;}
}int main()
{//初始化界面initgraph(640, 480); //建立一個矩形setbkcolor(RGB(214, 214, 214)); //設置背景顏色cleardevice();//設置了隨機數種子srand(time(NULL));player players;play_init(&players);player_drew(&players);//產生可以吃的小球Food food[FOOD_NUM] = { 0 };//初始化每個小球的信息for (int i = 0; i < FOOD_NUM; ++i){food_init(food + i);}while (1){BeginBatchDraw();cleardevice();//繪制食物for (int i = 0; i < FOOD_NUM; ++i){Food_drew(food + i);}player_drew(&players);EndBatchDraw();for (int i = 0; i < FOOD_NUM; ++i){//如果被吃了,那么就更新球的位置,半徑加1if (eatting(players.pos.x, players.pos.y, food[i].pos.x, food[i].pos.y) < players.r){food_init(food+i);players.r++;}}update(&players);//消息處理(按鍵 鼠標)ExMessage msg = { 0 };//獲取消息if (peekmessage(&msg)){move(&players, &msg);}Sleep(1); } _getch();closegraph(); //關閉界面return 0;
}