VPN termux about China.net

snake part.3

#include <GLES/gl.h>
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
const int SIZE = 11;
const int SCREEN_WIDTH = SIZE * 60;
const int SCREEN_HEIGHT = SIZE * 30;
const int AREA = SCREEN_WIDTH * SCREEN_HEIGHT;
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
SDL_Surface *backgroundImage = NULL;
SDL_Event event;
SDL_Texture *background = NULL;
/* kursor */
SDL_Surface *kursor = NULL;
/* draw kursor rect */
SDL_Rect kursorDane;
/* needs 4 rect to use direct event mouse */
SDL_Rect butonL;
SDL_Rect butonP;
SDL_Rect butonU;
SDL_Rect butonD;
const int CIMA = 1;
const int BAIXO = 2;
const int ESQUERDA = 3;
const int DIREITA = 4;
unsigned direcao = 0;

struct food
{

    SDL_Rect posLivre;
    int x;
    int y;

} food;

struct snake
{
    /* head postion */
    int x;
    int y;
    /* try this */
    snake *boody;
    /* catch this */
    SDL_Rect segmento[AREA];
    /* how long is snace */
    int totalSegmento = 2;
    /* ??? */
    int dimensao;


} snake, head =
{
20, 2}, speed =

{
2, 2};

bool BodyCollision(int x, int y)
{

    for (int i = 2; i < snake.totalSegmento; i++)
        if ((snake.segmento[i].x == x) && (snake.segmento[i].y == y))
            return true;

    return false;
}

void line(SDL_Renderer * renderer)
{                                /* draw kursor mouse */

    SDL_SetRenderDrawColor(renderer, 255, 255, 0, SDL_ALPHA_OPAQUE);
    SDL_RenderDrawLine(renderer, kursorDane.x, kursorDane.y, kursorDane.x + 100,
                       kursorDane.y + 100);

}

void go()
{
    switch (direcao)
    {
    case DIREITA:
        head.y += speed.y;
        break;
    case ESQUERDA:
        head.y -= speed.y;
        break;
    case CIMA:

        head.x += speed.x;
        break;
    case BAIXO:

        head.x -= speed.x;
        break;
    }

}

/* generator radnnom postion food */
int RandomNumber(int high)
{
    return (rand() % (high));
}

/* initiation postion food */
void foodint()
{
    food.x = RandomNumber(600);
    food.y = RandomNumber(300);
}

/* draw food */
SDL_Rect InitFood(SDL_Renderer * renderer)
{


    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0xFF);
    food.posLivre.x = food.x;
    food.posLivre.y = food.y;
    food.posLivre.w = SIZE;
    food.posLivre.h = SIZE;
    SDL_RenderFillRect(renderer, &food.posLivre);
    return food.posLivre;
}

bool CheckCollision(SDL_Rect r1, SDL_Rect r2)
{
    if (r1.x > r2.x + r2.w)
    {
        return false;
    }
    else if (r1.y > r2.y + r2.h)
    {
        return false;
    }
    else if (r1.x + r1.w < r2.x)
    {
        return false;
    }
    else if (r1.y + r1.h < r2.y)
    {
        return false;
    }

    else
    {
        return true;
    }
}
 int pad(SDL_Renderer *render){
butonP.x=200;
butonP.y=400;
butonP.w=66;
butonP.h=66;
butonL.x=000;
butonL.y=400;
butonL.w=66;
butonL.h=66;
butonU.y=300;
butonU.x=100;
butonU.w=66;
butonU.h=66;
butonD.x=100;
butonD.y=400;
butonD.w=66;
butonD.h=66;
    SDL_SetRenderDrawColor(renderer, 255 ,255, 0, 0xFF);
        SDL_RenderFillRect(renderer, &butonD);
            SDL_SetRenderDrawColor(renderer, 255, 255, 0, 0xFF);
            SDL_RenderFillRect(renderer, &butonU);
                SDL_SetRenderDrawColor(renderer, 255, 255, 0, 0xFF);
                SDL_RenderFillRect(renderer, &butonL);
                    SDL_SetRenderDrawColor(renderer, 255, 255, 0, 0xFF);
                    SDL_RenderFillRect(renderer, &butonP);
}
void player(SDL_Renderer * renderer)
{                                /* this func draw rectangle of PLAYER in post
                                   hit.x hit.y and width 50 height 10 */

    SDL_Rect hit;
    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0xFF);

    hit.w = 10;

    hit.h = 10;

    snake.segmento[0].x = head.x;

    snake.segmento[0].y = head.y;
    snake.segmento[0].w = SIZE;
    snake.segmento[0].h = SIZE;

    for (int i = snake.totalSegmento; i > 0; i--)
    {
        snake.segmento[i] = snake.segmento[i - 1];
    }
    if (CheckCollision(snake.segmento[0], food.posLivre))
    {

        snake.totalSegmento += 11;
        foodint();
    }
    if (BodyCollision(head.x, head.y))
    {
        SDL_Quit();

    }
    for (int i = snake.totalSegmento; i > 0; i--)
    {
        /* SDL_RenderCopy(renderer, snake.segmentTexture, &snake.sprite[0],
           &snake.segmento[i]); */


        SDL_RenderFillRect(renderer, &snake.segmento[i]);
    }                            /* check collision with ball and player */

    if (head.x > SCREEN_WIDTH)
    {
        head.x = 0;
    }
    if (head.y > SCREEN_HEIGHT)
    {
        head.y = 0;
    }
    if (head.x < 0)
    {
        head.x = SCREEN_WIDTH;
    }
    if (head.y < 0)
    {
        head.y = SCREEN_HEIGHT;
    }
}

bool InitWindow()
{

    int imgFlag = IMG_INIT_PNG;

    IMG_Init(imgFlag);

    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        printf("ERRO\n");
        return false;
    }
    else
    {

        window =
            SDL_CreateWindow("Snake", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
                             SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL);

        if (window == NULL)
        {
            printf("ERRO\n");
            return false;
        }
        else
        {
            renderer =
                SDL_CreateRenderer(window, -1,
                                   SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
        }

    }

    if ((backgroundImage = SDL_LoadBMP("img/bg.bmp")) == NULL)
        printf("ERRO %s", SDL_GetError());
    background = SDL_CreateTextureFromSurface(renderer, backgroundImage);

    return true;
}




bool GetKeys()
{


    while (SDL_PollEvent(&event) != 0)
    {

        if (event.type == SDL_QUIT)
        {
            return 1;
        }
        if (event.type == SDL_KEYDOWN)
        {
            switch (event.key.keysym.sym)
            {

            case SDLK_UP:
                if (direcao != DIREITA)
                    direcao = ESQUERDA;
                break;
            case SDLK_DOWN:
                if (direcao != ESQUERDA)
                    direcao = DIREITA;
                break;
            case SDLK_RIGHT:
                if (direcao != BAIXO)
                    direcao = CIMA;
                break;
            case SDLK_LEFT:
                if (direcao != CIMA)
                    direcao = BAIXO;
                break;
            }
        }
        if (event.type == SDL_MOUSEMOTION)
        {
            kursorDane.x = event.motion.x;
            kursorDane.y = event.motion.y;

        }                        // If a key was released
        if (event.type == SDL_MOUSEBUTTONDOWN)
        {
            if (event.button.button == SDL_BUTTON_LEFT &&
CheckCollision(butonL, kursorDane))
{
    if (direcao != CIMA)
                    direcao = BAIXO;
                break;
            }
            if (event.button.button == SDL_BUTTON_LEFT &&
CheckCollision(butonP, kursorDane))
            {
                /* right */
                        if (direcao != BAIXO)
                    direcao = CIMA;
                break;

            }
                        if (event.button.button == SDL_BUTTON_LEFT &&
CheckCollision(butonD, kursorDane))
            {
                        if (direcao != ESQUERDA)
                    direcao = DIREITA;
                break;

            }
                        if (event.button.button == SDL_BUTTON_LEFT &&
CheckCollision(butonU, kursorDane))
            {
        if (direcao != DIREITA)
                    direcao = ESQUERDA;
                break;

            }
        }
    }
    return 0;
}


void GameLoop(bool & quit)
{
    quit = GetKeys();
    SDL_RenderClear(renderer);
    go();
    // foodint();
    line(renderer);
pad(renderer);
    SDL_RenderCopy(renderer, background, NULL, NULL);
    InitFood(renderer);
    player(renderer);
    SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE);
    SDL_RenderPresent(renderer);
}

void Close()
{
    SDL_DestroyWindow(window);
    window = NULL;
    SDL_DestroyTexture(background);
    SDL_FreeSurface(backgroundImage);
    SDL_DestroyRenderer(renderer);
    IMG_Quit();
    SDL_Quit();
}

int main(int argc, char *argv[])
{

    bool quit = false;

    InitWindow();
    while (!quit)
    {
        GameLoop(quit);
    }
    Close();
    return 0;
}

No comments:

Post a Comment