c语言游戏代码打砖块的S-N图,打砖块.cpp · 江西农业大学/计科1602_刘德归_拼图游戏and打砖块 - Gitee.com...

#include

#include

#include`

#include

#include

#define Width 640

#define Height 480

#define HP 160

#define m 3

#define n 16

int s = 0;

SYSTEMTIME start;

IMAGE img;

IMAGE masks_2[10];

IMAGE dot;

int a[m][n] = { 0 };

int ball_x, ball_y;

int radius;

int bar_x;

int bar_left, bar_top, bar_right, bar_bottom;

char c = NULL;

char d = NULL;

int ball_vx, ball_vy;

void score()

{

if (s)

{

putimage(20, 500, &masks_2[s/10]);

putimage(52, 500, &masks_2[s%10]);

}

else

{

putimage(20, 500, &masks_2[0]);

putimage(52, 500, &masks_2[0]);

}

}

void drawrectangle()

{

srand((unsigned)time(NULL));

/*setfillcolor(YELLOW);*/

int b[3] = { RGB(rand()%256,rand() % 256,rand() % 256),RGB(rand() % 256,rand() % 256,rand() % 256),RGB(rand() % 256,rand() % 256,rand() % 256) };

for (int i = 0; i < m; i++)

{

for (int j = 0; j < n; j++)

{

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

{

setfillcolor(b[rand() % 3]);

solidrectangle(j * 40, i * 40, j * 40 + 38, i * 40 + 38);

}

}

}

}

void init()

{

initgraph(Width, Height+HP);

ball_x = Width / 2;//设置小球的x,y坐标//

ball_y = Height / 2;

ball_vx = 1;//设置小球的速度

ball_vy = 1;

radius = 20;//设置小球的半径

loadimage(&img, _T("./images/1.bmp"));

loadimage(&masks_2[0], _T("./images/t0.bmp"));

loadimage(&masks_2[1], _T("./images/t1.bmp"));

loadimage(&masks_2[2], _T("./images/t2.bmp"));

loadimage(&masks_2[3], _T("./images/t3.bmp"));

loadimage(&masks_2[4], _T( "./images/t4.bmp"));

loadimage(&masks_2[5], _T("./images/t5.bmp"));

loadimage(&masks_2[6], _T("./images/t6.bmp"));

loadimage(&masks_2[7], _T("./images/t7.bmp"));

loadimage(&masks_2[8], _T("./images/t8.bmp"));

loadimage(&masks_2[9], _T("./images/t9.bmp"));

loadimage(&dot, _T("./images/dot.bmp"));

bar_left = Width / 2 - 100;//添加挡板

bar_top =450;

bar_right = Width / 2 + 100;

bar_bottom = 480;

bar_x = 0;

}

void Time()

{

int t1, t2;

int i, j;

int a, b;

int sum;

SYSTEMTIME ti;

sum = 0;

GetLocalTime(&ti);// 获取当前时间

sum += ti.wHour - start.wHour;

sum *= 60;

sum += ti.wMinute - start.wMinute;

sum *= 60;

sum += ti.wSecond - start.wSecond;

i = 0;

j = 0;

t1 = sum;

t2 = sum / 60;

while (i != 2)

{

putimage(545,515, &dot);

putimage(545, 530, &dot);

if (i == 0)

{

t1 = sum % 60;

}

a = t1 % 10;

putimage(600- 32 * i, 500, &masks_2[a]);

t1 = t1 / 10;

i++;

}

while (j != 2)

{

b = t2 % 10;

putimage(500 - 32 * j, 500, &masks_2[b]);

t2 = t2 / 10;

j++;

}

}

void play()

{

while (1)

{

Time();

score();

setcolor(BLACK);//清楚上一个圆留下的痕迹

setfillcolor(BLACK);

fillcircle(ball_x, ball_y, radius);

ball_x = ball_x + ball_vx;//小球的移动是根据小球的x轴和速度的变化来确定的

ball_y = ball_y + ball_vy;

if (ball_x <= radius || (ball_x >= Width - radius))//判断小球是否碰壁(左右)

{

ball_vx = -ball_vx;

}

if (ball_y <= radius)//上

{

ball_vy = -ball_vy;

}

if ((ball_x <= bar_right+radius) && (ball_x >= bar_left-radius) && (ball_y == bar_top - radius))//是否碰板

{

ball_vy = -ball_vy;

}

if ((ball_x bar_top - radius))

{

ball_vx = -ball_vx;

}

if ((ball_x bar_top - radius))

{

ball_vx = -ball_vx;

}

if (ball_y == Height - radius)//落地处理

{

ball_vx = 0;

ball_vy = 0;

Sleep(100);

return;

}

for (int i = 0; i < m; i++)//碰砖处理

{

for (int j = 0; j < n; j++)

{

if (a[i][j] == 0 && ball_x <= j * 40 + 38 + 2 + radius && ball_x >= j * 40 - 2 - radius && ball_y <= i * 40 + 38 + radius && ball_y >= i * 40 - 3- radius)//判断小球碰到哪个砖块

{

if (ball_y <= i * 40 + 38 + radius && ball_y >= i * 40 - 3 - radius)

{

ball_vx = -ball_vx;

}

else if (ball_x <= j * 40 + 38 + 2 + radius && ball_x >= j * 40 - 2 - radius)

{

ball_vy = -ball_vy;

}

else

continue;

a[i][j] = 1;

s++;

ball_vy = -1 * ball_vy;

ball_vx = -1 * ball_vx;

setfillcolor(BLACK);

solidrectangle(j * 40, i * 40, j * 40 + 38, i * 40 + 38);

Sleep(3);

FlushBatchDraw();

break;

}

}

}

setfillcolor(CYAN);//配置颜色

fillcircle(ball_x, ball_y, radius);

Sleep(6);//阻塞3毫秒

FlushBatchDraw();

setcolor(BLACK);

setfillcolor(BLACK);

fillrectangle(bar_left, bar_top, bar_right, bar_bottom);

bar_left += bar_x;

bar_right += bar_x;

bar_x = 0;

setfillcolor(RED);//挡板

fillrectangle(bar_left, bar_top, bar_right, bar_bottom);

c = 0;//判断键盘输入的是a代表左d代表右,_kbhit()获取输入值

if (_kbhit())

{

c = _getch();

switch (c)

{

case'a':

if (bar_left >= 10 && bar_left <= 440)

{

bar_x = -10;

}

break;

case'd':

if (bar_right >= 200 && bar_right <= 630)

{

bar_x = 10;

}

break;

}

}

}

}

void stup() {

d='y';

switch (d)

{

case'y':

init();

GetLocalTime(&start);//初始化

drawrectangle(); //画砖块

putimage(0, 481, &img);

putimage(500, 500, &dot);

putimage(500, 500, &dot);

BeginBatchDraw();//允许批量绘制图形

play();//小球运动判断

EndBatchDraw();

_getch();//实现按任意键退出

closegraph();

break;

case'n':

return;

}

}

void main()

{

stup();

}

一键复制

编辑

Web IDE

原始数据

按行查看

历史

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值