Making a Duck Hunt like, the story of South Park Hunt

My first vidéo game !

Making a Duck Hunt like, the story of South Park Hunt

Once of the coolest project of the first year of epitech is the first graphical one.

The purpose of this project is to create a Duck Hunt Like game, in C with CSFML. Witch is pretty cool, it’s the first project where you can do what you want, use all your imagination, and not do something very linear.

Starting the project

First one, you need to understand the scope of the project. Epitech asking us a list of mandatory things. If you dont have it ? Sorry not sorry. Here is the basic for the project :

- The window must be closed using events.
- The program must manage the input from the mouse click.
- The program must contain animated sprites rendered thanks to sprite sheets.
- The program must contain moving (rotating, translating, or scaling) elements.
- The program must accept the “-h” option, then display a short description of the program,
and the available user inputs.

Create the window

First thing i want to do is create the window, a first window with a small menu (play and quit game), it’s pretty easy to do, and having a menu is a bonus soo let’s cook :

void do_menu_window(void)
{
    window_mode_t window_mode = {{1900, 1080, 32}};
    texture_data_t texture_data;
    sfRenderWindow *window;
    sfEvent event;
    sfSprite *sprite;

    window = create_window(&window_mode);
    texture_data.texture = SFTCF("src/params/welkom.jpg", NULL);
    sprite = create_sprite(&texture_data);
    while (sfRenderWindow_isOpen(window)) {
        handle_events(window, &event);
        DW(window, sprite, create_play(window, 50, 200),
            create_quit(window, 50, 700));
            can_use_clic(window, event);
    }
    sfSprite_destroy(sprite);
    sfTexture_destroy(texture_data.texture);
    sfRenderWindow_destroy(window);
}

This is my “main” function for my menu. It’s create the window with a background and with another function,

sfSprite *create_play(sfRenderWindow *window, float x, float y)
{
    sfTexture *texture =
            sfTexture_createFromFile("src/params/Start.png", NULL);
    sfSprite *sprite = sfSprite_create();

    sfSprite_setTexture(sprite, texture, sfTrue);
    sfSprite_setScale(sprite, (sfVector2f){1, 1});
    sfSprite_setPosition(sprite, (sfVector2f){x, y});
    return sprite;
}

sfSprite *create_quit(sfRenderWindow *window, float x, float y)
{
    sfTexture *texture =
            sfTexture_createFromFile("src/params/quit.png", NULL);
    sfSprite *sprite = sfSprite_create();

    sfSprite_setTexture(sprite, texture, sfTrue);
    sfSprite_setScale(sprite, (sfVector2f){1, 1});
    sfSprite_setPosition(sprite, (sfVector2f){x, y});
    return sprite;
}

create 2 buttons with actions when cliqued by the user. (I didn’t put all the code beacause it’s mostly a blog, and not my github account).

So now i have a fully functionnal menu. Next step : The game

The game

So, i still have some skill issue. Instead of cliquing on play and launch the game. The menu window will destroy himself and a new window will spawn. The menu window.

void do_create_window(void)
{
    sfSprite *sprite_kenny;
    window_mode_t window_mode = {{1900, 1080, 32}};
    texture_data_t texture_data;
    sfRenderWindow *window;
    sfEvent event;
    sfSprite *sprite;

    window = create_window(&window_mode);
    texture_data.texture = SFTCF("src/params/BUS_STOP.jpg", NULL);
    sprite = create_sprite(&texture_data);
    sprite_kenny = create_kenny(window, 50, 10);
    srand(time(NULL));
    while (sfRenderWindow_isOpen(window)) {
        handle_events(window, &event);
        if (kill_kenny(&sprite_kenny, window, event))
            sprite_kenny = create_kenny(window, rand() % 1900, rand() % 1080);
        move_kenny(sprite_kenny, window);
        DW(window, sprite, sprite_kenny, create_life(window, 50, 900));
    }
    continue_window(sprite, window, texture_data);
}

As you can see, the code is pretty similar. I just change the background, added my kenny sprite (Oh my god, they kill kenny…) and the south_park logo (should have been the life, but didn’t have time to fully implement it)

So now, i need to move on the next file, who is the file for our dear freind, Kenny McCormick

sfSprite *move_kenny(sfSprite *sprite, sfRenderWindow *window)
{
    sfVector2f pos = sfSprite_getPosition(sprite);

    sfSprite_setPosition(sprite, (sfVector2f){pos.x + 5, pos.y});
    if (pos.x > 1920) {
        sfSprite_setPosition(sprite, (sfVector2f){-100, pos.y});
    }
    if (pos.y > 1080) {
        sfSprite_setPosition(sprite, (sfVector2f){pos.x, 0});
    }
    return sprite;
}

sfSprite *create_kenny(sfRenderWindow *window, float x, float y)
{
    sfTexture *texture = SFTCF("src/params/Kenny.png", NULL);
    sfSprite *sprite = sfSprite_create();

    sfSprite_setTexture(sprite, texture, sfTrue);
    sfSprite_setScale(sprite, (sfVector2f){0.5, 0.5});
    sfSprite_setPosition(sprite, (sfVector2f){x, y});
    return sprite;
}

int kill_kenny(sfSprite **sprite, sfRenderWindow *window, sfEvent event)
{
    static int kenny_deaths;
    sfVector2i mouse_pos = sfMouse_getPositionRenderWindow(window);
    sfFloatRect bounds = sfSprite_getGlobalBounds(*sprite);
    sfTexture *texture = (sfTexture *)sfSprite_getTexture(*sprite);

    if (event.type == sfEvtMouseButtonPressed) {
        if (sfFloatRect_contains(&bounds, mouse_pos.x, mouse_pos.y)) {
            play_gun_music();
            kenny_deaths++;
            mini_printf("Oh my God, you've killed Kenny!\n");
            mini_printf("You're fucking bastard\n");
            mini_printf("Kenny has been killed %d times\n", kenny_deaths);
            mini_printf("\n");
            sfTexture_destroy(texture);
            sfSprite_destroy(*sprite);
            *sprite = create_kenny(window, rand() % 1900, rand() % 1080);
        }
    }
    return 0;
}

With this file

- Kenny can spawn
- We can kill kenny
- Kenny move
- Kenny Respawn

So actually i need to do

- The program must contain animated sprites rendered thanks to sprite sheets.
- The program must accept the “-h” option, then display a short description of the program,
and the available user inputs.

Do the -h (help) is the easylest :

int help_hunter(void)
{
    my_putstr("USAGE\n");
    my_putstr("./my_hunter\n");
    my_putstr("\n");
    my_putstr("DESCRIPTION\n");
    my_putstr("My_Hunter is a game based on the famous Duck Hunt game.\n");
    my_putstr("The goal is to kill as many Kenny as possible.\n");
    my_putstr("Kenny is floating but don't worry it's absolutly a thing.\n");
    my_putstr("\n");
    my_putstr("RULES\n");
    my_putstr("Kill Kenny with your Glock 17 (with ur left clic)\n");
    my_putstr("Kenny must die, no alive kenny will be tolered\n");
    my_putstr("\n");
    my_putstr("CREDITS\n");
    my_putstr("Alexis Drago-Beltran\n");
    my_putstr("Powered by Music\n");
    my_putstr("Cult of the reaper save me\n");
    my_putstr("\n");
    return 0;
}

So, skill issue : i didn’t do a sprite. And i need one ==> so i need to do some bonus (making people forgot i didn’t do sprite)

sfMusic *play_music(void)
{
    sfMusic *music = sfMusic_createFromFile("src/params/music/southsong.mp3");
    sfBool loop;

    sfMusic_setLoop(music, loop);
    sfMusic_play(music);
    return music;
}

sfMusic *play_gun_music(void)
{
    sfMusic *music = sfMusic_createFromFile("src/params/music/pan.mp3");

    sfMusic_play(music);
    return music;
}

This is a small file “musik” who…… Yeah it’s play music. When you start the game it’s play the south park intro, and in game, there is a gun sound when you shoot at kenny !

Summary

So, it may be low for a first post on a blog. But I wanted to quickly explain what I did and "how" without going into too much detail. Nevertheless the project was cool, it's a nice project where you can really do what you want and that's what's fun, and at the same time that's what can lose you