Созданные ответы форума
Просмотр 3 сообщений - с 1 по 3 (из 3 всего)
-
АвторСообщения
-
картинка
Вложения:
You must be logged in to view attached files.скрин кинуть не могу так как во время выпадения за пределы экрана быстро телепортирую персонажа назад,
но от прохождений сквозь блоки в центре карты это не спасает
но если переключиться на другое окно будет это:
C++123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692#include <SFML/Graphics.hpp>#include <cstdlib>#include <iostream>#include <vector>#include <list>#include "bars.h"#include "windows.h"#include "map.h"#include "camera.h"#include "inventory.h"#include "load.h"using namespace std;using namespace sf;class Entity {public:float dx, dy;//направлениеfloat px, py;//положениеfloat speed;//скоростьint w, h;//габаритыunsigned int health;//здоровьеbool life, isMove, onGround;Texture texture;Sprite sprite;Entity(Image &image, float X, float Y, int W, int H) {px = X; py = Y;w = W; h = H;speed = 0; health = 100;dx = 0; dy = 0;life = true; onGround = false; isMove = false;texture.loadFromImage(image);sprite.setTexture(texture);sprite.setOrigin(0, 0);}FloatRect getRect() {return FloatRect(px, py, w, h);}virtual void update(float time) = 0;};inline void worldUpdate(float time) {float waitTimer = 0;waitTimer += time;char buff;if (waitTimer > 1) {waitTimer = 0;for (int i = 1; i < HEIGHT_MAP - 1; i++) {for (int j = 1; j < WIDTH_MAP - 1; j++) {switch (TileMap[i][j]){case 'g':if (TileMap[i + 1][j] == ' ' || TileMap[i + 1][j] == 'w') {buff = TileMap[i][j];TileMap[i][j] = TileMap[i + 1][j];TileMap[i + 1][j] = buff;}break;case 'w':if (TileMap[i + 1][j] == ' ') {buff = TileMap[i][j];TileMap[i][j] = TileMap[i + 1][j];TileMap[i + 1][j] = buff;}if (TileMap[i][j + 1] == ' ') {buff = TileMap[i][j];TileMap[i][j] = TileMap[i][j + 1];TileMap[i][j + 1] = buff;}if (TileMap[i][j - 1] == ' ') {buff = TileMap[i][j];TileMap[i][j] = TileMap[i][j - 1];TileMap[i][j - 1] = buff;}break;}}}}}class Player :public Entity {public:enum {left, right, up, down, jump, stay}state;//состояние игрокаbool running = false, inWater = false;float frame;Player(Image &image, float X, float Y, float W, float H) :Entity(image, X, Y, W, H) {//началоб конец, ширина высотаframe = 0;speed = 0;dx = 0;dy = 0;onGround = false;isMove = false;health = 100;w = W;h = H;px = X;py = Y;sprite.setTextureRect(IntRect(0, 0, w, h));}float getpcX() {return px;}float getpcY() {return py;}void control() {if (Keyboard::isKeyPressed(Keyboard::A)) {if (px > 0) {state = left;running = true;speed = 0.1;}}elseif (Keyboard::isKeyPressed(Keyboard::D)) {if (px+20 < WindowWidht) {state = right;running = true;speed = 0.1;}}elserunning = false;if ((Keyboard::isKeyPressed(Keyboard::Space)) && (onGround)) {if (py < WindowHeight) {state = jump;onGround = false;dy = -0.5;if (inWater)dy = -0.2;}}if (Keyboard::isKeyPressed(Keyboard::S)) {if (py > 0) {state = down;speed = 0.1;}}}void CheckCollision(float Dx, float Dy) {for (int i = py / 10; i < (py + h) / 10; i++)for (int j = px / 10; j < (px + w) / 10; j++) {if ((i < HEIGHT_MAP) && (i < WIDTH_MAP)) {switch (TileMap[i][j]) {case '0':case 'b':case 'g':case 'z':case 't':if (Dy > 0) {py = i * 10 - h;dy = 0;onGround = true;}if (Dy < 0) {py = i * 10 + 10;dy = 0;onGround = false;}if (Dx > 0) {px = j * 10 - w;}if (Dx < 0) {px = j * 10 + 10;}break;case 'w':inWater = true;break;case ' '://inWater = false;//onGround = false;break;}//if (TileMap[i][j] == '0' || TileMap[i][j] == 'b' || TileMap[i][j] == 'g') {}}}}void update(float time) {control();if (dy == 0)onGround = true;switch (state) {case right:dx = speed;if (inWater)dx = speed / 3;if (running) {frame += 0.009*time;if (frame > 7)frame = 0;sprite.setOrigin(0, 0);sprite.setTextureRect(IntRect(w * int(frame), 0, w, h));sprite.setScale(1, 1);}break;case left:dx = -speed;if (inWater)dx = (-speed) / 3;if (running) {frame += 0.009*time;if (frame > 7)frame = 0;sprite.setOrigin(w, 0);sprite.setTextureRect(IntRect(w * int(frame), 0, w, h));sprite.setScale(-1, 1);}break;case jump://onGround = false;break;}if (!onGround) {frame += 0.009*time;if (frame > 7)frame = 7;sprite.setTextureRect(IntRect(50 * int(frame), 80, 50, h));}if (!running) {frame = 0;sprite.setTextureRect(IntRect(387, 0, w, h));}px += dx*time;CheckCollision(dx, 0);py += dy*time;CheckCollision(0, dy);if (!isMove)speed = 0;dy = dy + 0.0015*time;if (inWater)dy = dy + 0.0001*time;sprite.setPosition(px, py);}};class Enemy :public Entity {public:float frame;bool inWater;//walking;enum {left, right}state;float moveTimer = 0;Enemy(Image &image, float X, float Y, float W, float H) :Entity(image, X, Y, W, H) {frame = 0;speed = 0.07;dx = 0;dy = 0;onGround = false;inWater = false;health = 100;w = W;h = H;px = X;py = Y;float moveTimer = 0;sprite.setTextureRect(IntRect(0, 0, w, h));state = right;}void CheckCollision(float Dx, float Dy) {if ((px > WindowWidht - w) || (px <= 0) || (py>WindowHeight - h) || (py <= 9)) {px = 100;py = 100;}for (int i = py / 10; i < (py + h) / 10; i++)for (int j = px / 10; j < (px + w) / 10; j++) {if ((i < HEIGHT_MAP) && (i < WIDTH_MAP)) {switch (TileMap[i][j]) {case '0':case 'b':case 'g':case 'z':case 't':if (Dy > 0) {py = i * 10 - h;dy = 0;onGround = true;}if (Dy < 0) {py = i * 10 + 10;dy = 0;onGround = false;}if (Dx > 0) {px = j * 10 - w;state = left;moveTimer = 0;}if (Dx < 0) {px = j * 10 + 10;state = right;moveTimer = 0;}break;case 'w':inWater = true;break;case ' ':inWater = false;onGround = false;break;}}}}void update(float time) {if ((px > WindowWidht - w) || (px <= 0) || (py>WindowHeight - h) || (py <= 9)) {px = 100;py = 100;}px += dx*time;CheckCollision(dx, 0);py += dy*time;CheckCollision(0, dy);switch (state) {case right:if (inWater)dx = speed / 3;else {speed = 0.07;dx = speed;}//animationframe += 0.009*time;if (frame > 7)frame = 0;sprite.setOrigin(0, 0);sprite.setTextureRect(IntRect(w * int(frame), 0, w, h));sprite.setScale(1, 1);break;case left:if (inWater)dx = (-speed) / 3;else {speed = 0.07;dx = -speed;}//animationframe += 0.009*time;if (frame > 7)frame = 0;sprite.setOrigin(w, 0);sprite.setTextureRect(IntRect(w * int(frame), 0, w, h));sprite.setScale(-1, 1);break;}dy = dy + 0.0015*time;if (inWater)dy = dy + 0.0001*time;sprite.setPosition(px, py);if ((px > WindowWidht - w) || (px <= 0) || (py>WindowHeight - h) || (py <= 9)) {px = 100;py = 100;}}void rozvorot(float Px) {if (Px > px)state = right;if (Px < px)state = left;}};int main(){string strotv;cout << "Choose character (1/2): ";cin >> strotv;heroImage.loadFromFile("images/HeroAnim" + strotv + ".png");heroImage.createMaskFromColor(Color(160, 192, 64));RenderWindow window(VideoMode(WindowWidht, WindowHeight ), "Hallo, SFML or I am start to learn c++ SFML");//render windowview.reset(sf::FloatRect(0, 0, WindowWidht/1.5, WindowHeight/1.5));window.setKeyRepeatEnabled(false);loadImages();//создание обьектовPlayer p(heroImage, 0, 0, 40, 69);Inventory inventory1;HBar hbar;list<Enemy*> enemies;list<Enemy*>::iterator enIt;short int swordWidth, swordHeight;bool ifAttac = false;bool invShow = false;//открыт ли инвентарьbool chosen = false;//выбран ли предметchar invBuff = ' ';//буфер инвентаряfloat timerInv = 0,//кулдаун клацанья мышиtabTimer = 0,teleportTimer = 0,spawnTimer=0,attacTimer=0,hurtTimer =0;Sprite movSprite(cellTexture);movSprite.setTextureRect(IntRect(10, 0, 10, 10));Clock clock;while (window.isOpen()){float time = clock.getElapsedTime().asMicroseconds(); //дать прошедшее время в микросекундахclock.restart(); //перезагружает времяtime = time / 900;Event event;while (window.pollEvent(event)) {if (event.type == Event::Closed)window.close();}Vector2i pixelPos = Mouse::getPosition(window);//забираем коорд курсораVector2f pos = window.mapPixelToCoords(pixelPos);//переводим их в игровые (уходим от коорд окна)Vector2f center = window.getView().getCenter();Vector2f size = window.getView().getSize();spawnTimer += time;if (spawnTimer > 500)spawnTimer = 500;if (Keyboard::isKeyPressed(Keyboard::E))if (spawnTimer == 500) {spawnTimer = 0;enemies.push_back(new Enemy(enemyImage, pos.x, pos.y, 34, 60));}if (!invShow) {if (Mouse::isButtonPressed(Mouse::Left)) {TileMap[int(pos.y / 10)][int(pos.x / 10)] = ' ';}if (Mouse::isButtonPressed(Mouse::Right)) {TileMap[int(pos.y / 10)][int(pos.x / 10)] = '0';}if (Keyboard::isKeyPressed(Keyboard::G)) {TileMap[int(pos.y / 10)][int(pos.x / 10)] = 'g';}if (Keyboard::isKeyPressed(Keyboard::V)) {TileMap[int(pos.y / 10)][int(pos.x / 10)] = 'w';}if (Keyboard::isKeyPressed(Keyboard::Z)) {TileMap[int(pos.y / 10)][int(pos.x / 10)] = 'z';}if (Keyboard::isKeyPressed(Keyboard::T)) {TileMap[int(pos.y / 10)][int(pos.x / 10)] = 't';}}if (invShow) {timerInv += time;if (timerInv > 500)timerInv = 500;if ((pixelPos.x < 283) && (pixelPos.y < 191) && (pixelPos.x > 46) && (pixelPos.y > 46)) {if (!chosen) {if (timerInv > 60) {if (Mouse::isButtonPressed(Mouse::Left)) {//if ((event.mouseButton.button == Mouse::Left) && (event.MouseButtonReleased))unsigned int tempCell_x = int((pixelPos.x - 46) / 30);unsigned int tempCell_y = int((pixelPos.y - 46) / 30);chosen = true;timerInv = 0;invBuff = inventoryArray[tempCell_y][tempCell_x];inventoryArray[tempCell_y][tempCell_x] = ' ';//movSprite.setTextureRect(IntRect(10, 0, 10, 10));}}}if (chosen) {switch (invBuff) {case 'k': movSprite.setTextureRect(IntRect(10, 10, 10, 10)); break;case 'g': movSprite.setTextureRect(IntRect(0, 10, 10, 10)); break;case 'w': movSprite.setTextureRect(IntRect(0, 0, 10, 10)); break;case 'z': movSprite.setTextureRect(IntRect(20, 0, 10, 10)); break;case 't': movSprite.setTextureRect(IntRect(30, 0, 10, 10)); break;default: movSprite.setTextureRect(IntRect(10, 0, 10, 10));}movSprite.setPosition(pos.x, pos.y);if (timerInv > 60) {if (Mouse::isButtonPressed(Mouse::Left)) {unsigned int tempCell_x = int((pixelPos.x - 46) / 30);unsigned int tempCell_y = int((pixelPos.y - 46) / 30);chosen = false;timerInv = 0;inventoryArray[tempCell_y][tempCell_x] = invBuff;invBuff = ' ';}}}}}teleportTimer += time;if (teleportTimer > 5000)teleportTimer = 5000;if (teleportTimer == 5000)if (Keyboard::isKeyPressed(Keyboard::M)) {p.px = pos.x;p.py = pos.y;teleportTimer = 0;}tabTimer += time;if (tabTimer > 500)tabTimer = 500;if(tabTimer > 100)if (Keyboard::isKeyPressed(Keyboard::Tab)) {tabTimer = 0;invShow = !invShow;}///////////респавн если выпал/////////////if ((p.px > WindowWidht - p.w) || (p.px<=0) || (p.py>WindowHeight-p.h) || (p.py <= 9)) {p.px = 100;p.py = 100;}hurtTimer += time;if (hurtTimer > 3000)hurtTimer = 3000;for (enIt = enemies.begin(); enIt != enemies.end(); enIt++)//проходимся по эл-там списка{if ((*enIt)->getRect().intersects(p.getRect()))//если прямоугольник спрайта объекта пересекается с игроком{if (hurtTimer == 3000) {if (p.health >= 20)p.health -= 20;hurtTimer = 0;cout << p.health << endl;}}}worldUpdate(time);getPlayerCoordinateForView(p.getpcX(), p.getpcY(), WindowWidht, WindowHeight);window.setView(view);window.clear();window.draw(BackGroundg);for (enIt = enemies.begin(); enIt != enemies.end(); enIt++) {(*enIt)->update(time);(*enIt)->rozvorot(p.px);window.draw((*enIt)->sprite);}p.update(time);window.draw(p.sprite);attacTimer += time/2;if (attacTimer > 300)attacTimer = 300;swordHeight = 16;swordSprite.setPosition(p.px + p.w / 2, p.py + p.h / 2);switch (p.state) {case 0://leftswordSprite.setScale(-1, 1);swordWidth = -43;//swordSprite.setOrigin(43, 0);break;case 1://rightswordSprite.setScale(1, 1);swordWidth = 43;//swordSprite.setOrigin(0, 0);break;}if(attacTimer==300)if (Keyboard::isKeyPressed(Keyboard::Q)) {attacTimer = 0;for (enIt = enemies.begin(); enIt != enemies.end(); enIt++)if ((*enIt)->getRect().intersects(FloatRect(swordSprite.getPosition().x, swordSprite.getPosition().y, swordWidth, swordHeight))) {(*enIt)->health -= 20;(*enIt)->dy = -0.3;if((*enIt)->dx>0)(*enIt)->dx = 0.7;if ((*enIt)->dx<0)(*enIt)->dx = -0.7;if((*enIt)->health <= 0)enIt = enemies.erase(enIt);//убиваем врагаbreak;}window.draw(swordSprite);}for (int i = 0; i < HEIGHT_MAP; i++) {for (int j = 0; j < WIDTH_MAP; j++) {if (TileMap[i][j] == ' ' || TileMap[i][j] == 'b') s_map.setTextureRect(IntRect(10, 0, 10, 10)); //если встретили символ пробел, то рисуем 1й квадратикif (TileMap[i][j] == 's') s_map.setTextureRect(IntRect(0, 0, 10, 10));//если встретили символ s, то рисуем 2й квадратикif ((TileMap[i][j] == '0')) s_map.setTextureRect(IntRect(10, 10, 10, 10));//если встретили символ 0, то рисуем 3й квадратикif ((TileMap[i][j] == 'g')) s_map.setTextureRect(IntRect(0, 10, 10, 10));if ((TileMap[i][j] == 'w')) s_map.setTextureRect(IntRect(0, 0, 10, 10));if ((TileMap[i][j] == 'z')) s_map.setTextureRect(IntRect(20, 0, 10, 10));if ((TileMap[i][j] == 't')) s_map.setTextureRect(IntRect(30, 0, 10, 10));s_map.setPosition(j * 10, i * 10);//по сути раскидывает квадратики, превращая в карту. то есть задает каждому из них позицию. если убрать, то вся карта нарисуется в одном квадрате 32*32 и мы увидим один квадратwindow.draw(s_map);//рисуем квадратики на экран}}if (invShow) {inventory1.draw(window);for (int i = 0; i < invH; i++)for (int j = 0; j < invW; j++) {switch (inventoryArray[i][j]) {case 'k': cellSprite.setTextureRect(IntRect(10, 10, 10, 10)); break;case 'g': cellSprite.setTextureRect(IntRect(0, 10, 10, 10)); break;case 'w': cellSprite.setTextureRect(IntRect(0, 0, 10, 10)); break;case 'z': cellSprite.setTextureRect(IntRect(20, 0, 10, 10)); break;case 't': cellSprite.setTextureRect(IntRect(30, 0, 10, 10)); break;default: cellSprite.setTextureRect(IntRect(10, 0, 10, 10));}cellSprite.setOrigin(5, 5);cellSprite.setPosition((inventory1.s.getPosition().x + 30) + j * 20 , (inventory1.s.getPosition().y+ 30 + i * 20 ));window.draw(cellSprite);}window.draw(movSprite);}hbar.draw(window, p.health);window.display();}return 0;}Вложения:
You must be logged in to view attached files.“Я не понял суть вопроса”
суть вопроса:
если пропадают кадры – то пропадает кадр с проверкой на прохождение сквозь твердые объекты
тогда почему не пропадает кадр с силой притяжения и перемещения вниз?
Как сделать так чтобы при пропадании кадров тогда и не работала сила притяжения?
или как избежать этого?
-
АвторСообщения
Просмотр 3 сообщений - с 1 по 3 (из 3 всего)