Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - smiaro

Pages: [1]
1
General / Re: Problem with TileMap
« on: January 26, 2014, 02:26:41 pm »
This is my C++ project on university that I have to create :(
Could I e-mail you and show my code ?;>
I just started my adventure with SFML and classes in C++ so this is hard for me :(
I just want to create working game and gain good note :)

2
General / Re: Problem with TileMap
« on: January 26, 2014, 01:57:39 pm »
No, no ;) Everything should be good:) I just want to know how to manipulate textures position in fors :)
Because now I don't have sprite positions :)

3
General / Problem with TileMap
« on: January 26, 2014, 01:07:55 pm »
Hello, could you correct my code?;> Nothing display now...I know there should be positions but I don't know what to do... please help :)
void LevelClass::Load()
{
    LevelClass::ground_texture.loadFromFile( "data/sources/ground.png" );
    LevelClass::wall_texture.loadFromFile( "data/sources/wall.png" );
    LevelClass::ground_sprite.setTexture( ground_texture );
    LevelClass::wall_sprite.setTexture( wall_texture );
    ground_sprite.setPosition( 0, 0 );
    wall_sprite.setPosition( 50, 50 );
   
    std::fstream file( "data/sources/level1.txt", std::ios::in );
    for( int i = 0; i < 14; i++ ) {
        for( int j = 0; j < 14; j++ ) {
            file >> map[ i ][ j ];
        }
    }
    file.close();
   
}

void LevelClass::Draw( void )
{
    for( int i = 0; i < 14; i++ ) {
        for( int j = 0; j < 14; j++ ) {
            if( map[ i ][ j ] == 0 )
                 game.window.draw( LevelClass::wall_sprite );
           
            if( map[ i ][ j ] == 1 )
                 game.window.draw( LevelClass::ground_sprite );
           
        }
    }
}
 

4
General / Re: Problem with collision SFML 2.0
« on: December 30, 2013, 11:26:02 am »
So how to change this player ?;>

5
General / Problem with collision SFML 2.0
« on: December 30, 2013, 10:27:17 am »
Hello, I have problem with collisions, my character ignores conficlting sprites.
I have also problem because he can jumps if he is in air :(
Please correct my code, I have to pass my C++ project on university.

Player.cpp
#include <SFML/Graphics.hpp>
#include "Player.h"
#include "Level.h"


void Player::Draw(sf::RenderWindow & gameWindow)
{
        const float gravity = 0.9;
        int groundHeight = 368;
        sf::Vector2f velocity(sf::Vector2f(0, 0));
        float moveSpeed = 2.0f;
        float jumpSpeed = 10.0f;

        Level level;

        enum Direction { Down,Left, Right, Up }; //{0,1}
        sf::Vector2i source(1, Down);

        gameWindow.setKeyRepeatEnabled(false);

        sf::Texture playerImage;
        playerImage.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/player.png");
        sf::Sprite player(playerImage);
        player.setPosition(0, 50);

        while (gameWindow.isOpen()) {
                sf::Event event;
                while (gameWindow.pollEvent(event)) {
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                        {
                                source.y = Right;
                                if (level.Collision(level.cabinets) || level.Collision(level.microwaves) || level.Collision(level.fridges) ||
                                        level.Collision(level.boxs) || level.Collision(level.chairs) || level.Collision(level.ovens) ||
                                        level.Collision(level.tables))
                                        velocity.x = 0;
                                else
                                        velocity.x = +moveSpeed;
                        }
                        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                        {
                                source.y = Left;
                                if (level.Collision(level.cabinets) || level.Collision(level.microwaves) || level.Collision(level.fridges) ||
                                        level.Collision(level.boxs) || level.Collision(level.chairs) || level.Collision(level.ovens) ||
                                        level.Collision(level.tables))
                                        velocity.x = 0;
                                else
                                velocity.x = -moveSpeed;
                        }
                        else
                        {
                                source.y = Down;
                                velocity.x = 0;
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
                                velocity.y = -jumpSpeed;
                        if (event.key.code == sf::Keyboard::Escape)
                                return;
                        break; 
                }

                player.move(velocity.x, velocity.y);
                if (player.getPosition().y < groundHeight || velocity.y < 0)
                {
                        velocity.y += gravity;
                }
                else
                {
                        player.setPosition(player.getPosition().x, groundHeight - player.getScale().y);
                        velocity.y = 0;
                }

                source.x++;
                if (source.x * 32 >= playerImage.getSize().x)
                        source.x = 0;

                player.setTextureRect(sf::IntRect(source.x * 32, source.y * 32, 32, 32));
                level.Draw(gameWindow);
                gameWindow.draw(player);
                gameWindow.display();
                gameWindow.clear();
        }

}
 

Level.cpp
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "Level.h"
#include "Player.h"

void Level::Load1(sf::RenderWindow& gameWindow) {
        Player player;
        Draw(gameWindow);
        player.Draw(gameWindow);
}

void Level::Draw(sf::RenderWindow& gameWindow) {
        sf::Texture cabinet,bg,microwave,fridge,table,oven,box, chair;
        cabinet.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/cabinet.png");
        bg.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/bg.png");
        microwave.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/microwave.png");
        fridge.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/fridge.png");
        sf::Sprite cabinets(cabinet);
        sf::Sprite bgs(bg);
        sf::Sprite microwaves(microwave);
        sf::Sprite fridges(fridge);
        bgs.setPosition(0, 0);
        cabinets.setPosition(0, 405);
        fridges.setPosition(137, 295);
        microwaves.setPosition(30, 367);
        gameWindow.draw(bgs);
        gameWindow.draw(cabinets);
        gameWindow.draw(microwaves);
        gameWindow.draw(fridges);
}

//DODANE DLA SPROBOWANIA KOLIZJI
bool Level::Collision(sf::Sprite x)
{
        Player p;

        sf::IntRect playerRect(

                p.player.getPosition().x,
                p.player.getPosition().y,
                p.player.getGlobalBounds().width,
                p.player.getGlobalBounds().height
                );

        sf::IntRect spriteRect(

                x.getPosition().x,
                x.getPosition().y,
                x.getGlobalBounds().width,
                x.getGlobalBounds().height
                );

        if (playerRect.intersects(spriteRect))
                return true;
        else
                return false;
}

////
 

Level.h
#pragma once
class Level
{
public:
        void Load1(sf::RenderWindow& gameWindow);
        void Draw(sf::RenderWindow& gameWindow);

        //DODANE DLA SPROBOWANIA KOLIZJI
        sf::Sprite cabinets;
        sf::Sprite microwaves;
        sf::Sprite fridges;

        bool Collision(sf::Sprite x);
        ////
};

 

Player.h
#pragma once
class Player
{
public:
        void Draw(sf::RenderWindow& gameWindow);
       
        //DODANE DLA SPROBOWANIA KOLIZJI
        sf::Sprite player;
        ////
};


 

6
General / Problem with collision SFML 2.0
« on: December 29, 2013, 07:20:32 pm »
Hello, here is my player and level class code. First cpp files, then .h. I try to make simple collision but this doesn't work. Why?;> Help me pls. My character ignores collisions and just go and jump through sprites :( You can ignore CollisonRight, CollisionLeft. I want to use Collision method because I think collision check is good.
HEEELP :)

Player.cpp
#include <SFML/Graphics.hpp>
#include "Player.h"
#include "Level.h"


void Player::Draw(sf::RenderWindow & gameWindow)
{
        const float gravity = 0.9;
        int groundHeight = 368;
        sf::Vector2f velocity(sf::Vector2f(0, 0));
        float moveSpeed = 2.0f;
        float jumpSpeed = 10.0f;

        Level level;

        enum Direction { Down,Left, Right, Up }; //{0,1}
        sf::Vector2i source(1, Down);

        gameWindow.setKeyRepeatEnabled(false);

        sf::Texture playerImage;
        playerImage.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/player.png");
        sf::Sprite player(playerImage);
        player.setPosition(0, 50);

        while (gameWindow.isOpen()) {
                sf::Event event;
                while (gameWindow.pollEvent(event)) {
                        if (level.Collision(level.cabinets) || level.Collision(level.microwaves) || level.Collision(level.boxs) || level.Collision(level.fridges) || level.Collision(level.ovens) || level.Collision(level.chairs) || level.Collision(level.tables)) {
                                velocity.x = 0;
                                velocity.y = 0;
                        }
                        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                        {
                                source.y = Right;
                                velocity.x = +moveSpeed;
                        }
                        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                        {
                                source.y = Left;
                                velocity.x = -moveSpeed;
                        }
                        else
                        {
                                source.y = Down;
                                velocity.x = 0;
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
                                velocity.y = -jumpSpeed;
                        if (event.key.code == sf::Keyboard::Escape)
                                return;
                        break; 
                }

                player.move(velocity.x, velocity.y);
                if (player.getPosition().y < groundHeight || velocity.y < 0)
                {
                        velocity.y += gravity;
                }
                else
                {
                        player.setPosition(player.getPosition().x, groundHeight - player.getScale().y);
                        velocity.y = 0;
                }

                source.x++;
                if (source.x * 32 >= playerImage.getSize().x)
                        source.x = 0;

                player.setTextureRect(sf::IntRect(source.x * 32, source.y * 32, 32, 32));
                level.Draw(gameWindow);
                gameWindow.draw(player);
                gameWindow.display();
                gameWindow.clear();
        }

}

 

Level.cpp
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "Level.h"
#include "Player.h"

void Level::Load1(sf::RenderWindow& gameWindow) {
        Player player;
        Counts(gameWindow);
        Draw(gameWindow);
        player.Draw(gameWindow);
}

void Level::Draw(sf::RenderWindow& gameWindow) {
        sf::Texture cabinet,bg,microwave,fridge,table,oven,box, chair;
        cabinet.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/cabinet.png");
        bg.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/bg.png");
        microwave.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/microwave.png");
        fridge.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/fridge.png");
        table.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/table.png");
        oven.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/oven.png");
        box.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/box.png");
        chair.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/chair.png");
        sf::Sprite cabinets(cabinet);
        sf::Sprite bgs(bg);
        sf::Sprite microwaves(microwave);
        sf::Sprite fridges(fridge);
        sf::Sprite tables(table);
        sf::Sprite ovens(oven);
        sf::Sprite boxs(box);
        sf::Sprite chairs(chair);
        bgs.setPosition(0, 0);
        cabinets.setPosition(0, 405);
        fridges.setPosition(137, 295);
        tables.setPosition(262, 395);
        microwaves.setPosition(30, 367);
        ovens.setPosition(489, 355);
        boxs.setPosition(658, 455);
        chairs.setPosition(285, 350);
        gameWindow.draw(bgs);
        gameWindow.draw(cabinets);
        gameWindow.draw(microwaves);
        gameWindow.draw(fridges);
        gameWindow.draw(chairs);
        gameWindow.draw(tables);
        gameWindow.draw(ovens);
        gameWindow.draw(boxs);
}

void Level::Counts(sf::RenderWindow& gameWindow) {

        sf::SoundBuffer buffer,buffer2,buffer3,buffer4;
        buffer.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Sources/three.wav");
        sf::Sound sound,sound2,sound3,sound4;
        sound.setBuffer(buffer);
        sound.setVolume(50);

        sf::Texture three, two, one, start,bg;
        three.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/3.png");
        two.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/2.png");
        one.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/1.png");
        start.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/start.png");
        bg.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/bg.png");

        sf::Sprite threes(three);
        sf::Sprite twos(two);
        sf::Sprite ones(one);
        sf::Sprite starts(start);
        sf::Sprite bgs(bg);

        bgs.setPosition(0, 0);
        threes.setPosition(350, 240);
        twos.setPosition(350, 240);
        ones.setPosition(350, 240);
        starts.setPosition(340, 240);

        threes.setScale(0.4, 0.4);
        twos.setScale(0.4, 0.4);
        ones.setScale(0.4, 0.4);
        starts.setScale(0.4, 0.4);

        gameWindow.clear();
        gameWindow.draw(bgs);
        gameWindow.draw(threes);
        gameWindow.display();
        sound.play();
        sf::sleep(sf::seconds(1));

        gameWindow.clear();
        gameWindow.draw(bgs);
        gameWindow.draw(twos);
        gameWindow.display();
        buffer2.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Sources/two.wav");
        sound2.setBuffer(buffer2);
        sound2.setVolume(50);
        sound2.play();
        sf::sleep(sf::seconds(1));

        gameWindow.clear();
        gameWindow.draw(bgs);
        gameWindow.draw(ones);
        gameWindow.display();
        buffer3.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Sources/one.wav");
        sound3.setBuffer(buffer3);
        sound3.setVolume(50);
        sound3.play();
        sf::sleep(sf::seconds(1));

        gameWindow.clear();
        gameWindow.draw(bgs);
        gameWindow.draw(starts);
        gameWindow.display();
        buffer4.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Sources/go.wav");
        sound4.setBuffer(buffer4);
        sound4.setVolume(50);
        sound4.play();
        sf::sleep(sf::seconds(1));

        gameWindow.clear();
        //gameWindow.draw(ones);
        //gameWindow.draw(starts);
}

//DODANE DLA SPROBOWANIA KOLIZJI
bool Level::CollisionLeft(sf::Sprite x)
{
        Player p;
        if (x.getPosition().x < p.player.getPosition().x + p.player.getGlobalBounds().width)
                return true;
        return false;
}

bool Level::CollisionRight(sf::Sprite x)
{
        Player p;
        if (x.getPosition().x + x.getGlobalBounds().width >  p.player.getPosition().x)
                return true;
        return false;
}

bool Level::CollisionBottom(sf::Sprite x)
{
        Player p;
        if (x.getPosition().y > p.player.getPosition().y + p.player.getGlobalBounds().width)
                return true;
        return false;
}

bool Level::Collision(sf::Sprite x)
{
        Player p;
        float x1 = p.player.getPosition().x;
        float x2 = x.getPosition().x;
        float y1 = p.player.getPosition().y;
        float y2 = x.getPosition().y;
        float w1 = p.player.getGlobalBounds().width;
        float w2 = x.getGlobalBounds().width;
        float h1 = p.player.getGlobalBounds().height;
        float h2 = x.getGlobalBounds().height;

        return(((x1 - (w1 / 2) < x2 + (w2 / 2)) && (x2 + (w2 / 2) > x1 - (w1 / 2))) && ((x2 - (w2 / 2) < x1 + (w1 / 2)) && (x1 + (w1 / 2) > x2 - (w2 / 2))) && ((y1 - (h1 / 2) < y2 + (h2 / 2)) && (y2 + (h2 / 2) > y1 - (h1 / 2))) && ((y2 - (h2 / 2) < y1 + (h1 / 2)) && (y1 + (h1 / 2) > y2 - (h2 / 2))));
}

////

 

Level.h
#pragma once
class Level
{
public:
        void Load1(sf::RenderWindow& gameWindow);
        void Draw(sf::RenderWindow& gameWindow);
        void Counts(sf::RenderWindow& gameWindow);


        //DODANE DLA SPROBOWANIA KOLIZJI
        sf::Sprite cabinets;
        sf::Sprite microwaves;
        sf::Sprite fridges;
        sf::Sprite tables;
        sf::Sprite ovens;
        sf::Sprite boxs;
        sf::Sprite chairs;

        bool CollisionLeft(sf::Sprite x);
        bool CollisionRight(sf::Sprite x);
        bool CollisionBottom(sf::Sprite x);
        bool Collision(sf::Sprite x);
        ////
};
 

Player.h
#pragma once
class Player
{
public:
        void Draw(sf::RenderWindow& gameWindow);
        bool BoundingBoxTest(const sf::Sprite& Object1, const sf::Sprite& Object2);
       
        //DODANE DLA SPROBOWANIA KOLIZJI
        sf::Sprite player;
        ////
};
 

7
General / Help with character move
« on: December 16, 2013, 09:15:31 am »
Hello, I just watched SpriteSheet Animation tutorial and tried to put that code in my project I have downloaded character from famitsu generator. But when I press Up, down my character doesn't move. He is still turn left, why ?:)

Game.cpp - menuStates switch code:
case Game::Single:
                        {
                                sf::Event zdarzenie;
                                while (oknoAplikacji.pollEvent(zdarzenie))
                                {
                                       
                                        Level level;
                                        level.Zaladuj(oknoAplikacji);
                                        if (zdarzenie.type == sf::Event::Closed) stanGry = Game::Wychodzenie;

                                        if (zdarzenie.type == sf::Event::KeyPressed)
                                        {
                                                if (zdarzenie.key.code == sf::Keyboard::Escape) PokazMenu();
                                        }
                                }

                                break;
                        }
 

Player class code:
#include <SFML/Graphics.hpp>
#include "Player.h"

void Player::Rysuj(sf::RenderWindow & oknoAplikacji)
{
        enum Kierunek { Lewo, Prawo, Gora, Dol }; //{0,1,2,3}
        sf::Vector2i source(1, Dol);

        oknoAplikacji.clear(sf::Color(0, 0, 0));
        sf::Texture obrazGracza;
        obrazGracza.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/player.png");
        sf::Sprite player(obrazGracza);
        player.setPosition(0, 0);

        while (oknoAplikacji.isOpen()) {
                sf::Event zdarzenie;
                while (oknoAplikacji.pollEvent(zdarzenie)) {
                        switch (zdarzenie.type) {
                        case sf::Event::KeyPressed:
                                if (zdarzenie.key.code == sf::Keyboard::Left)
                                        source.y = Lewo;
                                else if (zdarzenie.key.code == sf::Keyboard::Right)
                                        source.y == Prawo;
                                else if (zdarzenie.key.code == sf::Keyboard::Up)
                                        source.x == Gora;
                                else if (zdarzenie.key.code == sf::Keyboard::Down)
                                        source.x == Dol;
                                break;
                        }
                }
                player.setTextureRect(sf::IntRect(source.x = 32, source.y = 32, 32, 32));
                oknoAplikacji.draw(player);
                oknoAplikacji.display();
                oknoAplikacji.clear();
        }
}
 

In my Level::Zaladuj method I have player.Rysuj();
Please help me :)

8
General / Re: Question about level map
« on: December 15, 2013, 02:02:25 pm »
I know how to draw image and how to use sprites but I don't know how to put character to go on that image in foreground and how to ceter my character and control camera. And how to do turns :( So i wrote here :)

9
General / Question about level map
« on: December 15, 2013, 12:35:16 pm »
Hello, sorry if topic is not in a correct place :)
I have to create game project in C++ for my university, I created Game States, Menu etc but now I want to create level and I need so much your help. (I use SFML 2.0)
I am not good in C++ and this library but I have to pass my subject and create nice looking game :)

In my game class I have something like this:
case Game::Play:
{
        sf::Event event;
        while (oknoAplikacji.pollEvent(event))
        {
                gameWindow.clear(sf::Color(0, 0, 0));

                //I think something with level class here :)

                gameWindow.display();

                if (event.type == sf::Event::Closed) gameState= Game::Exit;

                if (event.type == sf::Event::KeyPressed)
                {
                        if (event.key.code == sf::Keyboard::Escape) ShowMenu();
                }
        }
break;
}
 

I want to create level like in my attachment, in foreground I want to draw my own image and characters should move and jump on that image. I want to control my character and enemy moves automatic. Background should be static image. Game should be like tanks. My turn (10 seconds), enemy turn(10 seconds) and again and again...
How to create this level?:) Please help, this is very important for me:) I need simple solutions :) And I know my english is very bad, sorry :(

Pages: [1]