Созданные ответы форума
Просмотр 2 сообщений - с 1 по 2 (из 2 всего)
-
АвторСообщения
-
Боже я слепой. 😀 Спасибо!
C++123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236#include <SFML/Graphics.hpp>#include "map.h"#include "view.h"#include <iostream>#include <math.h>#include <cmath>#include <string>#include <ctime>#include <windows.h>#include <stdio.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;///////////////////////////////////ГЕНЕРАЦИЯ И ВЫВОД ЛАБИРИНТА В КОНСОЛЬ/////////////////////////////////////////////HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleTextAttribute(hConsole, (WORD)((0 << 4) | 11));setlocale(0, "rus");srand(time(0));std::string b[1000], t = "p";int a = 1, c[1000], c1 = 2, n, k = 0, ii = 0, oo = 0;ii = 21;oo = 37;for (int x = 0; x < ii*oo; x++){b[x] = " ";if (x < oo)b[x] = "#";if (x>ii*oo - oo - 1)b[x] = "#";if (x % oo == 0)b[x] = "#";if (x % oo == oo - 1)b[x] = "#";}while (a % 2 == 1)a = rand() % (oo - 4) + oo*ii - oo + 2;b[a] = ".";b[a - oo] = ".";a = 1;while (a % 2 == 1)a = rand() % (oo - 4) + 2;b[a] = ".";b[a + oo] = ".";a += oo * 2;std::cout << 1;while (c1>1){int a1 = a;if (k < 15){c[c1] = a;c1++;}b[a] = ".";k = 0;while (a1 == a && k<15){n = rand() % 4 + 1;if (n == 1 && b[a + 2] == " ")a++;if (n == 2 && b[a + oo * 2] == " ")a += oo;if (n == 3 && b[a - oo * 2] == " ")a -= oo;if (n == 4 && b[a - 2] == " ")a--;k++;}if (k == 15){c[c1 - 1] = 0;c1 -= 2;a = c[c1];c1++;}if (a>0)b[a] = ".";if (a < 0)c1 = 0;if (k < 15){if (n == 1)a++;if (n == 2)a += oo;if (n == 3)a -= oo;if (n == 4)a--;b[a] = ".";}}b[oo * 2] = "#";for (int x = 0; x < oo*ii; x++){if (b[x] == "#")b[x] = "";if (b[x] == " ")b[x] = "#";if (x>oo * 2 || x<oo)if (b[x] == ".")b[x] = " ";}for (int x = 0; x < oo*ii; x++){if (b[x] == ".")SetConsoleTextAttribute(hConsole, (WORD)((0 << 4) | 14));std::cout << b[x];if (x % oo == oo - 1)std::cout << std::endl;SetConsoleTextAttribute(hConsole, (WORD)((0 << 4) | 11));}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 < ii; i++)for (int j = 0; j < oo; 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;}В общем вот код, вставил в main.cpp генерацию и вывод лабиринта в консоль. Собственно, как исправить, вот эту часть кода , которая отвечает за отрисовку карты, чтобы вывести лабиринт?
C++123456for (int i = 0; i < ii; i++)for (int j = 0; j < oo; 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));Старую карту в коде оставил пока.
C++123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149#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;} -
АвторСообщения
Просмотр 2 сообщений - с 1 по 2 (из 2 всего)