C语言打砖块游戏
小雪菜本菜 人气:0#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; }
加载全部内容