C ++ achieved greed snake

xiaoxiao2021-03-06  71

The pass is passed under TC 3.0, because TC 3.0 does not support the Bool type, so a modified type variable Bool is defined in the program If you run in the VC or BC, you can take this BOOL type. Core algorithm for the snake How to achieve mobile and eaten food. When you encounter food, enter the next node in the current movement direction, and redraw this node with the color of the snake node. Then take the node refers to the nodes referred to, and The game frame inside the background color is redrawged from the team's node, so that the movement can be achieved. When you eat food, just enter the team .// Greedsnake.cpp # include #include #include #include #include #include #include "conf.h"

Typedef struct node {int x, y; struct node * next;} node;

TypedEf struct {node * head, * tail; int lay;} snake; typef struct {int LEFT, TOP, RIGHT, BOTTOM;} Frame

Typedef enum // four directions {UP, Down, Left, Right} Direction; type} Bool; Void InitgraphMode (); // Initialization Graphics Drive Void ClosegraphMode (); Void Foot (int, int); void foot (int, int); Void Head (int, int); void createframe (); // Draw Void createSnake () complete the entire game framework (); // Create a snake of two nodes, each section of the snake is a node Bool PlayGame in the queue ( ); // The main function of the game, INT Hit (int, int); // Judging whether the off-road, or hit itself, the two parameters are the X, Y coordinate BOOL GAMEOVER (); // Draw The dialog box void enqueue (Node); // into the team function node dequeue (); // clear keyboard buffer, this function can eliminate the keyboard buffer, this function can eliminate the non-stop button influences

Snake snake; Frame frame; void main () {InitGraphMode (); do {CreateFrame ();} while (PlayGame ()); CloseGraphMode ();} void InitGraphMode () {int gdriver = DETECT, gmode; initgraph (& gdriver, & gmode, "../ bgi /"); CLEARDEVICE ();} void closegraphmode () {cleangraphmode () {cleangraPH ();} void createframe () {setbkcolor (cyan); // The following four lines of code are used to calculate The upper left corner of the main frame and the coordinate frame.left = (getmaxx () 1-blockwidth * rowofframe) / 2; frame.top = (GetMaxy () 1-blockHeight * columnofframe) / 2; frame.right = frame.left blockWidth * RowOfFrame; frame.bottom = frame.top blockHeight * ColumnOfFrame; Head (frame.left 100, frame.top-20); setfillstyle (SOLID_FILL, LIGHTGRAY); bar (frame.left, frame. Top, frame.right, frame.bottom; setLinesTyle (Solid_Line, 1, 1); setColor (Darkgray); line (frame.right, frame.top, frame.right, frame.top); line (frame.left, Frame.top, frame.left, frame.bottom; setLinesTyle (Solid_Line, 1, 1); setColor (White); line (frame.left, frame.b Ottom, Frame.right, Frame.Bottom; Line (Frame.right, Frame.Top, Frame.right, Frame.bottom); SetLinesTyle (Dotted_Line, 1, 1); SetColor (Blue); for (int Row = 1 ; row x = frame.left blockwidth; node1-> y = frame.top; node1->

Next = null; snake.tail = node1; node2-> x = frame.left; node2-> y = frame.top; node2-> next = snake.tail; snake.head = node2; snake.length = 2; setFillStyle (Solid_Fill, Blue); bar (snake.head-> x 1, snake.head-> y 1, snake.Head-> x blockwidth-1, snake.head-> y blockHeight-1); bar (snake.tail-> x 1, snake.tail-> y 1, snake.tail-> x blockwidth-1, snake.tail-> y blockheight-1);} Bool PlayGame () {INT Speed = 300, Key; Direction CurrentDirection = Right; Node Randomnode; Node NewNode, Outnode; Bool NeedNode = true; Bool Overlap = False; Int Randx, Randy; CreateSnake ();

while (true) {if (neednode == true) {randomize (); do {randx = frame.left rand ()% RowOfFrame * BlockWidth; randy = frame.top rand ()% ColumnOfFrame * BlockHeight; for (Node * p = snake.head; p! = null; p = p-> next) // hit itself if (randx == p-> x && randy == p-> y) {Overlap = true; Break;}} while Overlap == true); randomnode.x = randx; randomnode.y = randy; randomnode.next = null; setFillStyle (Solid_Fill, Red); bar (randomnode.x 1, randomnode.x blockwidth -1, randomnode.y blockheight-1); NeedNode = false;}

IF ((Key = Bioskey (1))! = 0) {switch (key) {Case Esc: Return False; Case Up: if (CURRENTDIRECTION! = DOWN) CURRENTDIRECTION = Up; ClearKeyBuf (); Break; Case Down: IF (! CurrentDirection = up) CurrentDirection = down; ClearKeyBuf (); break; case lEFT: if (! CurrentDirection = right) CurrentDirection = left; ClearKeyBuf (); break; case rIGHT: if (! CurrentDirection = left) CurrentDirection = right; ClearKeybuf (); Break; Case Pageup: Speed ​​= Speed-100; IF (Speed ​​<100) Speed ​​= 100; ClearKeyBuf (); Break; Case Pagedown: Speed ​​= Speed ​​ 100; IF (Speed> 500) Speed ​​= 500; ClearKeybuf (); Break; default: Break;}} int headx = snake.tail-> x; int heady = snake.tai l-> y; switch (CurrentDirection) {case up: heady- = BlockHeight; break; case down: heady = BlockHeight; break; case left: headx- = BlockWidth; break; case right: headx = BlockWidth; break;} if (HETX, Heady) // WHETHER THE SNAKE HIT The Wall or itself returnomeOver (); else {// Eat if (headx == randomnode.x&& "== randomnode.y) {enqueue (randomnode); setFillStyle Solid_fill, blue); bar (randomnode.x 1, randomnode.y 1, randomnode.x-1 blockwidth, randomnode.y-1 blockHeight); NeedNode = true;} else // no et {newNode.x = Headx; NewNode.y = Heady; NewDe.next = NULL;

Enqueue (newNode); outNode = Dequeue (); setfillstyle (SOLID_FILL, LIGHTGRAY); bar (outNode.x 1, outNode.y 1, outNode.x BlockWidth-1, outNode.y BlockHeight-1); setfillstyle (Solid_Fill, Blue); Bar (NewNode.x 1, NewNode.y 1, NewNode.x-1 Blockwidth, NewNode.y-1 BlockHeight);}} delay (speed);}} void clearKeybuf () {DO BIOSKEY (0); WHILE (Bioskey (INT X, INT Y) {setColor (Blue); Outtextxy (x, y, "Writer: [T] Realxl E-mail: RealGeneral @ Hotmail.com ");} void head (int x, int y) {setColor (red); OutTextxy (x, y," greedy snake ");} void enqueue (node ​​innode) {node * p = new node; p -> x = innode.x; p-> y = innode.y; p-> next = INNODE.NEXT; snake.tail-> next = p; snake.tail = p; snake.length ;} node debrough () {Node * P = snake.head; node outnode = * p; snake.head = p-> next; snake.length -; delete p; return outnode;}

INT HIT (INT X, INT Y) {IF (x = frame.right || y = frame.bottom) // Hit the wall return 1; Node * p = snake.head-> next; for (int i = snake.length-1; i> 3; i -, p = p-> next) // hit itself if (x == P-> x && y = = P-> y) Return 1; Return 0;}

转载请注明原文地址:https://www.9cbs.com/read-110042.html

New Post(0)