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 |
#include <SFML/Graphics.hpp> #include "map.h" #include "view.h" using namespace sf; class Player { private: float x, y = 0; public: float w, h, dx, dy, speed = 0; int dir = 0; String File; Image image; Texture texture; Sprite sprite; Player(String F, float X, float Y, float W, float H){ 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); } float getplayercoordinateX(){ return x; } float getplayercoordinateY(){ return y; } }; int main() { RenderWindow window(sf::VideoMode(640, 480), "hero"); view.reset(sf::FloatRect(0, 0, 640, 480)); float CurrentFrame = 0; Image map_image; map_image.loadFromFile("images/map.png"); Texture map; map.loadFromImage(map_image); Sprite s_map; s_map.setTexture(map); Player p("main_hero.png", 320, 240, 30, 65); Clock clock; 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) || (Keyboard::isKeyPressed(Keyboard::A)))) { p.dir = 1; p.speed = 0.1; CurrentFrame += 0.005*time; if (CurrentFrame > 1) CurrentFrame -= 1; p.sprite.setTextureRect(IntRect(3 , 204, 65, 30)); getplayercoordinateforview(p.getplayercoordinateX(), p.getplayercoordinateY()); } if ((Keyboard::isKeyPressed(Keyboard::Right) || (Keyboard::isKeyPressed(Keyboard::D)))) { p.dir = 0; p.speed = 0.1; CurrentFrame += 0.005*time; if (CurrentFrame > 1) CurrentFrame -= 1; p.sprite.setTextureRect(IntRect(6 , 152, 64, 31)); getplayercoordinateforview(p.getplayercoordinateX(), p.getplayercoordinateY()); } if ((Keyboard::isKeyPressed(Keyboard::Up) || (Keyboard::isKeyPressed(Keyboard::W)))) { p.dir = 3; p.speed = 0.1; CurrentFrame += 0.005*time; if (CurrentFrame > 1) CurrentFrame -= 1; p.sprite.setTextureRect(IntRect(7 , 75, 30, 65)); getplayercoordinateforview(p.getplayercoordinateX(), p.getplayercoordinateY()); } if ((Keyboard::isKeyPressed(Keyboard::Down) || (Keyboard::isKeyPressed(Keyboard::S)))) { p.dir = 2; p.speed = 0.1; CurrentFrame += 0.005*time; if (CurrentFrame > 1) CurrentFrame -= 1; p.sprite.setTextureRect(IntRect(7 , 8, 30, 65)); getplayercoordinateforview(p.getplayercoordinateX(), p.getplayercoordinateY()); } p.update(time); window.setView(view); window.clear(Color::White); for (int i = 0; i < HEIGHT_MAP; i++) for (int j = 0; j < WIDTH_MAP; j++) { if (TileMap[i][j] == ' ') s_map.setTextureRect(IntRect(0, 0, 32, 32)); if (TileMap[i][j] == 'P') s_map.setTextureRect(IntRect(32, 0, 32, 32)); if ((TileMap[i][j] == '0')) s_map.setTextureRect(IntRect(64, 0, 32, 32)); s_map.setPosition(j * 32, i * 32); window.draw(s_map); } window.draw(p.sprite); window.display(); } return 0; } |