Users browsing this thread: 1 Guest(s)
Ailit's Game thread (Games + Source Code)
#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


Messages In This Thread
RE: teh best games - by Ailit - 02-10-2013, 10:03 AM
RE: teh best games - by Ailit - 02-22-2013, 09:36 PM

Forum Jump: