Myrnas Ng

@thanhsan_97

Viet Nam, Da nang

Institution: Da Nang

#include "Game.h" //Loi man hinh bi co lai //Loi game over int flag = 0; void Game::mainWindow(RenderWindow& window, World world) { while (window.isOpen()) { Event event; while (window.pollEvent(event)) { if (event.type == Event::Closed) window.close(); } if(event.type == Event::MouseButtonPressed) { if(event.mouseButton.button == Mouse::Left) { Vector2i mousePos = Mouse::getPosition(window); if(world.getStartButton().getGlobalBounds().contains(mousePos.x, mousePos.y)) { isMainWindow = false; gameLost = true; return; } } } window.clear(); window.draw(world.getMainWindowBackground()); window.draw(world.getTittle()); window.draw(world.getStartButton()); window.display(); } } void Game::run() { RenderWindow window(VideoMode(SCREEN_W, SCREEN_H), "Sonic Jump", Style::Close); window.setFramerateLimit(60); Image icon; icon.loadFromFile("img/sonic_icon.png"); window.setIcon(icon.getSize().x, icon.getSize().y, icon.getPixelsPtr()); gameLost = false; moveVelocity = 4, jumpForce = 15.0, trampolineForce = 40.0, rVelocity = 8; gravityForce = 0.5; highscore = 0; Sonic player(0, jumpForce); World world; world.loadImg(); world.setup(); Score score(highscore); score.loadFont(); score.setup(); player.loadImg(); player.setup(Vector2f(SCREEN_W / 2, SCREEN_H / 4 * -1.0)); Platforms platform; platform.loadImg(); platform.setup(); Sounds sound; sound.loadSound(); sound.setup(); Rocket rocket(0); rocket.loadImg(); rocket.setupMonster(); time = clock.getElapsedTime(); sf::Time t1 = sf::seconds(2.0); check = 0; isMainWindow = true; while(window.isOpen()) { time = clock.getElapsedTime(); if(time.asSeconds() >= t1.asSeconds()) { if(flag == 0) { if(rocket.setupRocket(world.getView())) { flag = 1; sound.warningSound(); } } } window.clear(); Event event; while(window.pollEvent(event)) { if(event.type == Event::Closed) { window.close(); } } if(isMainWindow) { while(1) { if(world.start(window)) { isMainWindow = false; break; } window.clear(); window.draw(world.getMainWindowBackground()); window.draw(world.getTittle()); window.draw(world.getStartButton()); window.display(); } } if(!gameLost && !isMainWindow) { if(flag == 1) { rocket.fall(rVelocity,world.getView()); if(rocket.checkReset(world.getView())) { flag = 0; time = clock.restart(); std::cout << time.asSeconds(); } } player.move(moveVelocity); player.fall(gravityForce); if(player.jump(gravityForce)) { world.updateView(window, player); } player.update(world.getView()); rocket.update(player.getSonic()); if(player.checkLose(window.getView())) { sound.loseSound(); gameLost = true; continue; } world.moveBackground(); platform.update(player.getSonic()); score.update(world.getView(), player.getHPosition()); for(auto& p: platform.getPlatforms()) { if(player.getBoundingBox().getGlobalBounds().intersects(p->getGlobalBounds()) && player.getIsFalling()) { player.setIsJumping(true); player.setIsFalling(false); sound.jumpSound(); } } //Player vs dambong if(player.getBoundingBox().getGlobalBounds().intersects(platform.getTrampoline().getGlobalBounds()) && player.getIsFalling()) { player.setVelocityUp(trampolineForce); player.setIsJumping(true); player.setIsFalling(false); platform.getTrampoline().setTexture(platform.trampolineJumpedTexture); sound.jumpSound(); } //Player vs monster if(player.getBoundingBoxOther().getGlobalBounds().intersects(platform.getMonster().getGlobalBounds())) { sound.loseSound(); gameLost = true; continue; } //player vs haha if(player.getBoundingBox().getGlobalBounds().intersects(platform.getBox().getGlobalBounds()) && player.getIsFalling()) { int ra = rand() % 100; if(ra % 2 == 0 && check == 0) { world.getBackgoundTexture().loadFromFile("img/background.png"); platform.platformsTexture.loadFromFile("img/platform2.png"); check = 1; } else if(check == 0) { world.getBackgoundTexture().loadFromFile("img/map2.png"); platform.platformsTexture.loadFromFile("img/platform.png"); check = 1; } } //player vs rocket if(player.getBoundingBoxOther().getGlobalBounds().intersects(rocket.getRocket().getGlobalBounds())) { sound.loseSound(); gameLost = true; continue; } } world.update(window); if(gameLost) { score.getScore().setPosition(window.getView().getCenter().x + SCREEN_W / 5, window.getView().getCenter().y + SCREEN_H / 5.5); score.getScore().setCharacterSize(40); score.getHighScore().setPosition(window.getView().getCenter().x + SCREEN_W / 5, window.getView().getCenter().y + SCREEN_H / 8); if(world.restart(window)) { //restart gameLost = false; player.setup(Vector2f(SCREEN_W / 2, SCREEN_H / 4 * -1.0)); player.setIsFalling(false); player.setIsJumping(true); world.setup(); score.setup(); world.setView(window.getDefaultView()); world.getView().move(0, SCREEN_H * -1.0); window.setView(world.getView()); platform.setup(); flag = 0; } } window.clear(); Render::draw(window,world.getBackgrounds(),player.getSonic(), platform.getPlatforms()); window.draw(score.getScore()); window.draw(platform.getTrampoline()); window.draw(platform.getBee()); window.draw(platform.getMonster()); window.draw(platform.getBox()); window.draw(platform.getBoundingBoxOther()); window.draw(player.getBoundingBoxOther()); if(flag == 1) { window.draw(rocket.getMark()); window.draw(rocket.getRocket()); } for( auto const& i : rocket.getMonsters()) { window.draw(*i); } if(gameLost){ window.draw(world.getGameOverBackground()); window.draw(world.getGameOverText()); window.draw(world.getRestartButton()); } window.display(); } } #include "Platforms.h" Platforms :: Platforms() { platformGap = 10; } void Platforms::loadImg() { platformsTexture.loadFromFile("img/platform.png"); trampolineTexture.loadFromFile("img/trampoline.png"); trampolineJumpedTexture.loadFromFile("img/trampoline1.png"); monsterTexture.loadFromFile("img/monster_2.png"); beeTexture.loadFromFile("img/monster_1.png"); boxTexture.loadFromFile("img/sonic_coin.png"); } void Platforms::setup() { hPlatformPosition = 0; int size = platforms.size(); for(int i = size - 1; i >= 0; i--) { delete platforms[i]; platforms.erase(platforms.begin() + i); } for (int i = 0; i < 20; i++) { Sprite *p = new Sprite; p->setTexture(platformsTexture); float x; while(x = rand() % (SCREEN_W - platForms.getTextureRect().width)) { if(x >= 0 && x < SCREEN_W - 100) { break; } } float y = rand() % (SCREEN_H - platForms.getTextureRect().height); p -> setPosition(x, y); hPlatformPosition = i * platformGap; platforms.push_back(p); } } void Platforms::createPlatforms(Sprite player) { platformGap = rand() % 20 + 50; if(player.getPosition().y - SCREEN_H < hPlatformPosition) { Sprite *p = new Sprite; float x; while(x = rand() % SCREEN_W ) { if(x >= 0 && x < SCREEN_W - 100) { break; } } p -> setPosition(x,hPlatformPosition - platformGap); hPlatformPosition = hPlatformPosition - platformGap; p -> setTexture(platformsTexture); platforms.push_back(p); //dambong if(trampoline.getPosition().y - SCREEN_H / 3 > player.getPosition().y) { trampoline.setTexture(trampolineTexture); trampoline.setPosition(p->getPosition().x + 5, p->getPosition().y - 20); } if(monster.getPosition().y - SCREEN_H / 3 > player.getPosition().y) { monster.setTexture(monsterTexture); monster.setPosition(p->getPosition().x + 50, p->getPosition().y - 30); boundingBoxOther.setPosition(monster.getPosition().x, monster.getPosition().y); boundingBoxOther.setSize(Vector2f(monster.getTextureRect().width, monster.getTextureRect().height)); boundingBoxOther.setFillColor(Color::Blue); } if(box.getPosition().y - SCREEN_H / 2 > player.getPosition().y) { box.setTexture(boxTexture); box.setPosition(p->getPosition().x + 60, p->getPosition().y - 30); } } } void Platforms::genMonto(View view) { float x = rand() % (SCREEN_W - platForms.getTextureRect().width); float y = rand() % (SCREEN_H - platForms.getTextureRect().height); bee.setTexture(beeTexture); bee.setPosition(x, view.getCenter().y - 50); } void Platforms::update(Sprite player) { for(int i = 0; i < platforms.size(); i++) { if(player.getPosition().y < platforms[i] -> getPosition().y - SCREEN_H / 2) { delete platforms[i]; platforms.erase(platforms.begin()+i); } } createPlatforms(player); }

Các lần nộp bài đã được ghi nhân
(Xem dạng tệp tin văn bản)

Problems

Danh sách các bài đã làm đạt yêu cầu:

TORCH

Danh sách các bài làm chưa đạt yêu cầu:

© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.