Idea Project โปรแกรม Piano โดย C++ 
Code นี้เขียนโดย นร.ของเรา คนหนึ่ง ปีหนึ่งจาก ม.มหิดล





















STEP 1


load Lib. จาก http://www.sfml-dev.org/

STEP 2


config Visual Studio ตามนี้เลย
http://www.sfml-dev.org/tutorials/2.1/start-vc.php


STEP 3


ตัวอย่าง Code

#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <stdio.h>
int main()
{
printf("\n");
printf("\n");
printf("\n ************* ******** ********* **** ***** *******");
printf("\n *************** ******** *********** ***** ***** ***********");
printf("\n ****** *** **** ************* ****** ***** *************");
printf("\n ****** *** **** ************** ******* ***** **** ****");
printf("\n ****** *** **** ************** ******** ***** *** ***");
printf("\n ****** *** **** ***** ***** ********* ***** ** **");
printf("\n ****** *** **** ***** ***** ********** ***** ** **");
printf("\n ***************** **** ***** ***** **************** ** **");
printf("\n *************** **** ************** ****** ******* ** **");
printf("\n ****** **** ************** ****** ****** ** **");
printf("\n ****** **** ***** ***** ***** ***** *** ***");
printf("\n ****** **** ***** ***** ***** **** **** ****");
printf("\n ****** **** ***** ***** ***** *** *************");
printf("\n ****** ******** ***** ***** ***** ** ***********");
printf("\n **** ******** **** **** *** * *******");
printf("\n");

sf::RenderWindow window(sf::VideoMode(500, 200), "SFML works!");
sf::RectangleShape rect[15];
for(int i=0;i< 15 ; i++)
{
rect.setSize (sf::Vector2f(20, 120));
rect.setPosition (20 + i * 22,50);
rect.setFillColor(sf::Color::White);
}
sf::RectangleShape rect2[14];
for(int j=0;j< 14 ; j++)
{
rect2[j].setSize (sf::Vector2f(16, 57));
rect2[j].setPosition (20 + j * 22+13,50);
rect2[j].setFillColor(sf::Color(255,50,100));
}
window.setKeyRepeatEnabled(false);
sf::SoundBuffer buffer[15];
sf::Sound sound[15];

for(int i=0;i<15 ; i++)
{
char temp [30];
sprintf (temp,"Notes\\%d.wav",i+1);
//printf("loading file %s ...\n",temp);
if (!buffer.loadFromFile(temp))
return -1;
sound.setBuffer(buffer);
}
int a[15];
for(int k=0;k<15;k++)
{
a[k]=0;
}
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
///////////////////////////////////////////////////////////////////////

if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode=='a')
{
if(a[0] == 0)
{
sound[0].play(); // 1
printf("0");
rect[0].setFillColor(sf::Color(50,50,50));
}
a[0] = 1;
}
}
if (event.type == sf::Event::KeyReleased )
{
if (event.key.code == sf::Keyboard::A)
{
a[0] = 0;
rect[0].setFillColor(sf::Color::White );
}
}
/////////////////////////////////////////
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode=='s')
{
if(a[1] ==0)
{
sound[1].play(); // 2
printf("0");
rect[1].setFillColor(sf::Color(50,50,50));
}
a[1] = 1;
}
}
if (event.type == sf::Event::KeyReleased )
{
if (event.key.code == sf::Keyboard::S)
{
a[1] = 0;
rect[1].setFillColor(sf::Color::White );
}
}
////////////////////////////////////////
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode=='d')
{
if(a[2] ==0)
{
sound[2].play(); // 3
printf("0");
rect[2].setFillColor(sf::Color(50,50,50));
}
a[2] = 1;
}
}
if (event.type == sf::Event::KeyReleased )
{
if (event.key.code == sf::Keyboard::D)
{
a[2] = 0;
rect[2].setFillColor(sf::Color::White );
}
}
/////////////////////////////////////////
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode=='f')
{
if(a[3] ==0)
{
sound[3].play(); // 4
printf("0");
rect[3].setFillColor(sf::Color(50,50,50));
}
a[3] = 1;
}
}
if (event.type == sf::Event::KeyReleased )
{
if (event.key.code == sf::Keyboard::F)
{
a[3] = 0;
rect[3].setFillColor(sf::Color::White );
}
}
////////////////////////////////////////
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode=='g')
{
if(a[4] ==0)
{
sound[4].play(); // 5
printf("0");
rect[4].setFillColor(sf::Color(50,50,50));
}
a[4] = 1;
}
}
if (event.type == sf::Event::KeyReleased )
{
if (event.key.code == sf::Keyboard::G)
{
a[4] = 0;
rect[4].setFillColor(sf::Color::White );
}
}
////////////////////////////////////////
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode=='h')
{
if(a[5] ==0)
{
sound[5].play(); // 6
printf("0");
rect[5].setFillColor(sf::Color(50,50,50));
}
a[5] = 1;
}
}
if (event.type == sf::Event::KeyReleased )
{
if (event.key.code == sf::Keyboard::H)
{
a[5] = 0;
rect[5].setFillColor(sf::Color::White );
}
}
////////////////////////////////////////
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode=='j')
{
if(a[6] ==0)
{
sound[6].play(); // 7
printf("0");
rect[6].setFillColor(sf::Color(50,50,50));
}
a[6] = 1;
}
}
if (event.type == sf::Event::KeyReleased )
{
if (event.key.code == sf::Keyboard::J)
{
a[6] = 0;
rect[6].setFillColor(sf::Color::White );
}
}
////////////////////////////////////////
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode=='k')
{
if(a[7] == 0)
{
sound[7].play(); // 8
printf("0");
rect[7].setFillColor(sf::Color(50,50,50));
}
a[7] = 1;
}
}
if (event.type == sf::Event::KeyReleased )
{
if (event.key.code == sf::Keyboard::K)
{
a[7] = 0;
rect[7].setFillColor(sf::Color::White );
}
}
////////////////////////////////////////
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode=='l')
{
if(a[8] == 0)
{
sound[8].play(); // 9
printf("0");
rect[8].setFillColor(sf::Color(50,50,50));
}
a[8] = 1;
}
}
if (event.type == sf::Event::KeyReleased )
{
if (event.key.code == sf::Keyboard::L)
{
a[8] = 0;
rect[8].setFillColor(sf::Color::White );
}
}
////////////////////////////////////////
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode=='z')
{
if(a[9] ==0)
{
sound[9].play(); // 10
printf("0");
rect[9].setFillColor(sf::Color(50,50,50));
}
a[9] = 1;
}
}
if (event.type == sf::Event::KeyReleased )
{
if (event.key.code == sf::Keyboard::Z)
{
a[9] = 0;
rect[9].setFillColor(sf::Color::White );
}
}
////////////////////////////////////////
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode=='x')
{
if(a[10] ==0)
{
sound[10].play(); // 11
printf("0");
rect[10].setFillColor(sf::Color(50,50,50));
}
a[10] = 1;
}
}
if (event.type == sf::Event::KeyReleased )
{
if (event.key.code == sf::Keyboard::X)
{
a[10] = 0;
rect[10].setFillColor(sf::Color::White );
}
}
////////////////////////////////////////
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode=='c')
{
if(a[11] ==0)
{
sound[11].play(); // 12
printf("0");
rect[11].setFillColor(sf::Color(50,50,50));
}
a[11] = 1;
}
}
if (event.type == sf::Event::KeyReleased )
{
if (event.key.code == sf::Keyboard::C)
{
a[11] = 0;
rect[11].setFillColor(sf::Color::White );
}
}
////////////////////////////////////////
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode=='v')
{
if(a[12] == 0)
{
sound[12].play(); // 13
printf("0");
rect[12].setFillColor(sf::Color(50,50,50));
}
a[12] = 1;
}
}
if (event.type == sf::Event::KeyReleased )
{
if (event.key.code == sf::Keyboard::V)
{
a[12] = 0;
rect[12].setFillColor(sf::Color::White );
}
}
////////////////////////////////////////
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode=='b')
{
if(a[13] == 0)
{
sound[13].play(); // 14
printf("0");
rect[13].setFillColor(sf::Color(50,50,50));
}
a[13] = 1;
}
}
if (event.type == sf::Event::KeyReleased )
{
if (event.key.code == sf::Keyboard::B)
{
a[13] = 0;
rect[13].setFillColor(sf::Color::White );
}
}
////////////////////////////////////////
if (event.type == sf::Event::TextEntered)
{
if (event.text.unicode=='n')
{
if(a[14] == 0)
{
sound[14].play(); // 15
printf("0");
rect[14].setFillColor(sf::Color(50,50,50));
}
a[14] = 1;
}
}
if (event.type == sf::Event::KeyReleased )
{
if (event.key.code == sf::Keyboard::N)
{
a[14] = 0;
rect[14].setFillColor(sf::Color::White );
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
window.clear();

for(int i=0;i< 15 ; i++)
{
window.draw (rect );
}
for(int i=0;i< 14 ; i++)
{
if(i!=3&&i!=6&&i!=10)
{
window.draw (rect2 );
}
}

window.display();
}
for(int i=0;i<15 ; i++)
{
sound.stop();
}
return 0;
}


STEP 4


หวังว่า ผู้อ่านจะสนุกนะครับ


http://expert-programming-tutor.com

[ view entry ] ( 1826 views )   |  permalink  |  $star_image$star_image$star_image$star_image$star_image ( 3 / 2486 )

<<First <Back | 1 | 2 | 3 | 4 | 5 | Next> Last>>