#include <stdio.h> #include <graphics.h> #include <conio.h> #include <time.h> #include <string.h> #define window_length 500 #define window_high 500 #define map_length (window_length/100) #define map_high (window_high/100) #define broad_length 200 #define broad_high 20 #define ball_x (window_length/2) #define ball_y (window_high/2) #define ball_r 10 #define ball_top (ball.y-ball.r) #define ball_bot (ball.y+ball.r) #define ball_left (ball.x-ball.r) #define ball_right (ball.x+ball.r) #define ball_dx (window_length/100) #define ball_dy (window_length/100) #define brick_length (window_length/(map_length)) #define brick_high 20 enum direction{ LEFT = VK_LEFT, RIGHT = VK_RIGHT}; int map[map_high][map_length] = { 0 }; struct{ int x; int y; int r; int dx; int dy; }ball = { ball_x,ball_y,ball_r,ball_dx,ball_dy}; //球的初始化 struct{ int x; int y; }broad = { window_length / 2 - broad_length / 2, window_high - broad_high}; //板子的初始化 void map_Checkpoint(){ for (int y = 0; y < map_high; y++){ for (int x = 0; x < map_length; x++){ map[y][x] = 1; } } } int hit_bricks_top(){ if (ball_top <= map_high*brick_high && ball_top%brick_high == 0 && map[ball_top/brick_high - 1][ball.x/brick_length] == 1) { map[ball_top / brick_high - 1][ball.x / brick_length] = 0; return 1; } return 0; } int hit_bricks_left(){ if (ball_top <= map_high*brick_high && ball_left%brick_length == 0 && map[ball.y / brick_high + 1][ball.x / brick_length - 1] == 1 ) { map[ball.y / brick_high + 1][ball.x / brick_length - 1] = 0; return 1; } return 0; } int hit_bricks_right(){ if (ball_top <= map_high*brick_high && ball_right%brick_length == 0 && map[ball.y / brick_high + 1][ball.x / brick_length + 1] == 1) { map[ball.y / brick_high + 1][ball.x / brick_length + 1] = 0; return 1; } return 0; } void move_ball(){ setfillcolor(RGB(64, 208, 88)); fillcircle(ball.x,ball.y,ball.r); //小球碰撞上部 if (ball_top == 0 || hit_bricks_top()) { ball.dy = -ball.dy; } //小球碰撞下部 else if (ball_bot == window_high-broad_high && ball.x >= broad.x &&ball.x <= broad.x+broad_length) { ball.dy = -ball.dy; } //小球碰撞左部 else if (ball_left <= 0 || hit_bricks_left()){ ball.dx = -ball.dx; } //小球碰撞右部 else if (ball_right >= window_length || hit_bricks_right()){ ball.dx = -ball.dx; } ball.x += ball.dx; ball.y += ball.dy; } void move_broad(){ setfillcolor(RGB(23,18,237)); fillrectangle(broad.x, broad.y, broad.x+broad_length, broad.y+broad_high); if (GetAsyncKeyState('A') || GetAsyncKeyState(LEFT)){ if (broad.x > 0){ broad.x -= 5; } else{ broad.x = 0; } } if (GetAsyncKeyState('D') || GetAsyncKeyState(RIGHT)){ if (broad.x < window_length - broad_length){ broad.x += 5; } else{ broad.x = window_length - broad_length; } } } void map_display(){ for (int y = 0; y < map_high; y++){ for (int x = 0; x < map_length; x++){ switch (map[y][x]){ case 0: setfillcolor(BLACK); solidrectangle(x*brick_length, y*brick_high, x*brick_length + brick_length, y*brick_high + brick_high); break; case 1: setfillcolor(RGB(170, 100, 110)); fillrectangle(x*brick_length, y*brick_high, x*brick_length + brick_length, y*brick_high + brick_high); break; } } } } int win_judge(){ if (ball_top > window_high){ outtextxy(window_length / 2 - 30, window_high / 2, "Game over!"); return 1; } int ret = 1; for (int y = 0; y < map_high; y++){ for (int x = 0; x < map_length; x++){ if (map[y][x] == 1){ ret = 0; break; } } } if (ret == 1) { outtextxy(window_length / 2 - 30, window_high / 2, "666"); return 2; } return 0; } void display(){ setbkcolor(BLACK); cleardevice(); map_Checkpoint(); int ret = 1; while (ret){ BeginBatchDraw(); //开始 双缓冲 cleardevice(); map_display(); move_broad(); move_ball(); win_judge(); Sleep(20); EndBatchDraw(); //结束 双缓冲 } } int main(){ srand((unsigned int)time(NULL)); initgraph(window_length, window_high); display(); closegraph(); return 0; }
----------------------------------------------------------------------------------------------------------------------