gpt4 book ai didi

c++ - 如何让我的圈子用键移动

转载 作者:行者123 更新时间:2023-11-28 06:29:41 25 4
gpt4 key购买 nike

我正在做我的第一个关于 SFML C++ 的项目,我正在尝试将它们结合起来。我想做的是拥有我制作的圆圈:

sf::CircleShape shape(50);
shape.setPosition(800, 450);

shape.setFillColor(sf::Color(100, 250, 50));

现在我尝试使用 W、A、S、D 或箭头键移动它。

但我不确定该怎么做,我尝试了几种方法,例如:

        if (sf::Keyboard::IsKeyPressed(sf::Keyboard::Up))
Sprite.Move(spriteSpeed * App.GetFrameTime(), 0);

但是我不确定我做错了什么,有人可以帮助我吗?提前致谢!

这是我在 atm 上的密码。

    #include "stdafx.h"
#include<SFML/Graphics.hpp>
#include<string>
#include<iostream>

int main()
{
//Here we declare the render window so we can talk to it.
sf::RenderWindow window;

//sf::VideoMode is to set the size of the window
//The seconds parameter (the string) is for setting the title
//The style is to show/hide the close button and the title bar, or to set full screen
window.create(sf::VideoMode(1600, 900), " My First SFML Game", sf::Style::Titlebar | sf::Style::Close | sf::Style::Resize);

//----------------------------------wait for a key to be pressed-------------------------------------
//This shows a message that you should press a key
/*std::cout << "Press a key to continue." << std::endl;*/
//---------------------------------------------------------------------------------------------------

//----------------------------------Showing a message------------------------------------------------
//Define the messages that will be showed, and the display text
std::string message = "Hello my name is Jean-Paul van Houten";
std::string display = "";

int index = 0;

window.setKeyRepeatEnabled(false);
//----------------------------------------------------------------------------------------------------

sf::CircleShape shape(50);
shape.setPosition(800, 450);

//this while loop will only be called if the window is open.
while(window.isOpen())
{
//Define the event variable
sf::Event eventSF;

//Check if there is an event
while(window.pollEvent(eventSF))
{


shape.setFillColor(sf::Color(100, 250, 50));
//shape.setPosition(eventSF.mouseMove.x 0 sha,eventSF.mouseMove.y);
window.clear();
switch(eventSF.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::MouseEntered:
std::cout << "Mouse within screen bounds" << std::endl;
break;
case sf::Event::MouseLeft:
std::cout << "Mouse outisde the screen bounds" << std::endl;
break;
case sf::Event::MouseMoved:
std::cout << "X: " << eventSF.mouseMove.y << " Y: " << eventSF.mouseMove.y << std::endl;

break;
case sf::Event::MouseButtonPressed:
if(eventSF.mouseButton.button == sf::Mouse::Left)
std::cout << "Left Button Pressed At: X: " << eventSF.mouseButton.x << " Y: " << eventSF.mouseButton.y << std::endl;
break;
case sf::Event::MouseWheelMoved:
std::cout << "Scrolled: " << eventSF.mouseWheel.delta << std::endl;
break;

case sf::Event::GainedFocus:
std::cout << "Window Active" << std::endl;
break;
case sf::Event::LostFocus:
std::cout << "Window Not Active" << std::endl;
break;
case sf::Event::Resized:
std::cout << "Width: " << eventSF.size.width << " Height: " << eventSF.size.height << std::endl;
break;

case sf::Event::TextEntered:
if(eventSF.text.unicode != 8)//(eventSF.text.unicode >= 33 && eventSF.text.unicode <= 126) //This is to only include the characters between the number, now we use punctuation and letters.
std::cout << (char)eventSF.text.unicode;
else if(eventSF.text.unicode == 8)
display = display.substr(0, display.length() - 1);

system("cls");
std::cout << display;
break;
}



window.draw(shape);

//If you release a key
if(eventSF.type == sf::Event::KeyReleased)
{
//and this key is the enter key
if(eventSF.key.code == sf::Keyboard::Return)
{
display += message[index];
index ++;
system("cls"); //CLS on windows, clear on mac/linux
std::cout << display;
}
}
}

window.display();

}
}

最佳答案

首先,您必须在事件轮询开关中添加 KeyPressed 事件处理,并在其中添加移动 Sprite 的代码

switch(eventSF.type)
{
[...]
case sf::Event::KeyPressed:
if(eventSF.key.code == sf::Keyboard::Up)
{
shape.move(0, 1)
}
break;

}

此外,

shape.setFillColor(sf::Color(100, 250, 50));

不应该在游戏循环中。

还有这个

window.clear();
window.draw(shape);
window.display();

应该在事件轮询循环之外:

while(window.isOpen())
{
//Define the event variable
sf::Event eventSF;

//Check if there is an event
while(window.pollEvent(eventSF))
{
[...]
}


window.clear();
window.draw(shape);
window.display();
}

关于c++ - 如何让我的圈子用键移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27841637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com