Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

[SFML 2.2] Problem z kolizją i programem

Ostatnio zmodyfikowano 2015-03-29 17:10
Autor Wiadomość
wojownik266
Temat założony przez niniejszego użytkownika
[SFML 2.2] Problem z kolizją i programem
» 2015-03-29 17:09:46
Czy ktoś mógłby zerknąć na ten kod i powiedzieć co jest źle napisane? Program się kompiluje ale zaraz po uruchomieniu i naciśnięciu klawisza strzałki rozkłada się (wywala) na całej linii.


C/C++
#include <SFML/Graphics.hpp>
#include "tilemap.hpp"
#include "fps.hpp"
#include "kolizja.h"

int main()
{
   
    sf::RenderWindow window( sf::VideoMode( 800, 600 ), "Labirynt" );
    sf::Texture backg;
    sf::Texture kula;
    sf::Texture brick[ 10 ]; // Znikaj¹ce cegie³ki
    backg.loadFromFile( "back.png" );
    sf::Sprite Back( backg );
    kula.loadFromFile( "kula.png" );
    sf::Sprite Kula( kula );
    brick[ 0 ].loadFromFile( "brick.png" );
    brick[ 1 ].loadFromFile( "brick.png" );
    brick[ 2 ].loadFromFile( "brick.png" );
    brick[ 3 ].loadFromFile( "brick.png" );
   
    sf::Sprite Brick_0( brick[ 0 ] );
   
    float brx_0 = 352, bry_0 = 480; // Cegie³ka pierwsza od do³u
   
    int x = 400, y = 540; // Kula bohater
   
    const int level[ 25 ][ 20 ] =
    {
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
        1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
        1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
        1, 0, 0, 0, 0, 0, 0, 1, 0, 0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, 0, 0, 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,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    };
   
    // create the tilemap from the level definition
    TileMap map;
    if( !map.load( "mapa.png", sf::Vector2u( 32, 32 ), * level, 25, 20 ) )
         return - 1;
   
    FPS fp;
    while( window.isOpen() )
    {
        sf::Event event;
        while( window.pollEvent( event ) )
        {
            if( event.type == sf::Event::Closed )
                 window.close();
            else if( sf::Keyboard::isKeyPressed( sf::Keyboard::Escape ) )
            {
                window.close();
            }
           
           
            else if( sf::Keyboard::isKeyPressed( sf::Keyboard::Up ) )
            {
               
                if( level[ x ][ y - 20 ] == 0 || level[ x ][ y - 20 ] == 2 )
                     y = 340;
               
            }
            else if( sf::Keyboard::isKeyPressed( sf::Keyboard::Down ) )
            {
               
                if( level[ x ][ y + 1 ] == 0 || level[ x ][ y + 1 ] == 2 )
                     y = 240;
               
            }
            fp.getFPS();
            fp.update();
           
           
           
           
        }
       
        Back.setPosition( 0, 0 );
        Kula.setPosition( x, y );
        Brick_0.setPosition( brx_0, bry_0 );
        window.clear();
        window.draw( Back );
        window.draw( Kula );
        window.draw( Brick_0 );
        window.draw( map );
        window.display();
    }
   
    return 0;
}

Problem opisany nieco wyżej powodują te oto instrukcje:

C/C++
else if( sf::Keyboard::isKeyPressed( sf::Keyboard::Up ) )
{
   
    if( level[ x ][ y - 20 ] == 0 || level[ x ][ y - 20 ] == 2 )
         y = 340;
   
}
else if( sf::Keyboard::isKeyPressed( sf::Keyboard::Down ) )
{
   
    if( level[ x ][ y + 1 ] == 0 || level[ x ][ y + 1 ] == 2 )
         y = 240;
   
}
P-129450
pekfos
» 2015-03-29 17:10:55
C/C++
if( level[ x ][ y - 20 ] == 0 || level[ x ][ y - 20 ] == 2 )
     y = 340;

C/C++
int x = 400, y = 540; // Kula bohater
const int level[ 25 ][ 20 ] =
Indeksy.
P-129451
« 1 »
  Strona 1 z 1