[C Entry, c Entry

Source: Internet
Author: User

[C Entry, c Entry

The food in the game is not as complex as it is. Especially in the greedy Snake game, what I think of food is very simple:

1. It must belong to the world before it can appear in the world. It is impossible for a food that does not belong to the world to appear in the world; but there may be a food that belongs to the world, but it does not appear in the world (that is, the color of food is the same as that of the world, therefore, you cannot see the food ). This is like a ghost. It may exist in this world, but we cannot see it.

2. A food belonging to the world has a place in the world.

3. It has color and size.

Therefore, the structure definition of food is obvious!

typedef struct Food{    World *world;    int x, y, size;    SDL_Color color;} Food;

 

With food, if I want to create it in the world, I have to provide two basic functions to manipulate it:

extern Food * SNK_CreateFood(World *world, int size);extern void SNK_DestroyFood(Food *food);

Now, food can be created in the world, and the world begins to become colorful.

 

However, the problem arises: When you create a food, you only know how much food you want to create, where you want to create it, and what colors you want to create. At the beginning, you do not know how much food you want to create. After the food is created, we must try to improve it to make food different. Therefore, I set the following two functions to change the attributes of a food, provided that the food must exist:

extern void SNK_SetFoodPosition(Food *food, int x, int y);extern void SNK_SetFoodColor(Food *food, Uint8 r, Uint8 g, Uint8 b, Uint8 a);

 

At the beginning, I expected that a food with color and size could exist in the world. With the above method, I was enough to create such a food. Therefore, I want to present the food that meets my vision in the world:

extern void SNK_DrawFood(Food *food);

 

At this point, all the methods of food are defined. Although there are only a few functions, they are enough in the world of snakes.

 

The complete food definition in my snk-food.h document is as follows:

#ifndef SNAKE_FOOD_H_HEADER#define SNAKE_FOOD_H_HEADER#define FOOD_INIT_COLOR 0, 0xff, 0, 0xff#include "snk-world.h"typedef struct Food{    World *world;    int x, y, size;    SDL_Color color;} Food;extern Food * SNK_CreateFood(World *world, int size);extern void SNK_DestroyFood(Food *food);extern void SNK_DrawFood(Food *food);extern void SNK_SetFoodPosition(Food *food, int x, int y);extern void SNK_SetFoodColor(Food *food, Uint8 r, Uint8 g, Uint8 b, Uint8 a);#endif

Macro FOOD_INIT_COLOR is used only in the SNK_SetFoodColor function for convenience.

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.