[C Entry, c Entry

Source: Internet
Author: User

[C Entry, c Entry

Snake is the main character of the game, and the features to be implemented are also the most complicated. Because snakes have more than attributes and behaviors. It will change, eat, and grow bigger! And it will die! This is terrible. I have never understood complex code and can't write complicated code. So for snakes, I am very entangled in how to simply implement it.

 

There is no doubt that snakes also have the attributes of food. A snake must exist in the world, with its location and color. In this way, we can at least deduce a simple definition of a snake. But this is far from enough. If a snake is active, it must have speed and direction. If it can eat it, it must grow up. Therefore, aside from everything else, the simplest form should at least be like this:

typedef struct Snake{    World *world;    int x, y, size;    SDL_Color color;    int speed, length, direction;    struct Body    {        int x, y;        int direction;        struct Body *next;    } *body;} Snake;

What I want is simplicity. As a result, it's enough to bite your teeth!

 

Since I chose to create such a snake, I can only accept it.

extern Snake * SNK_CreateSnake(World *world, int size, int x, int y);extern void SNK_DestroySnake(Snake *snake);

 

To control the attributes of a snake, multiple functions must be implemented. As attributes interact with each other, more functions must be implemented. For example, if you change the direction of a snake, there will be two results: 1. Active, 2. will die! There are also two results for eating food: 1. Being able to grow up, and 2. being killed. Therefore, I subdivided these functions into attribute functions, behavior functions, and additional functions.

 

Property-related functions:

extern void SNK_SetSnakeSpeed(Snake *snake, int speed);extern void SNK_SetSnakeDirection(Snake *snake, int direction);extern void SNK_SetSnakeColor(Snake *snake, Uint8 r, Uint8 g, Uint8 b, Uint8 a);

Behavior-related functions:

extern void SNK_MoveSnake(Snake *snake);extern void SNK_GrowSnake(Snake *snake);

Additional functions:

extern int SNK_HasIntersection(Snake *snake, SDL_Rect rect);extern int SNK_GetSnakeStatus(Snake *snake);

 

When such a snake is completely created, I can expose it to the world without any worries.

extern void SNK_DrawSnake(Snake *snake);

 

This is the simplest way I can think of to implement a snake. Yes, it's just a snake! You will find that from the ground up, the snake game is all about eating food, but I have not defined it as a snake, including any food-related functions or variables, I just created my life from the perspective of a creator. What a snake eats is not what I want to care about. I created it and made rules to adapt it to the world.

 

The complete snake definition in the snk-snake.h file is as follows:

#ifndef SNK_SNAKE_H_HEADER#define SNK_SNAKE_H_HEADER#define SNAKE_UP            (1 << 0)#define SNAKE_LEFT          (1 << 1)#define SNAKE_DOWN          (1 << 2)#define SNAKE_RIGHT         (1 << 3)#define SNAKE_DIED          (1 << 4)#define SNAKE_MOVABLE       (1 << 5)#define SNAKE_INIT_COLOR    0,    0xff, 0, 0xff#define SNAKE_DIED_COLOR    0xff, 0,    0, 0xff#define SNAKE_CTMV_COLOR    0xff, 0xff, 0, 0xff#define SNAKE_INIT_SPEED    900#include "snk-world.h"typedef struct Snake{    World *world;    int x, y, size;    SDL_Color color;    int speed, length, direction;    struct Body    {        int x, y;        int direction;        struct Body *next;    } *body;} Snake;extern Snake * SNK_CreateSnake(World *world, int size, int x, int y);extern void SNK_DestroySnake(Snake *snake);extern int SNK_HasIntersection(Snake *snake, SDL_Rect rect);extern void SNK_MoveSnake(Snake *snake);extern void SNK_DrawSnake(Snake *snake);extern void SNK_GrowSnake(Snake *snake);extern int SNK_GetSnakeStatus(Snake *snake);extern void SNK_SetSnakeSpeed(Snake *snake, int speed);extern void SNK_SetSnakeDirection(Snake *snake, int direction);extern void SNK_SetSnakeColor(Snake *snake, Uint8 r, Uint8 g, Uint8 b, Uint8 a);#endif

The first few rows of macros define the direction of the snake, and then the state of the snake: Dead or movable. The following macros are used only when setting properties for convenience. The value of "Snake _ init_speed" is set to 900 ms, indicating the speed of the snake. It is a time value, not a moving distance.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.