1

I'm trying implement a doodle jump video game. So far my program works well except the fact that platform are being generated rapidly rather than slowly being incremented as the player proceeds upwards. I'm using the cinder framework to implement it and using the poScene cinderblock to help me with the game flow and animation. The doodle character and each platform is a single jpg.

From my understanding, I think the platform is being generated every time update() is being called, which occurs at every frame. I tried using "std::chrono_duration_cast" to get time in ticks to cause a delay in the update function being called, but it doesn't seem to work.

I also tried calling the manipulatePlatform in Setup() but if I do that, the image of platform is not being generated.

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
halfer
  • 19,824
  • 17
  • 99
  • 186

0 Answers0