Я пытаюсь реализовать видеоигру с каракули прыжком. Пока что моя программа работает хорошо, за исключением того факта, что платформа генерируется быстрее, а не постепенно увеличивается по мере того, как игрок движется вверх. Я использую Cinder Framework для его реализации и использую CinderBlock PoScene, чтобы помочь мне с ходом игры и анимацией. Каракули и каждая платформа - это один jpg.
Насколько я понимаю, платформа генерируется каждый раз, когда вызывается update (), который происходит в каждом кадре. Я попытался использовать "std :: chrono_duration_cast", чтобы получить время в тиках, чтобы вызвать задержку вызова функции обновления, но, похоже, она не работает.
Я также пытался вызвать манипулировать платформой в Setup (), но если я это сделаю, образ платформы не генерируется.
using namespace cinder;
using cinder::app::KeyEvent;
namespace myapp {
using namespace po::scene;
using cinder::app::KeyEvent;
using po::scene::ImageView;
using po::scene::View;
MyApp::MyApp() {
state_ = GameState::kPlaying;
speed_ = 20;
}
void MyApp::setup() {
SoundSetUp(background_sound_,"BackgroundMusic.wav");
mViewController = ViewController::create();
my_scene = Scene::create(mViewController);
//setUpIntro();
}
void MyApp::DrawBackground() {
cinder::gl::Texture2dRef texture2D = cinder::gl::Texture::create(
cinder::loadImage(MyApp::loadAsset("background.jpg")));
cinder::gl::draw(texture2D, getWindowBounds());
}
void MyApp::SetUpCharacter() {
my_scene->getRootViewController()->getView()->addSubview(character);
}
void MyApp::SetUpPlatform() {
my_scene->getRootViewController()->getView()->addSubview(platform);
}
void MyApp::ManipulatePlatform() {
cinder::gl::Texture2dRef texture_platform = cinder::gl::Texture::create(
cinder::loadImage(MyApp::loadAsset("platform.jpg")));
platform = po::scene::ImageView::create(texture_platform);
my_scene->getRootViewController()->getView()->addSubview(platform);
point array[20];
for (int i = 0; i < 10; i++) {
array[i].x = cinder::Rand::randInt() % 400;
array[i].y = cinder::Rand::randInt() % 533;
}
if (y < h) {
for (int i = 0; i < 10; i++) {
y = h;
array[i].y = array[i].y - change_in_height;
if (array[i].y > 533) {
array[i].y = 0;
array[i].x = cinder::Rand::randInt() % 400;
}
}
}
auto end = std::chrono::steady_clock::now();
for (int i = 0; i < 10; i++) {
platform->setPosition(array[i].x, array[i].y);
}
}
void MyApp::SimulateGame() {
cinder::gl::Texture2dRef texture_character = cinder::gl::Texture::create(
cinder::loadImage(MyApp::loadAsset("doodle.jpg")));
character = po::scene::ImageView::create(texture_character);
my_scene->getRootViewController()->getView()->addSubview(character);
point array[20];
change_in_height = change_in_height + 0.2;
y = y + change_in_height;
if (y > 500) {
change_in_height = change_in_height - 10;
}
for (int i = 0; i < 10; i++) {
if ((x + 50 > array[i].x) && (x + 20 < array[i].x + 68)
&& (y + 70 > array[i].y) && (y + 70 < array[i].y + 14) && (change_in_height > 0)) {
change_in_height = -10;
}
}
character->setPosition(x, y);
}
void MyApp::update() {
auto start = std::chrono::steady_clock::now();
ManipulatePlatform();
SimulateGame();
auto end = std::chrono::steady_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::seconds>(end - start).count();
while (duration < 100) {
std::cout << duration++;
}
my_scene->update();
}
void MyApp::draw() {
my_scene->getRootViewController()->getView()->removeAllSubviews();
DrawBackground();
SetUpPlatform();
SetUpCharacter();
my_scene->draw();
}
void MyApp::keyDown(KeyEvent event) {
switch (event.getCode()) {
case KeyEvent::KEY_RIGHT:
x = x + 50;
break;
case KeyEvent::KEY_LEFT:
x = x - 50;
}
}
void MyApp::ResetGame() {
my_scene->getRootViewController()->getView()->removeAllSubviews();
}
void MyApp::SoundSetUp(cinder::audio::VoiceRef &audio_object, std::string file_path) {
cinder::audio::SourceFileRef sourceFile = cinder::audio::load(MyApp::loadAsset(file_path));
audio_object = cinder::audio::Voice::create(sourceFile);
audio_object->start();
}
void MyApp::setUpIntro() {
intro_background_doodle_jump = cinder::gl::Texture::create(
cinder::loadImage(MyApp::loadAsset("intro.jpg")));
intro_background_doodle_jump->setCleanBounds(cinder::Area(0, 0 , getWindowWidth(), getWindowHeight()));
}
}// namespace myapp