C++ 使用 openGL 实现吃豆人游戏

本文详细介绍了如何使用C++和OpenGL库开发一个吃豆人游戏,涉及的知识点包括C++编程基础、模板库和OpenGL的窗口初始化、绘图等。通过实例展示了地图构建、角色控制、怪物设计和游戏流程,适合学习OpenGL和游戏开发入门者。
一、游戏须知
1.1  实验内容
      本节实验主要通过 C++ 和 openGL 库来实现了一个吃豆人的小游戏。主要的课程内容涉        及到 C++ 相关的序                
       列容器 vector 、deque 和迭代器,以及 openGL 相关的窗口初始化与绘图功能。
1.2 知识点
      C++ 编程基础
      C++ 模板库使用
      openGl 库使用
1.3 实验环境
      xfce 终端
      ubuntu16.04
      openGl 库
    首先在 include 创建 main.h,用来存放头文件和一些全局变量,全局变量的定义是方便所      有的模块访问并反映游戏的状态。全局变量定义后,在主函数中进行赋值,所有模块就都      能访问了。
二、实现的效果图
三、代码实现
首先在 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, 
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值