首页 > 学院 > 开发设计 > 正文

SFML game fun

2019-11-14 12:29:51
字体:
来源:转载
供稿:网友
#include"SFML/Graphics.hpp"using namespace std;#include<iostream>;#include<SFML/Audio.hpp>int main(){ sf::RenderWindow window(sf::VideoMode(800, 600), "Hello from SFML"); // setting the framerate limit to 60 FPS window.setFramerateLimit(60); window.setKeyRepeatEnabled(false); bool play = true; // Event object holding all events sf::Event event; bool aPRessed = false; bool aReleased = false; bool space = false; bool leftClick = false; bool rightClick = false; bool returnReleased = false; bool leftPressed = false; bool rightPressed = false; //Variables int numberOfClicks = 0; int mouseX=0; int mouseY=0; int rectXPosition = 0; // Images sf::Texture image1; if (image1.loadFromFile("Data/image2.png") == -1) { return -1; } // Render shapes sf::RectangleShape rect; rect.setSize(sf::Vector2f(150, 150)); rect.setPosition(0, 0); rect.setFillColor(sf::Color::White); rect.setTexture(&image1); sf::CircleShape circle; circle.setRadius(50); circle.setPosition(100, 100); circle.setFillColor(sf::Color::Blue); // Font sf::Font font; if (font.loadFromFile("Data/28 Days Later.ttf") == 0) { return 1; } // Text sf::Text title; title.setFont(font); title.setCharacterSize(100); title.setString("Hello Game"); title.setPosition(300, 80); title.setColor(sf::Color::Magenta); // Sounds /*sf::SoundBuffer ExplosionBuffer; if (ExplosionBuffer.loadFromFile("Data/Explosion4.wav") == 0) { return 1; } sf::Sound explosion; explosion.setBuffer(ExplosionBuffer); explosion.play(); explosion.setLoop(true); explosion.setVolume(100);*/ // Music sf::Music drumLoop; if (drumLoop.openFromFile("Data/music.ogg") == 0) { return 1; } drumLoop.setLoop(true); drumLoop.play(); // game loop while (play == true) { // EVENTS while (window.pollEvent(event)) { if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::A) { aPressed = true; } if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::A) { aReleased = true; } // Space key pressed if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Space) { space = true; } // Space key released if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Space) { space = false; } if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Left) { leftPressed = true; } if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Right) { rightPressed = true; } if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Left) { leftPressed = false; } if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Right) { rightPressed = false; } if (event.type == sf::Event::MouseButtonPressed&& event.mouseButton.button == sf::Mouse::Left) { leftClick = true; } if (event.type == sf::Event::MouseButtonPressed&& event.mouseButton.button == sf::Mouse::Right) { rightClick = true; } if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Return) { returnReleased = true; } if (event.type == sf::Event::MouseMoved) { mouseX = event.mouseMove.x; mouseY = event.mouseMove.y; } if (event.type == sf::Event::Closed) { play = false; } }// LOGIC if (aPressed == true) { // print to the console std::cout << "The A key has been pressed!/n"; aPressed = false; } if (aPressed == true) { std::cout << "The A key has been released!/n"; aReleased = false; } if (leftClick == true) { numberOfClicks++; std::cout << "Number of clicks is" << numberOfClicks << "/n"; if (mouseX < 200) { play = false; } leftClick = false; } if (rightClick == true) { numberOfClicks--; std::cout << "Number of clicks is" << numberOfClicks << "/n"; rightClick = false; } if (returnReleased == true) { std::cout << "Return has been released/n"; returnReleased = false; } if (leftPressed == true && rightPressed == true) { std::cout << "left and right are pressed down/n"; } // Print out the position of mouse. std::cout << "Mouse x:" << mouseX << "mouse y:" << mouseY << endl; rectXPosition++; // X position variable of the rectangle rect.setPosition(rectXPosition, rectXPosition); // we set the rectangles X // and Y position to the variable rectPosition // RENGERING window.clear(); window.draw(rect); window.draw(circle); window.draw(title); window.display(); } // This is the end of the "While" loop // Clean up and close the window window.close(); return 0;}

这里写图片描述


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表