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.


Topics - Kiblinix

Pages: [1]
1
Graphics / Moving one shape over another using the mouse
« on: March 17, 2012, 12:17:06 am »
I have two circle shapes on the screen, and when the left mouse button is held down, and the mouse cursor is over one of the circles, it should "pick it up" and the circle follows the mouse cursor until the left mouse button is released.
There is an issue when I pick up one of the circles, and move it over the other, because then this causes the second shape to be picked up also and I cannot separate them since they are on top of each other.

Here is my input code for the circles:
 
Code: [Select]
if ( (sf::Mouse::IsButtonPressed(sf::Mouse::Button::Left)) && (sf::Mouse::GetPosition(App).x > circle.GetPosition().x - circle.GetRadius())
    && (sf::Mouse::GetPosition(App).x < circle.GetPosition().x + circle.GetRadius())
 && (sf::Mouse::GetPosition(App).y > circle.GetPosition().y - circle.GetRadius())
 && (sf::Mouse::GetPosition(App).y < circle.GetPosition().y + circle.GetRadius()) )
{
circle.SetPosition(sf::Mouse::GetPosition(App).x, sf::Mouse::GetPosition(App).y);
}


This picks up the circle fine but causes the issue mentioned above :(

I did try to add a bool so it would only pick it up if it didn't have one picked up already, and here is the code for that:

Code: [Select]
bool nodeGrabbed = false;
if ( (nodeGrabbed == false) && (sf::Mouse::IsButtonPressed(sf::Mouse::Button::Left)) && (sf::Mouse::GetPosition(App).x > circle.GetPosition().x - circle.GetRadius())
&& (sf::Mouse::GetPosition(App).x < circle.GetPosition().x + circle.GetRadius())
&& (sf::Mouse::GetPosition(App).y > circle.GetPosition().y - circle.GetRadius())
&& (sf::Mouse::GetPosition(App).y < circle.GetPosition().y + circle.GetRadius()) )
{
nodeGrabbed = true;
circle.SetPosition(sf::Mouse::GetPosition(App).x, sf::Mouse::GetPosition(App).y);
}
else
{
nodeGrabbed = false;
}


But this still did not work :(

Maybe I should mention that each shape is in a class and the two different shapes I have created are in a vector, and I go through this vector to check for user input, so maybe this has something to do with it as it just gets the input for both at the same time?

Any help would be appreciated

2
Graphics / Ball movement and bouncing in the Pong example
« on: August 07, 2011, 11:35:59 pm »
I am trying to recreate pong to help me along with my learning on C++ and SFML, but I've ran into a few problems when trying to make the ball move and bounce.

I managed to get the ball to move on its own, and to bounce off the walls and the paddles easily, but the problem was that it didn't seem "realistic". The ball was always bouncing at the same angle, as what I did was this:

Code: [Select]
float velX = 0.15f;
float velY = 0.35f;

// Move the ball in the game loop
Ball.Move(velX * App.GetFrameTime(), velY * App.GetFrameTime());

// When the ball hits the top or bottom of the screen
velY = -velY;

// When the ball hits a paddle
velX = -velX;


Doing it like this meant that the user has no control over where the ball goes, as it would just bounce around at the same angle and be pretty boring.

I reasoned that to do this I'd need to use trigonometry, so I attempted that.

Correct me if I'm wrong, but this is what I did to try and work it out:

where x is the angle I want the ball to move, and hyp is the ball's speed?

So to move the ball I would do:
Code: [Select]
Ball.Move(App.GetFrameTime() * ballSpeed * cos(ballAngle), App.GetFrameTime() * ballSpeed * sin(ballAngle));

Is this correct? And excuse the cos and sin bits there, I don't know how to do those in C++ yet.

3
Graphics / Classes and using static
« on: July 17, 2011, 11:01:28 pm »
For the past two days I have been trying to create a bullet class which can then be used to let the player fire bullets.

I have been struggling to get the bullet class to use the same image that only has to load once instead of loading it every time a bullet is created, but this leads to some problems.

I had a look in the 1.6 sprites tutorial: http://www.sfml-dev.org/tutorials/1.6/graphics-sprite.php , specifically the Images and Sprite management section, more specifically, this piece of code:

Code: [Select]
class Missile
{
public :

    static bool Init(const std::string& ImageFile)
    {
        return Image.LoadFromFile(ImageFile);
    }

    Missile()
    {
        Sprite.SetImage(Image); // every sprite uses the same unique image
    }

private :

    static sf::Image Image; // shared by every instance

    sf::Sprite Sprite; // one per instance
};


I do not understand this part:
Code: [Select]
static bool Init(const std::string& ImageFile)
    {
        return Image.LoadFromFile(ImageFile);
    }


I understand the rest of the class, but I am not sure what I am meant to do with this function, do I use it after I have created the bullet?

Code: [Select]
Missile missile1;
missile1.Init("Missile.png");


Like so? Because if I just copy the Missile class exactly into my code (I wouldn't normally do this but I've been working on it for a while now), and then try to just create a Missile, I get this error:

error LNK2001: unresolved external symbol "private: static class sf::Image Missile::Image" (?Image@Missile@@0V0sf@@A)

Here is my program that produces the problem:

Code: [Select]
#include <SFML/Graphics.hpp>

class Missile
{
public :
    static bool Init(const std::string& ImageFile)
    {
        return Image.LoadFromFile(ImageFile);
    }

    Missile()
    {
        Sprite.SetImage(Image);
    }

private :
    static sf::Image Image;
    sf::Sprite Sprite;
};

int main()
{
sf::RenderWindow App(sf::VideoMode(640, 480), "Space Shooter");
App.SetFramerateLimit(60);

Missile missile1;

while (App.IsOpened())
{
sf::Event Event;
while (App.PollEvent(Event))
{
if (Event.Type == sf::Event::Closed)
App.Close();
}

App.Clear(sf::Color::White);
App.Display();
}

return 0;
}


Any help would be greatly appreciated, and as a side note, I'm using SFML 2.0.

4
SFML projects / Fruit Chase
« on: July 10, 2011, 11:49:17 pm »
In some spare time I decided I wanted to create a simple game with C++ and (of course) SFML.
Hence, Fruit Chase was born!

All you have to do is move your character around the screen, and when he gets to some fruit, he'll eat it! When he eats fruit, he speeds up. If the character touches the sides of the screen, game over, so the game gets progressively more difficult.

Download here: https://legacy.sfmluploads.org/index.php?page=view_file&file_id=41

Source code is in there too, so feel free to take a look at it, but don't expect any amazing code :P

This is the first game I have made since I decided to start using and practising SFML, so any criticism is greatly appreciated :)

How high can you score?

5
Graphics / Help with game and sprite movement
« on: May 17, 2011, 09:31:45 pm »
Hey, I'm currently in the progress of creating my first game using SFML.
My game is going to be like Pickin' Sticks, but have the movement of something like Pokemon / Snake.
Basically, the character will have to collect an object, like in Pickin' Sticks, and will move by when I press a key, he sets off in that direction (No holding down the button to get him to move and then releasing to stop). I also want him to be walking on the tiles on the floor only, not in between.

Here's what the game looks like so far (Ugly but what I need to visualise it at the moment):



Here is my character movement code:
Code: [Select]
float ElapsedTime = Clock.GetElapsedTime();
Clock.Reset();
float charSpeed = 50.f;

if ((sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::W))
Character.Move(0, -charSpeed * ElapsedTime);  
if ((sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::A))
Character.Move(-charSpeed * ElapsedTime, 0);
if ((sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::S))
Character.Move(0, charSpeed * ElapsedTime);
if ((sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::D))
Character.Move(charSpeed * ElapsedTime, 0);


My problem at the moment is that the character sprite moves fine, until I tap a key, for example 'g', and then the sprite stops. How do I prevent this?

My next problem is, how do I keep the sprite between the tiles, so there is no overlapping on the black lines? I've thought about this for a while but haven't thought of a way to implement it.

Lastly, my code for creating all those tiles etc is quite messy:
Code: [Select]
const float imgWidth = Tile1.GetWidth();      // Tile1 is just the image version.
const float imgHeight = Tile1.GetHeight();
const float horizontalTiles = App.GetWidth() / imgWidth;
const float verticalTiles = App.GetHeight() / imgHeight;

std::vector<sf::Sprite> Tiles;
for (int i = 0; i < horizontalTiles; i++)
{
for (int j = 0; j < verticalTiles; j++)
{
sf::Sprite Tile;
Tile.SetImage(Tile1);
Tile.SetPosition(i * imgWidth, j * imgHeight);

Tiles.push_back(Tile);
}
}

Code: [Select]
for (unsigned int i = 0; i < Tiles.size(); i++)
App.Draw(Tiles[i]);


Is there a "cleaner" way of doing this? What if I also wanted to change a few of the middle tiles colour using .SetColor? I imagine to do this I would use for loops again, but I can't really think of how to do it without changing nearly all of them.

Thanks.  :)

Pages: [1]