// The idea of using the list in the game comes from the golden beetle site, thank you here.
/ * Greedy snake game engine: Made by Emilmatthew 05/1 / 16Compile Enviroment TC3.0 Note 1 Please set the BGI file path 2 to control the snake move, the Enter key is paused, the ESC button exits. * // * Difficulties and focus: 1) Use the structure of the linked list to represent the snake object, ask us to learn to use a chain table. 2) Draw, use a abstract class AbstractDraw, there are three: Drawhead, DrawBody, DrawTail, DrawBean They are used to distinguish, body, tail and beans. At the beginning, they must be painted. During the game process, only the head and wipe the tail. 3) Since TC3 does not support friend, the insertion of the linked list needs to be placed in the Snake class itself, slightly discomfort. 3) Collision Detection: a) Plus a random number of random procedures with a number of groups that do not intersect the snake, and draw a bean. When the coordinates of the snake head are coordinated with the bean coordinates, the snake growth section. B) Collision detection with the wall, CHECK, whether X, Y is out of bounds.
Core algorithm description:
1 Declaring the object and logo variables in the game: ScreenLayout Test; Drawhead SDH; DrawBody SDB; Cleantail Sct ... 2 Initialization The graphics library, draw the scene graphics and the complete snake. SDH.DRAW (H.GETX (), H.Gety ()); sdb.draw (b1.getx (), b1.get ()); .... At the same time, use a detection function without randomness with the snake intersection Grain beans. 3 Use a while (Gamestatur! = EXIT) to control the process of the entire game {4.1 using while (Bioskey (1) == 0) Enter a loop {a) Render when the button is entered, first entering the dynamics Drawing zone, use GotBean that is returned to the new frame: Is it necessary to grow one section? If (gotbean) is inserted into the action and change the relevant direction at the head and the second section. Values and MX, MY, MDIR values. The snake head turns a snake, and the snake head is drawn in the MDIR (direction identifier variable). And generate new beans. Else traversed to the snake list, pass the DIR (direction identification variable) of the previous section (note that the head is traversed from the end of the chain table), and changes the value of MX, MY and DIR The snake head change snake, painting the snake head, rub the snake tail. Delay2 (0.1); // Self-compiled Delay function, accuracy 0.05 seconds, no major change in the performance of the machine B) datacrest { 1 Touch detection: Yes, GameStatur = five; 2 whether it bits his own test.: Yes, if GameStatur = Failure 3 encountered an obstacle: Yes, GameStatur = Failure 4 does not eat the test of beans: Yes, Gotbean = true;} 4.2 After pressing the key, use key = bioskey (0); get the key value, and then use Switch (Key) {Case Keyup: ... to do the corresponding data processing. Among them: The upper and lower left and down will change the DIR direction of the head. Esc makes GameStatur into exit; Enter enables the game to be suspended, use an embedded keyboard detection.} // looping body This is the core implementation of the greedy game, using the chain list to make the algorithm's design easy Of course, there is another scrutiny and improvement in realization. * / # include
int const KeyUp = 0x4800; int const KeyDown = 0x5000; int const KeyLeft = 0x4b00; int const KeyRight = 0x4d00; int const KeyEnter = 0x1c0d; int const KeyEsc = 0x11b; void Delay2 (double Times, float SecPerFrame); enum GameStaturs {Gaming , WIN, FAILURE, EXIT}; Enum Direction {Up, Down, Left, Right}; GameStaturs GameStatur = Gaming; Float SecPERFRAME = 0.05;
Struct Obstacle {public: int x; int y; bool flag; obstacle () {x = 0; y = 0; flag = false;}}; struct bean {public: int X; int y; bool flag; bean () {x = 0; y = 0; FLAG = false;}};
// An integrated drawing class class supercol {public: void Rect2 (int TX, int Ty, int bx, int use, int co = 10); Void Printword (int X, int y, const char far * textstring, int in Font = 1, int direction = 0, int CHARSIZE = 0, int coL = 12); void bar2 (int TX, int Ty, int bx, int code, int patterncol = blue, int pattern = solid_fill); Void Shapefill (int x, int y, int bordercol, int pattern = solid_fill; void circle2 (int X, int y, int r, int co = red); void circle3 (int X, int y, int R, int 网Scol = red, int fcol = red, int pattern = solid_fill; void line2 (int TX, int Ty, int bx, int use, int co = 10); Void Ellipse2 (int X, int y, int xradius, int yradius , int col = 0, int end = 360); Void Backoutword (int TX, int Ty, int bx, int code, int colb = yellow, int colf= Green, int FONT, INT FONT = 1, int channel = 0);}; void supercol :: Rect2 (int TX, int Ty, int bx, int code, int co {setColor (col); Rectangle (TX, TY, BX , by); void supercol :: PrintWord (int X, Int, Int Direction, Int Charsize, INT COL) {setColor (col); setTextStyle (font, direction, charsize); Outtextxy (x, y, texTstring ); Void supercol :: bar2 (int TX, INT TY, INT BX, INT BY, INT PATTERNCOL, INT PATTERNS) {setfillstyle (patterns, patterncol); bar (TX, TY, BX, by);} void supercol: : Shapefill (int x, int y, int bordercol, int patterncol, int pattern) {setfillstyle (patterns, patterncol); floodfill (x, y, bordercol);} void supercol :: circle2 (int X, int y, int R) , INT col); Circle (x, y, r);} void supercol :: line2 (int TX, INT TY, INT BX, INT BY, INT COL) {setColor (Col); line (TX , TY, BX, BY);