1
General / Help with sound playing and hover effect
« on: February 16, 2020, 07:16:51 pm »
Hello!
I am trying to create a BlackJack Game, this is what I have done yet, I am at high school level, and I would like to know if there is a better way of creating the hover effect than checking every frame if the position of the mouse is/isnt in the button range.
And also I would like to play a music when the mouse enters a button's range
I have the following situation: I have a rectangle with a loaded texture and I would like to create an effect that plays a sound and starts moving the shape when my mouse enters the shape,
I know how to do the sound thing but I have 0 idea on how to work with classes and time in sfml
I saw [this]() video but I couldnāt understand much since I dont work with classes or visual studio
Could you help me do that?
P.S:If you have some spare time I would also appreciate if you could stay for 10 minutes with me on discord or skype.
I am trying to create a BlackJack Game, this is what I have done yet, I am at high school level, and I would like to know if there is a better way of creating the hover effect than checking every frame if the position of the mouse is/isnt in the button range.
And also I would like to play a music when the mouse enters a button's range
I have the following situation: I have a rectangle with a loaded texture and I would like to create an effect that plays a sound and starts moving the shape when my mouse enters the shape,
I know how to do the sound thing but I have 0 idea on how to work with classes and time in sfml
I saw [this]() video but I couldnāt understand much since I dont work with classes or visual studio
Could you help me do that?
P.S:If you have some spare time I would also appreciate if you could stay for 10 minutes with me on discord or skype.
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <iostream>
sf::RenderWindow window(sf::VideoMode::getDesktopMode(), "Jackul Negru",sf::Style::Fullscreen);
void interface()
{
int w=sf::VideoMode::getDesktopMode().width;
int h=sf::VideoMode::getDesktopMode().height;
int unith=h/12;
int unitw=w/20;
int bposx=4*unitw;
int b1posy=2.5*unith;
int b2posy=6*unith;
int b3posy=9.5*unith ;
sf::RectangleShape button1(sf::Vector2f(4*unitw,2*unith));
sf::RectangleShape button2(sf::Vector2f(4*unitw,2*unith));
sf::RectangleShape button3(sf::Vector2f(4*unitw,2*unith));
sf::RectangleShape aceandjack(sf::Vector2f(6*unitw,9*unith));
button1.setPosition(bposx,b1posy);
button2.setPosition(bposx,b2posy);
button3.setPosition(bposx,b3posy);
aceandjack.setPosition(10.5*unitw,10*unith);
sf::Texture b3t;
sf::Texture b2t;
sf::Texture b1t;
sf::Music metin;
sf::Texture aceandjackt;
b1t.loadFromFile(".\\Imgs\\Play.png");
b2t.loadFromFile(".\\Imgs\\options.png");
b3t.loadFromFile(".\\Imgs\\Exit.png");
metin.openFromFile(".\\Sounds\\metin.ogg");
aceandjackt.loadFromFile(".\\Imgs\\AceandJackspades.png");
b1t.setSmooth(true);
b2t.setSmooth(true);
button1.setTexture(&b1t);
button2.setTexture(&b2t);
button3.setTexture(&b3t);
aceandjack.setTexture(&aceandjackt);
sf::Rect<float> size1 = button1.getGlobalBounds();
sf::Rect<float> size2 = button2.getGlobalBounds();
sf::Rect<float> size3 = button3.getGlobalBounds();
button1.setOrigin(sf::Vector2f(size1.width/2,size1.height/2));
button2.setOrigin(sf::Vector2f(size2.width/2,size2.height/2));
button3.setOrigin(sf::Vector2f(size3.width/2,size3.height/2));
aceandjack.setOrigin(0.0f,aceandjack.getSize().y);
bool buton1=false;
bool buton2=false;
bool buton3=false;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) /// If escape pressed ---> Exit Interface.
window.close();
if(sf::Mouse::getPosition(window).x > button1.getGlobalBounds().left && sf::Mouse::getPosition(window).x < button1.getGlobalBounds().left+button1.getGlobalBounds().width && sf::Mouse::getPosition(window).y > button1.getGlobalBounds().top && sf::Mouse::getPosition(window).y< button1.getGlobalBounds().top+ button1.getGlobalBounds().height)
{
button1.setScale(1.2,1.2); ///If the Mouse position is between button1's parameters , it enlarges it, creating the hover effect.
metin.play();
if(event.type == sf::Event::MouseButtonPressed)
if(event.mouseButton.button == sf::Mouse::Left)
window.close(); ///If the Mouse is pressed while between button1's execute this.**/
/// buton1=true;
}
if(sf::Mouse::getPosition(window).x<button1.getGlobalBounds().left || sf::Mouse::getPosition(window).x>button1.getGlobalBounds().left+button1.getGlobalBounds().width || sf::Mouse::getPosition(window).y< button1.getGlobalBounds().top || sf::Mouse::getPosition(window).y>button1.getGlobalBounds().top+button1.getGlobalBounds().height)
button1.setScale(1,1); ///If the Mouse leaves button1's parameters it shrinks back to it's normal size, completing the hover effect.
/// buton1=false;
if(sf::Mouse::getPosition(window).x > button2.getGlobalBounds().left && sf::Mouse::getPosition(window).x < button2.getGlobalBounds().left+button2.getGlobalBounds().width && sf::Mouse::getPosition(window).y > button2.getGlobalBounds().top && sf::Mouse::getPosition(window).y< button2.getGlobalBounds().top+ button2.getGlobalBounds().height)
{
button2.setScale(1.2,1.2); ///Same hover effect as button1 ^^^
if(event.type == sf::Event::MouseButtonPressed)
if(event.mouseButton.button == sf::Mouse::Left)
window.close();
}
if(sf::Mouse::getPosition(window).x<button2.getGlobalBounds().left || sf::Mouse::getPosition(window).x>button2.getGlobalBounds().left+button2.getGlobalBounds().width || sf::Mouse::getPosition(window).y< button2.getGlobalBounds().top || sf::Mouse::getPosition(window).y>button2.getGlobalBounds().top+button2.getGlobalBounds().height)
button2.setScale(1,1); ///Same hover effect as button1 ^^^
if(sf::Mouse::getPosition(window).x > button3.getGlobalBounds().left && sf::Mouse::getPosition(window).x < button3.getGlobalBounds().left+button3.getGlobalBounds().width && sf::Mouse::getPosition(window).y > button3.getGlobalBounds().top && sf::Mouse::getPosition(window).y< button3.getGlobalBounds().top+ button3.getGlobalBounds().height)
{
button3.setScale(1.2,1.2); ///Same hover effect as button1 ^^^
if(event.type == sf::Event::MouseButtonPressed)
if(event.mouseButton.button == sf::Mouse::Left)
window.close();
}
if(sf::Mouse::getPosition(window).x<button3.getGlobalBounds().left || sf::Mouse::getPosition(window).x>button3.getGlobalBounds().left+button3.getGlobalBounds().width || sf::Mouse::getPosition(window).y< button3.getGlobalBounds().top || sf::Mouse::getPosition(window).y>button3.getGlobalBounds().top+button3.getGlobalBounds().height)
button3.setScale(1,1); ///Same hover effect as button1 ^^^
}
/* if(buton1==true)
{
if(event.mouseButton.button == sf::Mouse::Left)
window.close(); ///If the Mouse is pressed while between button1's execute this.
button1.setScale(1.2,1.2);
metin.play();
}
else if(buton1==false)
button1.setScale(1,1);*/
window.clear();
window.draw(aceandjack);
window.draw(button1);
window.draw(button2);
window.draw(button3);
window.display();
}
}
int main()
{
interface();
return 0;
}
#include <SFML/Audio.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <iostream>
sf::RenderWindow window(sf::VideoMode::getDesktopMode(), "Jackul Negru",sf::Style::Fullscreen);
void interface()
{
int w=sf::VideoMode::getDesktopMode().width;
int h=sf::VideoMode::getDesktopMode().height;
int unith=h/12;
int unitw=w/20;
int bposx=4*unitw;
int b1posy=2.5*unith;
int b2posy=6*unith;
int b3posy=9.5*unith ;
sf::RectangleShape button1(sf::Vector2f(4*unitw,2*unith));
sf::RectangleShape button2(sf::Vector2f(4*unitw,2*unith));
sf::RectangleShape button3(sf::Vector2f(4*unitw,2*unith));
sf::RectangleShape aceandjack(sf::Vector2f(6*unitw,9*unith));
button1.setPosition(bposx,b1posy);
button2.setPosition(bposx,b2posy);
button3.setPosition(bposx,b3posy);
aceandjack.setPosition(10.5*unitw,10*unith);
sf::Texture b3t;
sf::Texture b2t;
sf::Texture b1t;
sf::Music metin;
sf::Texture aceandjackt;
b1t.loadFromFile(".\\Imgs\\Play.png");
b2t.loadFromFile(".\\Imgs\\options.png");
b3t.loadFromFile(".\\Imgs\\Exit.png");
metin.openFromFile(".\\Sounds\\metin.ogg");
aceandjackt.loadFromFile(".\\Imgs\\AceandJackspades.png");
b1t.setSmooth(true);
b2t.setSmooth(true);
button1.setTexture(&b1t);
button2.setTexture(&b2t);
button3.setTexture(&b3t);
aceandjack.setTexture(&aceandjackt);
sf::Rect<float> size1 = button1.getGlobalBounds();
sf::Rect<float> size2 = button2.getGlobalBounds();
sf::Rect<float> size3 = button3.getGlobalBounds();
button1.setOrigin(sf::Vector2f(size1.width/2,size1.height/2));
button2.setOrigin(sf::Vector2f(size2.width/2,size2.height/2));
button3.setOrigin(sf::Vector2f(size3.width/2,size3.height/2));
aceandjack.setOrigin(0.0f,aceandjack.getSize().y);
bool buton1=false;
bool buton2=false;
bool buton3=false;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) /// If escape pressed ---> Exit Interface.
window.close();
if(sf::Mouse::getPosition(window).x > button1.getGlobalBounds().left && sf::Mouse::getPosition(window).x < button1.getGlobalBounds().left+button1.getGlobalBounds().width && sf::Mouse::getPosition(window).y > button1.getGlobalBounds().top && sf::Mouse::getPosition(window).y< button1.getGlobalBounds().top+ button1.getGlobalBounds().height)
{
button1.setScale(1.2,1.2); ///If the Mouse position is between button1's parameters , it enlarges it, creating the hover effect.
metin.play();
if(event.type == sf::Event::MouseButtonPressed)
if(event.mouseButton.button == sf::Mouse::Left)
window.close(); ///If the Mouse is pressed while between button1's execute this.**/
/// buton1=true;
}
if(sf::Mouse::getPosition(window).x<button1.getGlobalBounds().left || sf::Mouse::getPosition(window).x>button1.getGlobalBounds().left+button1.getGlobalBounds().width || sf::Mouse::getPosition(window).y< button1.getGlobalBounds().top || sf::Mouse::getPosition(window).y>button1.getGlobalBounds().top+button1.getGlobalBounds().height)
button1.setScale(1,1); ///If the Mouse leaves button1's parameters it shrinks back to it's normal size, completing the hover effect.
/// buton1=false;
if(sf::Mouse::getPosition(window).x > button2.getGlobalBounds().left && sf::Mouse::getPosition(window).x < button2.getGlobalBounds().left+button2.getGlobalBounds().width && sf::Mouse::getPosition(window).y > button2.getGlobalBounds().top && sf::Mouse::getPosition(window).y< button2.getGlobalBounds().top+ button2.getGlobalBounds().height)
{
button2.setScale(1.2,1.2); ///Same hover effect as button1 ^^^
if(event.type == sf::Event::MouseButtonPressed)
if(event.mouseButton.button == sf::Mouse::Left)
window.close();
}
if(sf::Mouse::getPosition(window).x<button2.getGlobalBounds().left || sf::Mouse::getPosition(window).x>button2.getGlobalBounds().left+button2.getGlobalBounds().width || sf::Mouse::getPosition(window).y< button2.getGlobalBounds().top || sf::Mouse::getPosition(window).y>button2.getGlobalBounds().top+button2.getGlobalBounds().height)
button2.setScale(1,1); ///Same hover effect as button1 ^^^
if(sf::Mouse::getPosition(window).x > button3.getGlobalBounds().left && sf::Mouse::getPosition(window).x < button3.getGlobalBounds().left+button3.getGlobalBounds().width && sf::Mouse::getPosition(window).y > button3.getGlobalBounds().top && sf::Mouse::getPosition(window).y< button3.getGlobalBounds().top+ button3.getGlobalBounds().height)
{
button3.setScale(1.2,1.2); ///Same hover effect as button1 ^^^
if(event.type == sf::Event::MouseButtonPressed)
if(event.mouseButton.button == sf::Mouse::Left)
window.close();
}
if(sf::Mouse::getPosition(window).x<button3.getGlobalBounds().left || sf::Mouse::getPosition(window).x>button3.getGlobalBounds().left+button3.getGlobalBounds().width || sf::Mouse::getPosition(window).y< button3.getGlobalBounds().top || sf::Mouse::getPosition(window).y>button3.getGlobalBounds().top+button3.getGlobalBounds().height)
button3.setScale(1,1); ///Same hover effect as button1 ^^^
}
/* if(buton1==true)
{
if(event.mouseButton.button == sf::Mouse::Left)
window.close(); ///If the Mouse is pressed while between button1's execute this.
button1.setScale(1.2,1.2);
metin.play();
}
else if(buton1==false)
button1.setScale(1,1);*/
window.clear();
window.draw(aceandjack);
window.draw(button1);
window.draw(button2);
window.draw(button3);
window.display();
}
}
int main()
{
interface();
return 0;
}