|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
动这一下之后,后面按键就无效了
我测试了,第一下那个dx,dy是传进去了的,应该是后面的按键就没传进去
- #pragma once
- #include<graphics.h>
- #include<cstring>
- #include<vector>
- //#include<MyPlane.h> 如果这里这么用,会发现 当前.h文件和MyPlane.h文件的彼此头文件相互包含,会报错
- class MyPlane; //告诉编译器,需要这个类,其他功能结构都没变
- using namespace std;
- class GameMain
- {
- public:
- //构造函数 用于初始化参数,游戏界面设置
- GameMain();
- //注意,你需要在 GameMain 类的析构函数中删除 MyPlane
- //类的实例*以避免内存泄漏。在 GameMain 类中添加一个析构函数,并在其中删除 MP :GameMain.h文件
- ~GameMain(); // 添加析构函数
- //游戏界面初始化
- void init();
- void paly(); // 开始游戏
- int* getBgSize();
- int rows;
- int cols;
- private:
- void keyEvent(); //按键控制
- void updateWindow(); //渲染界面
- private:
- bool update;
- // 添加这一行,这样其他地方调用这个类的时候,就不用每次都申明,不同地方申明,还是不同的对象,无法
- //实现同时操作一辆飞机
- MyPlane *MP;
- //背景图像参数
- IMAGE imgBg; //背景图片
- //创建一个动态数组bullets,里面的元素都是一个个Bullet对象
- //std::vector<Bullet> bullets; // 添加子弹容器
- };
复制代码- #include "GameMain.h"
- #include "MyPlane.h"
- #include "Bullet.h"
- #include<stdlib.h>
- #include <time.h>
- #include<graphics.h>
- #include <conio.h>
- #include"iostream"
- #include"string"
- #include "fstream"
- //音效
- #include<mmsystem.h>
- #pragma comment(lib,"winmm.lib")
- #pragma comment(lib,"WINMM.LIB")
- using namespace std;
- GameMain::GameMain()
- {
- rows = 438;
- cols = 675;
- MP = new MyPlane(this); // 在创建 MyPlane 实例时传递 GameMain 类的指针
- // 为 MyPlane 类对象的指针 MP 分配内存空间
- //MP = new MyPlane();
- //将游戏界面的宽度指定成背景图片的显示宽度
- }
- //析构函数主要作用就是释放资源, 避免内存泄漏
- GameMain::~GameMain()
- {
- delete MP; // 删除 MyPlane 实例
- }
- void GameMain::init()
- {
- //播放背景音乐
- mciSendString("play E:/res/bg.mp3 repeat", 0, 0, 0);
- //创建游戏窗口
- *initgraph(rows, cols);
- //加载背景图片
- loadimage(&imgBg, "E:/Leecode_April/plane/image/background.png");
- update = true;
- }
- void GameMain::paly()
- {
- init();
- update = true;
- while (1)
- {
- BeginBatchDraw();
- //接收用户的输入
- keyEvent();
- if (update == true) {
- //更新游戏画面
- updateWindow();
- }
-
- system("pause");
- EndBatchDraw();
- }
- }
- int* GameMain::getBgSize()
- {
- int num[] = { rows ,cols };
- int* ptr = new int[2];
- for (int i = 0; i < 2; i++) {
- ptr[i] = num[i];
- }
- return ptr;
- }
- void GameMain::keyEvent()
- {
- unsigned char ch;
- int dx = 0;
- int dy = 0;
- //判断当前有没有按键输入,如果他没有按键输入,程序正常执行
- if (_kbhit()) {
- ch = _getch();
- //按下字母,返回的就是字母,按下的是方向键
- //上,先后返回: 224 72 下:224 80 左:75 224,75 右:224 77
- if (ch == 224) {
- ch = _getch();
- switch (ch) {
- case 72:
- dy = -MP->pace;
- break;
- case 80:
- dy = MP->pace;
- break;
- case 75:
- dx = -MP->pace;
- break;
- case 77:
- dx = MP->pace;
- break;
- default:
- break;
- }
- }
- }
- if (dx != 0 || dy != 0) //左右移动
- {
- // MyPlane MP;
- MP->Move(dx,dy);
- // MP->Move(-20,- 20);
- update = true;
- }
- }
- void GameMain::updateWindow()
- {
- putimage(0, 0, &imgBg);
- BeginBatchDraw();
- //绘制我方飞机
- //MyPlane MP;
- IMAGE TEMP_my;
- TEMP_my = MP->getImage_Myplane();
- putimage(MP->My_current_posiX, MP->My_current_posiY, &TEMP_my);
- //绘制子弹
- //Bullet BT;
- //IMAGE TEMP_BT;
- //TEMP_BT = BT.getImage_Bullet();
- //putimage(MP->My_current_posiX, MP->My_current_posiY, &TEMP_BT);
- //for (Bullet& bullet : bullets) {
- // bullet.update(); // 更新子弹位置
- // IMAGE TEMP_BT;
- // TEMP_BT = bullet.getImage_Bullet();
- // putimage(bullet.getPosX(), bullet.getPosY(), &TEMP_BT);
- //}
- EndBatchDraw();
- }
复制代码
- #pragma once
- #include<graphics.h>
- #include<cstring>
- #include<vector>
- //#include "GameMain.h"
- using namespace std;
- class GameMain;
- class MyPlane
- {
- public:
- MyPlane(GameMain* gm); // MyPlane 类的构造函数中使用了 GameMain 类的指针 GM 。但是,你没有初始化这个指针
- //static IMAGE getImages(); //私有成员可以在public里面,通过函数来间接访问和修改
- void Move(int dx, int dy);
- static IMAGE getImage_Myplane();
- int My_current_posiX;
- int My_current_posiY;
- int width;
- int height;
- int pace; //每次按键飞机移动的距离
- private:
- static IMAGE imgMyplane;
- GameMain *GM;
- };
复制代码
- #pragma once
- #include<graphics.h>
- #include<cstring>
- #include<vector>
- #include "MyPlane.h"
- #include "GameMain.h"
- using namespace std;
- //静态成员,需要在类外进行声明
- IMAGE MyPlane::imgMyplane; // 这一行为 imgMyplane 分配内存空间
- //`MyPlane` 类在头文件 `MyPlane.h` 中声明,而 `imgMyplane` 的定义位于与类实现相关联的源文件
- //`MyPlane.cpp` 中。这样,您就解决了 `LNK2001` 错误。
- //如果您将所有代码放在同一个文件中,那么只需在类定义之后添加 `IMAGE MyPlane::imgMyplane; ` 这一行即可。
- //IMAGE MyPlane::imgMyplane; // 这一行为 imgMyplane 分配内存空间
- MyPlane::MyPlane(GameMain* gm):GM(gm)
- {
- width = 100;
- height = 125;
- pace = 20;
- loadimage(&imgMyplane, "E:/Leecode_April/plane/image/me1.png");
- //初始化飞机的位置在图像的正下方的中心
-
- My_current_posiX = GM->rows / 2 - width / 2;
- My_current_posiY = GM->cols - height;
- }
- IMAGE MyPlane::getImage_Myplane()
- {
- return imgMyplane;
- }
- void MyPlane::Move(int dx, int dy)
- {
-
- My_current_posiX += dx;
- My_current_posiY += dy;
- //判断是否超出了边界,还要考虑飞机本身的宽度
- if (My_current_posiX > (GM->rows - width / 2) || My_current_posiX < width / 2)
- {
- My_current_posiX -= dx;
- }
- if (My_current_posiX > (GM->cols - height / 2) || My_current_posiX < height / 2)
- {
- My_current_posiY -= dy;
- }
- }
复制代码
在 GameMain::paly() 函数的 while (1) 循环中,删除 BeginBatchDraw(); 和 EndBatchDraw(); 函数调用。这些函数实际上是用于双缓冲技术,用于防止屏幕闪烁,但在这种情况下,它们可能会干扰按键事件的处理。将 GameMain::paly() 函数修改为:
- void GameMain::paly()
- {
- init();
- update = true;
- while (1)
- {
- //接收用户的输入
- keyEvent();
- if (update == true) {
- //更新游戏画面
- updateWindow();
- }
- }
- }
复制代码
|
|