How to realize jigsaw puzzle in C language
This article introduces the relevant knowledge of "how to realize jigsaw puzzles in C language". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Make a jigsaw puzzle Mini Game
# include / / reference the graphics library header file # include # includetypedef enum {UP, DOWN, LEFT, RIGHT, UNKOWN} DRCT;const int level = 5persDRCT getKey () {char key; DRCT ret; key = _ getch (); switch (key) {case'astats: case'asides: ret = LEFT; break; case's databases: case'shoes: ret = DOWN; break; case'dudes: case'Downs: ret = RIGHT; break; case'wages: case'Warehouse: ret = UP; break; default: ret = UNKOWN Break;} return ret;} void drawImage (int layout [level] [level], IMAGE* pimg) {int w, h; w = h = pimg- > getwidth () / level; int x1, y1, x2, y2; setcolor (RGB (255,255,255); setfillcolor (RGB (0,0,0)); for (int I = 0; I
< level; ++i) { for (int j = 0; j < level; ++j) { x1 = j * w; y1 = i * h; if (layout[i][j] != -1) { x2 = layout[i][j] % level * w; y2 = layout[i][j] / level * h; putimage(x1, y1, w, h, pimg, x2, y2); } else { fillrectangle(x1, y1, x1 + w, y1 + h); } } } for (int i = 1; i < level; ++i) { line(0, i*h, pimg->Getwidth (), iTunh);} for (int I = 1; I)
< level; ++i) { line(i*w, 0, i*w, pimg->Getheight ();}} int isGameOver (int layout [level] [level]) {for (int I = 0; I < level; iTunes +) {for (int j = 0; j < level; jacks +) {if (! (I = = level-1 & & j = = level-1)) {if (layout [I] [j]! = i*level + j) {return 0;}} return 1;} int updateLayout (int layout [level] [level], DRCT d) {int x = 0, t = 0 Int row, col, ilevel; j, a, b; for (I = 0; I < level; iForce +) / / find the black frame coordinates iLI j {for (j = 0; j < level; j +) {if (layout [I] [j] = =-1) {a = I; b = j;} switch (d) {case UP: {row = a + 1; col = b; break;} case DOWN: {row = a-1; col = b; break } case LEFT: {row = a; col = b + 1; break;} case RIGHT: {row = a; col = b-1; break;} default: {row = a, col = b; break;}} / / calculate (row,col) if (rowlevel-1 | | collevel-1) {x = 0;} else {x = 1; t = layout [a] [b] based on the value of d Layout [a] [b] = layout [row] [col]; layout [row] [col] = t;} / / coordinate (row,col) method: exchange and ret=1 / / otherwise: ret=0 return x;} int main () {int layout [level] [level] = {0, 5, 7, 7, 8, 9, 10, 11, 11, 11, 12, 13, 14, 15, 16, 18, 19, 20, 21, 22, 23} initgraph (850,500); / create a drawing window with a size of 640x480 pixel IMAGE img,imgTip Loadimage (& img, _ T (". / image/0.jpg")); / / loadimage (& imgTip, _ T (". / image/0.jpg"), 200200); drawImage (layout, & img); putimage (650,0, & imgTip); while (! isGameOver (layout)) {DRCT d = getKey (); / / modify the data if (updateLayout (layout, d) drawImage (layout, & img) in the layout array according to the value of d } putimage (0,0, & img); outtextxy (100,50, _ T ("awesome"); _ getch (); closegraph (); / / close the drawing window} "how C language implements jigsaw puzzles". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!