#include
#include
#include
#include
#include
#pragma comment(lib, "WINMM.LIB")
#define BRICK_NUM 100
//形狀類型
#define SQUARE 1
#define STAR 2
#define DIAMOND 3
typedef struct Point
{
int x;
int y;
} Point;
//球資料模型
typedef struct Ball
{
//坐标
Point point;
//形狀,普通 火球 雷射
int shape;
//水準方向 r:1 l:-1 : ‘l‘ ‘r‘
int hdir;
//垂直方向 u :1 d:-1
int vdir;
//速度
int speed;
} Ball;
//木闆
typedef struct Board
{
//坐标
Point point;
//長度
int length;
//方向 左(-1) 右(1)
int dir;
//速度
int speed;
} Board;
//建構類型,摸拟磚塊
typedef struct Brick
{
//坐标
Point point;
//形狀 :1:正方形(普通磚塊 10) 2(圓形 20) 3:
int shape;
} Brick;
//磚塊
Brick bricks[BRICK_NUM];
//建構一個球
Ball ball;
//建構一個木闆
Board board;
//計數器,
int count=0;
//總分值
int totalScore=0;
void init()
{
srand(time(0));
//初始化球
ball.point.x=36;
ball.point.y=22;
ball.shape=1;
ball.speed=200;
ball.vdir=1;//向上 u 向下 d 向左 l 向右 r
ball.hdir=-1;
//初始化木闆
board.point.x=32;
board.point.y=23;
board.length=5;
board.speed=300;
board.dir=-1;
//初始化磚塊
initBrick();
}
void setPosition(Point point)
{
//使用者坐标資訊轉換成内部坐标資訊
COORD coord= {point.x,point.y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
void showBall()
{
setPosition(ball.point);
//顔色:r g b
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY) ;
printf("◎");
}
void showBorad()
{
int index=0;
setPosition(board.point);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED | FOREGROUND_INTENSITY) ;
for(index=0; index
{
printf("〓");
}
}
int createRan()
{
return rand()%3+1;// 0 12
}
void initBrick()
{
//确定第一個磚塊位置 x ,y 程式邏輯會引起修改
int x=10;
int y=2;
int i=0;
//儲存最初坐标
int index=0;
//100=25*4
for(index=0; index
{
//确定每一行起始坐标
if(index!=0 && index%25==0) //25 50 75
{
//換行
x=bricks[i*25].point.x;
y=bricks[i*25].point.y+1;
i++;
}
//具體化
bricks[index].point.x=x;
bricks[index].point.y=y;
//形狀
bricks[index].shape=createRan();
x=x+2;
}
}
void showBrick()
{
int index=0;
for(index=0; index
{
setPosition(bricks[index].point);
switch(bricks[index].shape)
{
case SQUARE:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_INTENSITY);
printf("■");
break;
case STAR:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_GREEN | FOREGROUND_INTENSITY);
printf("★");
break;
case DIAMOND:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE | FOREGROUND_INTENSITY);
printf("◆");
break;
}
}
}
int ballToBoard()
{
//木闆的啟始x值
int bxs=board.point.x;
int bxe=board.point.x+board.length*2;
if( (ball.point.x>=bxs && ball.point.x<=bxe) && ball.point.y==board.point.y-1 )
{
return 1;
}
return 0;
}
void selDir(){
//函數:異步
if(GetAsyncKeyState(VK_LEFT)){
board.dir=-1;
}else if(GetAsyncKeyState(VK_RIGHT)){
board.dir=1;
}
}
void moveBoard()
{
int index=0;
setPosition(board.point);
//清除
for(index=0; index
{
printf(" ");
}
//新坐标:
board.point.x+=2*board.dir;
//重新
setPosition(board.point);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED | FOREGROUND_INTENSITY) ;
for(index=0; index
{
printf("〓");
}
}
int ballToBrick(){
int index=0;
for(index=0;index
if(bricks[index].point.x==ball.point.x && bricks[index].point.y==ball.point.y){
return bricks[index].shape;;
}
}
return 0;
}
void moveBall()
{
int left=4;
int right=64;
int top=2;
int bottom=24;
//水準、垂直增量
int stepx=2;
int stepy=1;
int shape=0;
Point p={76,10};
//得到小球原來位置
Point oldPoint=ball.point;
srand(time(0));
//原來位置圖形清除
setPosition(oldPoint);
printf(" ");
//檢查水準方向
if(ball.point.x
{
//改變水準方向
ball.hdir=1;
}
else if(ball.point.x>right)
{
ball.hdir=-1;
}
//檢查垂直方向
if(ball.point.y
{
ball.vdir=-1;
}
else if(ball.point.y>bottom)
{
ball.vdir=1;
}
else if(ballToBoard()==1)
{
ball.vdir=1;
}
//确定小球的新的位置
ball.point.x=oldPoint.x+stepx*ball.hdir;
ball.point.y=oldPoint.y-stepy*ball.vdir;
//判斷是否碰到磚塊 0 1 2 3
shape=ballToBrick();
//計算碰了多少塊磚塊
count+=shape>0?1:0;
setPosition(p);
totalScore+=shape*10;
printf("小球碰了%d磚塊,計分:%d",count,totalScore);
//重新繪制
setPosition(ball.point);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY) ;
printf("◎");
}
void paintWall()
{
int index=0;
Point point;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),11);
//上邊。下邊
for(index=0; index<70; index=index+2)
{
point.x=index;
point.y=0;
setPosition(point);
printf("■");
point.y=26;
setPosition(point);
printf("■");
}
//左邊、右邊akes[0].x=oldX+2;
for(index=1; index<=26; index++)
{
point.x=0;
point.y=index;
setPosition(point);
printf("■");
point.x=68;
setPosition(point);
printf("■");
}
}
void show()
{
paintWall();
showBrick();
showBall();
showBorad();
}
int main()
{
init(); show(); while(1) { moveBall(); selDir(); moveBoard(); //速度 Sleep(ball.speed); } return 0;}