购买
下载掌阅APP,畅读海量书库
立即打开
畅读海量书库
扫码下载掌阅APP

2.4 游戏程序框架改进

为了便于开发游戏,本书设计了一个简单的程序框架,如2-4-1.cpp所示。

2-4-1.cpp

 1    #include <graphics.h>    
 2    #include <conio.h>  
 3    #include <stdio.h>  
 4      
 5    // 定义全局变量  
 6      
 7    void startup()  //  初始化函数  
 8    {  
 9    }  
10      
11    void show()  // 绘制函数  
12    {  
13    }  
14      
15    void updateWithoutInput() // 和输入无关的更新  
16    {  
17    }  
18      
19    void updateWithInput()  // 和输入有关的更新  
20    {  
21    }  
22      
23    int main() //  主函数  
24    {  
25        startup();  // 初始化函数,仅执行一次    
26        while (1)   // 一直循环  
27        {  
28            show();  // 进行绘制  
29            updateWithoutInput(); // 和输入无关的更新   
30            updateWithInput();    // 和输入有关的更新  
31        }  
32        return 0;  
33    }  

首先在程序开头定义一些全局变量作为游戏数据变量,它们在整个程序中均可以访问。具体的游戏功能在startup()、show()、updateWithoutInput()、updateWithInput()四个函数中实现。

程序从主函数开始,首先运行一次startup(),进行游戏的初始化。然后开始循环执行三个函数:show()进行绘制,updateWithoutInput()执行和输入无关的更新,updateWithInput()执行和输入有关的更新。

利用游戏开发框架,2-3-2.cpp可以修改为2-4-2.cpp。

2-4-2.cpp

 1    #include <graphics.h>    
 2    #include <conio.h>  
 3    #include <stdio.h>  
 4    #define WIDTH 800  // 游戏画面宽度  
 5    #define HEIGHT 600 // 游戏画面高度  
 6    #define G  0.3  // 重力加速度  
 7      
 8    struct Bird // 小鸟结构体  
 9    {  
10        float x, y, vy, radius; // 小球的圆心坐标、y方向速度、半径大小  
11    };  
12      
13    // 定义全局变量  
14    Bird bird; // 定义小鸟变量  
15      
16    void startup()  //  初始化函数  
17    {  
18        bird.radius = 20; // 小球半径     
19        bird.x = WIDTH / 6; // 小球圆心的x坐标  
20        bird.y = HEIGHT / 3;  // 小球圆心的y坐标  
21        bird.vy = 0;  // 小球的y方向初始速度为0  
22      
23        initgraph(WIDTH, HEIGHT); // 新建一个画布  
24        BeginBatchDraw(); // 开始批量绘制  
25    }  
26      
27    void show()  // 绘制函数  
28    {  
29        cleardevice();  // 清空画面  
30        fillcircle(bird.x, bird.y, bird.radius);  // 绘制小球  
31        FlushBatchDraw(); // 批量绘制  
32        Sleep(10);  // 暂停10毫秒  
33    }  
34      
35    void updateWithoutInput() // 和输入无关的更新  
36    {  
37        bird.vy = bird.vy + G;  // 根据重力加速度更新小球的y方向速度  
38        bird.y = bird.y + bird.vy;    // 根据小球的y方向速度更新其y坐标  
39      
40        // 如果小球碰到上下边界  
41        if (bird.y >= HEIGHT - bird.radius || bird.y <= bird.radius)  
42        {  
43            bird.y = HEIGHT / 3;  // 小球圆心的y坐标  
44            bird.vy = 0;  // 小球的y方向初始速度为0  
45        }  
46    }  
47      
48    void updateWithInput()  // 和输入有关的更新  
49    {  
50        if (_kbhit()) // 当按键时  
51        {  
52            char input = _getch(); // 获得输入字符  
53            if (input == ' ') // 当按下空格键时  
54                bird.vy = -10; // 给小球一个向上的速度  
55        }  
56    }  
57      
58    int main() //  主函数  
59    {  
60        startup();  // 初始化函数,仅执行一次    
61        while (1)   // 一直循环  
62        {  
63            show();  // 进行绘制  
64            updateWithoutInput(); // 和输入无关的更新   
65            updateWithInput();    // 和输入有关的更新  
66        }  
67        return 0;  
68    }  

当绘制元素较多时,画面中会出现明显的闪烁,这时可以使用批量绘图函数。

在2-4-2.cpp中,第24行的BeginBatchDraw()用于开始批量绘图,执行后任何绘图操作都将暂时不输出到屏幕上;第31行的FlushBatchDraw()用于对未完成的绘制任务执行批量绘制,并将之前的绘图输出。 Q9AiUIdO6ToMPuJmdaMSn4mn+E1pOFoK3b0gJ3WnDPB1vQsv6q3YQ3zVBDjuREQO

点击中间区域
呼出菜单
上一章
目录
下一章
×