c语言小球消砖块增加一行砖块,数组实现反弹小球3:消砖块.cpp

#include

#include

#include

#include

#define High 15

#define Width 20

int ball_x,ball_y;

int ball_vx,ball_vy;

int position_x,position_y;

int ridus;

int left,right;

int score;

int ball_number;

int canvas[High][Width] = {0};

void gotoxy(int x,int y)

{

HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

COORD pos;

pos.X = x;

pos.Y = y;

SetConsoleCursorPosition(handle,pos);

}

void startup()

{

ridus = 5;

position_x = High-1;

position_y = Width/2;

ball_number=0;

score=0;

left = position_y - ridus;

right = position_y + ridus;

ball_x = position_x-1;

ball_y = position_y;

ball_vx = -1;

ball_vy = 1;

canvas[ball_x][ball_y] = 1;

int k,i;

for(k=left;k<=right;k++)

canvas[position_x][k] = 2;

for(k=0;k

for(i=0;i

canvas[i][k] = 3;

}

void show()

{

gotoxy(0,0);

int i,j;

for(i=0;i

{

for(j=0;j

{

if(canvas[i][j] == 0)

printf(" ");

else if(canvas[i][j] == 1)

printf("0");

else if(canvas[i][j] == 2)

printf("*");

else if(canvas[i][j] == 3)

printf("#");

}

printf("|\n");

}

for(j=0;j

printf("-");

printf("\n");

printf("反弹小球数:%d\n",ball_number);

printf("消掉的砖块数:%d\n",score);

}

void updateWithoutInput()

{

if(ball_x==High-2)

{

if((ball_y>=left)&&(ball_y<=right))

{

ball_number++;

+printf("\a");

}

else

{

printf("游戏失败\n");

system("pause");

exit(0);

}

}

static int speed = 0;

if(speed<7)

speed++;

if(speed == 7)

{

speed = 0;

canvas[ball_x][ball_y] = 0;

ball_x = ball_x + ball_vx;

ball_y = ball_y + ball_vy;

canvas[ball_x][ball_y] = 1;

if((ball_x==0)||(ball_x==High-1))

ball_vx = -ball_vx;

if((ball_x==0)||(ball_y==Width-1))

ball_vy = -ball_vy;

if(canvas[ball_x-1][ball_y] = 3)

{

ball_vx = -ball_vx;

canvas[ball_x-1][ball_y] = 0;

score++;

printf("\a");

}

}

}

void updateWithInput()

{

char input;

if(kbhit())

{

input = getch();

if(input == 'a'&&left>0)

{

canvas[position_x][right] = 0;

position_y--;

left = position_y - ridus;

right = position_y + ridus;

canvas[position_x][left] = 2;

}

if(input == 'd'&&right

{

canvas[position_x][left] = 0;

position_y++;

left = position_y - ridus;

right = position_y + ridus;

canvas[position_x][right] = 2;

}

}

}

int main()

{

startup();

while(1)

{

show();

updateWithoutInput();

updateWithInput();

}

return 0;

}

一键复制

编辑

Web IDE

原始数据

按行查看

历史

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值