Graphics Library Programming Intermediate
One: Basic mapping operations
- Prepare before mapping, such as adding related header file graphics.h, creating a window for mapping, etc.
- Use the IMAGE type to define an identifier to represent a picture.
- Use loadimage(); to load a resource: that is, to pass the location of the picture resource to a variable of the IMAGE type just defined, so that the name and the corresponding resource are bound.(This graphics library only supports picture maps in.bmp and.jpg formats)
- putimage(); paste the picture on the created window.
The first two parameters indicate the coordinate system location where the upper left corner of the picture is pasted into the created window.The third parameter is the address of the picture variable.
loadimage();
The first parameter is the address of the IMAGE variable.
The second parameter is the location of the picture resource, which is the path.
There are two kinds of relative and absolute references to the path of picture resources.
Project properties have been changed to multibytes (see: Visual programming of graphics (1))
Relative reference:
When the picture resource is in the same directory as the.cpp file, we can use its relative path to reference it.For example, we put the picture: 1.jpg and the.cpp file in the same directory.We can use relative references, which only need the name of the picture resource.
For example:
This method of reference.
Absolute reference:
Absolute references can be used more widely when the picture resource is not in the same directory as the.cpp file, such as when the picture resource is in a folder in the same directory as.cpp or under a folder on a computer disk.Then we can't use relative references.At this point we have to use its absolute position.
This is the case:
How to get absolute paths:
The object name here is its absolute location.We can copy and paste it into the code.
Note: Due to the \character escaping problem, we need to change the single backslash \ to \double backslash or to / single slash when using
Additionally, when loading pictures using loadimage(), you can zoom the pictures using the third and fourth parameters.
Example:
#include<graphics.h> #include<stdio.h> #include<stdlib.h> #include<conio.h> int main(void) { initgraph(800, 600); IMAGE m; //Loadimage (&m,'C:\Users\\Fuhelin\Desktop\\\Picture Coordinates\images\.jpg'); absolute reference loadimage(&m, "1.jpg"); putimage(0, 0, &m); _getch(); closegraph(); return 0; }
Effect:
We can use m.getwidth() and m.getheight() to get the length and width of the picture. If we pass the size of the picture we get to initgraph() to create the window, we can avoid the inconsistency between the size of the window we create and the size of the picture.
For example:
2: Basic mouse operation
Basic Framework
- Declare a mouse-type message: MOUSEMSG m;
- Get the mouse message: m=GetMouseMsg();
- Get the basic properties of the mouse message: mouse coordinates: m.x and m.y; mouse type: m.uMsg (marker for message type)
Basic application examples:
Code:
#include<stdio.h> #include<graphics.h> #include<stdlib.h> #include<conio.h> int main(void) { initgraph(800, 600); MOUSEMSG m; while (1) { m = GetMouseMsg(); switch (m.uMsg) { case WM_LBUTTONDOWN: //L:left setfillcolor(RED); solidcircle(m.x, m.y, 50); break; case WM_RBUTTONDOWN: //R:right button:button down:down setfillcolor(WHITE); solidcircle(m.x, m.y, 50); break; case WM_MOUSEMOVE: //W:Windows M:message mouse:mouse move:move circle(m.x, m.y, 10); break; default: break; } } closegraph(); return 0; }
Effect:
Press the left mouse button, draw a red filling circle with a radius of 50 at the current position, right mouse button, and draw a white filling circle with a radius of 50 at the current position.
Menu model for mouse action:
#include<stdio.h> #include<graphics.h> #include<stdlib.h> #include<conio.h> void makeMenu() { setfillcolor(RGB(192,192,192)); fillrectangle(300, 200, 500, 250); fillrectangle(300, 250 + 10, 500, 250 + 10 + 50); settextstyle(35, 0, "Bold"); settextcolor(BLUE); setbkmode(TRANSPARENT); outtextxy(300 + 25, 200 + 10, "Start the game"); outtextxy(300 + 25, 250 + 10 + 10, "End Game"); MOUSEMSG m; while (1)//Monitor Mouse { m = GetMouseMsg(); if (m.x >= 300 && m.x <= 500 && m.y >= 200 && m.y <= 250) { setfillcolor(YELLOW); fillrectangle(300, 200, 500, 250); outtextxy(300 + 25, 200 + 10, "Start the game"); if (m.uMsg == WM_LBUTTONDOWN) { break; } } else if (m.x >= 300 && m.x <= 500 && m.y >= 260 && m.y <= 310) { setfillcolor(YELLOW); fillrectangle(300, 250 + 10, 500, 250 + 10 + 50); outtextxy(300 + 25, 250 + 10 + 10, "End Game"); if (m.uMsg == WM_LBUTTONDOWN) { Sleep(3000);//Close program after 3000 milliseconds of hibernation exit(0); } } else { setfillcolor(RGB(192,192,192)); fillrectangle(300, 200, 500, 250); fillrectangle(300, 250 + 10, 500, 250 + 10 + 50); outtextxy(300 + 25, 200 + 10, "Start the game"); outtextxy(300 + 25, 250 + 10 + 10, "End Game"); } } } int main(void) { initgraph(800, 600); makeMenu(); //Game(); break after clicking to start the game; exit the loop to execute the next statement game(); we put the mouse action menu before the game module closegraph(); return 0; }
3: Music playback
- Playing music requires additional header file mmsystem.h and static library #pragma comment(lib,'winmm.lib')
- Use mciSendString and some instructions to control music play
- For example: mciSendString("open say no cry. mp3 alias music", 0,0,0);
It directs open: open music file alias: alias (alias music file when its name is too long, which is more convenient in later references)
Some other instructions:
Play: play
Stop:stop
Pause: pause
resume:continue
close:off
Example:
#include<stdio.h> #include<stdlib.h> #include<conio.h> #include<graphics.h> #include<mmsystem.h> #pragma comment(lib,"winmm.lib") int main() { mciSendString("open Say or don't cry.mp3 alias music", 0, 0, 0); mciSendString("play music repeat", 0, 0, 0); _getch(); return 0; }
Fourth: Graphical Push Box
2-D Array and Push Box:
#include<stdio.h> #include<stdlib.h> #include<conio.h> #include<graphics.h> //Module design concept: try to avoid cross-functional, achieve high cohesion, bottom coupling. ///* // //1.Wall: ^ // //0.Empty space: two spaces // //3.Destination // //4.Box // //5: People // //3+4: Box to Destination <. //*/ int cas = 0; int map[3][8][8] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 3, 4, 0, 0, 1, 1, 0, 1, 0, 5, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 4, 3, 0, 4, 1, 1, 4, 1, 0, 0, 1, 3, 1, 1, 3, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 5, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 4, 3, 0, 4, 1, 1, 4, 1, 0, 0, 1, 3, 1, 1, 3, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 0, 0, 0, 4, 3, 1, 1, 4, 1, 0, 5, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 4, 3, 0, 4, 1, 1, 0, 4, 0, 0, 1, 3, 1, 1, 0, 3, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; void gotoxy(int x, int y) //Move the cursor to (x,y) position, print a picture in the game, and then pull the cursor to (x,y) for printing. { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(handle, pos); } void drawGraph() { printf("Push box No.%d shut\n", cas+1); for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { switch (map[cas][i][j]) { case 0: printf(" ");//Print open space break; case 1: printf("■");//Print wall break; case 3: printf("☆");//Print Destination break; case 4: printf("★");//Print box break; case 5: case 8://People arrive at their destination printf("※");//Printer break; case 7: printf("●");//Box to destination break; } } printf("\n"); } } void keyDown() { //Location int i; int j; for (i = 0; i < 8; i++) { for (j = 0; j < 8; j++) { if (map[cas][i][j] == 5 || map[cas][i][j] == 8) { break; } } if (map[cas][i][j] == 5 || map[cas][i][j] == 8) { break; } } printf("%d%d", i, j); char userKey = _getch(); switch (userKey) { case 'W': case 'w': case 72: if (map[cas][i - 1][j] == 0 || map[cas][i - 1][j] == 3) { map[cas][i - 1][j] += 5; map[cas][i][j] -= 5; } else if (map[cas][i - 1][j] == 4 || map[cas][i - 1][j] == 7) { if (map[cas][i - 2][j] == 0 || map[cas][i - 2][j] == 3) { map[cas][i][j] -= 5; map[cas][i - 1][j] += 1; map[cas][i - 2][j] += 4; } } break; case 'S': case 's': case 80: if (map[cas][i + 1][j] == 0 || map[cas][i + 1][j] == 3) { map[cas][i + 1][j] += 5; map[cas][i][j] -= 5; } else if (map[cas][i + 1][j] == 4 || map[cas][i + 1][j] == 7) { if (map[cas][i + 2][j] == 0 || map[cas][i + 2][j] == 3) { map[cas][i][j] -= 5; map[cas][i + 1][j] += 1; map[cas][i + 2][j] += 4; } } break; case 'A': case 'a': case 75: if (map[cas][i][j - 1] == 0 || map[cas][i][j - 1] == 3) { map[cas][i][j - 1] += 5; map[cas][i][j] -= 5; } else if (map[cas][i][j - 1] == 4 || map[cas][i][j - 1] == 7) { if (map[cas][i][j - 2] == 0 || map[cas][i][j - 2] == 3) { map[cas][i][j] -= 5; map[cas][i][j - 1] += 1; map[cas][i][j - 2] += 4; } } break; case 'D': case 'd': case 77: if (map[cas][i][j + 1] == 0 || map[cas][i][j + 1] == 3) { map[cas][i][j + 1] += 5; map[cas][i][j] -= 5; } else if (map[cas][i][j + 1] == 4 || map[cas][i][j + 1] == 7) { if (map[cas][i][j + 2] == 0 || map[cas][i][j + 2] == 3) { map[cas][i][j] -= 5; map[cas][i][j + 1] += 1; map[cas][i][j + 2] += 4; } } break; } } int gameOver() { int flag = 1; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (map[cas][i][j] == 4) { flag = 0; } } } return flag; } int main(void) { while (1) { gotoxy(0, 0); drawGraph(); keyDown(); if (gameOver() == 1) { cas++; if (cas == 3) { //drawGraph(); break; } } } system("cls"); printf("Victory\n"); return 0; }
Graphical optimization:
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<conio.h> #include<graphics.h> int cas = 0; int map[3][8][8] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 3, 4, 0, 0, 1, 1, 0, 1, 0, 5, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 4, 3, 0, 4, 1, 1, 4, 1, 0, 0, 1, 3, 1, 1, 3, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 5, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 4, 3, 0, 4, 1, 1, 4, 1, 0, 0, 1, 3, 1, 1, 3, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 0, 0, 0, 4, 3, 1, 1, 4, 1, 0, 5, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 4, 3, 0, 4, 1, 1, 0, 4, 0, 0, 1, 3, 1, 1, 0, 3, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; IMAGE img[6]; int imgIndex[6] = { 0,1,3,4,5,7 }; void loadResource() { for (int i = 0; i < 6; i++) { char fileName[20] = ""; sprintf(fileName, "%d.bmp", imgIndex[i]); loadimage(&img[i], fileName); } } void gotoxy(int x, int y) //Move the cursor to (x,y) position, print a picture in the game, and then pull the cursor to (x,y) for printing. { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(handle, pos); } void drawGraph() { //printf("Push box closing \n on%d", CAS + 1); for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { int x = 60 * j; int y = 60 * i; switch (map[cas][i][j]) { case 0: putimage(x, y, &img[0]); //printf(");//Print empty space break; case 1: putimage(x, y, &img[1]); //printf("?"); print wall break; case 3: putimage(x, y, &img[2]); //printf(""); //Print destination break; case 4: putimage(x, y, &img[3]); //printf("");//Print box break; case 5: case 8://People arrive at their destination //printf("");//printer putimage(x, y, &img[4]); break; case 7: putimage(x, y, &img[5]); //printf("<");//box to destination break; } } //printf("\n"); } } void keyDown() { //Location int i; int j; for (i = 0; i < 8; i++) { for (j = 0; j < 8; j++) { if (map[cas][i][j] == 5 || map[cas][i][j] == 8) { break; } } if (map[cas][i][j] == 5 || map[cas][i][j] == 8) { break; } } //printf("%d%d", i, j); char userKey = _getch(); switch (userKey) { case 'W': case 'w': case 72: if (map[cas][i - 1][j] == 0 || map[cas][i - 1][j] == 3) { map[cas][i - 1][j] += 5; map[cas][i][j] -= 5; } else if (map[cas][i - 1][j] == 4 || map[cas][i - 1][j] == 7) { if (map[cas][i - 2][j] == 0 || map[cas][i - 2][j] == 3) { map[cas][i][j] -= 5; map[cas][i - 1][j] += 1; map[cas][i - 2][j] += 4; } } break; case 'S': case 's': case 80: if (map[cas][i + 1][j] == 0 || map[cas][i + 1][j] == 3) { map[cas][i + 1][j] += 5; map[cas][i][j] -= 5; } else if (map[cas][i + 1][j] == 4 || map[cas][i + 1][j] == 7) { if (map[cas][i + 2][j] == 0 || map[cas][i + 2][j] == 3) { map[cas][i][j] -= 5; map[cas][i + 1][j] += 1; map[cas][i + 2][j] += 4; } } break; case 'A': case 'a': case 75: if (map[cas][i][j - 1] == 0 || map[cas][i][j - 1] == 3) { map[cas][i][j - 1] += 5; map[cas][i][j] -= 5; } else if (map[cas][i][j - 1] == 4 || map[cas][i][j - 1] == 7) { if (map[cas][i][j - 2] == 0 || map[cas][i][j - 2] == 3) { map[cas][i][j] -= 5; map[cas][i][j - 1] += 1; map[cas][i][j - 2] += 4; } } break; case 'D': case 'd': case 77: if (map[cas][i][j + 1] == 0 || map[cas][i][j + 1] == 3) { map[cas][i][j + 1] += 5; map[cas][i][j] -= 5; } else if (map[cas][i][j + 1] == 4 || map[cas][i][j + 1] == 7) { if (map[cas][i][j + 2] == 0 || map[cas][i][j + 2] == 3) { map[cas][i][j] -= 5; map[cas][i][j + 1] += 1; map[cas][i][j + 2] += 4; } } break; } } int gameOver() { int flag = 1; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (map[cas][i][j] == 4) { flag = 0; } } } return flag; } void makeMenu() { initgraph(800, 600); setfillcolor(RGB(192, 192, 192)); fillrectangle(300, 200, 500, 250); fillrectangle(300, 250 + 10, 500, 250 + 10 + 50); settextstyle(35, 0, "Bold"); settextcolor(BLUE); setbkmode(TRANSPARENT); outtextxy(300 + 25, 200 + 10, "Start the game"); outtextxy(300 + 25, 250 + 10 + 10, "End Game"); MOUSEMSG m; while (1)//Monitor Mouse { m = GetMouseMsg(); if (m.x >= 300 && m.x <= 500 && m.y >= 200 && m.y <= 250) { setfillcolor(YELLOW); fillrectangle(300, 200, 500, 250); outtextxy(300 + 25, 200 + 10, "Start the game"); if (m.uMsg == WM_LBUTTONDOWN) { break; } } else if (m.x >= 300 && m.x <= 500 && m.y >= 260 && m.y <= 310) { setfillcolor(YELLOW); fillrectangle(300, 250 + 10, 500, 250 + 10 + 50); outtextxy(300 + 25, 250 + 10 + 10, "End Game"); if (m.uMsg == WM_LBUTTONDOWN) { Sleep(3000);//Close program after 3000 milliseconds of hibernation exit(0); } } else { setfillcolor(RGB(192, 192, 192)); fillrectangle(300, 200, 500, 250); fillrectangle(300, 250 + 10, 500, 250 + 10 + 50); outtextxy(300 + 25, 200 + 10, "Start the game"); outtextxy(300 + 25, 250 + 10 + 10, "End Game"); } } closegraph(); } int main(void) { loadResource(); makeMenu(); initgraph(8 * 60, 8 * 60); while (1) { //gotoxy(0, 0); //initgraph(8 * 60, 8 * 60); drawGraph(); keyDown(); if (gameOver() == 1) { cas++; if (cas == 3) { break; } } //closegraph(); } closegraph(); return 0; }
Here we add the makeMenu() menu that was previously written with the mouse
Effect: