2015-09-01 76 views
0

因此,最近我開始在C++和SFML中製作Pacman克隆。一切都很順利開發遊戲,直到我需要創建牆壁碰撞。碰撞時我不太好,所以我期待着有問題。Pacman在C++和SFML中的碰撞代碼幫助

我想要做的是檢查牆壁是否高於你,除了你或其他什麼,然後停止玩家,如果你碰撞牆。

問題是,當你撞上一堵牆時,你就會停下來提前。有時候你會陷入其中。這不會與代碼相混淆,因爲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啄是動畫,讓無關面臨着問題。 setVelocity設置玩家的速度,方向就是玩家的方向。方向由enumuration設定。

這裏是我的代碼,如果你想看到所有其他的文件和類(儘管它們可能與問題無關)或其他信息,只要問我代碼或解釋,我會發布它!

但是enough呀,我們走吧!

的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, 1, 1, 2, 2, 2, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, // 9 
3, 3, 3, 3, 3, 3, 3, 3, 1, 0, 0, 0, 0, 0, 0, 0, 1, 3, 3, 3, 3, 3, 3, 3, 3, // 10 
1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, // 11 
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, // 12 
1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 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(); 
} 

} 

你去那裏。如果您對如何解決或解決此問題有任何建議,請隨時將其發佈給我。請記住,如果您需要更多信息,請問我。提前致謝!

回答

0

如何移動這兩條線:

pacman.update(mapData, maze.map); 
pacman.sprite.move(pacman.xvel, pacman.yvel); // Move Pacman 

在這之後的檢查:

if (pacman.direction == Right ... 

看來你更新1日再檢查。

+0

嗯,沒有。它似乎仍然不起作用。 =( –

+0

以及我不確定'pacman.update'和'pacman.sprite.move'是做什麼的。如果'pacman.update'用當前pacman位置更新迷宮數組,那麼你必須在'update'前面加上'move' – StahlRat

+0

這是pacman.update()的樣子: 1.獲取方向和設置速度 2.動畫 3.獲取位置和瓷磚位置,即xpos/tilesize = xtile; –