2
« on: June 12, 2011, 03:15:07 am »
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <cmath>
//Stuff Included
int speed= 10;
//Global Speed For Player
bool coll(sf::Sprite& s1, sf::Sprite& s2)
// Collision Function
{
float left1, left2, right1, right2, top1, top2, bot1, bot2;
//Floats for our bounding boxes
left1= s1.GetPosition().x;
left2=s2.GetPosition().x;
right1=s1.GetPosition().x +s1.GetSize().x;
right2=s2.GetPosition().x +s2.GetSize().x;
top1=s1.GetPosition().y;
top2=s2.GetPosition().y;
bot1=s1.GetPosition().y+s1.GetScale().y;
bot2=s2.GetPosition().y+s2.GetScale().y;
//All of the Bounding Boxes' floats now have values!
if(bot1>top2)
{
return true;
}
else if(top1<bot2)
{
return true;
}
else if(right1>left2)
{
return true;
}
else if(right2>left1)
{
return true;
}
//return true if the sides of the Rectangles intersect
else
{
return false;
}
//return false if no sides intersect
}
int main()
{
sf::RenderWindow w(sf::VideoMode(500,500,32),"Scout-Man");
//Putting up the Render Window
sf::Image Scout;
if(!bat.LoadFromFile("Scout.png"))
{
return EXIT_SUCCESS();
}
//Loading Player Image
sf::Image wall;
if(!wall.LoadFromFile("wall.png"))
{
return EXIT_SUCCESS();
}
//Loading Wall Image
sf::Sprite pc;
sf::Sprite w1;
w1.SetImage(wall);
sf::Sprite w2;
w2.SetImage(wall);
w1.SetPosition(100,460);
w2.SetPosition(280,460);
pc.SetImage(Scout);
pc.SetPosition(250,250);
//Setting up the sprites and their values
while(w.IsOpened())
//Main Loop
{
sf::Event Event;
while(w.GetEvent(Event))
//Get Events
{
if(Event.Type==sf::Event::Closed)
{
w.Close();
}
//Close the Window if User Exits
if(Event.Type==sf::Event::KeyPressed && Event.Key.Code==sf::Key::W)
{
pc.SetPosition(pc.GetPosition().x, pc.GetPosition().y-speed);
}
else if(Event.Type==sf::Event::KeyPressed && Event.Key.Code==sf::Key::S)
{
pc.SetPosition(pc.GetPosition().x, pc.GetPosition().y+speed);
}
else if(Event.Type==sf::Event::KeyPressed && Event.Key.Code==sf::Key::A)
{
pc.SetPosition(pc.GetPosition().x-speed, pc.GetPosition().y);
}
else if(Event.Type==sf::Event::KeyPressed && Event.Key.Code==sf::Key::D)
{
pc.SetPosition(pc.GetPosition().x+speed, pc.GetPosition().y);
}
//Allow the Player to Move if he presses one of the WASD Keys
if(coll(pc,w2) || coll(pc,w1))
{
speed= -12;
}
else
{
speed= 10;
}
//If the Player Collides with one of the Walls, he will be stopped
}
w.Clear();
w.Draw(pc);
w.Draw(w1);
w.Draw(w2);
w.Display();
//Draw and Display
}
return EXIT_SUCCESS();
//Exit The Game after the While Loop Ends
}
All my code just in case
I've been looking at Some simple Collision Detection Algorithms and they all look very nice, but none of them have worked so far!
What Will happen is my character will move in the opposite directions (like W goes down, A goes right) if I include the function in my Main Loop. I know it's not the movement code, as I've taken out the collision algorithm and it worked fine. And to add insult to injury, the you don't even collide wit the stupid walls! javascript:emoticon(':cry:')
P.S Yeah the codes not great, but I'm going for an incredibly simple first project game. Sorta like Pac-Man. Yes I know a tile system would be better, but I don't feel like setting one up for my first project in SFML when free movement will work fine when I can get it to work.
Thanks in Advance! javascript:emoticon(':D')
[/i]