SFML и C++ Уроки \ Разработка игр › Форумы › SFML Graphics › Вывод карты › Ответ в теме: Вывод карты
<p style=”text-align: start;”><span style=”font-size: 16px; line-height: 1.5; text-align: center;”>Сам код</span></p>
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 |
map.h #include <SFML/Graphics.hpp> #include <iostream> #include <windows.h> using namespace sf; const int visota = 32; const int shirb = 32; String TileMap[visota][shirb] = { " ", " ", " s ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", }; vid.h //камера #include <SFML/Graphics.hpp> using namespace sf; View vid; View getPlayercoordinateForView(float x, float y) { vid.setCenter(x + 100, y);//слежка return vid;// возврат- обновление камеры } main.cpp #include <SFML/Graphics.hpp> #include <iostream> #include <windows.h> #include "map.h" #include "vid.h" using namespace sf; class Player { public: float x, y, w, h, dx, dy, speed = 0; int dir = 0; //направление //bool onGround; String File; Image image; Texture texture; Sprite sprite; Player(String F, float X, float Y, float W, float H) { //onGround = false; File = F;//имя файла и расширение w = W; h = H;//высота и ширина image.loadFromFile("images/" + File); 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); } float getPlayerCoordinataX() { return x; } float getPlayerCoordinataY() { return y; } }; int main() { RenderWindow window(VideoMode(700, 500), "window"); vid.reset(FloatRect(0, 0, 700, 500)); Image map_image; map_image.loadFromFile("images/map.png"); Texture map; map.loadFromImage(map_image); Sprite s_map; s_map.setTexture(map); float Kadr = 0;//текущий кадр Clock clock; Player snow("snowman.png", 50, 25, 112, 116); while (window.isOpen()) { float time = clock.getElapsedTime().asMicroseconds(); clock.restart(); time = time / 500; sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); } if (Keyboard::isKeyPressed(Keyboard::Left)) { snow.dir = 1; snow.speed = 0.1; Kadr += 0.005*time; if (Kadr > 4) Kadr -= 4; snow.sprite.setTextureRect(IntRect(120 * int(Kadr), 231, 116, 116)); } if (Keyboard::isKeyPressed(Keyboard::Right)) { snow.dir = 0; snow.speed = 0.1;//направление вправо Kadr += 0.005*time; if (Kadr > 4) Kadr -= 4; snow.sprite.setTextureRect(IntRect(120 * int(Kadr), 118, 116, 115)); } if (Keyboard::isKeyPressed(Keyboard::Up)) { snow.dir = 3; snow.speed = 0.1;//направление вниз Kadr += 0.005*time; if (Kadr > 4) Kadr -= 4; snow.sprite.setTextureRect(IntRect(120 * int(Kadr), 120, 116, 115)); } if (Keyboard::isKeyPressed(Keyboard::Down)) { snow.dir = 2; snow.speed = 0.1; Kadr += 0.005*time; if (Kadr > 4) Kadr -= 4; snow.sprite.setTextureRect(IntRect(120 * int(Kadr), 120, 116, 115)); } getPlayercoordinateForView(snow.getPlayerCoordinataX(), snow.getPlayerCoordinataY()); snow.update(time); window.setView(vid); window.clear(); for (int i = 0; i < visota; i++) for (int j = 0; j < shirb; j++) { if ((TileMap[i][j] == ' ')) s_map.setTextureRect(IntRect(0, 0, 40, 40)); if ((TileMap[i][j] == 'o')) s_map.setTextureRect(IntRect(465, 80, 270, 155)); if ((TileMap[i][j] == 's')) s_map.setTextureRect(IntRect(915, 40, 1050, 167)); s_map.setPosition(i * 32, j * 32); window.draw(s_map); } window.draw(snow.sprite); window.display(); } return 0; } |