所以,最近我开始用C ++和SFML制作Pacman克隆。在我需要创造墙碰撞之前,一切都很顺利。我对碰撞并不是那么好,所以我在期待遇到问题。
我要做的是检查墙壁是否在你的上方,除了你或其他什么,然后如果你碰到墙壁就停止播放器。
问题在于,当你撞墙时,你会早早停下来。有时你会陷入其中。这不应该与使得当墙壁在你身上或你的东西上时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
//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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();
}
}
你去吧。如果您对如何解决或解决此问题有任何建议,请随时将其发布给我。请记住,如果您需要更多信息,请问我。提前谢谢!
答案 0 :(得分:0)
如何移动这两行:
pacman.update(mapData, maze.map);
pacman.sprite.move(pacman.xvel, pacman.yvel); // Move Pacman
检查完毕后:
if (pacman.direction == Right ...
似乎你更新1st然后检查。