SFML и C++ Уроки \ Разработка игр › Форумы › SFML Graphics › Персонаж не подходит вплотную к верхней и правой стенкам карты. Помогите!
В этой теме 2 ответа, 3 участника, последнее обновление BunDem 6 года/лет, 4 мес. назад.
Просмотр 3 сообщений - с 1 по 3 (из 3 всего)
-
АвторСообщения
-
C++123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249#include <SFML/Graphics.hpp>#include <iostream>#include "map.h"#include "view.h"using namespace sf;//##############################################################################################################class Player{private:float x, y;public:float w, h, dx, dy, speed;int dir; // Направление движенияString File;Image image;Texture texture;Sprite sprite;Player(String F, float X, float Y, float W, float H){dx = 0;dy = 0;speed = 0;dir = 0;File = F;w = W;h = H;image.loadFromFile(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;}sprite.setPosition(x, y);x += dx * time;y += dy * time;speed = 0;interactionWithMap();}void interactionWithMap(){for(int counter_i = y/32; counter_i < (y + h)/32; counter_i ++)for (int counter_j = x/32; counter_j < (x + w)/32; counter_j ++){if(TileMap[counter_i][counter_j] == '0'){if(dy > 0) y = counter_i * 32 - h;if(dy < 0) y = counter_i * 32 + 32;if(dx > 0) x = counter_j * 32 - w;if(dx < 0) x = counter_j * 32 + 32;}}}float GetPlayerCoordinateX()//этим методом будем забирать координату Х{return x;}float GetPlayerCoordinateY()//этим методом будем забирать координату Y{return y;}};//##############################################################################################################int main(){RenderWindow window(VideoMode(640, 480), "Game");view.reset(FloatRect(0, 0, 640, 480));Clock clock;float currentFrame = 0;Player p("hero.png", 250, 250, 48.0, 48.0);Image map_image;map_image.loadFromFile("map.png");Texture map1;map1.loadFromImage(map_image);Sprite s_map;s_map.setTexture(map1);while (window.isOpen()){float time = clock.getElapsedTime().asMicroseconds();clock.restart();time = time / 800;Event event;while (window.pollEvent(event)){if (event.type == 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 > 3) currentFrame -= 3;p.sprite.setTextureRect(IntRect(96 * int(currentFrame), 96, 96, 96));}if (Keyboard::isKeyPressed(Keyboard::Right)||Keyboard::isKeyPressed(Keyboard::D)){p.dir = 0; p.speed = 0.1;currentFrame += 0.005*time;if(currentFrame > 3) currentFrame -= 3;p.sprite.setTextureRect(IntRect(96 * int(currentFrame), 192, 96, 96));}if (Keyboard::isKeyPressed(Keyboard::Up)||Keyboard::isKeyPressed(Keyboard::W)){p.dir = 3; p.speed = 0.1;currentFrame += 0.005*time;if(currentFrame > 3) currentFrame -= 3;p.sprite.setTextureRect(IntRect(96 * int(currentFrame), 288, 96, 96));}if (Keyboard::isKeyPressed(Keyboard::Down)||Keyboard::isKeyPressed(Keyboard::S)){p.dir = 2; p.speed = 0.1;currentFrame += 0.005*time;if (currentFrame > 3) currentFrame -= 3;p.sprite.setTextureRect(IntRect(96 * int(currentFrame), 0, 96, 96));}GetPlayerCoordinateForView(p.GetPlayerCoordinateX(), p.GetPlayerCoordinateY());p.update(time);window.setView(view);window.clear();//##############################################################################################################for(int counter_i = 0; counter_i < HEIGHT_MAP; counter_i ++)for (int counter_j = 0; counter_j < WIDTH_MAP; counter_j ++){if(TileMap[counter_i][counter_j] == ' ') s_map.setTextureRect(IntRect(0, 0, 32, 32));if(TileMap[counter_i][counter_j] == 's') s_map.setTextureRect(IntRect(32, 0, 32, 32));if(TileMap[counter_i][counter_j] == '0') s_map.setTextureRect(IntRect(64, 0, 32, 32));s_map.setPosition(counter_j * 32, counter_i * 32);window.draw(s_map);}window.draw(p.sprite);window.display();}return 0;}// ФАйл map.h#include <SFML/Graphics.hpp>const int HEIGHT_MAP = 25;//размер карты высотаconst int WIDTH_MAP = 40;//размер карты ширинаsf::String TileMap[HEIGHT_MAP] = {"0000000000000000000000000000000000000000","0 0","0 s 0","0 0","0 0","0 0","0 0","0 0","0 0","0 0","0 0","0 0","0 0","0 0","0 0","0 0","0 0","0 0","0 0","0 0","0 0","0 0","0 0","0 0","0000000000000000000000000000000000000000",};//Файл Fiew.h#include <SFML/Graphics.hpp>using namespace sf;sf::View view;void GetPlayerCoordinateForView(float x, float y){float tempX=x, tempY=y;if(x < 320) tempX = 320;if(x > 960) tempX = 960;if(y < 240) tempY = 240;if(y > 554) tempY = 554;view.setCenter(tempX, tempY);}
Обрежь картинку спрайта ровно или задай координаты так, чтоб не было пустых мест вокруг нее
он “подходит” вплотную, просто в уроке текстура обрезается с невидимым куском, который тоже есть в спрайте, как сказали выше, надо обрезать, либо взять текстуру без лишних краев, чтобы кадры анимации были ровными.
-
АвторСообщения
Просмотр 3 сообщений - с 1 по 3 (из 3 всего)
Для ответа в этой теме необходимо авторизоваться.