[Original] greedy Snake game with C ++ (tc3.0)-(1)

Source: Internet
Author: User
Tags gety

// The idea of using the linked list in the game comes from the golden beetle site. Thank you.

/* Snake Game Engine: Made by emilmatthew 05/1/16
Compile enviroment tc3.0
Considerations
1. Set the BGI file path.
2. Move the upstream and downstream keys to control the snake movement. Press the Enter key to pause and press the ESC key to exit.
*/
/*
Difficulties and highlights:
1) The structure of the linked list is used to represent the object of a snake. We are required to use a linked list class.
2) When drawing, an abstract class abstractdraw is used, and its derived classes have three:
Drawhead, drawbody, drawtail, and drawbean are used to draw the head, body, tail, and beans respectively.
Painting is required at the beginning. During the game, you only need to re-draw the head and erase the tail.
3) Since TC3 does not support youyuan, You need to insert the linked list into the snake class, which is slightly uncomfortable.
3) Collision Detection:
A) add a random number to an array to generate a position that does not conflict with the snake and draw beans. When the coordinates of the Snake Head and the beans are duplicated, the snake grows.
B) collision detection with the wall. Check whether X and Y are out of bounds.

Core Algorithm Description:

1. Declare related objects and variable variables in the game:
Screenlayout test; drawhead SDH; drawbody SDB; cleantail SCT...
2. initialize the graphics library to draw scene graphics and complete snakes at the beginning.
SDH. Draw (H. getx (), H. Gety (); SDB. Draw (b1.getx (), b1.gety ());....
At the same time, a detection function is used to randomly generate a bean at the intersection of the snake.
3. Use a while (gamestatur! = Exit) to control the entire game process {
4.1 Use while (bioskey (1) = 0) to enter a loop without pressing a button {
A) render: first, enter the dynamic drawing area and use the gotbean returned each time to determine the method for drawing a new frame: Do you want to add a section?
If (gotbean)
Insert the chain table in the header and the second section, and change the related direction values and MX, my, and mdir values.
The snake head changes to the snake body and draws the snake head at the mdir (Direction identifier variable. And generate new beans.
Else
Traverse the snake's linked list and pass the Dir (Direction identifier variable) of the previous section to a section (note that it is traversing from the end of the linked list to the header ),
And change the values of MX, my, and Dir accordingly.
The snake head changes to the snake body, draw a snake head at the mdir (Direction identifier variable), and wipe the snake tail.
Delay2 (0.1); // self-compiled delay function with an accuracy of 0.05 seconds. It does not change greatly with machine performance.
B) datachecking {
1 touch Edge Detection: Yes, then gamestatur = failure;
2. Check whether your detection is successful.: Yes, then gamestatur = failure
3. obstacle: Yes, gamestatur = failure
4 check whether beans are eaten: Yes, then gotbean = true;
}
4.2 press the key and use key = bioskey (0); to obtain the key value.
Switch (key ){
Case keyup :... Perform corresponding data processing.
The upper and lower left keys change the Dir direction of the header.
ESC changes gamestatur to exit;
Enter indicates that the game is suspended. Use an embedded keyboard for detection.
} // Looping body
This is the core implementation of greedy games. The use of linked lists makes the design of algorithms much easier. Of course, the details of the implementation are further refined and improved.
*/
# Include <dos. h>
# Include <bios. h>
# Include <time. h>
# Include <graphics. h>
# Include <conio. h>
# Include <stdio. h>
# Include <iostream. h>
# Include <stdio. h>
# Include <stdlib. h>
Char Address [] = "C: // TC // BGI ";
Int score = 0;
Typedef int bool;
Const int true = 1;
Const int false = 0;

Const int fudu = 10;

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 secperframes = 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 by, int Col = 10 );
Void printword (int x, int y, const char far * textstring, int font = 1,
Int direction = 0, int charsize = 0, int Col = 12 );
Void bar2 (INT Tx, int ty, int BX, int by, int patterncol = blue, int patterns = solid_fill );
Void shapefill (int x, int y, int bordercol, int patterncol = blue, int patterns = solid_fill );
Void circle2 (int x, int y, int R, int Col = red );
Void circle3 (int x, int y, int R, int Scol = red, int fcol = red, int patterns = solid_fill );
Void line2 (INT Tx, int ty, int BX, int by, int Col = 10 );
Void ellipse2 (int x, int y, int xradius, int yradius, int Col = green, int stangle = 0,
Int endangle = 360 );
Void backoutword (INT Tx, int ty, int BX, int by, const char far * textstring,
Int COLB = yellow, int colf = green, int font = 1, int charsize = 0, int direction = 0 );
};
Void supercol: rect2 (INT Tx, int ty, int BX, int by, int col ){
Setcolor (COL );
Rectangle (TX, Ty, BX, );
}
Void supercol: printword (int x, int y, const char far * textstring, int font, 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, );
}
Void supercol: shapefill (int x, int y, int bordercol, int patterncol, int patterns ){
Setfillstyle (patterns, patterncol );
Floodfill (X, Y, bordercol );
}
Void supercol: circle2 (int x, int y, int R, int col ){
Setcolor (COL );
Circle (X, Y, R );
}
Void supercol: line2 (INT Tx, int ty, int BX, int by, int col ){
Setcolor (COL );
Line (TX, Ty, BX, );
}
Void supercol: circle3 (int x, int y, int R, int Scol, int fcol, int patterns ){
Circle2 (X, Y, R, Scol );
Shapefill (X, Y, Scol, fcol, patterns );
}
Void supercol: ellipse2 (int x, int y, int xradius, int yradius, int Col, int stangle, int endangle ){
Setcolor (COL );
Ellipse (X, Y, stangle, endangle, xradius, yradius );
}
Void supercol: backoutword (INT Tx, int ty, int BX, int by, const char far * textstring, int COLB,
Int colf, int font, int charsize, int direction ){
Bar2 (TX, Ty, BX, by, COLB );
Printword (Tx + 3, Ty + 3, textstring, Font, direction, charsize, colf );
}

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.