接上篇,我们进行基本的游戏控制之后,接下来我们开始将必要的显示信息,以及游戏的分数信息实时更新显示到屏幕上去。

这里介绍outtextxy函数,outtextxy函数的作用是向屏幕指定位置打印文本信息。

如代码:

1
outtextxy(100,200,"Welcome to www.dotcpp.com");

意思就是向屏幕坐标(100,200)处开始显示"Welcome to www.dotcpp.com" 这串字符串

对于固定信息这么写没问题,但对于分数这样需要实时更新的地方,则需要字符串的拼接则需要sprintf函数帮忙,如

1
sprintf(ScoreBuffer,"%s %d","当前分数:",score);

我们将"当前分数:"作为固定字符串,每次更新分数score,然后每次覆盖接到后面即可,利用循环的每次重绘,就可以实现分数的更新。

相对应的,分数的更新时机也需要我们检测,很明显就是挡板接住才算一次成功才应该增加相应的分数。因此需要更改底部触碰时候的代码逻辑


完整代码如下:

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//弹球游戏
//www.dotcpp.com
#include <graphics.h>        // 引用图形库头文件
#include <conio.h>
#include <stdio.h>
 
#define WIDTH 640
#define HIGH 480
#define R 10
#define STEP 10
 
int Board_x1=150,Board_y1=HIGH-5,Board_x2=250,Board_y2=HIGH;
int pause()        //暂停
{
    while(1)
    {
        Sleep(100);
        if(GetAsyncKeyState(67) & 0x8000 )
        {
            break;
        }
    }
    return 0;
}
int main()
{
    int i;
    char ch;
    int score=0;
    char control;
    TCHAR ScoreBuffer[100]={"当前分数:"};
     
    int Vx=3,Vy=3;
    int x=(WIDTH-200)-100,y=HIGH/2;   //表示小球初始圆心坐标
    initgraph(WIDTH, HIGH);    // 创建绘图窗口 640x480 像素
    fillrectangle(WIDTH-200,0,WIDTH-195,HIGH);//绘制右墙
    outtextxy(WIDTH-150, HIGH-350, ScoreBuffer);
    outtextxy(WIDTH-180, HIGH-250, _T("按P暂停   按C继续"));
    outtextxy(WIDTH-180, HIGH-270, _T("按A向左   按D向右"));
     
    BeginBatchDraw();
    while(1)
    {  
        //绘制小球
        setfillcolor(GREEN);   
        fillcircle(x,y,R);
 
        //绘制挡板
        setfillcolor(YELLOW);
        fillrectangle(Board_x1,Board_y1,Board_x2,Board_y2);
        //显示分数
        settextcolor(WHITE);
        sprintf(ScoreBuffer,"%s %d","当前分数:",score);
        outtextxy(WIDTH-150, HIGH-350, ScoreBuffer);
        FlushBatchDraw();
        
        Sleep(5);
        //原位置擦除小球图案
        setcolor(BLACK);
        setfillcolor(BLACK);
        fillcircle(x,y,R);
 
        //原位置擦除挡板
        setfillcolor(BLACK);
        fillrectangle(Board_x1,Board_y1,Board_x2,Board_y2);
     
        if(GetAsyncKeyState(65) & 0x8000) //向左
        {
            if(Board_x1-STEP<=0)
            {
                Board_x1=0;
                Board_x2=100;
            }
            else
            {
                Board_x1=Board_x1-STEP;
                Board_x2=Board_x2-STEP;
            }
        }
        if(GetAsyncKeyState(68) & 0x8000) //向右
        {
            if(Board_x2+STEP>=WIDTH-205)
            {
                Board_x1=WIDTH-305;
                Board_x2=WIDTH-205;
            }
            else
            {
                Board_x1=Board_x1+STEP;
                Board_x2=Board_x2+STEP;
            }
        }
        if(GetAsyncKeyState(80) & 0x8000)//暂停
        {
            pause();
        }
        x=x+Vx;
        y=y+Vy;
        if(x<=R || x>=WIDTH-203-R)
        {
            Vx=-Vx;
        }
        if(y<=R)
        {
            Vy=-Vy;
        }
        //触底
        if(y>=HIGH-R-5)
        {
            if(x>=Board_x1-R && x<=Board_x2+R)  //是否在板子上
            {
                Vy=-Vy;
                score+=10;
                //重新计算并显示分数
                clearrectangle(WIDTH-150, HIGH-350,WIDTH-50,HIGH-400);
                sprintf(ScoreBuffer,"%s %d","当前分数:",score);
                settextcolor(WHITE);
                outtextxy(WIDTH-150, HIGH-350, ScoreBuffer);
            }
            else//游戏结束
            {
 
                settextcolor(WHITE);
                //setbkmode(TRANSPARENT);
                outtextxy(WIDTH-120, HIGH-200, _T("游戏结束!"));
                Vy=-Vy;
            }
        }
    }
    EndBatchDraw();
    getch();                // 按任意键继续
    closegraph();           // 关闭绘图窗口
    return 0;
}

请大家结合全局逻辑理解,期间不少坐标位置,大家需要自己测试并确定具体大小。具体效果如下:

弹球游戏运行效果


下一节我们进行优化~

点赞(60)

C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:

一点编程也不会写的:零基础C语言学练课程

解决困扰你多年的C语言疑难杂症特性的C语言进阶课程

从零到写出一个爬虫的Python编程课程

只会语法写不出代码?手把手带你写100个编程真题的编程百练课程

信息学奥赛或C++选手的 必学C++课程

蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程

手把手讲解近五年真题的蓝桥杯辅导课程

Dotcpp在线编译      (登录可减少运行等待时间)
#include<stdio.h>
int main()
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX