C語言實現(xiàn)圖形化打磚塊游戲
更新時間:2022年05月13日 10:47:11 作者:小雪菜本菜
這篇文章主要為大家詳細介紹了C語言實現(xiàn)圖形化打磚塊游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C語言實現(xiàn)圖形化打磚塊游戲的具體代碼,供大家參考,具體內(nèi)容如下
#include<stdio.h>? #include<conio.h> #include<easyx.h> #include<Windows.h> #define WINDOW_Width ?768 #define WINDOW_Height 1073? #define BrickWidth 81 #define BrickHeight 27 #define ROWS 5 ? ? //行 #define COLS 8 ? ? //列 enum bk { null, red, orange, yellow, green, blue }; #define Broad_Width 110 #define Broad_Height 20 int brick[ROWS][COLS] = { 0 }; IMAGE img[6], img_ball[2]; IMAGE img_bk, img_board; ? int Ball_x = 6; //球的x方向向量 ?? int Ball_y = -6;//球的y方向向量 ? #define Radius 5 int Broad_x = (WINDOW_Width - Broad_Width) / 2 +25; //Broad初始化 int Broad_y = WINDOW_Height - Broad_Height-100; ? int circle_x = WINDOW_Width / 2-16; ? ? ? ? ? ? ? //circle初始化 int circle_y = WINDOW_Height - Radius - Broad_Height-122 ; void loadRescource() { ?? ?loadimage(img + 1, "./res/redblock.png",78,25); ?? ?loadimage(img + 2, "./res/orangeblock.png",78,25); ?? ?loadimage(img + 3, "./res/yellowblock.png",78,25); ?? ?loadimage(img + 4, "./res/greenblock.png",78,25); ?? ?loadimage(img + 5, "./res/blueblock.png",78,25); ? ?? ?loadimage(&img_bk, "./res/bk.png"); ? ?? ?loadimage(img_ball + 0, "./res/ball0.png"); ?? ?loadimage(img_ball + 1, "./res/ball.png"); ? ?? ?loadimage(&img_board, "./res/board.png"); ? } void drawBall() { ?? ?putimage(circle_x, circle_y, &img_ball[0]); ?? ?putimage(circle_x, circle_y, &img_ball[1]); ? } ? ? void ballMove() { ?? ?circle_x += Ball_x; ?? ?circle_y += Ball_y; ? } int ?DisappearBrick() { ?? ?int ZK_COL =( circle_x-60) / BrickWidth; ?? ?int ZK_ROW = (circle_y-250) / BrickHeight; ? ?? ?if (ZK_ROW < 5 && ZK_COL < 8 && brick[ZK_ROW][ZK_COL] != -1) ?? ?{ ?? ??? ?brick[ZK_ROW][ZK_COL] = -1; ?? ??? ?return 1; ? ?? ?}return 0; } //碰撞檢測int circle_x, int circle_y void CollisionDetection() { ?? ?//球如果往右邊移動,檢測球是否撞上右邊沿 ?? ?if (Ball_x > 0 && circle_x >= WINDOW_Width - Radius-55) ?? ?{ ?? ??? ?Ball_x = -6; ?? ?} ?? ?//球如果往上邊移動,檢測球是否撞上上邊沿 ?? ?if (Ball_y < 0 && circle_y <= Radius || DisappearBrick()) ?? ?{ ?? ??? ?Ball_y = 6; ?? ?} ?? ?//球如果往左邊移動,檢測球是否撞上左邊沿 ?? ?if (Ball_x < 0 && circle_x <= Radius +35) ?? ?{ ?? ??? ?Ball_x = 6; ?? ?} ?? ?//球如果往下邊移動,檢測球是否撞上下邊沿 ?? ?if (Ball_y > 0 && circle_y >= WINDOW_Height - Radius) ?? ?{ ?? ??? ?Ball_y = 0; ?? ??? ?Ball_x = 0; ? ?? ?} ?? ?//檢測球是否撞上板子,只能從上往下撞,只有兩種極限情況 ?? ?if ((Ball_y > 0) && (circle_y >= (Broad_y - Radius) - 25) &&//球y坐標 ?? ??? ?(circle_x >= Broad_x) && (circle_x <= (Broad_x + Broad_Width))) ?? ?{ ?? ??? ?Ball_y = -6; ?? ?} ? } //鍵盤控制 void KeyControl() { ?? ?CollisionDetection();?? ?//不管有沒有按鍵都要碰撞檢測 ?? ?int ch; ?? ?if (true == _kbhit()) ?? ?{ ? ? ? ? ? ? ? ? ? ? ? //檢測是否有按鍵 ?? ??? ?ch = _getch();//獲取按鍵的值 ?? ??? ?switch (ch) ?? ??? ?{ ?? ??? ?case 'a': ?? ??? ?case 'A': ?? ??? ?case 75: ?? ??? ??? ?if(Broad_x >= Radius + 35+38) ?? ??? ??? ?Broad_x -= 50; ?? ??? ??? ?break; ?? ??? ?case 'd': ?? ??? ?case 'D': ?? ??? ?case 77: ?? ??? ??? ?if (Broad_x <= WINDOW_Width - Radius - 55-149) ?? ??? ??? ?Broad_x += 50; ?? ??? ??? ?break; ?? ??? ?} ?? ?} } void DrawAllBrick() { ?? ?for (int i = 0; i < ROWS; i++) ?? ?{ ?? ??? ?for (int j = 0; j < COLS; j++) ?? ??? ??? ?switch (brick[i][j]) ?? ??? ??? ?{ ?? ??? ??? ?case 1: ?? ??? ??? ??? ?putimage(j * 81 + 60, i * 27 + 250, &img[1]); ?? ??? ??? ??? ?break; ?? ??? ??? ?case 2: ?? ??? ??? ??? ?putimage(j * 81 + 60, i * 27 + 250, &img[2]); ?? ??? ??? ??? ?break; ?? ??? ??? ?case 3: ?? ??? ??? ??? ?putimage(j * 81 + 60, i * 27 + 250, &img[3]); ?? ??? ??? ??? ?break; ?? ??? ??? ?case 4: ?? ??? ??? ??? ?putimage(j * 81 + 60, i * 27 + 250, &img[4]); ?? ??? ??? ??? ?break; ?? ??? ??? ?case 5: ?? ??? ??? ??? ?putimage(j * 81 + 60, i * 27 + 250, &img[5]); ?? ??? ??? ?} ?? ?} ? } void init(int ?brick[][COLS]) { ?? ?int k = 1; ?? ?for (int i = 0; i < ROWS; i++) ?? ?{ ? ?? ??? ?for (int j = 0; j < COLS; j++) ?? ??? ?{ ? ?? ??? ??? ?brick[i][j] = k; ? ?? ??? ?} ?? ??? ?k++; ?? ?} ? } ? int main() { ?? ?initgraph(WINDOW_Width, WINDOW_Height/*,EW_SHOWCONSOLE*/); ?? ?init(brick); ?? ?loadRescource(); ? ?? ?while (true) ?? ?{ ?? ??? ?BeginBatchDraw(); ? ?? ??? ?putimage(0, 0, &img_bk); ? ?? ??? ?putimage(Broad_x, Broad_y, &img_board); ? ?? ??? ?DrawAllBrick(); ? ?? ??? ?drawBall(); ?? ??? ?ballMove(); ?? ??? ?KeyControl(); ?? ??? ?DisappearBrick(); ? ?? ??? ?EndBatchDraw(); ?? ??? ?Sleep(20); ?? ?} ?? ?while (1); ?? ?return 0; }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。