Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)  (Read 69512 times)

0 Members and 1 Guest are viewing this topic.

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew
« Reply #30 on: January 11, 2014, 12:42:03 pm »
oops :D

well i am working with visual studio 2013 :P

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew
« Reply #31 on: January 14, 2014, 09:43:02 pm »
added Coop mode:

Quote
cl13:
- Coop added
-> second player is controlled by mouse (left mouse button for shoot)
-> players share their life until one is dead
-> a dead player will be revieved when the other gains 100 life
-> only first player can switch weapons
-> both share the same weapon at a time

release is up to date on the main post as always ;)
i did not test any bugs. if you find something please tell me

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew
« Reply #32 on: January 17, 2014, 09:42:08 pm »
a quick info:
i think it may be important to mention some things
  • http://www.microsoft.com/en-us/download/details.aspx?id=40784  if you cannot start the game because dll files are missing (now the 2013 link)
  • if you want to help me with the code posted on github, please write it here, else i maybe wont see it
  • the second boss can only be hit by the third weapon, try to change weapon or fly from left to right (or the other way around) to escape the apples
  • the coop can be buggy at the moment, because i did not have the time to test it
  • i am currently working on adding the boss for the cows, which will be a machine
  • after this boss i will implement buffs, which appear after a monkey is killed (picked random)

as you can see the main focus is on expanding the game. after i have my basic enemy types i plan to make a multiplayer mode (LAN and / or Web) depending on how good i manage to get that going.
For the android users here: if there is an ending in sight an app will be programmed to get this on android

well, thats it for the moment ;)

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #33 on: January 18, 2014, 05:50:13 pm »
Nice improvements  :)

Some ideas:
  • Pause
  • Something like a progress bar to show the reload time. So you know when you can shoot again, especially for the thrid weapon
  • Maybe stop the normal enemies to spawn during a bossfight (on crazy its crazy  ;) )
  • Please show the close button in the window. It's annoying to press escape and navigate through the menu just to close the game


AlexAUT

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #34 on: January 19, 2014, 01:08:40 am »
thank xou!

i wanted to do a pause option but i do not konw ho i can implement it :(
maybe you can follow my code (i will help you if you want)
reload time for the third weapon is already implemented but not correctly shown
normal enemies spawning durning boss spawn... i will do this
for closing the game, i will implement a button as you wish ;)

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #35 on: January 19, 2014, 01:26:40 am »
i wanted to do a pause option but i do not konw ho i can implement it :(

I would really like to dig through the whole code and help you, but sadly currently I have no time for it  :'(

But I had a look at the main parts and there are 2 options for you.

  • You could create a new state for the pause menu. The pause screen then changes, after a user input, the current state to menu or to game back again.
  • Other option (easier and imo better) (thats how I implemented it in kroniax) you have a bool in the game class like mPaused, and if this is true, do not update all the game objects (player, enemies etc) so they do not move but you draw them with a overlay on top of it (Here you can see an example in C# http://msdn.microsoft.com/en-us/library/bb195026.aspx you don't need to track the pause key :)

I hope I described both ways well enough to follow

Maybe I will have time in about 2weeks during my military service to have a closer look


i will do this for closing the game, i will implement a button as you wish ;)

Why not set the window style to sf::Style::Close or sf::Style::Default and then respond to the sf::Event::Closed event (Would be easier than a button made by yourself  :) )?

AlexAUT
« Last Edit: January 19, 2014, 01:32:08 am by AlexAUT »

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #36 on: January 19, 2014, 10:22:54 am »
i understand what you mean with the pause. i try it ;)
and i can also change the window style :P

thank you for your comments

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #37 on: January 19, 2014, 05:36:16 pm »
okay, i implemented a pause option. but there is a problem i need help with^^

all the movements are bound to elapsedTime = clock.restart().asMilliseconds() in their speed or spawn.
if i pause it stays like this, but if i resume the game, the timer gets very high a moment and that is not wanted, because the game "jumps" forward. my theory is that the clock simply doesn't get restarted. so the longer you stay in pause the higher gets the elapsedTime after you resume. here is the code:

//Game.cpp

#include "Game.h"

HighscoreManager Game::getHighScore()
{
        return highscore;
        elapsedTime = 0;
}

int Game::Run(sf::RenderWindow &window)
{
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        //++++++++++++++++++++++++++++++++-PREGAME STUFF-+++++++++++++++++++++++++++++++++++++++                                                                               
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        bool Running = true;
        paused = false;

        //sound & music
        IOsound iosound;
        iosound.ReadSoundSettings(volume);
        IngameSound sound;
        sound.LoadSoundBuffer();
        sound.setBuffer(volume);
        IngameMusic music;
        music.LoadMusic(volume);
        music.PlayMusic("ingamesong");

        //background and HUD
        MovableBackground bg("graphics//core//background.jpg", window);
        Background hud("graphics//core//hud.png");
        bgSpeed = 0.3;
        bgDirection = true;
        Healthbar hudHealth;
        WeaponManager weapon;

        //basic stuff
        IOtwoPlayer secPlayer;
        Player player1("graphics//player.png");
        Player2 player2("graphics//player2.png");

        player1health = player1.getHealth();
        player2health = player2.getHealth();

        //what happens if sec player is active
        if (!secPlayer.ReadSettings())
        {
                player2.active = false;
               
        }

        Game::highscore;
        UpdateManager updateMng;
        RenderManager renderMng;
        filename = "screenshot.png";

        //unlock pew
        boss1Dead = false;
        gotPew = false;
        pewOnCooldown = false;
        GUIcircleShape pewCD;

        //counter + random-x-spawn
        bulletTimeCount     = 0;
        enemyTimeCount      = 0;
        enemyFormationCount = 0;
        shitCount               = 0;
        healthDropCount     = 0;
        cowTimeCount        = 0;
        randomX                     = 0;
        showLvUp                    = 0;
        damageChill                 = 0;
        boss1WeaponCount    = 0;
        boss2WeaponCount    = 0;

        //health, points & alive
        Health health("graphics//health.png");
        health.setPosition(0, 12.5);

        Text pHealthS(18);
        pHealthS.setPosition(23, 12.5);
        pHealthS.setColor(sf::Color::Green);

        points = 0;
        Text pointS(25);
        pointS.setPosition(650, 8);
        pointS.setColor(sf::Color::Black);
        std::stringstream pointStream;

        Text lvUp("NEW WEAPON", 60);
        lvUp.setColor(sf::Color::Yellow);
        lvUp.setOrigin(lvUp.getGlobalBounds().width / 2, lvUp.getGlobalBounds().height / 2);
        lvUp.setPosition(window.getSize().x / 2, window.getSize().y / 2);

        Text gameOver("GAME OVER", 56);
        gameOver.setPosition(210, 250);
        gameOver.setColor(sf::Color::White);


        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        //+++++++++++++++++++++++-UPDATE STUFF & INITIALIZE-++++++++++++++++++++++++++++++++++++                                                                               
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        while (Running)
        {
                //handle events
                while (window.pollEvent(event))
                {
                        //close
                        if (event.type == sf::Event::Closed)
                        {
                                return (-1);
                        }
                        //menu
                        if (event.type == sf::Event::KeyPressed)
                        {
                                if (event.key.code == sf::Keyboard::Escape)
                                {
                                        //reset stuff (bugfix)
                                        Game::ClearStuff();

                                        return (0);
                                }
                                if (event.key.code == sf::Keyboard::P)
                                {
                                        if (!paused)
                                        {
                                                Game::StartPause();
                                        }
                                        else
                                                Game::EndPause();
                                }
                        }
                }

                std::cout << elapsedTime << std::endl;

                if (!paused)
                {
                        //initialize counts & background movement
                        elapsedTime = clock.restart().asMilliseconds();//<-SEE HERE
                        enemyTimeCount += elapsedTime;
                        bulletTimeCount += elapsedTime;
                        p2bulletTimeCount += elapsedTime;
                        shitCount += elapsedTime;
                        boss1WeaponCount += elapsedTime;
                        boss2WeaponCount += elapsedTime;
                        healthDropCount += elapsedTime;
                        cowTimeCount += elapsedTime;
                        damageChill += elapsedTime;
                        enemyFormationCount += elapsedTime;
                        bg.Update(window, elapsedTime, bgSpeed, bgDirection);

                        //sync health
                        player1health = player1.getHealth();
                        player2health = player2.getHealth();

                        //spawns
                        updateMng.EnemySpawn(enemyTimeCount, enemyv, enemyFormationv, randomX);//enemy
                        updateMng.EnemyFormationSpawn(enemyFormationCount, enemyFormationv, boss2v);//enemyFormation
                        updateMng.HealthDropSpawn(healthDropCount, healthv, randomX);//health
                        updateMng.SpaceMonkeySpawn(points, monkeyv, boss2v);//monkey
                        updateMng.ShitSpawn(shitCount, monkeyv, shitv, sound);//shit
                        updateMng.Boss1Spawn(points, boss1v);//boss1
                        updateMng.UnlockPewSpawn(boss1Dead, unlockPewv);//unlockpew
                        updateMng.Boss1WeaponSpawn(boss1WeaponCount, b1Weaponv, boss1v, sound);//b1Weapon
                        updateMng.CowSpawn(cowTimeCount, cowv, randomX, sound);//cow
                        updateMng.Boss2Spawn(boss2v, points);//boss2
                        updateMng.Boss2WeaponSpawn(boss2WeaponCount, boss2Weaponv, boss2v);//b2Weapon
                        pewCD.Update(pewOnCooldown, elapsedTime);//pewCooldown

                        //player weapon spawn
                        if (player1.active)
                        {
                                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
                                {
                                        updateMng.BulletSpawn(bulletTimeCount, bulletv, highscore, weapon, player1, sound);//bullet
                                        updateMng.DoubleShotSpawn(bulletTimeCount, dShotv, highscore, weapon, player1, sound);//Doubleshot
                                        updateMng.PewSpawn(pewv, highscore, weapon, player1, sound, pewOnCooldown);//Pew
                                }
                        }

                        if (player2.active)
                        {
                                if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left))
                                {
                                        updateMng.BulletSpawn2(p2bulletTimeCount, bulletv, highscore, weapon, player2, sound);//bullet
                                        updateMng.DoubleShotSpawn2(p2bulletTimeCount, dShotv, highscore, weapon, player2, sound);//Doubleshot
                                        updateMng.PewSpawn2(pewv, highscore, weapon, player2, sound, pewOnCooldown);//Pew
                                }
                        }

                        //hud update
                        hudHealth.Update(window, player1.getHealth());
                        weapon.setWeapon(elapsedTime, points, gotPew);
                        weapon.Update();
                }
                //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                //++++++++++++++++++++++++++++++++-DRAW & COLLISION-++++++++++++++++++++++++++++++++++++                                                                               
                //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                window.clear();
                bg.Render(window);

                if (!paused)
                {

                        if (player1.active)
                        {
                                Rm::StdDraw(healthv, elapsedTime, window);//health
                                Rm::StdDraw(unlockPewv, elapsedTime, window);//unlockPew
                                Rm::StdDraw(monkeyv, elapsedTime, window);//monkey
                                Rm::StdDraw(boss1v, elapsedTime, window);//boss1
                                Rm::StdDraw(cowv, elapsedTime, window);//cow
                                Rm::StdDraw(enemyFormationv, elapsedTime, window);//enemyFormation
                                Rm::StdDraw(boss2v, elapsedTime, window);//boss2
                                Rm::EnemyStdWeapon(shitv, shititerator, window, elapsedTime);//shit
                                Rm::EnemyStdWeapon(b1Weaponv, bWeaponIt, window, elapsedTime);//b1Weapon

                                renderMng.Boss2WeaponDraw(boss2Weaponv, boss2WeaponIt, elapsedTime, player1, window);//b2Weapon
                                renderMng.EnemyDraw(enemyv, elapsedTime, highscore, window);//Enemy
                                renderMng.BulletDraw(bulletv, bulletviterator, points, sound, highscore, enemyv, monkeyv, shitv, boss1v, boss1Dead, boss2Weaponv, enemyFormationv, window, elapsedTime);//bullet
                                renderMng.DoubleShotDraw(dShotv, dShotIt, points, sound, highscore, enemyv, monkeyv, shitv, boss1v, boss1Dead, boss2Weaponv, enemyFormationv, window, elapsedTime);//doubleShot
                                renderMng.PewShotDraw(pewv, pewIt, points, sound, highscore, enemyv, monkeyv, shitv, boss1v, boss2v, boss2Weaponv, enemyFormationv, window, elapsedTime);//Pew
                        }

                        //player collision
                        if (player1.active)
                        {
                                coll::PlayerEnemyInactive(enemyv, player1, sound);//enemy
                                coll::PlayerEnemyInactive(shitv, player1, sound);//shit
                                coll::PlayerEnemyInactive(b1Weaponv, player1, sound);//b1Weapon
                                coll::PlayerEnemyInactive(boss2Weaponv, player1, sound);//b2Weapon
                                coll::PlayerEnemyInactive(enemyFormationv, player1, sound);//enemyFormation

                                coll::PlayerHealthGet(healthv, player1, sound);//health
                                coll::PlayerUnlockPew(unlockPewv, player1, sound, gotPew, pewOnCooldown);//unlockPew

                                //for enemies that are not set inactive there is a damagechill.. otherwise player would instantly die
                                if (damageChill > 500)
                                {
                                        coll::PlayerEnemyActive(boss2v, player1, sound);//boss2
                                        coll::PlayerEnemyActive(cowv, player1, sound);//cow
                                        coll::PlayerEnemyActive(monkeyv, player1, sound);//monkey
                                        coll::PlayerEnemyActive(boss1v, player1, sound);//boss1
                                        damageChill = 0;
                                }
                        }

                        if (player2.active)
                        {
                                coll::PlayerEnemyInactive(enemyv, player2, sound);//enemy
                                coll::PlayerEnemyInactive(shitv, player2, sound);//shit
                                coll::PlayerEnemyInactive(b1Weaponv, player2, sound);//b1Weapon
                                coll::PlayerEnemyInactive(boss2Weaponv, player2, sound);//b2Weapon
                                coll::PlayerEnemyInactive(enemyFormationv, player2, sound);//enemyFormation

                                coll::PlayerHealthGet(healthv, player2, sound);//health
                                coll::PlayerUnlockPew(unlockPewv, player2, sound, gotPew, pewOnCooldown);//unlockPew

                                //for enemies that are not set inactive there is a damagechill.. otherwise player would instantly die
                                if (damageChill > 500)
                                {
                                        coll::PlayerEnemyActive(boss2v, player2, sound);//boss2
                                        coll::PlayerEnemyActive(cowv, player2, sound);//cow
                                        coll::PlayerEnemyActive(monkeyv, player2, sound);//monkey
                                        coll::PlayerEnemyActive(boss1v, player2, sound);//boss1
                                        damageChill = 0;
                                }
                        }

                        //health & points into string
                        pHealthS.Update(healthStream, player1.getHealth());
                        pointS.Update(pointStream, points);

                        //players
                        if (player1.active)
                        {
                                player1.Update(window, elapsedTime);
                                player1.Render(window);

                                //sync health
                                if (player2.active)
                                {
                                        if (player2.getHealth() < player1.getHealth())
                                        {
                                                player1.setHealth(player2health);
                                        }
                                        if (player1.getHealth() < player2.getHealth())
                                        {
                                                player2.setHealth(player1health);
                                        }
                                }
                        }

                        if (player2.active)
                        {
                                player2.Update(window, elapsedTime);
                                player2.Render(window);
                        }

                        //revieve a player
                        if (secPlayer.ReadSettings())
                        {
                                if (player1.active && player1.getHealth() == 100 && !player2.active)
                                {
                                        player2.active = true;
                                        player2.setHealth(100);
                                }
                                if (player2.active && player2.getHealth() == 100 && !player1.active)
                                {
                                        player1.active = true;
                                        player1.setHealth(100);
                                }
                        }

                        //draw HUD
                        hudHealth.RenderBG(window);
                        hudHealth.Render(window);
                        hud.Render(window);
                        pHealthS.Render(window);
                        health.Render(window);
                        pointS.Render(window);
                        weapon.Render(window);
                        pewCD.Render(window);

                        //new weapon draw
                        if (points >= 1000 && points <= 1050)
                        {
                                showLvUp += elapsedTime;
                                if (showLvUp >= 0 && showLvUp <= 2000)
                                {
                                        lvUp.Render(window);
                                }
                                else if (showLvUp > 2000)
                                {
                                        showLvUp = 0;
                                }
                        }

                        //Game over
                        if (!player1.active)
                        {
                                //adjust highscore and show "Game Over"
                                highscore.setPoints(points);
                                gameOver.Render(window);
                                window.display();

                                //reset stuff (bugfix)
                                Game::ClearStuff();

                                //delay until the highscore screen appears
                                sf::sleep(sf::seconds(1));
                                return 5;
                        }
                }
                window.display();
        }
        return -1;
}

void Game::ClearStuff()
{
        points = 0;

        //restart counters
        enemyTimeCount = 0;
        bulletTimeCount = 0;
        p2bulletTimeCount = 0;
        shitCount = 0;
        boss1WeaponCount = 0;
        boss2WeaponCount = 0;
        healthDropCount = 0;
        cowTimeCount = 0;
        damageChill = 0;
        enemyFormationCount = 0;

        //clear vectors
        healthv.clear();
        enemyv.clear();
        monkeyv.clear();
        shitv.clear();
        boss1v.clear();
        cowv.clear();
        boss2v.clear();
        boss2Weaponv.clear();
        enemyFormationv.clear();
}

void Game::StartPause()
{
        paused = true;
        //TODO
}

void Game::EndPause()
{
        //TODO
        paused = false;
}

i hope you manage to get through it (i think my style is not very good, because i am learning this mostly by myself) if anyone has a simple answer i would be pleased to hear it ;)
« Last Edit: January 19, 2014, 05:41:32 pm by nebula »

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #38 on: January 19, 2014, 05:48:55 pm »
nevermind i solved it simply by adding:
                if (paused)
                {
                        clock.restart();
                }

world can be so easy :D

so i will add some stuff to let the pause look like the game is paused and upload it ;)

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #39 on: January 19, 2014, 06:20:02 pm »
Okay, I uploaded todays changes.

    cl14:
    • some fixes
        ->first boss will now be cleared as intended
        ->after respawn the revieved player should have 100 life
        ->window style is on default again
    • added pause menu (just press P)
    furthermore i added a readme.txt to the release, where the controls are shown and some information ;)

kingcools

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #40 on: January 19, 2014, 07:23:34 pm »
You can easily improve your code:

split your Game::Run() function into many small functions.

on first look your Game::Run() function could look like this:


Game::Run()
{
//All the following functions could be added to another init() function as well, then you would just write init()
//here instead of flooding the run function with ~5 init functions
initSound();
initBackGround();
initPlayers();
initCounter();
initPoints();

while(running)
{
handleEvent();
if(!pause)
{
update(); //all those time updates you do
draw();    //contains specialicst draw functions for all the stuff you need to draw
}
}
}
}


}

This should be your run() method. Way easier to grasp, maintain and it is more motivating to work with it.


I find it way more relaxing to have many rather small functions then a few giant ones.
Rule of thumb for me is something like "7 lines is great (of course not always doable), max around 40 lines."
Beyond about 40 lines it just becomes a mess and something is most likely wrong.
« Last Edit: January 19, 2014, 07:26:18 pm by kingcools »

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #41 on: January 19, 2014, 07:49:13 pm »
thank you ;) i will edit my code like you say. it seems logic

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #42 on: January 31, 2014, 06:06:11 pm »
Hey,

I've been really buisy lately, so I just managed to do a little hotfix regarding to the coop thing.

if you are at the third boss, there is now tracking added for the sec player if the first is dead ;)

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #43 on: February 14, 2014, 06:40:18 pm »
Looks like an funny shooter, made just 4fun. Like it! :)
« Last Edit: February 14, 2014, 07:01:47 pm by Cadisol87 »

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: [SFML 2.1 and C++] 2D Shooter - Pew (Open Source)
« Reply #44 on: February 21, 2014, 04:47:37 pm »
Looks like an funny shooter, made just 4fun. Like it! :)

Thank you ;)

I thought that I keep you guys updated :D Been really busy recently..
I managed to get the intro to start when the user wants it to. But the project got really spaghetti-like.
Because of that i thought about some changes in the structure. I have no idea of how to make an uml-diagram out of my c++ code, so i painted it :'D. Here is the photo:




For the ones that have no idea of my code, i will put the music into the main.cpp and make one file out of all the IOxxx files. As you can imagine, this will take some time ;)

« Last Edit: February 21, 2014, 05:02:57 pm by nebula »