SFML и C++ Уроки \ Разработка игр › Форумы › Логика игр › Бомбы › Ответ в теме: Бомбы
Вот теперь работает всё для одной бомбы: Она летит до конца окна, и начинает все с начала, так же работает столкновением с персонажем(если столкновение,они оба замирают, потом сделаю взрыв), бомба- объект v класса Player, но не получается сделать, чтоб бомб было много, и они выводились с рандомной координатой x.
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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
class Player { // класс Игрока float x, y; public: float w, h, dx, dy, speed = 0; bool life; int dir = 0; String File; Image image; Texture texture; Sprite sprite; Player(String F, float X,float Y, float W, float H){ File = F; life = true; w = W; h = H; image.loadFromFile("images/" + File); texture.loadFromImage(image); sprite.setTexture(texture); x = X; y = Y; sprite.setTextureRect(IntRect(0, 0, w, h)); } FloatRect getRect(){ return FloatRect(x, y, 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); if( life == false){ speed = 0; } } void update2(float time, int gameTime, bool stoop) //функция время, работает бесконечно, давая движение бомбе. { if (stoop){ if (gameTime > 0.5){ dx = 0; dy = 0.1; y += dy*time; sprite.setPosition(x, y); if (y > 500){ y = 0; sprite.setPosition(x, y); } } } } float getplayercoordinateX(){ return x; } float getplayercoordinateY(){ return y; } }; int main() { srand(time(NULL)); RenderWindow window(sf::VideoMode(490, 500), "Lesson 8. kychka-pc.ru"); //Font font; //font.loadFromFile("CyrilicOld.ttf"); //Text text("", font, 20); //text.setColor(Color::Red); //text.setStyle(Text::Bold); Image map_image; map_image.loadFromFile("images/0009000.png");//карта Texture map;//текстура карты map.loadFromImage(map_image);//заряжаем текстуру картинкой Sprite s_map;//создаём спрайт для карты s_map.setTexture(map);//заливаем текстуру спрайтом float CurrentFrame = 0; Clock clock; Clock gameTimeClock; int gameTime = 0; bool stoop=true; Player p("sold2.png", 232, 440, 25.0, 33.0); float xx = 0 + rand() % 300; Player v("vrg.png", xx, 0, 23, 42); while (window.isOpen()) { float time = clock.getElapsedTime().asMicroseconds(); if (p.life) gameTime = gameTimeClock.getElapsedTime().asSeconds(); clock.restart(); time = time / 800; sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); } p.sprite.setScale(1.5, 1.5); v.sprite.setScale(1.2, 1.2); if (p.life) { if ((Keyboard::isKeyPressed(Keyboard::Left))) { p.dir = 1; p.speed = 0.2; if (p.getplayercoordinateX() < 0){ p.speed = 0; }//ЗАПРЕЩЕНО ВЛЕВО CurrentFrame += 0.3*time; if (CurrentFrame > 3) CurrentFrame -= 3; p.sprite.setTextureRect(IntRect(30 * int(CurrentFrame), 32, 30, 32)); } if ((Keyboard::isKeyPressed(Keyboard::Right))) { p.dir = 0; p.speed = 0.2; if (p.getplayercoordinateX() > 452){ p.speed = 0; } CurrentFrame += 0.3*time; if (CurrentFrame > 3) CurrentFrame -= 3; p.sprite.setTextureRect(IntRect(30 * int(CurrentFrame), 64, 31, 32)); } if ((Keyboard::isKeyPressed(Keyboard::Up))) { p.dir = 3; p.speed = 0.2; CurrentFrame += 0.3*time; if (CurrentFrame > 3) CurrentFrame -= 3; p.sprite.setTextureRect(IntRect(30 * int(CurrentFrame), 96, 31, 32)); } if ((Keyboard::isKeyPressed(Keyboard::Down))) { p.dir = 2; p.speed = 0.2;//направление вверх, см выше if (p.getplayercoordinateY() > 440){ p.speed = 0; } CurrentFrame += 0.3*time; if (CurrentFrame > 3) CurrentFrame -= 3; p.sprite.setTextureRect(IntRect(30 * int(CurrentFrame), 0, 31, 32)); } if (v.getRect().intersects(p.getRect())) { p.life = false; stoop = false; } } p.update(time);/ v.update2(time, gameTime, stoop); window.draw(v.sprite); for (int i = 0; i < HEIGHT_MAP; i++) for (int j = 0; j < WIDTH_MAP; j++) { if ((TileMap[i][j] == '0')) s_map.setTextureRect(IntRect(0, 0, 500, 500)); window.draw(p.sprite); window.draw(v.sprite); window.display(); } return 0; } |