Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

[C++][Allegro] Problem ze zmianą "długości" wężą

Ostatnio zmodyfikowano 2016-03-15 15:34
Autor Wiadomość
Dredo
Temat założony przez niniejszego użytkownika
[C++][Allegro] Problem ze zmianą "długości" wężą
» 2016-03-15 15:34:38
Snake prawie skończony lecz hmm wszystko powinno działać jak należy,ale długość węża nie chce się zmienić.
SnakLength powinno zmieniać się o 1"kostkę" co każdy pkt(Carrotz).
Gdzie znajduje się błąd,bo nnie mogę tego znaleźć.
Sam oficjalnie jestem noobem więc no :P
Ach i jeszcze program kompiluje się pomyślnie,więc tym bardziej nie wiem gdzie jest błąd.
C/C++
#include <allegro.h>
#include <winalleg.h>
#include <stdio.h>
#define TICS_PER_SECOND 50
#define MAX_FRAMESKIP 10
#define MAX_MENU_STATES 4

#define MAX_SPEED 10
const int SKIP_TICKS = 1000 / TICS_PER_SECOND;

BITMAP * buffer;



struct Vec2
{
    int x;
    int y;
};

const Vec2 ARENA_SIZE { 50, 50 };

enum States
{
    state_menu,
    state_game,
    state_continue,
    state_exit,
    state_leaderboard,
};

enum Positions
{
    pos_game = 30,
    pos_continue = 50,
    pos_exit = 70,
    pos_leaderboard = 90,
};

States menu( int * state )
{
    int value;
    if( keypressed() )
    {
        value = readkey();
        switch( value >> 8 )
        {
        case KEY_DOWN:
            if( * state < MAX_MENU_STATES )( * state ) ++;
           
            break;
        case KEY_UP:
            if( * state > 1 )( * state ) --;
           
            break;
        case KEY_ENTER:
            return States( * state );
        }
    }
    return state_menu;
}

void game( Vec2 * snake, Vec2 * direction, int * SnakeLength, int speed, int * counter, Vec2 * CurrentCarrotz, int * carrotz )
{
    int value;
    if( keypressed() )
    {
        value = readkey();
        switch( value >> 8 )
        {
        case KEY_DOWN:
            if( direction->y == 0 )
            {
                direction->x = 0;
                direction->y = 1;
            }
            break;
        case KEY_UP:
            if( direction->y == 0 )
            {
                direction->x = 0;
                direction->y = - 1;
            }
            break;
        case KEY_RIGHT:
            if( direction->x == 0 )
            {
                direction->x = 1;
                direction->y = 0;
            }
            break;
        case KEY_LEFT:
            if( direction->x == 0 )
            {
                direction->x = - 1;
                direction->y = 0;
            }
            break;
        }
    }
    if( MAX_SPEED - speed == * counter )
    {
        Vec2 tmp1 = snake[ 0 ], tmp2;
        for( int i = 1; i <* SnakeLength; i++ )
        {
           
            tmp2 = snake[ 1 ];
            snake[ i ] = tmp1;
            tmp1 = tmp2;
           
        }
        snake[ 0 ].x += direction->x;
        snake[ 0 ].y += direction->y;
        * counter = 0;
    }
    ( * counter ) ++;
   
    if( snake[ 0 ].x == CurrentCarrotz->x && snake[ 0 ].y == CurrentCarrotz->y )
    {
        * SnakeLength += 1;
        * carrotz += 1;
        CurrentCarrotz->x = rand() % ARENA_SIZE.x;
        CurrentCarrotz->y = rand() % ARENA_SIZE.y;
        snake[ * SnakeLength - 1 ].x = snake[ * SnakeLength - 2 ].x - direction->x;
        snake[ * SnakeLength - 1 ].y = snake[ * SnakeLength - 2 ].y - direction->y;
    }
   
}
void display_game( Vec2 * snake, int SnakeLength, Vec2 CurrentCarrotz, int carrotz )
{
    int color_bialy = makecol( 255, 255, 255 );
    int rozmiar_gry_piks = SCREEN_H - 50;
    int color_zielony = makecol( 0, 255, 0 );
    int color_czerwony = makecol( 255, 0, 0 );
    int color_zloty = makecol( 255, 215, 0 );
   
    char text_carrotz[ 20 ];
    sprintf( text_carrotz, "Carrotz: %d", carrotz );
   
    const Vec2 ARENA_POSITION = { SCREEN_W - SCREEN_H + 50 / 2, 50 };
    rect( buffer, ARENA_POSITION.x - 600, ARENA_POSITION.y, ARENA_POSITION.x + rozmiar_gry_piks, SCREEN_H - 1, color_czerwony );
    textout_centre_ex( buffer, font, text_carrotz, SCREEN_W / 2, 25, color_zloty, 0 );
    float fieldsize =( rozmiar_gry_piks / ARENA_SIZE.y );
    for( int i = 0; i < SnakeLength; i++ )
    {
        rectfill( buffer,
        ARENA_POSITION.x + fieldsize * snake[ i ].x,
        ARENA_POSITION.y + fieldsize * snake[ i ].y,
        ARENA_POSITION.x + fieldsize * snake[ i ].x + fieldsize - 2,
        ARENA_POSITION.y + fieldsize * snake[ i ].y + fieldsize - 2,
        color_zielony );
    }
    rectfill( buffer,
    ARENA_POSITION.x + fieldsize * CurrentCarrotz.x,
    ARENA_POSITION.y + fieldsize * CurrentCarrotz.y,
    ARENA_POSITION.x + fieldsize * CurrentCarrotz.x + fieldsize - 2,
    ARENA_POSITION.y + fieldsize * CurrentCarrotz.y + fieldsize - 2,
    color_zloty );
}
void display_menu( States state )
{
    int color_bialy = makecol( 255, 255, 255 );
    int color_czerwony = makecol( 255, 0, 0 );
    int color_niebieski = makecol( 0, 0, 255 );
    int color_zielony = makecol( 0, 255, 0 );
   
    textout_centre_ex( buffer, font, "SNAKE by Kacper", SCREEN_W / 2, 10, color_niebieski, 0 );
    textout_centre_ex( buffer, font, "New Game", SCREEN_W / 2, 30, color_bialy, 2 );
    textout_centre_ex( buffer, font, "Leadrboard", SCREEN_W / 2, 70, color_bialy, 2 );
    textout_centre_ex( buffer, font, "Continue", SCREEN_W / 2, 50, color_bialy, 2 );
    textout_centre_ex( buffer, font, "Exit", SCREEN_W / 2, 90, color_czerwony, 2 );
   
    int position;
    switch( state )
    {
    case state_game:
        position = pos_game;
        break;
    case state_continue:
        position = pos_continue;
        break;
    case state_leaderboard:
        position = pos_leaderboard;
        break;
    case state_exit:
        position = pos_exit;
        break;
    }
    rectfill( buffer, SCREEN_W / 2 - 100, position, SCREEN_H - 180, position + 10, color_zielony );
}

inline void init()
{
    allegro_init();
    set_color_depth( 32 );
    set_gfx_mode( GFX_AUTODETECT_FULLSCREEN, 1366, 768, 0, 0 );
   
    install_timer();
    install_keyboard();
    install_mouse();
}

inline void deinit()
{
    destroy_bitmap( buffer );
    clear_keybuf();
    allegro_exit();
}

int main()
{
    init();
    Vec2 snake[ ARENA_SIZE.x * ARENA_SIZE.y ];
    Vec2 direction = { 0, - 1 };
    Vec2 CurrentCarrotz = { 5, 5 };
    int SnakeLength = 5;
    int carrotz = 0;
   
    for( int i = 0; i < SnakeLength; i++ )
    {
        snake[ i ].x = ARENA_SIZE.x / 2;
        snake[ i ].y = ARENA_SIZE.y / 2 + i;
    }
   
    States currentState = state_menu;
    int currentMenuState = 1;
    int next_game_tick = GetTickCount();
    int loops;
    int speed = 5, counter = 0;
    float interpolation;
    buffer = create_bitmap( SCREEN_W, SCREEN_H );
    while( !key[ KEY_ESC ] )
    {
        loops = 0;
        while( GetTickCount() > next_game_tick && loops < MAX_FRAMESKIP )
        {
            loops++;
            next_game_tick += SKIP_TICKS;
            switch( currentState )
            {
            case state_menu:
                currentState = menu( & currentMenuState );
                break;
            case state_game:
                game( snake, & direction, & SnakeLength, speed, & counter, & CurrentCarrotz, & carrotz );
                break;
            }
        }
       
        clear( buffer );
        switch( currentState )
        {
        case state_menu:
            display_menu( States( currentMenuState ) );
            break;
        case state_game:
            display_game( snake, SnakeLength, CurrentCarrotz, carrotz );
            break;
        }
        blit( buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H );
       
    }
    deinit();
    return 0;
}
END_OF_MAIN()
P-146033
« 1 »
  Strona 1 z 1