SFML game move

本文通过使用SFML库在C++中创建了一个简单的2D游戏示例,演示了如何处理键盘输入、移动矩形对象并检测碰撞。游戏包含两个矩形,一个可以由玩家通过键盘控制移动和旋转,另一个静止不动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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;
}

效果图,继续~~

效果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值