Users browsing this thread: 1 Guest(s)
Ailit's Game thread (Games + Source Code)
#1
So I wanted to test out my skills with SFML
Finally got back into programming legit and SFML 2 so I decided to make a picking sticks game..

A picking sticks game is one where the player walks around like a topdown and when he collides with a stick you move the stick and he goes gets it again.

This type of game is important for beginners because it practices how to load resources, draw images and deal correctly with logic.

I added a twist though.. the player's speed (the crow) increases with the more sticks he picks up. No real reason behind it, just being a dick.

The graphics are poop (I know I could have done a WAY better job)
I'll provide the source code too so ya'll can learn from it and what not.

Workin' on a mac port (who the hell would want to play this game? lol)

Picking Sticks

Code!
Code:
#include <iostream>
#include <SFML\Window.hpp>
#include <SFML\Graphics.hpp>
#include <SFML\Audio.hpp>

int main(void)
{
    srand(clock());

    sf::RenderWindow sf_window(sf::VideoMode(420, 380), "Picking Sticks", sf::Style::Close);
    sf_window.setFramerateLimit(60);

    sf::Event sf_event;
    sf::Keyboard sf_keyboard;

    sf::Texture sf_texture;
    if(!sf_texture.loadFromFile("resources/background.png"))
        return EXIT_FAILURE;

    sf::Sprite sf_background;
    sf_background.setTexture(sf_texture, false);

    sf::Texture sf_sprite_a;
    if(!sf_sprite_a.loadFromFile("resources/crow.png"))
        return EXIT_FAILURE;

    sf::Sprite sf_spr_crow;
    sf_spr_crow.setTexture(sf_sprite_a, false);

    sf::Texture sf_sprite_b;
    if(!sf_sprite_b.loadFromFile("resources/stick.png"))
        return EXIT_FAILURE;

    sf::Sprite sf_spr_stick;
    sf_spr_stick.setTexture(sf_sprite_b, false);

    sf::SoundBuffer sf_sndbuffer;
    sf_sndbuffer.loadFromFile("resources/sound.wav");

    sf::Sound sf_snd_crow;
    sf_snd_crow.setBuffer(sf_sndbuffer);

    int pos_x = sf_window.getSize().x / 2;
    int pos_y = sf_window.getSize().y / 2;
    int counter = 0;
    int speed   = 1;

    while(sf_window.isOpen())
    {
        while(sf_window.pollEvent(sf_event))
        {
            if(sf_event.type == sf::Event::Closed)
                sf_window.close();
        }

        if(sf_keyboard.isKeyPressed(sf::Keyboard::Escape))
            sf_window.close();
        if(sf_keyboard.isKeyPressed(sf::Keyboard::Left))
            pos_x -= speed;
        if(sf_keyboard.isKeyPressed(sf::Keyboard::Right))
            pos_x += speed;
        if(sf_keyboard.isKeyPressed(sf::Keyboard::Up))
            pos_y -= speed;
        if(sf_keyboard.isKeyPressed(sf::Keyboard::Down))
            pos_y += speed;

        if(pos_x < 0)
            pos_x = 0;
        if(pos_x > sf_window.getSize().x - sf_spr_crow.getLocalBounds().height)
            pos_x = sf_window.getSize().x - sf_spr_crow.getLocalBounds().height;
        if(pos_y < 0)
            pos_y = 0;
        if(pos_y > sf_window.getSize().y - sf_spr_crow.getLocalBounds().width)
            pos_y = sf_window.getSize().y - sf_spr_crow.getLocalBounds().width;
        sf_spr_crow.setPosition(pos_x, pos_y);

        if(sf_spr_crow.getGlobalBounds().intersects(sf_spr_stick.getGlobalBounds()))
        {
            if(sf_snd_crow.getStatus() != sf::Sound::Playing)
                sf_snd_crow.play();
            sf_spr_stick.setPosition(rand() % 344, rand() % 316);
            counter++;
            speed++;
        }

        sf_window.clear();

        sf_window.draw(sf_background);
        sf_window.draw(sf_spr_crow);
        sf_window.draw(sf_spr_stick);
        std::cout << counter << '\r';

        sf_window.display();
    }

    return 0;
}

Here is a screenshot
[Image: kYPlpQb.png]
BONUS APP! (Windows Only.. Sowwie Sad)
Ever wanted to insult your palls with style?
Download now this program I wrote that does it for you! Shakespeare style!

[Image: CPCgroa.png]
Shakespear Scoffer

This program is only possible on Micro$oft Windows because I used their SAPI libs for the voice synth.. if you guys want a mac port or linux I'll take up the challenge, it's a pretty fun app after all Smile Also, source code if you guys want it too.

So this is going to be my demo dump thread. New games and cool apps I write will come here (including source code) so stay tuned! I'm working on a bomberman game now (so ambitious.. oh dear)

As for copying what ever I post in this thread, go nuts. I could really care less, just don't claim you made any of it. If you edit it and use it as your own, no need to give creds just happy to help. Basically the "Do what ever the fuck you want" license on everything I post in here.
Thanked by: Phaze
#2
I forgot to statically link libgcc! If the game doesn't run thats my fault so let me know

ok I edited and updated the new links, sorry I forgot that! Big Grin

forgot to statically link libstdc++

I'm going to be statically linking as many libs as I can to avoid the missing dll errors
Because I have a path variable that points to the libs I didn't notice they weren't being linked statically.
Sorry about that, problem fixed now.

I think I'll make a shmup today..
Thanked by: Phaze
#3
Ok so I skipped bomberman game and decided to make bullet hell game Big Grin
The project is currently called "SHMUP" since that is what it is.
Currently I have yet to add any enemies or power ups. The player is a ship and only has one type of shot. Stay tuned ;D

The source is kind of in a floating stage because I have no real design plan for the game engine. I'm thinking of templating away some more of the classes to make things more generic and versatile. I'd hate to have to write a class for each and every single game object xD but never the less here is what I've written so far.

class.hpp
Code:
#include <iostream>
#include <vector>
#include <string>

#ifdef SHMUP_DEBUG
#include <curses.h>
#endif

#include <SFML\window.hpp>
#include <SFML\graphics.hpp>
#include <SFML\audio.hpp>

using namespace std;

/**************************************************/
/**Template Resource Class**/
/**************************************************/
template<class T_sf_class> class resources
{
    public:
        resources();
        ~resources();

        bool res_load(string directory);

        vector<T_sf_class> res_array;
};

template<class T_sf_class> resources<T_sf_class>::resources()
{

}

template<class T_sf_class> resources<T_sf_class>::~resources()
{
    res_array.empty();
}

template<class T_sf_class> bool resources<T_sf_class>::res_load(string directory)
{
    T_sf_class new_res;

    if(!new_res.loadFromFile(directory))
        return false;

    res_array.push_back(new_res);

    return true;
}

/**************************************************/
/**Template Object Class**/
/**************************************************/
template<class T_obj> class objects
{
    public:
        objects();
        ~objects();

        void add_obj(sf::Texture& texture);
        void add_obj(sf::Texture& texture, sf::IntRect frame);

        vector<T_obj> obj_array;
};

template<class T_obj> objects<T_obj>::objects()
{

}

template<class T_obj> objects<T_obj>::~objects()
{

}

template<class T_obj> void objects<T_obj>::add_obj(sf::Texture& texture)
{
    T_obj new_obj;

    new_obj.sprite.setTexture(texture);

    obj_array.push_back(new_obj);
}

template<class T_obj> void objects<T_obj>::add_obj(sf::Texture& texture, sf::IntRect frame)
{
    T_obj new_obj;

    new_obj.sprite.setTexture(texture);
    new_obj.sprite.setTextureRect(frame);

    obj_array.push_back(new_obj);
}

/**************************************************/
/**Player Class**/
/**************************************************/
class player
{
    public:
        player();
        ~player();

        bool update();

        sf::Sprite      sprite;
        float           speed;
        unsigned int    power;
        unsigned int    lives;
        unsigned int    t_shot;
        int             t_anim;
};

player::player()
{
    speed  = 1.5f;
    power  = 1;
    lives  = 3;
    t_shot = 0;
    t_anim = 0;
}

player::~player()
{

}

bool player::update()
{
    if(t_shot > 0)
        t_shot--;

    if(t_anim < 0)
    {
        if(t_anim < -5) //frame 2
        {
            sprite.setTextureRect(sf::IntRect(0, 16, 32, 16));
        }
        if(t_anim < -10)
        {
            sprite.setTextureRect(sf::IntRect(0, 32, 48, 16));
            t_anim = -10;
        }
        t_anim++;
    }

    if(t_anim == 0)
        sprite.setTextureRect(sf::IntRect(0, 0, 16, 16));

    if(t_anim > 0)
    {
        if(t_anim > 5) //frame 2
        {
            sprite.setTextureRect(sf::IntRect(0, 48, 64, 16));
        }
        if(t_anim > 10)
        {
            sprite.setTextureRect(sf::IntRect(0, 64, 80, 16));
            t_anim = 10;
        }
        t_anim--;
    }

    return true;
}
/**************************************************/
/**Enemy Class**/
/**************************************************/
class enemy
{
    public:
        enemy();
        ~enemy();

        bool update();

        sf::Sprite      sprite;
        float           speed;
        unsigned int    power;
};

enemy::enemy()
{

}

enemy::~enemy()
{

}

bool enemy::update()
{
    return true;
}

/**************************************************/
/**Player Shot Class**/
/**************************************************/
class p_shot
{
    public:
        p_shot();
        ~p_shot();

        bool update();
        void animate();

        sf::Sprite      sprite;
        float           speed;
        float           angle;
        unsigned int    power;
        unsigned int    damage;

        unsigned int    t_anim;
};

p_shot::p_shot()
{
    speed   = 5.0f;
    power   = 1;
    damage  = 2;
}

p_shot::~p_shot()
{

}

bool p_shot::update()
{
    sprite.move(0.0f, -speed);

    if(t_anim < 5)
    {
        sprite.setTextureRect(sf::IntRect(0, 0, 16, 16));
    }
    if(t_anim > 5)
    {
        sprite.setTextureRect(sf::IntRect(0, 16, 32, 16));
    }
    if(t_anim > 10)
    {
        t_anim = 0;
    }

    t_anim++;

    return true;
}

/**************************************************/
/**Enemy Shot Class**/
/**************************************************/
class e_shot
{
    public:
        e_shot();
        ~e_shot();

        bool update();

        sf::Sprite      sprite;
        float           speed;
        unsigned int    power;
};

e_shot::e_shot()
{

}

e_shot::~e_shot()
{

}

bool e_shot::update()
{
    return true;
}

/**************************************************/
/**PowerUp Class**/
/**************************************************/
class powerup
{
    public:
        powerup();
        ~powerup();

        bool update();

        sf::Sprite   sprite;
        unsigned int p_type;
};

powerup::powerup()
{

}

powerup::~powerup()
{

}

bool powerup::update()
{
    return true;
}

/**************************************************/
/**Explosion Class**/
/**************************************************/
class explosion
{
    public:
        explosion();
        ~explosion();

        bool update();

        sf::Sprite sprite;
};

explosion::explosion()
{

}

explosion::~explosion()
{

}

bool explosion::update()
{

    return true;
}

/**************************************************/
/**Background Class**/
/**************************************************/
class background
{
    public:
        background();
        ~background();

        bool update();

        sf::Sprite sprite;
        sf::IntRect intrect;
};

background::background()
{

}

background::~background()
{

}

bool background::update()
{
    intrect = sprite.getTextureRect();
    intrect.top--;

    if(intrect.top < -(int)sprite.getTexture()->getSize().y)
        intrect.top = 0;

    sprite.setTextureRect(intrect);

    return true;
}

main.cpp
Code:
/**************************************************/
/**Preprocessor**/
/**************************************************/
#include "class.hpp"

/**************************************************/
/**Main Function**/
/**************************************************/
int main(void)
{
    #ifdef SHMUP_DEBUG
    initscr();
    resize_term(16, 32);
    #endif

/**************************************************/
/**Initializing Game**/
/**************************************************/
    sf::View sf_view;
    sf_view.setCenter(420 / 2, 380 / 2);
    sf_view.setSize(420, 380);

    sf::Event sf_event;
    sf::Keyboard sf_keyboard;

    bool isfullscreen = false;
    sf::RenderWindow *sf_window = new sf::RenderWindow(sf::VideoMode(420, 380), "SHMUP", isfullscreen == true ? sf::Style::Fullscreen : sf::Style::Close);
    sf_window->setMouseCursorVisible(false);
    sf_window->setView(sf_view);
    sf_window->setFramerateLimit(60);
/**************************************************/
/**Textures and Objects**/
/**************************************************/
    resources<sf::Texture> res_textures;

    res_textures.res_load("resources/ship.png");//0
    res_textures.res_load("resources/shot.png");//1
    res_textures.res_load("resources/boom.png");//2
    res_textures.res_load("resources/background.png");
    res_textures.res_array.back().setRepeated(true);

    player obj_player;
    obj_player.sprite.setTexture(res_textures.res_array.at(0));
    obj_player.sprite.setTextureRect(sf::IntRect(0, 0, 16, 16));

    objects<p_shot> obj_p_shots;

    objects<enemy> obj_enemies;

    objects<e_shot> obj_e_shots;

    objects<powerup> obj_powerups;

    objects<explosion> obj_explosions;

    objects<background> obj_backgrounds;
    obj_backgrounds.add_obj(res_textures.res_array.at(3));


/**************************************************/
/**SndBuffers and Sounds**/
/**************************************************/
    resources<sf::SoundBuffer> res_sounds;

    res_sounds.res_load("resources/shot.ogg");
    res_sounds.res_load("resources/pickup.ogg");
    res_sounds.res_load("resources/ohfuck.ogg");

    vector<sf::Sound> sound_channels;

    sf::Sound snd_shot;
    snd_shot.setBuffer(res_sounds.res_array.at(0));
    sf::Sound snd_pickup;
    snd_pickup.setBuffer(res_sounds.res_array.at(1));
    sf::Sound snd_ohfuck;
    snd_ohfuck.setBuffer(res_sounds.res_array.at(2));

    sound_channels.push_back(snd_shot);
    sound_channels.push_back(snd_pickup);
    sound_channels.push_back(snd_ohfuck);

/**************************************************/
/**Game Loop**/
/**************************************************/
    while(sf_window->isOpen())
    {
/**************************************************/
/**Input Handling**/
/**************************************************/
        while(sf_window->pollEvent(sf_event))
        {
            if(sf_event.type == sf::Event::Closed)
                sf_window->close();
        }
        if(sf_keyboard.isKeyPressed(sf::Keyboard::Escape))
        {
            sf_window->close();
        }
        if(sf_keyboard.isKeyPressed(sf::Keyboard::F11))
        {
            isfullscreen = !isfullscreen;
            delete sf_window;
            sf_window = new sf::RenderWindow(sf::VideoMode(420, 380), "SHMUP", isfullscreen == true ? sf::Style::Fullscreen : sf::Style::Close);
            sf_window->setMouseCursorVisible(false);
            sf_window->setView(sf_view);
            sf_window->setFramerateLimit(60);
        }
/**************************************************/
/**Gameplay Handling**/
/**************************************************/
        if(sf_keyboard.isKeyPressed(sf::Keyboard::Up))
        {
            obj_player.sprite.move(0.0f, -obj_player.speed);
        }
        if(sf_keyboard.isKeyPressed(sf::Keyboard::Down))
        {
            obj_player.sprite.move(0.0f, +obj_player.speed);
        }
        if(sf_keyboard.isKeyPressed(sf::Keyboard::Left))
        {
            obj_player.t_anim += 2;
            obj_player.sprite.move(-obj_player.speed, 0.0f);
        }
        if(sf_keyboard.isKeyPressed(sf::Keyboard::Right))
        {
            obj_player.t_anim -= 2;
            obj_player.sprite.move(+obj_player.speed, 0.0f);
        }
        if(sf_keyboard.isKeyPressed(sf::Keyboard::Z) && obj_player.t_shot == 0)
        {
            sound_channels.at(0).play();

            obj_p_shots.add_obj(res_textures.res_array.at(1), sf::IntRect(0, 0, 16, 16));
            obj_p_shots.obj_array.back().sprite.setPosition(obj_player.sprite.getPosition());
            obj_player.t_shot = 10;
        }
/**************************************************/
/**Update Objects**/
/**************************************************/
        //obj_player
        obj_player.update();


        //obj_p_shots
        for(unsigned int i = 0; i < obj_p_shots.obj_array.size(); i++)
        {
            if(!obj_p_shots.obj_array[i].update())
                obj_p_shots.obj_array.erase(obj_p_shots.obj_array.begin() + i);

            if(obj_p_shots.obj_array[i].sprite.getPosition().y + 16 < 0)
                obj_p_shots.obj_array.erase(obj_p_shots.obj_array.begin() + i);
        }
        //obj_enemies
        for(unsigned int i = 0; i < obj_enemies.obj_array.size(); i++)
        {
            if(!obj_enemies.obj_array[i].update())
                obj_enemies.obj_array.erase(obj_enemies.obj_array.begin() + i);

        }
        //obj_e_shots
        for(unsigned int i = 0; i < obj_e_shots.obj_array.size(); i++)
        {
            if(!obj_e_shots.obj_array[i].update())
                obj_e_shots.obj_array.erase(obj_e_shots.obj_array.begin() + i);

        }
        //obj_powerups
        for(unsigned int i = 0; i < obj_powerups.obj_array.size(); i++)
        {
            if(!obj_powerups.obj_array[i].update())
                obj_powerups.obj_array.erase(obj_powerups.obj_array.begin() + i);

        }
        //obj_backgrounds
        for(unsigned int i = 0; i < obj_backgrounds.obj_array.size(); i++)
        {
            if(!obj_backgrounds.obj_array[i].update())
                obj_backgrounds.obj_array.erase(obj_backgrounds.obj_array.begin() + i);

        }

/**************************************************/
/**Update Frame**/
/**************************************************/
        sf_window->clear();

        for(vector<background>::iterator i = obj_backgrounds.obj_array.begin(); i != obj_backgrounds.obj_array.end(); i++)
            sf_window->draw(i->sprite);
        for(vector<p_shot>::iterator i = obj_p_shots.obj_array.begin(); i != obj_p_shots.obj_array.end(); i++)
            sf_window->draw(i->sprite);
        for(vector<enemy>::iterator i = obj_enemies.obj_array.begin(); i != obj_enemies.obj_array.end(); i++)
            sf_window->draw(i->sprite);
        for(vector<e_shot>::iterator i = obj_e_shots.obj_array.begin(); i != obj_e_shots.obj_array.end(); i++)
            sf_window->draw(i->sprite);
        for(vector<powerup>::iterator i = obj_powerups.obj_array.begin(); i != obj_powerups.obj_array.end(); i++)
            sf_window->draw(i->sprite);

        sf_window->draw(obj_player.sprite);

        sf_window->display();

        #ifdef SHMUP_DEBUG
        wclear(stdscr);
        printw("player anim   %d\n", obj_player.t_anim);
        printw("# of bullets  %d\n", obj_p_shots.obj_array.size());
        printw("# of entities %d", obj_p_shots.obj_array.size() + obj_e_shots.obj_array.size() + obj_enemies.obj_array.size() + obj_powerups.obj_array.size());
        wrefresh(stdscr);
        #endif
    }
/**************************************************/
/**Free Memory**/
/**************************************************/
    return EXIT_SUCCESS;
}

Here is a snapshot
[Image: L4Tqs8A.png]

Here is a link to the app
SHMUP

Keys:
D-pad
Z to shoot
F11 to fullscreen toggle

Also I'll add a snapshot of the PickingSticks game, because the graphics are.. OH LOL.
Thanked by: Phaze
#4
Going to have to organize my object vectors for collision detections using some sort of QuadTree thingy.. *SIGH* ..stay tuned
(I should really be working on those Metroid sprites..)
Thanked by:
#5
I've completed my research on data structures and have come up with a pretty neat class that manages quadtrees.. but I have bigger plans now for my projects and this little shmup.. creating a fully fledged game engine with scripting language intergration (Lua) and advanced 2d physics (Box2d). I'm going to wait until Laurent releases SFML 2.0 officially before continuing any further work on this game. Until then I'll be studying Box2d, Lua and Qt. See you in a bit.
Thanked by:


Forum Jump: