SFML и C++ Уроки \ Разработка игр › Форумы › SFML System, Window и другие вопросы › Подключение карты лабиринта к игре
В этой теме 8 ответов, 3 участника, последнее обновление Павел Букреев 8 года/лет, 10 мес. назад.
-
АвторСообщения
-
Привет. Хочу, чтобы карта имела вид случайно генерируемого лабиринта. Собственоо на шел рабочий код составления лабиринта
C++123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166#include <iostream>#include <math.h>#include <cmath>#include <string>#include <ctime>#include <windows.h>#include <stdio.h>using namespace std;int main(){HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleTextAttribute(hConsole, (WORD)((0 << 4) | 11));setlocale(0, "rus");srand(time(0));string b[1000],t="p";int a = 1, c[1000], c1 = 2, n, k = 0,ii=0,oo=0;while (ii % 2 == 0 || oo % 2 == 0 || ii < 5 || oo < 5 || (ii + 2)*(oo + 2) >= 1000 || oo>70){cout << "Введите нечетную высоту,ширену, рекомендовано 15 27." << endl << "Самая жесть 23 37: ";cin >> ii >> oo;if (ii < 5) cout << "Маленикая высота!!!" << endl;if (oo < 5) cout << "Маленикая ширина!!!" << endl;if ((ii + 2)*(oo + 2) >= 1000) cout << "Слишком большой лабиринт" << endl;if (oo>70)cout << "Слишком большая ширина" << endl;if (oo % 2 == 0 || ii % 2 == 0)cout << "Числа должны быть нечетными!!" << endl;}ii += 2;oo += 2;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;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] = " ";}system("CLS");for (int x = 0; x < oo*ii; x++){cout << b[x];if (x % oo == oo-1)cout << endl;}//Прохождениеint a1,a2,m[1000],j,h=0;c1 = 0;cout << "Хотите следить за прохождением лабиринта? ";while (h==0){cin >> t;if (t == "yes"){h = 1;j = 0;}if (t == "no"){h = 1;j = 1;}}k = j;for (int x = 0; x < oo; x++){for (int y = 0; y < oo*ii; y++)b[y] = b[y + 1];}for (int x = 0; x < oo;x++)if (b[x] == ".")a = x;for (int x = oo*(ii-3)-1; x < oo*(ii-2); x++)if (b[x] == " ")a1 = x;for (int x = 0; x < 1000; x++)c[x] = 0;b[a] = ".";while (a != a1){if (k == 0){system("CLS");for (int x = 0; x < oo*ii; x++){cout << b[x];if (x % oo == oo - 1)cout << endl;}Sleep(150);}n = 0;a2 = a;while (a2 == a){n++;k = j;if (n == 1 && b[a - oo] ==" "&&a - oo>0&&m[a-oo]!=666)a -= oo;if (n == 2 && b[a - 1] == " "&&m[a -1] != 666)a--;if (n == 3 && b[a + 1] == " "&&m[a +1] != 666)a++;if (n == 4 && b[a + oo] == " "&&m[a + oo] != 666)a += oo;if (n == 5){k = 1;n = 0;c1--;b[a] = " ";m[a] = 666;c1--;a = c[c1];c1++;}}b[a] = ".";c[c1] = a;c1++;}system("CLS");for (int x = 0; x < oo*ii; x++){if (b[x] == ".")SetConsoleTextAttribute(hConsole, (WORD)((0 << 4) | 14));cout << b[x];if (x % oo == oo - 1)cout << endl;SetConsoleTextAttribute(hConsole, (WORD)((0 << 4) | 11));}return 0;}В уроках мы заполняем карту по длине и ширине. Тут мы вводим длину и ширину сами (переменные li и oo). Собственно в файле main.cpp, в фрагменте, где мы отрисововаем карту. Переменные
C++1HEIGHT_MAPC++1WIDTH_MAPC++12for (int i = 0; i < HEIGHT_MAP; i++)for (int j = 0; j < WIDTH_MAP; j++)заменяю на переменные li и oo, на что висуал студио ругается, что таких переменных не знает. Можете посоветовать, как сделать, чтобы лабиринт выводился на карту?
Предоставь код проекта, в котором не получается генерировать карту.
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;}Это вроде код с урока, а где твой не рабочий код?
там не li , а ii. две И
Боже я слепой. 😀 Спасибо!
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));Старую карту в коде оставил пока.
TileMap тут уже не рисуется. Чтобы нарисовалось надо весь массив который у тебя есть – ii, oo , закинуть в String TileMap. или не рисовать TileMap
Либо проверять
if (b[i][j] == ‘#’) s_map.setTextureRect(IntRect(0, 0, 32, 32));
Другу скинул код, вывод в коде лабиринта в консоль делается так:
C++12345678for (int x = 0; x < oo*ii; x++){if (b[x] == ".")SetConsoleTextAttribute(hConsole, (WORD)((0 << 4) | 14));cout << b[x];if (x % oo == oo - 1)cout << endl;SetConsoleTextAttribute(hConsole, (WORD)((0 << 4) | 11));}будет что то вроде этого
C++12345678910int i = 0; int j = 0;for (int x = 0; x < oo*ii; x++){i++;if (b[x] == '.') s_map.setTextureRect(IntRect(0, 0, 32, 32));if (b[x] == '#') s_map.setTextureRect(IntRect(32, 0, 32, 32));if (x % oo == oo - 1) j++;s_map.setPosition(j * 32, i * 32);window.draw(s_map);} -
АвторСообщения
Для ответа в этой теме необходимо авторизоваться.