SFML и C++ Уроки \ Разработка игр › Форумы › SFML System, Window и другие вопросы › непонятная телепортация › Ответ в теме: непонятная телепортация
вот
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 |
#include <SFML/Graphics.hpp> #include <SFML/Audio.hpp> #include "map.h" #include "view.h" #include <iostream> using namespace sf; class Hero { private: public: float x, y; float width, height, dx, dy, speed; int dir; String File; Image img; Texture texture; Sprite sprt; bool isMove; Hero(String F, float X, float Y, float W, float H) { File = F;width = W; height = H; isMove = false; img.loadFromFile("images/" + File); img.createMaskFromColor(Color(255, 255, 255)); texture.loadFromImage(img); sprt.setTexture(texture); x = X; y = Y; sprt.setTextureRect(IntRect(width, height, width, height)); sprt.setOrigin(width / 2, height / 2);// } void Update(float time) { intMap(); 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; default:dx = 0; dy = 0; } x += dx*time; y += dy*time; speed = 0; sprt.setPosition(x, y); } void intMap() { for (int i = y / 32; i < (y + height) / 32; i++) for (int j = x / 32; j < (x + width) / 32; j++) { if (TileMap[i][j] == 'g') { if (dy>0) { y = i * 32 - height; } if (dy<0) { y = i * 32 + 32; } if (dx>0) { x = j * 32 - width; } if (dx < 0) { x = j * 32 + 32; } } if (TileMap[i][j] == 's') { if (dy>0) { y = i * 32 - height; } if (dy<0) { y = i * 32 + 32; } if (dx>0) { x = j * 32 - width; } if (dx < 0) { x = j * 32 + 32; } } } } }; int main() { sf::RenderWindow window(sf::VideoMode(800, 600), "Devil"); int tempX = 0, tempY = 0; float distance=0; Clock mus; sf::Music music; if (!music.openFromFile("sounds/bg.ogg")) return -1; // error music.play(); music.setVolume(10);//!!!ГРОМКОСТЬ НУЖНО БУДЕТ ВЕРНУТЬ!!!// view.reset(FloatRect(0, 0, 800, 600)); Image imgMap; imgMap.loadFromFile("images/map.png"); Texture tMap; tMap.loadFromImage(imgMap); Sprite sMap; sMap.setTexture(tMap); float CurrentFrame = 0; Hero player("player.png", 250, 250, 32, 48); player.sprt.setTextureRect(IntRect(0, 0, 32, 48)); Clock clock; getCoord(player.x, player.x); while (window.isOpen()) { float tm = mus.getElapsedTime().asSeconds(); if (tm > 12.1){ mus.restart(); music.play(); } float time = clock.getElapsedTime().asMicroseconds(); clock.restart(); time = time / 800; Vector2i pixelPos = Mouse::getPosition(window); Vector2f pos = window.mapPixelToCoords(pixelPos); sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); if (event.type == Event::MouseButtonPressed) if (event.key.code == Mouse::Left){ player.isMove = true; tempX = pos.x; tempY = pos.y; } } if (player.isMove) { distance = sqrt((tempX - player.x)*(tempX - player.x) + (tempY - player.y)*(tempY - player.y)); if (distance > 2) { player.x = 0.1*time*(tempX - player.x) / distance; player.y = 0.1*time*(tempY - player.y) / distance; } else{ player.isMove = false; std::cout << "ok\n"; } } window.setView(view); player.Update(time); window.clear(); for (int i = 0; i < hMap; i++) for (int j = 0; j < wMap; j++) { if (TileMap[i][j] == ' ')sMap.setTextureRect(IntRect(0, 0, 32, 32)); if (TileMap[i][j] == 's')sMap.setTextureRect(IntRect(32, 32, 32, 32)); if (TileMap[i][j] == 'g')sMap.setTextureRect(IntRect(0, 32, 32, 32)); if (TileMap[i][j] == '1')sMap.setTextureRect(IntRect(32, 0, 32, 32)); if (TileMap[i][j] == '2')sMap.setTextureRect(IntRect(64, 0, 32, 32)); if (TileMap[i][j] == '4')sMap.setTextureRect(IntRect(128, 0, 32, 32)); if (TileMap[i][j] == '6')sMap.setTextureRect(IntRect(192, 0, 32, 32)); sMap.setPosition(j * 32, i * 32); window.draw(sMap); } window.draw(player.sprt); window.display(); } return 0; } |