SFML и C++ Уроки \ Разработка игр › Форумы › SFML Graphics › Элементы карты (к примеру камень с урока – s) › Ответ в теме: Элементы карты (к примеру камень с урока – s)
Просто вывел весь код проекта.
Main.cpp:
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
#include <SFML/Graphics.hpp> #include "map.h" #include "view.h" #include "iostream" #include "sstream" using namespace std; using namespace sf; class Map { public: float FloatOriginX, FloatOriginY, Width, Heigth; String File; Image MapIm; Texture MapTr; Sprite MapSr; Map(String Im) { MapIm.loadFromFile("images/" + Im); MapTr.loadFromImage(MapIm); MapSr.setTexture(MapTr); } }; char *StreetName[5][5] = { "Oxford", "King,s Road", "Carnaby", "Baker", "Whitehall", "Downing", "Floral", "Dryden", "Macklin", "Cleveland", "Howland", "Chitty", "Hanson", "Hallam", "Mansfield", "Duchess", "Harley", "Wimpole", "Vere", "Barrett", "Duke", "Wigmore", "Baker", "George", "Aybrook" }; int StreetNum[5][5] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 }; class MainText { public: Text text; MainText(const string &sText, const Font &font, unsigned int iSize, const Color &cColor, const Uint32 &uStyle, float x, float y) { text.setString(sText); text.setFont(font); text.setCharacterSize(iSize); text.setFillColor(cColor); text.setStyle(uStyle); text.setPosition(Vector2f(x, y)); } }; int main() { RenderWindow window(VideoMode(1120, 737), "Route"); view.reset(FloatRect(0, 0, 1120, 737)); Map m("labels.png"); Font font; font.loadFromFile("fonts/CyrilicOld.ttf"); float VertexPosX[25], VertexPosY[25]; int Count; Clock clock; while (window.isOpen()) { float time = clock.getElapsedTime().asMicroseconds(); clock.restart(); time = time / 800; Event event; while (window.pollEvent(event)) { if (event.type == Event::Closed) window.close(); } if (Keyboard::isKeyPressed(Keyboard::C)) { Vector2i pixelPos = Mouse::getPosition(window); Vector2f pos = window.mapPixelToCoords(pixelPos); cout << pos.x << endl; cout << pos.y << endl; } ViewMap(time); window.setView(view); window.clear(); for (int i = 0; i < HEIGHT_MAP; i++) for (int j = 0; j < WIDTH_MAP; j++) { Count = 0; switch (TileMap[i][j]) { case '0': m.MapSr.setTextureRect(IntRect(64, 0, 32, 32)); break; case 'v': m.MapSr.setTextureRect(IntRect(32, 0, 32, 32)); break; case '-': m.MapSr.setTextureRect(IntRect(128, 0, 32, 32)); break; case '|': m.MapSr.setTextureRect(IntRect(96, 0, 32, 32)); break; default: m.MapSr.setTextureRect(IntRect(0, 0, 32, 32)); break; } m.MapSr.setPosition(j * 32, i * 32); window.draw(m.MapSr); } MainText Number1X("1", font, 25, Color::Green, Text::Bold, 108, 50); MainText Number2X("2", font, 25, Color::Green, Text::Bold, 300, 50); MainText Number3X("3", font, 25, Color::Green, Text::Bold, 492, 50); MainText Number4X("4", font, 25, Color::Green, Text::Bold, 682, 50); MainText Number5X("5", font, 25, Color::Green, Text::Bold, 878, 50); MainText Number1Y("1", font, 25, Color::Blue, Text::Bold, 60, 94); MainText Number2Y("2", font, 25, Color::Blue, Text::Bold, 60, 222); MainText Number3Y("3", font, 25, Color::Blue, Text::Bold, 60, 350); MainText Number4Y("4", font, 25, Color::Blue, Text::Bold, 60, 480); MainText Number5Y("5", font, 25, Color::Blue, Text::Bold, 60, 607); MainText GreenCub("<>", font, 20, Color::Green, Text::Bold, 90, 640); MainText BlueCub("<>", font, 20, Color::Blue, Text::Bold, 90, 670); MainText NumberB("Номер автобусов", font, 20, Color::White, Text::Bold, 134, 640); MainText NumberT("Номер троллейбусов", font, 20, Color::White, Text::Bold, 134, 670); MainText V00(StreetName[0][0], font, 20, Color::White, Text::Bold, 145, 82); MainText V01(StreetName[0][1], font, 20, Color::White, Text::Bold, 340, 82); MainText V02(StreetName[0][2], font, 20, Color::White, Text::Bold, 550, 82); MainText V03(StreetName[0][3], font, 20, Color::White, Text::Bold, 730, 82); MainText V04(StreetName[0][4], font, 20, Color::White, Text::Bold, 915, 82); MainText V10(StreetName[1][0], font, 20, Color::White, Text::Bold, 160, 210); MainText V11(StreetName[1][1], font, 20, Color::White, Text::Bold, 355, 210); MainText V12(StreetName[1][2], font, 20, Color::White, Text::Bold, 525, 210); MainText V13(StreetName[1][3], font, 20, Color::White, Text::Bold, 730, 210); MainText V14(StreetName[1][4], font, 20, Color::White, Text::Bold, 908, 210); MainText V20(StreetName[2][0], font, 20, Color::White, Text::Bold, 150, 339); MainText V21(StreetName[2][1], font, 20, Color::White, Text::Bold, 350, 339); MainText V22(StreetName[2][2], font, 20, Color::White, Text::Bold, 540, 339); MainText V23(StreetName[2][3], font, 20, Color::White, Text::Bold, 745, 339); MainText V24(StreetName[2][4], font, 20, Color::White, Text::Bold, 910, 339); MainText V30(StreetName[3][0], font, 20, Color::White, Text::Bold, 150, 467); MainText V31(StreetName[3][1], font, 20, Color::White, Text::Bold, 350, 467); MainText V32(StreetName[3][2], font, 20, Color::White, Text::Bold, 550, 467); MainText V33(StreetName[3][3], font, 20, Color::White, Text::Bold, 750, 467); MainText V34(StreetName[3][4], font, 20, Color::White, Text::Bold, 930, 467); MainText V40(StreetName[4][0], font, 20, Color::White, Text::Bold, 150, 595); MainText V41(StreetName[4][1], font, 20, Color::White, Text::Bold, 340, 595); MainText V42(StreetName[4][2], font, 20, Color::White, Text::Bold, 550, 595); MainText V43(StreetName[4][3], font, 20, Color::White, Text::Bold, 745, 595); MainText V44(StreetName[4][4], font, 20, Color::White, Text::Bold, 915, 595); window.draw(Number1X.text); window.draw(Number2X.text); window.draw(Number3X.text); window.draw(Number4X.text); window.draw(Number5X.text); window.draw(Number1Y.text); window.draw(Number2Y.text); window.draw(Number3Y.text); window.draw(Number4Y.text); window.draw(Number5Y.text); window.draw(GreenCub.text); window.draw(BlueCub.text); window.draw(NumberB.text); window.draw(NumberT.text); window.draw(V00.text); window.draw(V01.text); window.draw(V02.text); window.draw(V03.text); window.draw(V04.text); window.draw(V10.text); window.draw(V11.text); window.draw(V12.text); window.draw(V13.text); window.draw(V14.text); window.draw(V20.text); window.draw(V21.text); window.draw(V22.text); window.draw(V23.text); window.draw(V24.text); window.draw(V30.text); window.draw(V31.text); window.draw(V32.text); window.draw(V33.text); window.draw(V34.text); window.draw(V40.text); window.draw(V41.text); window.draw(V42.text); window.draw(V43.text); window.draw(V44.text); window.display(); } return 0; } |
Map.h:
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <SFML/Graphics.hpp> using namespace sf; using namespace std; const int HEIGHT_MAP = 23;//размер карты высота const int WIDTH_MAP = 35;//размер карты ширина String TileMap[HEIGHT_MAP] = { "00000000000000000000000000000000000", "0 0", "0 0", "0 v-----v-----v-----v-----v----- 0", "0 | | | | | 0", "0 | | | | | 0", "0 | | | | | 0", "0 v-----v-----v-----v-----v----- 0", "0 | | | | | 0", "0 | | | | | 0", "0 | | | | | 0", "0 v-----v-----v-----v-----v----- 0", "0 | | | | | 0", "0 | | | | | 0", "0 | | | | | 0", "0 v-----v-----v-----v-----v----- 0", "0 | | | | | 0", "0 | | | | | 0", "0 | | | | | 0", "0 v-----v-----v-----v-----v----- 0", "0 0", "0 0", "00000000000000000000000000000000000", }; |
View.h:
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <SFML/Graphics.hpp> using namespace std; using namespace sf; View view; View ViewMap(float time){ if (Keyboard::isKeyPressed(Keyboard::W)) view.move(0, -0.1*time); if (Keyboard::isKeyPressed(Keyboard::S)) view.move(0, 0.1*time); if (Keyboard::isKeyPressed(Keyboard::D)) view.move(0.1*time, 0); if (Keyboard::isKeyPressed(Keyboard::A)) view.move(-0.1*time, 0); if (Keyboard::isKeyPressed(Keyboard::Q)) view.setCenter(560, 369); return view; } |