SFML и C++ Уроки \ Разработка игр › Форумы › SFML System, Window и другие вопросы › Не работает проект из урока 14
В этой теме 4 ответа, 3 участника, последнее обновление Павел Букреев 8 года/лет, 3 мес. назад.
-
АвторСообщения
-
В проекте не отображается текст миссий. Вроде всё сделал правильно
C++123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294#include <iostream>#include <SFML/Graphics.hpp>#include <sstream>#include "view.h"#include "mission.h"#include "map.h";using namespace sf;using namespace std;class Player{private:float x, y;public:float w, h, dx, dy, speed;int dir, health;bool life;String File;Image image;Texture texture;Sprite sprite;int PlayerScore;Player(String F, float X, float Y, float W, float H){PlayerScore = 0;health = 100;life = true;dx = 0; dy = 0; speed = 0; dir = 0;File = F;w = W; h = H;image.loadFromFile("images/" + File);image.createMaskFromColor(Color(41, 33, 59));texture.loadFromImage(image);sprite.setTexture(texture);x = X; y = Y;sprite.setTextureRect(IntRect(0, 0, W, H));}void update(float time){switch (dir){case 0:dx = speed; dy = 0; break;case 1:dx = -speed; dy = 0; break;case 2:dx = 0; dy = speed; break;case 3:dx = 0; dy = -speed; break;}x += dx * time;y += dy * time;speed = 0;sprite.setPosition(x, y);interactionWithMap();if (health <= 0){life = false;}}void interactionWithMap(){for (int i = y / 32; i < (y + h) / 32; i++)for (int j = x / 32; j<(x + w) / 32; j++){if (TitleMap[i][j] == '0'){if (dy>0){y = i * 32 - h;}if (dy<0){y = i * 32 + 32;}if (dx>0){x = j * 32 - w;}if (dx < 0){x = j * 32 + 32;}}if (TitleMap[i][j] == 's') {PlayerScore++;TitleMap[i][j] = ' ';}if (TitleMap[i][j] == 'f'){health -= 40;TitleMap[i][j] = ' ';}if (TitleMap[i][j] == 'h'){health += 20;TitleMap[i][j] = ' ';}}}float getplayercoordinateX(){return x;}float getplayercoordinateY(){return y;}};int main(){RenderWindow window(VideoMode(640, 480), "YarikRak");view.reset(FloatRect(0, 0, 640, 480));float CurrentFrame = 0;Clock clock;int gameTime = 0;Clock gameTimeClock;Player p("hero.png", 250, 250, 96.0, 96.0);Image map_image;map_image.loadFromFile("images/map.png");Texture map;map.loadFromImage(map_image);Sprite s_map;s_map.setTexture(map);Image quest_image;quest_image.loadFromFile("images/missionbg.jpg");quest_image.createMaskFromColor(Color(0, 0, 0));Texture quest_texture;quest_texture.loadFromImage(quest_image);Sprite s_quest;s_quest.setTexture(quest_texture);s_quest.setTextureRect(IntRect(0, 0, 340, 510));s_quest.setScale(0.6f, 0.6f);Font font;font.loadFromFile("CyrilicOld.ttf");Text text("", font, 20);text.setColor(Color::Black);bool showMissionText = true;while (window.isOpen()){float time = clock.getElapsedTime().asMicroseconds();if (p.life)gameTime = gameTimeClock.getElapsedTime().asSeconds();clock.restart();time = time / 800;Event event;while (window.pollEvent(event)){if (event.type == Event::Closed)window.close();if (event.type == Event::KeyPressed)if ((event.key.code == Keyboard::Tab)) {switch (showMissionText) {case true: {std::ostringstream playerHealthString;playerHealthString << p.health;std::ostringstream task;task << getTextMission(getCurrentMission(p.getplayercoordinateX()));cout << task.str();text.setString("Здоровье: " + playerHealthString.str() + "\n" + task.str());showMissionText = false;break;}case false: {text.setString("");showMissionText = true;break;}}}}if (p.life){if (Keyboard::isKeyPressed(Keyboard::Left) || Keyboard::isKeyPressed(Keyboard::A)){p.dir = 1; p.speed = 0.1;CurrentFrame += 0.005*time;if (CurrentFrame > 3) CurrentFrame -= 3;p.sprite.setTextureRect(IntRect(96 * int(CurrentFrame) + 6, 136, 89, 55));getplayercoordinates(p.getplayercoordinateX(), p.getplayercoordinateY());}else if (Keyboard::isKeyPressed(Keyboard::Right) || Keyboard::isKeyPressed(Keyboard::D)){p.dir = 0; p.speed = 0.1;CurrentFrame += 0.005*time;if (CurrentFrame > 3) CurrentFrame -= 3;p.sprite.setTextureRect(IntRect(96 * int(CurrentFrame) + 6, 231, 89, 55));getplayercoordinates(p.getplayercoordinateX(), p.getplayercoordinateY());}else if (Keyboard::isKeyPressed(Keyboard::Up) || Keyboard::isKeyPressed(Keyboard::W)){p.dir = 3; p.speed = 0.1;CurrentFrame += 0.005 * time;if (CurrentFrame > 3) CurrentFrame -= 3;p.sprite.setTextureRect(IntRect(96 * int(CurrentFrame), 307, 96, 96));getplayercoordinates(p.getplayercoordinateX(), p.getplayercoordinateY());}else if (Keyboard::isKeyPressed(Keyboard::Down) || Keyboard::isKeyPressed(Keyboard::S)){p.dir = 2; p.speed = 0.1;CurrentFrame += 0.005 * time;if (CurrentFrame > 3) CurrentFrame -= 3;p.sprite.setTextureRect(IntRect(96 * int(CurrentFrame), 0, 96, 96));getplayercoordinates(p.getplayercoordinateX(), p.getplayercoordinateY());}if (Mouse::isButtonPressed(Mouse::Left)){p.sprite.setColor(Color::Red);}}p.update(time);window.setView(view);window.clear();if ((getCurrentMission(p.getplayercoordinateX())) == 0) {for (int i = 0; i < HEIGHT_MAP; i++)for (int j = 0; j < WIDTH_MAP; j++){if (TitleMap[i][j] == ' ') s_map.setTextureRect(IntRect(0, 0, 32, 32));if (TitleMap[i][j] == 's') s_map.setTextureRect(IntRect(32, 0, 32, 32));if ((TitleMap[i][j] == '0')) s_map.setTextureRect(IntRect(64, 0, 32, 32));if ((TitleMap[i][j] == 'f')) s_map.setTextureRect(IntRect(96, 0, 32, 32));if ((TitleMap[i][j] == 'h')) s_map.setTextureRect(IntRect(128, 0, 32, 32));s_map.setPosition(j * 32, i * 32);window.draw(s_map);}}if ((getCurrentMission(p.getplayercoordinateX())) >= 1) {for (int i = 0; i < HEIGHT_MAP; i++)for (int j = 0; j < WIDTH_MAP; j++){if (TitleMap[i][j] == ' ') s_map.setTextureRect(IntRect(64, 0, 32, 32));if (TitleMap[i][j] == 's') s_map.setTextureRect(IntRect(32, 0, 32, 32));if ((TitleMap[i][j] == '0')) s_map.setTextureRect(IntRect(0, 0, 32, 32));if ((TitleMap[i][j] == 'f')) s_map.setTextureRect(IntRect(96, 0, 32, 32));if ((TitleMap[i][j] == 'h')) s_map.setTextureRect(IntRect(128, 0, 32, 32));s_map.setPosition(j * 32, i * 32);window.draw(s_map);}}if (p.life){std::ostringstream playerScoreString;playerScoreString << p.PlayerScore;text.setString("Собрано камней:" + playerScoreString.str());text.setPosition(view.getCenter().x - 270, view.getCenter().y - 220);window.draw(text);std::ostringstream playerHealthString;playerHealthString << p.health;text.setString("Здоровье:" + playerHealthString.str());text.setPosition(view.getCenter().x + 190, view.getCenter().y - 220);window.draw(text);}else{std::ostringstream playerFinalScore;playerFinalScore << p.PlayerScore;std::ostringstream gameTimeString;gameTimeString << gameTime;text.setString("ИГРА ОКОНЧЕНА!!!""\nСобрано камней: " + playerFinalScore.str() + "\nВремя:" + gameTimeString.str());text.setPosition(view.getCenter().x - 90, view.getCenter().y - 50);window.draw(text);}if (!showMissionText) {text.setPosition(view.getCenter().x + 125, view.getCenter().y - 130);s_quest.setPosition(view.getCenter().x + 115, view.getCenter().y - 130);window.draw(s_quest); window.draw(text);}window.draw(p.sprite);window.display();}}C++12345678910111213141516171819202122232425262728int getCurrentMission(int x){int mission = 0;if ((x>0) && (x<600)) { mission = 0; }if (x>400) { mission = 1; }if (x>700) { mission = 2; }if (x>2200) { mission = 3; }return mission;}std::string getTextMission(int currentMission) {std::string missionText = "";switch (currentMission){case 0: missionText = "\nНачальный этап и \nинструкции к игре"; break;case 1: missionText = "\nMission 1\n\nВот твоя первая\n миссия, на\n этом уровне \nтебе стоит опасаться\n ... бла-бла-бла ..."; break;case 2: missionText = "\nMission 2\n Необходимо решить\n логическую задачку,\n чтобы пройти дальше "; break;case 3: missionText = "\nИ так далее \nи тому подобное....."; break;}return missionText;};Вложения:
You must be logged in to view attached files.Отдебаж путём вывода текста в консоль, и посмотри какая часть кода выполняется, если увидишь что-то подозрительное, то добавь вывод значений условных переменных.
а шрифт подхватывается?
Шрифт подхватывается, это видно на скрине(т. к здоровье и прочие индикаторы работают), в консоли дебажил(на скрине тоже видно). В консоль текст выводится(но подключил без локали, поэтому не на русском), а на свиток – нет. Заранее спасибо за ответ
может на свиток текст рисуешь раньше свитка и свиток перерисовывается сверху?
-
АвторСообщения
Для ответа в этой теме необходимо авторизоваться.