首先在 include 创建 main.h,用来存放头文件和一些全局变量,全局变量的定义是方便所有的模块访问并反映游戏的状态。全局变量定义后,在主函数中进行赋值,所有模块就都能访问了。
//main.h
#ifndef _MAIN_H_
#define _MAIN_H_
#include <vector>
#include <GL/glut.h>
#include <iostream>
#include <cstring>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <deque>
#include <stdlib.h>
using namespace std;
extern bool replay; //检查是否启动游戏
extern bool over; //检查游戏是否结束
extern float squareSize; //一个单元大小
extern float xIncrement; // x坐标
extern float yIncrement; // y坐标
extern int rotation; // 方向
extern float* monster1; //第一个怪物的坐标和方向
extern float* monster2; //第二个怪物的坐标和方向
extern float* monster3; //第三个怪物的坐标和方向
extern float* monster4; //第四个怪物的坐标和方向
extern vector<int> border; //墙坐标
//障碍物坐标 (为了清晰分为三部分)
extern vector<int> obstaclesTop;
extern vector<int> obstaclesMiddle;
extern vector<int> obstaclesBottom;
extern deque<float> food;
extern vector<vector<bool>> bitmap; // 2d图像,可移动区域
extern bool* keyStates; // 按键状态
extern int points; // 得分
#endif
1.4初始化地图:
在主函数中对全局变量进行赋值,然后初始化一个窗口。在这里将使用到 openGL 的初始化接口。具体包括:
void glutInit(int*argc,char**argv);初始化
void glutInitDisplayMode(unsighed int mode);定义显示方式 mode:是一个 GLUT 库里预定义的
可能的布尔组合,使用 mode 去指定颜色模式,数量和缓冲区类型。
void glutInitWindowSize(int width,int height);设置窗口大小 width:窗口宽度 height:窗口
高度
void glutInitWindowPositon(int x,int y);确定窗口位置(默认左上角) x:横坐标 y:纵坐标
Int glutCreateWindow(char* title);设置窗口的标题 title:标题内容
void glutDisplayFunc(void(*func)(void);注册当前窗口的显示回调函数 void (*func)(void):回调
函数名称,在这里我们用的是 display
void glutReshapeFunc(void(*func)(int width,int height));重新设置窗口 void(*func)(int
width,int height):回调函数名称,在这里我们用的是 reshape
void glutIdleFunc(void(*func)(void));调用渲染函数 void(*func)(void):回调函数名称,系统空闲
时
调用,在这里我们用的是 display
void glutKeyboardFunc(void(*func)(unsigned char key,int x,int y));处理按键事件
void glutKeyboardUpFunc(void (*func)(unsigned char key,int x,int y));处理松开按键事件
void glutMainLoop(void);循环执行
将下面的代码写入 Code/Pacman/src/Pacman.cpp 文件中:
//Pacman.cpp
#include "main.h"
#include "control.h"
#include "food.h"
#include "gameresult.h"
#include "gameover.h"
#include "gamestart.h"
#include "init.h"
#include "monster.h"
#include "createpacman.h"
#include "laberynth.h"
using namespace std;
bool replay = false; //检查是否启动游戏
bool over = true; //检查游戏是否结束
float squareSize = 50.0; //一个单元大小
float xIncrement = 0; // x坐标
float yIncrement = 0; // y坐标
int rotation = 0; // 方向
float* monster1 = new float[3] {10.5, 8.5, 1.0}; //第一个怪物的坐标和方向
float* monster2 = new float[3] {13.5, 1.5, 2.0}; //第二个怪物的坐标和方向
float* monster3 = new float[3] {4.5, 6.5, 3.0}; //第三个怪物的坐标和方向
float* monster4 = new float[3] {2.5, 13.5, 4.0}; //第四个怪物的坐标和方向
vector<int> border = { 0, 0, 15, 1, 15, 15, 14, 1, 0, 14, 15, 15, 1, 14, 0, 0 }; //墙坐标
//障碍物坐标 (为了清晰分为三个)
vector<int> obstaclesTop = { 2, 2, 3, 6, 3, 6, 4, 5, 4, 2, 5, 4, 5, 3, 6, 5, 6, 1, 9, 2, 7, 2, 8, 5, 9, 5, 10, 3, 10, 4, 11, 2, 11, 5, 12, 6, 12, 6, 13, 2 };
vector<int> obstaclesMiddle = { 2, 9, 3, 7, 3, 7, 4, 8, 4, 9, 5, 11, 5, 6, 6, 10, 6, 10, 7, 8, 7, 8, 8, 9, 6, 7, 7, 6, 8, 6, 9, 7, 10, 6, 9, 10, 9, 10, 8, 8, 11, 9, 10, 11, 11, 8, 12, 7, 12, 7, 13, 9 };
vector<int> obstaclesBottom = { 2, 10, 3, 13, 3, 13, 4, 12, 5, 12, 6, 13, 6, 13, 7, 11, 8, 11, 9, 13, 9, 13, 10, 12, 11, 12, 12, 13, 12, 13, 13, 10 };
deque<float> food = { 1.5, 1.5, 1.5, 2.5, 1.5, 3.5, 1.5, 4.5, 1.5, 5.5, 1.5, 6.5, 1.5, 7.5, 1.5, 8.5, 1.5, 9.5, 1.5, 10.5, 1.5, 11.5, 1.5, 12.5, 1.5, 13.5, 2.5, 1.5, 2.5, 6.5, 2.5, 9.5, 2.5, 13.5, 3.5, 1.5, 3.5, 2.5, 3.5, 3.5, 3.5, 4.5, 3.5, 6.5, 3.5, 8.5, 3.5, 9.5, 3.5, 10.5, 3.5, 11.5, 3.5, 13.5, 4.5, 1.5, 4.5, 4.5, 4.5, 5.5, 4.5, 6.5, 4.5, 7.5, 4.5, 8.5, 4.5, 11.5, 4.5, 12.5, 4.5, 13.5, 5.5, 1.5, 5.5, 2.5, 5.5, 5.5, 5.5, 10.5, 5.5, 11.5, 5.5, 13.5, 6.5, 2.5, 6.5, 3.5, 6.5, 4.5, 6.5, 5.5, 6.5, 7.5, 6.5, 10.5, 6.5, 13.5, 7.5, 5.5, 7.5, 6.5, 7.5, 7.5, 7.5, 9.5, 7.5, 10.5, 7.5, 11.5, 7.5, 12.5, 7.5, 13.5, 8.5, 2.5, 8.5, 3.5, 8.5, 4.5, 8.5, 5.5, 8.5, 7.5, 8.5, 10.5, 8.5, 13.5, 9.5,