吃豆人碰撞代码帮助在c++和SFML
Pacman collision code help in C++ and SFML
所以,最近我开始用c++和SFML制作《吃豆人》的克隆。一切都很顺利,直到我需要创造墙壁碰撞。我不太擅长碰撞,所以我预料会有问题。
我要做的是检查墙壁是否在你的上方,旁边或其他地方,然后在你撞到墙壁时阻止玩家。
问题是当你碰壁时,你停得太早了。有时你会陷入其中。不要将这与让吃豆人在墙壁上方或其他地方无法转弯的代码混淆,而是在撞到墙壁时停止。
这是我刚才提到的关于转向碰撞的代码(它也可以工作,所以不需要修复):
// All of the movement functions.
if (Keyboard::isKeyPressed(Keyboard::Up) && mapData[xtile + (ytile - 1) * 25] != 1 && mapData[xtile + (ytile - 1) * 25] != 2){
direction = Directions{ Up };
setVelocity(0.0, -2.5);
source.x = 73;
}
if (Keyboard::isKeyPressed(Keyboard::Down) && mapData[xtile + (ytile + 1) * 25] != 1 && mapData[xtile + (ytile + 1) * 25] != 2){
direction = Directions{ Down };
setVelocity(0.0, 2.5);
source.x = 110;
}
if (Keyboard::isKeyPressed(Keyboard::Left) && mapData[(xtile + 1) + ytile * 25] != 1 && mapData[(xtile - 1) + ytile * 25] != 2){
direction = Directions{ Left };
setVelocity(-2.5, 0.0);
source.x = 0;
}
if (Keyboard::isKeyPressed(Keyboard::Right) && mapData[(xtile - 1) + ytile * 25] != 1 && mapData[(xtile + 1) + ytile * 25] != 2){
direction = Directions{ Right };
setVelocity(2.5, 0.0);
source.x = 38;
}
。X是用于动画的,所以这与手头的问题无关。setVelocity为玩家设置速度,方向就是玩家的方向。方向由枚举设置。
这是我的代码,如果你想看到所有其他的文件和类(虽然他们可能是无关的问题)或其他一些信息,只要问我的代码或解释,我会张贴它!
废话少说了,我们开始吧!
main.cpp
// Headers for the program.
#include <iostream>
#include <vector>
#include <SFML/Graphics.hpp>
// Game headers.
#include "renderer.h"
#include "pacman.h"
#include "pellet.h"
#include "map.h"
using std::cout;
using namespace sf;
// All of theese functions will be used for doing things in the future.
void render();
void logic();
enum Directions{ Up = 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, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, // 13
1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, // 14
1, 3, 1, 1, 1, 3, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 3, 1, 1, 1, 3, 1, // 15
1, 3, 3, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3, 3, 1, // 16
1, 1, 1, 3, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 3, 1, 3, 1, 1, 1, // 17
1, 3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 3, 1, // 18
1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, // 19
1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, // 20
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // 21
};
// The maze for the game.
Map maze{ mapData };
// Make the renderer.
Renderer renderer(Vector2u(800, 800), "Pac-man");
// Make our yellow friend
Pacman pacman(358, 353);
int main(){
while (renderer.window.isOpen()){ // As long as the window is open, do this loop.
Event event; // A event var for only one thing, closing the window.
while (renderer.window.pollEvent(event)){ // Poll a event...
if (event.type == Event::Closed){ // Check if the event is a close event...
renderer.window.close(); // ... and if it is a close event then close the window.
}
}
logic(); // Doing our logic before the rendering.
render(); // Rendering.
}
}
void render(){
for (int x = 0; x < maze.map.size(); x++){
renderer.Render(maze.map[x]);
}
renderer.Render(pacman.sprite); // Render Pacman.
renderer.window.display(); // Display...
renderer.window.clear(); // ... And then clear the image.
}
void logic(){ // Here we are going to do all of our game logic.
pacman.update(mapData, maze.map);
pacman.sprite.move(pacman.xvel, pacman.yvel); // Move Pacman
// The problem most likely occurs with theese ifs:
if (pacman.direction == Up && !pacman.sprite.getGlobalBounds().intersects(maze.map[pacman.xtile + (pacman.ytile - 1) * 25].getGlobalBounds()) && mapData[
pacman.xtile + (pacman.ytile - 1) * 25] == 1){
pacman.yvel = 0;
}
if (pacman.direction == Down && !pacman.sprite.getGlobalBounds().intersects(maze.map[pacman.xtile + (pacman.ytile + 1) * 25].getGlobalBounds()) && mapData[
pacman.xtile + (pacman.ytile + 1) * 25] == 1){
pacman.yvel = 0;
}
if (pacman.direction == Left && !pacman.sprite.getGlobalBounds().intersects(maze.map[(pacman.xtile - 1) + pacman.ytile * 25].getGlobalBounds()) && mapData[
(pacman.xtile - 1) + pacman.ytile * 25] == 1){
pacman.xvel = 0;
}
if (pacman.direction == Right && !pacman.sprite.getGlobalBounds().intersects(maze.map[(pacman.xtile + 1) + pacman.ytile * 25].getGlobalBounds()) && mapData[
(pacman.xtile + 1) + pacman.ytile * 25] == 1){
pacman.xvel = 0;
}
// pellet collision (fully working)
if (mapData[pacman.xtile + pacman.ytile * 25] == 3){
mapData[pacman.xtile + pacman.ytile * 25] = 0;
maze.map[pacman.xtile + pacman.ytile * 25].setColor(sf::Color(0, 0, 0, 0));
pacman.addPoint();
}
}
好了。如果你对如何解决或绕过这个问题有任何建议,请随时发布给我。记住,如果你需要更多信息,尽管问我。提前感谢!
如何移动这两行:
pacman.update(mapData, maze.map);
pacman.sprite.move(pacman.xvel, pacman.yvel); // Move Pacman
检查完毕后:
if (pacman.direction == Right ...
似乎你先更新然后检查。
相关文章:
- SFML纹理像播放器
- 如何使用AngelScript注册SFML Vector2运算符
- 如何修复sfml c++代码编译错误
- SFML library: http request
- 落砂模拟碰撞检测C++和SFML
- 使用 SFML 和 C++ 将 Pixel 打印到屏幕上
- SFML RenderWindow打开窗口需要很长时间
- 矢量下标超出SFML游戏中的范围
- 获取 SFML 窗口的 HWND 和高可用性?
- 将 Vulkan 与 SFML 结合使用?
- SFML 文本未绘制在窗口上
- SFML 碰撞永远不会在我的系统中注册
- 使用对象数组对 SFML 进行动画处理
- (SFML)按下键时,播放器构造函数未使用正确的动画进行更新
- 如何使用 SFML 在贪吃蛇游戏中定义游戏结束?
- 如何通过多类"Union variable" (sfml) 使用轮询事件
- 1d.exe找不到SFML文件?
- 当我尝试通过 mingw 使用 CMake 和 SFML 库编译项目时出错
- Declaration of IntRect SFML
- SFML:错误:")"标记之前的预期主表达式