C++和SFML中Pacman冲突代码的帮助 最近,我开始在C++和SFML中制作Pacman克隆。开发游戏时一切都很顺利,直到我需要创建墙碰撞。我不擅长碰撞,所以我以为会有问题

C++和SFML中Pacman冲突代码的帮助 最近,我开始在C++和SFML中制作Pacman克隆。开发游戏时一切都很顺利,直到我需要创建墙碰撞。我不擅长碰撞,所以我以为会有问题,c++,sfml,pacman,C++,Sfml,Pacman,我想做的是检查是否有一堵墙在你上方,除了你或其他什么,然后如果你撞到了墙,就让玩家停下来 问题是,当你撞到墙时,你很早就停下来了。有时你会陷入困境。这并不是要与代码混淆,因为代码使Pacman在你身上有一堵墙的时候不能转身,而是在遇到墙的时候停下来 这是我刚才提到的关于旋转碰撞的代码,它也可以工作,因此无需修复: // All of the movement functions. if (Keyboard::isKeyPressed(Keyboard::Up) && mapDat

我想做的是检查是否有一堵墙在你上方,除了你或其他什么,然后如果你撞到了墙,就让玩家停下来

问题是,当你撞到墙时,你很早就停下来了。有时你会陷入困境。这并不是要与代码混淆,因为代码使Pacman在你身上有一堵墙的时候不能转身,而是在遇到墙的时候停下来

这是我刚才提到的关于旋转碰撞的代码,它也可以工作,因此无需修复:

// 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;
}
source.x thingy是用于动画的,因此这与当前的问题无关。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 = 1, Down = 2, Left = 3, Right = 4 }; // pacman's directions 

// Level data array. For the map.
int mapData[] =
{
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, // 1
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, // 2
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, // 3
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, // 4
1, 3, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 3, 1, // 5
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, // 6
1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, // 7
0, 0, 0, 0, 1, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 3, 1, 0, 0, 0, 0, // 8
1, 1, 1, 1, 1, 3, 1, 3, 1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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 ...

似乎您先更新,然后再检查。

我不确定pacman.update和pacman.sprite.move会做什么。如果pacman.update使用当前pacman位置更新迷宫阵列,则必须在更新之前移动pacman.update的外观如下:1。获取方向并设定速度2。动画3。获取位置和平铺位置。ie xpos/tilesize=xtile;