#include <stdio.h>
#include <easyx.h>
#include <conio.h>
#define WINDOW_W 569
#define WINDOW_H 600
#define BRICK_W 71
#define BRICK_H 20
#define BRICK_R 5
#define BRICK_C 8
#define RADIUS 15
#define BOARD_W 200
#define BOARD_H 20
void drawBrickWall(int x, int y);
void displaysAllBrick(void);
void drawBoard(void);
void drawBall(void);
void displaysGame(void);
void ballMove(void);
void keyDown(void);
void collisionDetection(void);
bool brickWall[BRICK_R][BRICK_C] = { 0 };
bool isContinue = true;
int ball_x, ball_y;
int ballDir_x = 10, ballDir_y = -10;
int board_x, board_y;
int main(int argc, char const *argv[])
{
initgraph(WINDOW_W, WINDOW_H);
board_x = (WINDOW_W - BOARD_W)/ 2;
board_y = WINDOW_H - BOARD_H;
ball_x = WINDOW_W / 2;
ball_y = WINDOW_H - BOARD_H - RADIUS;
while (isContinue)
{
displaysGame();
ballMove();
keyDown();
Sleep(10);
}
while (1);
return 0;
}
void drawBrickWall(int x, int y)
{
if (brickWall[y][x] == 0)
{
setlinecolor(BLACK);
setfillcolor(YELLOW);
fillrectangle(x*BRICK_W, y*BRICK_H, x*BRICK_W + BRICK_W, y*BRICK_H + BRICK_H);
}
return;
}
void displaysAllBrick(void)
{
for (int i = 0; i < BRICK_R; ++i)
{
for (int j = 0; j < BRICK_C; ++j)
{
if (brickWall[i][j] == 0)
drawBrickWall(j, i);
}
}
return;
}
void drawBoard(void)
{
setlinecolor(RED);
setfillcolor(BLUE);
fillrectangle(board_x, board_y, board_x + BOARD_W, board_y + BOARD_H);
return;
}
void drawBall(void)
{
setlinecolor(MAGENTA);
setfillcolor(MAGENTA);
fillcircle(ball_x, ball_y,RADIUS);
return;
}
void displaysGame(void)
{
BeginBatchDraw();
cleardevice();
displaysAllBrick();
drawBall();
drawBoard();
EndBatchDraw();
return;
}
void ballMove(void)
{
ball_x += ballDir_x;
ball_y += ballDir_y;
return;
}
void collisionDetection(void)
{
if ((ballDir_x > 0) && (ball_x >= (WINDOW_W - RADIUS)))
ballDir_x = -3;
if ((ballDir_x < 0) && (ball_x <= RADIUS))
ballDir_x = 3;
if ((ballDir_y < 0) && (ball_y <= RADIUS))
ballDir_y = 3;
if ((ballDir_y > 0) && (ball_y >= (WINDOW_H - RADIUS)))
{
isContinue = false;
LOGFONT font = { 0 };
font.lfHeight = 40;
font.lfWidth = 20;
settextstyle(&font);
outtextxy(WINDOW_W / 2-125, WINDOW_H / 2, L"Game Over!!!");
}
if ((ballDir_y > 0) && (ball_x >= board_x) && (ball_x <= (board_x + BOARD_W)) \
&& (ball_y >= (board_y - RADIUS)))
ballDir_y = -3;
int flag = 0;
for (int i = 0; i < BRICK_R; ++i)
{
for (int j = 0; j < BRICK_C; ++j)
{
int x = j * BRICK_W;
int y = i * BRICK_H;
if (brickWall[i][j] == 0 && (ball_x>=x -RADIUS) &&(ball_x<= x+BRICK_W+RADIUS)&&(ball_y<=y+BRICK_H+RADIUS)\
&&(ball_y>= y+RADIUS))
{
ballDir_y = -1 * ballDir_y;
brickWall[i][j] = 1;
flag = 1;
break;
}
}
if (flag == 1) break;
}
int count = 0;
for (int i = 0; i < BRICK_R; ++i)
{
for (int j = 0; j < BRICK_C; ++j)
{
if (brickWall[i][j] == 0)
++count;
}
}
if (count == BRICK_R * BRICK_C)
{
LOGFONT font = { 0 };
font.lfHeight = 40;
font.lfWidth = 20;
settextstyle(&font);
outtextxy(WINDOW_W / 2 - 125, WINDOW_H / 2, L"Game victory");
}
return;
}
void keyDown(void)
{
collisionDetection();
if (_kbhit() == 1)
{
int ch = _getch();
switch (ch)
{
case 'A':
case 'a':
case 75:
board_x -= 15;
break;
case 'D':
case 'd':
case 77:
board_x += 15;
break;
}
}
if (board_x <= 0)
board_x = 0;
if (board_x >= (WINDOW_W - BOARD_W))
board_x = (WINDOW_W - BOARD_W);
return;
运行结果:
|