SFML game move
来源:互联网 发布:java开发bi方向 编辑:程序博客网 时间:2024/05/19 15:23
#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; sf::Font font; // States bool rButton = false; // R button/key bool leftButton = false, rightButton = false; bool upButton = false, downButton = false; // Variables int rectRotation = 0; // Rotation of the shape int xVelocity = 0, yVelocity = 0; // Shapes sf::RectangleShape shape1; shape1.setSize(sf::Vector2f(100, 100)); shape1.setPosition(100, 300); // Shapes sf::RectangleShape shape2; shape2.setSize(sf::Vector2f(100, 200)); shape2.setPosition(200, 200); shape2.setFillColor(sf::Color::Blue); // game loop while (play == true) { // EVENTS while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) { play = false; } if (event.type == sf::Event::KeyPressed&&event.key.code == sf::Keyboard::R) { rButton = true; } if (event.type == sf::Event::KeyReleased&&event.key.code == sf::Keyboard::R) { rButton = false; } if (event.type == sf::Event::KeyPressed) { if (event.key.code == sf::Keyboard::Left) leftButton = true; if (event.key.code == sf::Keyboard::Right) rightButton = true; if (event.key.code == sf::Keyboard::Up) upButton = true; if (event.key.code == sf::Keyboard::Down) downButton = true; } // Realeased keyboard if (event.type == sf::Event::KeyReleased) { if (event.key.code == sf::Keyboard::Left) leftButton = false; if (event.key.code == sf::Keyboard::Right) rightButton = false; if (event.key.code == sf::Keyboard::Up) upButton = false; if (event.key.code == sf::Keyboard::Down) downButton = false; } } // LOGIC if (rButton == true) { rectRotation++; shape1.setRotation(rectRotation); } // Movement // x axis if (rightButton == true) { xVelocity = 5; } if (leftButton == true) { xVelocity = -5; } if (leftButton == true && rightButton == true) { xVelocity = 0; } if (leftButton == false && rightButton == false) { xVelocity = 0; } // Y axis if (downButton == true) { yVelocity = 5; } if (upButton == true) { yVelocity = -5; } if (upButton == true && downButton == true) { yVelocity = 0; } if (upButton == false && downButton == false) { yVelocity = 0; } // Move the shape by adding the velocity /*shape1.move(xVelocity, yVelocity); if (shape1.getGlobalBounds().intersects(shape2.getGlobalBounds()) == true) { shape1.move(-xVelocity, -yVelocity); }*/ shape1.move(xVelocity, 0); if (shape1.getGlobalBounds().intersects(shape2.getGlobalBounds()) == true) { shape1.move(-xVelocity, 0); } shape1.move(0, yVelocity); if (shape1.getGlobalBounds().intersects(shape2.getGlobalBounds()) == true) { shape1.move(0, -yVelocity); } // RENDERING window.clear(); window.draw(shape1); window.draw(shape2); window.display(); } // This is the end of the "While" loop // Clean up and close the window window.close(); return 0;}
效果图,继续~~
0 0
- SFML game move
- SFML game fun
- SFML Game Development Book (0)
- SFML学习纪要
- 342. One false move may lose the game. 一着不慎,满盘皆输
- 342. One false move may lose the game. 一着不慎,满盘皆输
- 【SeedCoder2015年 热身题6 搜索】Move Game (题目)
- Move
- Move
- Move
- Move..
- Move!Move!!Move!!!
- sfml画地图的一种方法
- SFML 2.1 开发之旅 1
- 在linux下运行sfml example
- SFML 与 VS2015 的安装配置
- Move Move Look
- Move.c && Move.h
- 计算机编码窥探(ASCII-Unicode-UTF-8)
- 关于ActiveMQ的简单整理(3)
- 汉诺塔ix C语言
- opencv图像处理常用函数
- Day_6.(2)
- SFML game move
- id取模分表分表
- 11.图片
- hdu 2046 骨牌铺方格(递推 斐波那契数列)
- RedHat linux 安装 Oppenoffice 方法
- Spring mvc Interceptor 解决Session超时配置流程
- mysql和sqlserver在变量上的区别
- FunDA(5)- Reactive Streams:Play with Iteratees
- A Walk Through the Forest(最短路径+DFS)