SFML и C++ Уроки \ Разработка игр › Форумы › SFML Graphics › Автоматическое движение спрайта(Help me!)
В этой теме 6 ответов, 3 участника, последнее обновление BunDem 6 года/лет, 9 мес. назад.
-
АвторСообщения
-
#include <SFML/Graphics.hpp>
#include <stdio.h>
#include <time.h>//float Rotation = 0;
using namespace sf;
int main()
{
srand(time(NULL));
//Texture herotexture;
//herotexture.loadFromFile(“D:/programka/zachet123/image/111.png”);//Sprite herosprite;
// herosprite.setTexture(herotexture);
// herosprite.setTextureRect(IntRect(0, 0, 132, 133));
// herosprite.setPosition(350,330);
// //herosprite.setPosition(415, 390);
// //herosprite.setPosition(205, 200);
// herosprite.setRotation(0);sf::RenderWindow window(sf::VideoMode(800, 800), “SUN”);
RectangleShape rectamble(Vector2f(500000, 500000));
rectamble.setFillColor(Color(255, 165, 0));Texture herotexture2;
herotexture2.loadFromFile(“D:/programka/zachet123/image/333.png”);Sprite herosprite2;
herosprite2.setTexture(herotexture2);
herosprite2.setTextureRect(IntRect(0, 0, 400, 400));
herosprite2.setPosition(0, 0);
herosprite2.setRotation(0);/*sf::ConvexShape convex;
convex.setFillColor(sf::Color::Yellow);
convex.setPointCount(8);
convex.setPoint(0, sf::Vector2f(142, 82));
convex.setPoint(1, sf::Vector2f(260, 82));
convex.setPoint(2, sf::Vector2f(320, 140));
convex.setPoint(3, sf::Vector2f(320, 250));
convex.setPoint(4, sf::Vector2f(262, 310));
convex.setPoint(5, sf::Vector2f(154, 310));
convex.setPoint(6, sf::Vector2f(100, 250));
convex.setPoint(7, sf::Vector2f(100, 150));RectangleShape line(Vector2f(150, 10));
line.rotate(45);
line.setPosition(40, 20);
line.setFillColor(Color::Yellow);RectangleShape line2(Vector2f(150, 10));
line2.rotate(67);
line2.setPosition(165, 10);
line2.setFillColor(Color::Yellow);RectangleShape line3(Vector2f(150, 10));
line3.rotate(95);
line3.setPosition(240, 10);
line3.setFillColor(Color::Yellow);RectangleShape line4(Vector2f(150, 10));
line4.rotate(120);
line4.setPosition(330, 20);
line4.setFillColor(Color::Yellow);RectangleShape line5(Vector2f(150, 10));
line5.rotate(167);
line5.setPosition(390, 130);
line5.setFillColor(Color::Yellow);RectangleShape line6(Vector2f(150, 10));
line6.rotate(200);
line6.setPosition(400, 250);
line6.setFillColor(Color::Yellow);RectangleShape line7(Vector2f(150, 10));
line7.rotate(230);
line7.setPosition(350, 350);
line7.setFillColor(Color::Yellow);RectangleShape line8(Vector2f(150, 10));
line8.rotate(260);
line8.setPosition(240, 390);
line8.setFillColor(Color::Yellow);RectangleShape line9(Vector2f(150, 10));
line9.rotate(290);
line9.setPosition(140, 390);
line9.setFillColor(Color::Yellow);RectangleShape line10(Vector2f(150, 10));
line10.rotate(320);
line10.setPosition(50, 350);
line10.setFillColor(Color::Yellow);RectangleShape line11(Vector2f(150, 10));
line11.rotate(350);
line11.setPosition(10, 250);
line11.setFillColor(Color::Yellow);RectangleShape line12(Vector2f(150, 10));
line12.rotate(380);
line12.setPosition(10, 130);
line12.setFillColor(Color::Yellow);CircleShape square(10, 4);
square.setFillColor(Color(0, 150, 0));
square.setPosition(170, 150);CircleShape square2(10, 4);
square2.setFillColor(Color(0, 150, 0));
square2.setPosition(240, 150);sf::ConvexShape convex2;
convex2.setFillColor(sf::Color::Red);
convex2.setPointCount(3);
convex2.setPoint(0, sf::Vector2f(170, 240));
convex2.setPoint(1, sf::Vector2f(260, 240));
convex2.setPoint(2, sf::Vector2f(215, 260));
*/
while (window.isOpen())
{sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}/*herosprite.rotate(Rotation);
herosprite.setOrigin(66, 66);
Rotation += 0.0005;herosprite2.rotate(Rotation);
herosprite2.setOrigin(200, 200);
Rotation += 0.0005;*//*
if ((Keyboard::isKeyPressed(Keyboard::Left)) || (Keyboard::isKeyPressed(Keyboard::A)))
{
herosprite.move(-0.1, 0);
}if ((Keyboard::isKeyPressed(Keyboard::Right)) || (Keyboard::isKeyPressed(Keyboard::D)))
{
herosprite.move(0.1, 0);
}if ( (Keyboard::isKeyPressed(Keyboard::Up)) || (Keyboard::isKeyPressed(Keyboard::W)))
{
herosprite.move(0, -0.1);
}if ((Keyboard::isKeyPressed(Keyboard::Down)) || (Keyboard::isKeyPressed(Keyboard::S)))
{
herosprite.move(0, 0.1);
}if ((Keyboard::isKeyPressed(Keyboard::Left)) || (Keyboard::isKeyPressed(Keyboard::A)))
{
herosprite2.move(-0.1, 0);
}if ((Keyboard::isKeyPressed(Keyboard::Right)) || (Keyboard::isKeyPressed(Keyboard::D)))
{
herosprite2.move(0.1, 0);
}if ((Keyboard::isKeyPressed(Keyboard::Up)) || (Keyboard::isKeyPressed(Keyboard::W)))
{
herosprite2.move(0, -0.1);
}if ((Keyboard::isKeyPressed(Keyboard::Down)) || (Keyboard::isKeyPressed(Keyboard::S)))
{
herosprite2.move(0, 0.1);
}*/window.clear();
window.draw(rectamble);
/*window.draw(convex);
window.draw(line);
window.draw(line2);
window.draw(line3);
window.draw(line4);
window.draw(line5);
window.draw(line6);
window.draw(line7);
window.draw(line8);
window.draw(line9);
window.draw(line10);
window.draw(line11);
window.draw(line12);*/
/*window.draw(square);
window.draw(square2);
window.draw(convex2);*/
window.draw(herosprite2);
/*window.draw(herosprite);*/
window.display();
}return 0;
}как сделать так что бы он сам передвигался , а не я с помощью клавиш ?
например двигался по углам окна?Вложения:
You must be logged in to view attached files.спасибо конечно но по клику мыши я знаю
а вот как сделать что бы он сам передвигался , без никакой помощи ?
Создай переменную которая будет хранить этапы движения спрайта и массив контрольных точек. Направляй спрайт в контролние точки.Когда спрай окажеться в нужной контрольной точке начнеться следующий етап движения и так дале.
C++123456789int Part=0;int X[4]{600, 600, 0, 0}, y[4]{0,600,600,0};switch (Part){case 0:move(0.1,0)if(sprite.getPosition().x==X[Part]&&sprite.getPosition().y==Y[Part])Part++ break;case 1:move(0,0.1)if(sprite.getPosition().x==X[Part]&&sprite.getPosition().y==Y[Part])Part++break;case 2:move(-0.1,0)if(sprite.getPosition().x==X[Part]&&sprite.getPosition().y==Y[Part])Part++ break;case 3:move(0,-0.1)if(sprite.getPosition().x==X[Part]&&sprite.getPosition().y==Y[Part])Part=0 break;}Вот как-то так.
спасибо большое выручил)
Ошибка 2 error C3861: move: идентификатор не найден
вот мне такое пишет(
Вы просто принцип под свой код адаптируйте. Такой переменной нет у вас, значит задайте.
-
АвторСообщения
Для ответа в этой теме необходимо авторизоваться.