#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");
window
.setFramerateLimit(
60);
window
.setKeyRepeatEnabled(
false);
bool play
= true;
sf
::Event event;
sf
::Font font;
bool rButton
= false;
bool leftButton
= false, rightButton
= false;
bool upButton
= false, downButton
= false;
int rectRotation
= 0;
int xVelocity
= 0, yVelocity
= 0;
sf
::RectangleShape shape1;
shape1
.setSize(sf
::Vector2f(
100,
100));
shape1
.setPosition(
100,
300);
sf
::RectangleShape shape2;
shape2
.setSize(sf
::Vector2f(
100,
200));
shape2
.setPosition(
200,
200);
shape2
.setFillColor(sf
::Color::Blue);
while (play
== true)
{
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;
}
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;
}
}
if (rButton
== true) {
rectRotation
++;
shape1
.setRotation(rectRotation);
}
if (rightButton
== true) {
xVelocity
= 5;
}
if (leftButton
== true) {
xVelocity
= -5;
}
if (leftButton
== true && rightButton
== true) {
xVelocity
= 0;
}
if (leftButton
== false && rightButton
== false)
{
xVelocity
= 0;
}
if (downButton
== true) {
yVelocity
= 5;
}
if (upButton
== true) {
yVelocity
= -5;
}
if (upButton
== true && downButton
== true) {
yVelocity
= 0;
}
if (upButton
== false && downButton
== false)
{
yVelocity
= 0;
}
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);
}
window
.clear();
window
.draw(shape1);
window
.draw(shape2);
window
.display();
}
window
.close();
return 0;
}
效果图,继续~~
转载请注明原文地址: https://ju.6miu.com/read-660623.html