SFML community forums

General => SFML projects => Topic started by: Mortal on August 20, 2015, 11:23:36 pm

Title: Space Invaders Clone - Game finished
Post by: Mortal on August 20, 2015, 11:23:36 pm
Game finished

hello everybody i just finished reading the SFML Development Book. it really a great book many thanks to all authors. i have made a simple space invaders based on what i have learned from the Book.

here a new video with latest update where the player can spawn
https://youtu.be/0iaJHnNpWKo


source code is available hère:
https://github.com/MORTAL2000/Space-Invaders-Clone (https://github.com/MORTAL2000/Space-Invaders-Clone)

NOTE: it tested on VS 2015.
Title: Re: Space Invaders Clone
Post by: Verra on August 21, 2015, 08:05:10 am
Looks pretty well done. Good job.  :P
Title: Re: Space Invaders Clone
Post by: bisthebis on August 21, 2015, 02:14:57 pm
That's nice. I've dowloaded it :)
Title: Re: Space Invaders Clone
Post by: Mortal on August 21, 2015, 02:41:02 pm
Looks pretty well done. Good job.  :P

thanks @Verra really i appreciate. i have made little changes differ than example in Book. it supports mouse in GUI when user navigate buttons , i dont know why in the book says it's difficult to implement mouse functionality. it's appeared easy to do it. it only needs to iterate throughout all components to see whether or not the mouse position contains in buttons boundaries. of course, the annoying part in mouse implementation is when it has sound and continuously beeps when it is hovering throughout buttons. now, in the game, sound only beep once when the mouse hover the buttons.

however, the game runs only in release mode. it doesn't work fine in debug mode, the frame rate drop to zero especially when i implemented the collision detection. i tried to use QuadTree as many people suggest but no avail, maybe i implemented the QuadTree wrongly i'm not sure.

once again thanks for your kind words :)   
Title: Re: Space Invaders Clone
Post by: Mortal on August 21, 2015, 02:41:58 pm
That's nice. I've dowloaded it :)

thanks good to hear that  :)
Title: Re: Space Invaders Clone
Post by: SeriousITGuy on August 27, 2015, 09:50:03 am
Hey MORTAL,

ah this is so old school and amazing, great work. Art and sound is awesome I love this stuff!
As I started working on my "practice games" Space Invaders definitly comes on my todo list ;)

Cheers!
Title: Re: Space Invaders Clone
Post by: Mortal on August 28, 2015, 09:35:30 pm
thanks SeriousITGuy

nice to hear that you were back to practice games keep it up. indeed space invaders has tobe in your list. it easy to made and also fun. i have made it earlier but with ascii text-base. this is my first graphics game :-[

btw, @Hapax is doing his practice games nowadays, whats that a coincidence  ???. i have looked to his practice game, he made awesome mini-libs, really it's worth looking at.
Title: Re: Space Invaders Clone
Post by: Brax on August 28, 2015, 11:11:22 pm
I've downloaded the source code and tried to compile it, but unfortunately my Visual Studio 2013 is sending back these errors:

(click to show/hide)

After searching a bit, I found out (http://en.sfml-dev.org/forums/index.php?topic=11992.msg85540#msg85540) that it is a compiler error. So if anybody is using VS 2013 (like me), he is going to have a tough time compiling it. :(
Title: Re: Space Invaders Clone
Post by: Mortal on August 29, 2015, 04:25:52 pm
hi Brax

sorry for what you had through, probably i should mention that in main post.

yes indeed thats why i installed VS 2015 since i realized, VS 2013 which i had, it has a bug with std::bind. but i regretted for installing VS 2015. most of third party libraries such lua and glew even current SFML need to recompile once again to fit VS 2015. special thanks to@eXpl0it3r for his night build for SFML. personally i don't know how to build those libraries by my own yet.

quick fix you may convert those error std::binds to lambda expressions
Title: Re: Space Invaders Clone
Post by: Mortal on September 02, 2015, 07:22:19 pm
UPDATE:

i have re-written QuadTree class. now, the game seems work fine in debug mode. i would like to know, how to get maximum efficiency from QuadTree. i know it can be done by storing the objects in leaves only. i have tried to achieve that, and somehow i managed to store objects in leaves as possible. but i'm not sure whether or not i did it correctly this time.

thanks in advance.
Title: Re: Space Invaders Clone
Post by: Satus on September 02, 2015, 07:44:32 pm
If you use tabs for spacing, code on github starts looking a little bit ugly  :D
Title: Re: Space Invaders Clone
Post by: Nexus on September 02, 2015, 07:47:02 pm
If you use tabs for spacing, code on github starts looking a little bit ugly
http://stackoverflow.com/a/23522945

They've finally recognized that not everybody is using spaces... ::)
Title: Re: Space Invaders Clone
Post by: Satus on September 02, 2015, 07:48:00 pm
If you use tabs for spacing, code on github starts looking a little bit ugly
http://stackoverflow.com/a/23522945
Oh, I didn't know it, thanks you.
Title: Re: Space Invaders Clone
Post by: Mortal on September 02, 2015, 09:03:50 pm
If you use tabs for spacing, code on github starts looking a little bit ugly
http://stackoverflow.com/a/23522945

They've finally recognized that not everybody is using spaces... ::)

thanks Nexus,
Title: Re: Space Invaders Clone
Post by: Satus on September 02, 2015, 09:07:15 pm
Well, the distance between function return type and name is still quite impressive even with ?ts=1  :)
https://github.com/MORTAL2000/Space-Invaders-Clone/blob/master/QuadTree.hpp?ts=1
Title: Re: Space Invaders Clone
Post by: Mortal on September 02, 2015, 09:30:59 pm
yeah, i test the ts=1 with my IDE for functions, it looks nice especially when it has description comments. i'm going to use this style in next game  ;)
Title: Re: Space Invaders Clone
Post by: Mortal on September 20, 2015, 07:41:41 pm
update

after reading an old post http://en.sfml-dev.org/forums/index.php?topic=7427.0 (http://en.sfml-dev.org/forums/index.php?topic=7427.0) about Sprite Masking by using sf::RenderTexture and sf::Image.

 i implemented them in the game, now the game use single texture for shield and result some how acceptable.
Title: Re: Space Invaders Clone
Post by: Mortal on September 22, 2015, 08:21:27 am
i added new video for latest update, after implemented sprite masking and pixel collision detection.

here snippet from sprite masking by using sf::RenderTexture and sf::Image

void Shield::updateCurrent(sf::Time dt, CommandQueue& commands)
{
        if (!mOnHit)
                return;

        mRenderTexture.clear();

        mRenderTexture.draw(*this, sf::BlendNone);
        mRenderTexture.display();

        float radius = (mRectOnHit.height > ExplosionRadius) ? mRectOnHit.height : ExplosionRadius;
        sf::CircleShape circle(radius, 10);
        circle.setPosition(mPositionOnHit.x, mPositionOnHit.y + circle.getRadius() / 2.f * mSign);
        circle.setFillColor(sf::Color::Transparent);
        centerOrigin(circle);

        mRenderTexture.draw(circle, sf::BlendNone);
        mRenderTexture.display();

        sf::Vector2u position(static_cast<std::size_t>(getPosition().x - mTexture.getSize().x / 2u), static_cast<std::size_t>(getPosition().y - mTexture.getSize().y / 2u));
        mImage.copy(mRenderTexture.getTexture().copyToImage(), 0u, 0u, sf::IntRect(position.x, position.y, mTexture.getSize().x, mTexture.getSize().y));
        mImage.createMaskFromColor(sf::Color::Transparent);

        updateSprite();
}

and here the pixel collision detection for bullets:

bool PixelcollidesPair(const Shield& shield, const Projectile& bullet)
{
        auto bulletBounds = static_cast<sf::Rect<std::size_t>>(bullet.getBoundingRect());
        auto shieldBounds = static_cast<sf::Rect<std::size_t>>(shield.getBoundingRect());

        auto width = bulletBounds.left + bulletBounds.width;
        auto height = bulletBounds.top + bulletBounds.height;

        sf::Vector2u position(bulletBounds.left, bulletBounds.top);

        if (!bulletBounds.intersects(shieldBounds))
                return false;

        for (auto x = position.x; x < width; ++x)
        {
                for (auto y = position.y; y < height; ++y)
                {
                        auto relX = x - shieldBounds.left;

                        auto relY = (bullet.getCategory() & Category::PlayerProjectile) ? y - shieldBounds.top : y - shieldBounds.top - bulletBounds.height;

                        if (shield.getPixel(relX, relY))
                                return true;
                }
        }

        return false;
}
Title: Re: Space Invaders Clone
Post by: Hapax on September 24, 2015, 08:19:34 pm
Ah, this is the Space Invaders clone you were talking about!  :)

Just watched the videos.
In the 'new' one, it's evident that when a side of the group of invaders is removed, the remaining invaders don't continue to the edge of the screen; they move as if the entire group is still present. Just something I noticed. Not sure why the "mission" was "successful" either  ;D

I also find it weird that when you die, you just animate death but carry on as normal  :o

It looks good, though. Quite similar to what the original seems to have looked like (except with smoother motion in yours). The firing rate seems quite quick too. Maybe I'll keep this in mind for mine  :D

Keep it up. Looking forward to seeing the final result  :)
Title: Re: Space Invaders Clone
Post by: Mortal on September 25, 2015, 04:25:00 pm
Quote
Ah, this is the Space Invaders clone you were talking about!
yup, this is my first graphics game. ;D

Quote
In the 'new' one, it's evident that when a side of the group of invaders is removed, the remaining invaders don't continue to the edge of the screen; they move as if the entire group is still present.
yes, this is due to that every invader ship moves independently by fixing distance. to change that it needs to be considered in early stage. for this game, it needs to re-write whole game structure which i don't like, it's better to start by using different game structure.

Quote
Not sure why the "mission" was "successful" either
hahaha, yeah you are right.. it seems i should die but fortunately i had 2 lives when that ship collided with me, i just lost one live but i won  :P

Quote
I also find it weird that when you die, you just animate death but carry on as normal
the original game perform death animation when the player got hit. i just mimic the original game.

Quote
Keep it up. Looking forward to seeing the final result
thanks alot sir.
Title: Re: Space Invaders Clone
Post by: Hapax on September 25, 2015, 08:51:52 pm
this is my first graphics game. ;D
Good work!  :)

the original game perform death animation when the player got hit. i just mimic the original game.
The original pauses while the animation plays and then resumes with a full ship. In yours, you can continue to move and fire while it's exploding, which looks a little bit odd.
https://www.youtube.com/watch?v=D1jZaIPeD5w&t=1m22s
(I left this as a link because it starts from a specific time, which seems to be ignored by the embedded player)

thanks alot sir.
You're welcome but please don't call me Sir  :)
Title: Re: Space Invaders Clone
Post by: Mortal on September 26, 2015, 06:20:57 pm
update

i add a new video in main post for implement a new movement algorithm for invaders. result seems okay so far. inspired by what Hapax did  to his latest practice games. thanks millions Hapax

here code snippet of adapt enemy movements
void World::adaptEnemyMovements()
{
        bool changeDirection = false;

        for (const auto& i : mEnemyNodes)
        {
                Spaceship& enemy = static_cast<Spaceship&>(*i);

                if (enemy.getType() == Spaceship::Boss)
                        continue;

                if (!getBattlefieldBounds().contains(enemy.getPosition()))
                        changeDirection = true;
        }

        // let invaders moving down and update condition of change direction
        for (const auto& i : mEnemyNodes)
        {
                Spaceship& enemy = static_cast<Spaceship&>(*i);

                if (enemy.getType() == Spaceship::Boss)
                        continue;

                enemy.requestChangeDirection(changeDirection);
        }
}

and here update movement pattern it's exactly same mostly
void Spaceship::updateMovementPattern(sf::Time dt)
{
        // Enemy Spaceships: Movement pattern
        const std::vector<Direction>& directions = Table[mType].directions;

        if (!directions.empty())
        {
                // Moved long enough in horizontal direction: Change direction to move down
                if(mChaneDirction && directions[mDirectionIndex].distance == 0.f)
                {
                        mDirectionIndex = (mDirectionIndex + 1) % directions.size();
                        mTravelledDistance = 0.f;
                        mChaneDirction = false;
                }

                // Moved long enough in vertical direction: Change direction to move aside
                if (directions[mDirectionIndex].distance != 0.f && mTravelledDistance > directions[mDirectionIndex].distance)
                {
                        mDirectionIndex = (mDirectionIndex + 1) % directions.size();
                        mTravelledDistance = 0.f;
                }

                // Compute velocity from direction
                auto radians = toRadian(directions[mDirectionIndex].angle + 90.f);
                auto vx = getMaxSpeed() * std::cos(radians);
                auto vy = getMaxSpeed() * std::sin(radians);

                setVelocity(vx, vy);

                mTravelledDistance += getMaxSpeed() * dt.asSeconds();

                applyAnimation(dt);
        }
}



unfortunately, there is a bug, invaders some time drop down, especially when it got hit at both edges right and left of screen.



Title: Re: Space Invaders Clone
Post by: Mortal on September 28, 2015, 01:56:46 pm
update

- fixed the bug where invaders some time drop down, especially when it got hit at both edges right and left of screen
- Spaceship is become so hard to maintain, so that, it splitted into 3 classes Boss, Invaders and Player.
- old Player class renamed to PlayerController.


still damn bug is there. if you play long enough in the game the block of invaders SOME TIME drop down with no obvious reason why is that happen. it's hard to track down this kind of bugs to know whats really cause it and my compiler doesn't help me so much here.
Title: Re: Space Invaders Clone
Post by: Mortal on September 30, 2015, 02:49:19 pm
added new video for latest updates on the main post.

Latest update
- new movement pattern to invaders block by implementing simple state-mechanism
- implement dirty flag efficiently
- implement fixed time-step in game loop
- boss can spawn and entering the scene occasionally
- new bullet texture
- bullets can collide with each others
- fixed the bug where the block of invaders suddenly drop down


Title: Re: Space Invaders Clone
Post by: Mortal on October 01, 2015, 09:33:39 pm
i added video to latest update

https://youtu.be/0iaJHnNpWKo (https://youtu.be/0iaJHnNpWKo)

update
- player can spawn after its death
- whole screen freeze while player animating death and game resume after player recreated
- player bullet only fire when current bullet is destroyed (no more fire rate in the game)
 
Title: Re: Space Invaders Clone - Game finished
Post by: Mortal on October 04, 2015, 08:03:30 pm
update

- upgraded to latest sfml version 2.3.2.
- reduced the death animation interval for player
- add LifeNode class
- add ScoreNode class
- add InvadersController class
- add green bottom line to SpriteNode class
- major clean up and code-style
Title: Re: Space Invaders Clone - Game finished
Post by: Mortal on October 13, 2015, 04:29:11 am
update

- re-write QuadTree class, now it is much readable
- re-write the helper function 'derivedAction' in Command class, by using SFINAE instead of dynamic_casting
- adding PlayerFactory Class for managing its spawning
- adding BossFactory class for managing its spawning
- adding GameConstants file for common constant variables
- fixed a bug where the button's sound always peep twice when user navigates throughout buttons by the mouse
- more clean up
Title: Re: Space Invaders Clone - Game finished
Post by: Hapax on October 22, 2015, 08:39:41 pm
Congratulations on completing the game!  :)
Title: Re: Space Invaders Clone - Game finished
Post by: Mortal on October 22, 2015, 09:12:55 pm
thanks Hapax

i did what you have told me to do in earlier post, like player's behavior and bullets fire rate. the only left from what you have observed from first attempt is invaders movements where lowest row moves first then the one above until it reaches the most top row just like what is in the link that you gave it. i was attempting to wrap my head around this whole thing but i couldn't even it sounds like easy to implement.
Title: Re: Space Invaders Clone - Game finished
Post by: Hapax on October 22, 2015, 09:35:58 pm
The stuttering movement of the "invaders" isn't required; it's only aesthetic here really.
I see you changed the fast fire rate to the original Space Invaders style where it will only fire after the previous bullet ceases to exist. I prefer your original firing style but it was just too fast  ;)

One thing I did notice (I don't think I mentioned this previously) is that your enemies speed up based on their position (they speed up as they get closer to the bottom) whereas in the original, they speed up whenever an Invader was destroyed. Not sure which is better but I just thought I'd mention it.
Title: Re: Space Invaders Clone - Game finished
Post by: Mortal on October 24, 2015, 12:59:43 pm
update

- removing old InvadersController class.
- adding a new Invaders class.
- now invaders move faster if one of them get killed.
- adding Generic Random class by using c++14 feature.
- implement type checking for template (functions, class)
- eliminate incidental control flow and implement correct tool from STD algorithm header in Invaders class.
- fixed bug, some time the game doesn't end when all invaders killed
- cleanup
 
Title: Re: Space Invaders Clone - Game finished
Post by: Nexus on October 28, 2015, 10:13:40 pm
I think you should focus a bit more on the functional changes rather than minor code modifications... Your project is not a library, as such most people are probably interested in how they're affected as a player ;)

Of course, if there are changes that you'd like to highlight in the code or discuss in public, don't hesitate to mention them.
Title: Re: Space Invaders Clone - Game finished
Post by: Mortal on October 29, 2015, 12:49:43 am
such most people are probably interested in how they're affected as a player ;)

thanks Nexus for the advice, sure i will keep that in my mind. the project has finished, it was like my homework  assignment after reading SFML Development Book. i just i wanna try to make little game by my own. whole project even it is for beginner is quite massive for me. most of these minor details were took alot of time from me too as well to grasp and implement them on this project. btw, this is my first graphic game. most of games that i have made was an ascii like hangman, tictacteo etc.

any way, thanks once again for your patient i really appreciate it most.