[TOC]

1.按空格键控制小球起跳

_kbhit()函数当用户有键盘输入时返回1,否则返回0.

_getch()函数可以储存用户输入的字符在变量中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<graphics.h>//引用图形库头文件
#include <conio.h>//_kbhit()所必要的头文件
#include <stdio.h>

int main()
{
float width = 600;//画面宽度
float height = 600;//画面高度
initgraph(height, width);//新建画布

float ball_x, ball_y, ball_vy;//小球圆心坐标x、y,y方向速度
float g=0.6;//重力
float radius = 20;//小球半径
ball_x = width / 4;
ball_y = height - radius;
ball_vy = 0;

while (1) {
if (_kbhit()) {//当按键时
char input = _getch();//获得输入字符
if(input == ' ') ball_vy = -16;//给小球一个向上的初速度

}

ball_vy =ball_vy + g;//利用加速度更新xy速度
ball_y = ball_y + ball_vy;
if (ball_y >= height - radius) {//如果小球落到地上
ball_vy = 0;//y速度为0
ball_y = height - radius;//重置
}
cleardevice();//清屏函数,在绘制新小球前清除画面上的内容
fillcircle(ball_x, ball_y, radius);
Sleep(10);//表示程序暂停10毫秒

}
closegraph(); //关闭绘图窗口
return 0;

}

2.方块的绘制和移动

一如函数fillcircle(ball_x, ball_y, radius);可以画圆,函数fillrectangle(left,top,right,bottom)可以画矩形。其中前两个是左上角的xy坐标,后两个是右下角的xy坐标。

1
2
3
4
5
6
7
float rect_left_x, rect_top_y, rect_width, rect_height;
rect_height = 100;//方块高度
rect_width = 20;//方块宽度
rect_left_x = width * 3 / 4;//方块左边x坐标
rect_top_y = height - rect_height;//方块顶部y坐标

fillrectangle(rect_left_x, height - rect_height, rect_left_x + rect_width, height);//画方块

由于方块最底部在窗口底部,因此其底部y坐标为窗口高度height。rect_height记录方块的高度,所以方块最顶部的y坐标rect_top_y为height - rect_height。

微信截图_20240708190907

进一步,添加变量rect_vx记录方块在x方向上的速度,并初始化为-3.

在while语句中,让方块从右向左移动。当方块到达窗口最左边时,再让其从最右边出现。

1
2
3
4
5
6
//前略
float ball_vy = 0;
//中略
rect_left_x += rect_vx;
if (rect_left_x <= 0)//如果方块跑到最左边
rect_left_x = width;//在最右边重新出现

微信截图_20240708190945

3.小球和方块的碰撞判断

第一种情况:方块最左边在小球最右边的左侧 或者两者x坐标相同

第二种情况:方块最右边在小球最左边的右侧 或者两者x坐标相同

第三种情况:方块最上边在小球最下边的上侧 或者两者y坐标相同

    //如果小球碰到方块
    if ((rect_left_x <= ball_x + radius)
        && (rect_left_x + rect_width >= ball_x - radius)
        && (height - rect_height <= ball_y + radius)) {
        Sleep(100);//慢动作效果
    }

4.随机高度和速度

rand()可以生成随机数(整数)

int() float()可以实现数据类型的转化

1
2
3
4
5
if (rect_left_x <= 0) {//如果方块跑到最左边
rect_left_x = width;//在最右边重新出现
rect_height = rand() % int(height / 4) + height / 4;//设置方块随机高度
rect_vx = rand() / float(RAND_MAX) * 4 - 7;//设置方块随机速度
}

5.得分的计算和显示

定义整型变量记录游戏的得分,并初始化为0;

当方块跑到画面最左边,得分增加1;

当方块碰到小球时,得分清零。

另外,利用EasyX的文字输出功能,可输出score

1
2
3
4
TCHAR s[20];//定义字符串数组
_stprintf_s(s, _T("%d"), score);//将score转化为字符串
settextstyle(40, 0, _T("宋体"));//设置文字大小、字体
outtextxy(50, 30, s);//输出得分文字

6.总代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<graphics.h>//引用图形库头文件
#include <conio.h>
#include <stdio.h>

int main(){
float width = 600;//画面宽度
float height = 600;//画面高度
initgraph(height, width);//新建画布

float ball_x, ball_y, ball_vy;//小球圆心坐标x、y,y方向速度
float g=0.6;//重力
float radius = 20;//小球半径
ball_x = width / 4;
ball_y = height - radius;
ball_vy = 0;

float rect_left_x, rect_top_y, rect_width, rect_height,rect_vx;
rect_height = 100;//方块高度
rect_width = 20;//方块宽度
rect_left_x = width * 3 / 4;//方块左边x坐标
rect_top_y = height - rect_height;//方块顶部y坐标
rect_vx = -3;

int score = 0;//得分
while (1) {
if (_kbhit()) {//当按键时
char input = _getch();//获得输入字符
if(input == ' ') ball_vy = -16;//给小球一个向上的初速度

}

ball_vy =ball_vy + g;//利用加速度更新xy速度
ball_y = ball_y + ball_vy;
if (ball_y >= height - radius) {//如果小球落到地上
ball_vy = 0;//y速度为0
ball_y = height - radius;//重置
}
rect_left_x += rect_vx;
if (rect_left_x <= 0) {//如果方块跑到最左边
rect_left_x = width;//在最右边重新出现
rect_height = rand() % int(height / 4) + height / 4;//设置方块随机高度
rect_vx = rand() / float(RAND_MAX) * 4 - 7;//设置方块随机速度
score++;//得分加一
}
//如果小球碰到方块
if ((rect_left_x <= ball_x + radius)
&& (rect_left_x + rect_width >= ball_x - radius)
&& (height - rect_height <= ball_y + radius)) {
Sleep(100);//慢动作效果
score = 0;//得分清零
}
cleardevice();//清屏函数,在绘制新小球前清除画面上的内容
fillcircle(ball_x, ball_y, radius);//画圆
fillrectangle(rect_left_x, height - rect_height, rect_left_x + rect_width, height);//画方块
TCHAR s[20];//定义字符串数组
_stprintf_s(s, _T("%d"), score);//将score转化为字符串
settextstyle(40, 0, _T("宋体"));//设置文字大小、字体
outtextxy(50, 30, s);//输出得分文字
Sleep(10);//表示程序暂停10毫秒
}
closegraph(); //关闭绘图窗口
return 0;

}