Fork me on GitHub

c语言 弹弹球小游戏

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
#include<mmsystem.h>
#pragma comment(lib, "WINMM.LIB")

#define BRICK_NUM 100
//形状类型
#define SQUARE 1
#define STAR 2
#define DIAMOND 3

/*
建立对应模型:20/80 程序:核心代码20% 非核心的代码80%
1、砖块:描述砖块
2、球
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<board.length; 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<BRICK_NUM; 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<BRICK_NUM; 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;
}
}
}


/*
检查小球是否碰到了木板
1 碰
0 没有碰到
*/
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<board.length; index++)
{
printf(" ");
}
//新坐标:
board.point.x+=2*board.dir;
//重新
setPosition(board.point);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED | FOREGROUND_INTENSITY) ;
for(index=0; index<board.length; index++)
{
printf("〓");
}
}

/*
检查小球是否碰到砖块
0
1
2
3
*/
int ballToBrick(){
int index=0;
for(index=0;index<BRICK_NUM;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<left)
{
//改变水平方向
ball.hdir=1;
}
else if(ball.point.x>right)
{
ball.hdir=-1;
}

//检查垂直方向
if(ball.point.y<top)
{
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("◎");
}

/*
功能模块:绘制围墙 1B=8bit A:1B 中文:2B
*/
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;
}

posted @ 2018-07-14 21:11  雪人奥特曼  阅读(1300)  评论(2编辑  收藏  举报