C ++-implemented console-snake and Snake

Source: Internet
Author: User

C ++-implemented console-snake and Snake

Saturday can finally be used for a whole period of time.

If you want to write a snake, the first time you write about 140 lines is not too much.

In the future, ACM won't be able to compete.

Ps: I wanted to write a project, but I wrote it in a file for your convenience.

No major bugs tested, but no tests are successful.

 

 

Knowledge point:

1: refresh the window system ("cls ");

2: time. h is used to compile the clock system;

3: SetConsoleCursorPosition (GetStdHandle (STD_OUTPUT_HANDLE), coord); reset the cursor position, that is, if you want to output it there, move the cursor to that place and output it.

At first, another idea was to update the rear of the snake by just locating the cursor without storing arrays and maps.

4: others are some basic C ++ syntaxes.

You can take it for fun.

Code:

// Basic knowledge clock_t clock (void) returns the time unit from the beginning of the program to the present. The unit for converting CLOCKS_PER_SEC to the second is equivalent to 1000.0/* after the exe is opened, the normal game screen is output and a prompt is displayed in the middle of the map to start the game by pressing any key */# include <iostream> # include <windows. h> # include <conio. h> # include <time. h ># include <list ># include <string> using namespace std; class pai_snake {private: COORD zz; // used for cache forward vertex; list <COORD> snake; // Save the snake string ma [15]; // Save the int ction of the game screen; // Save the int grates in the forward direction of the current snake; int level; // The int rate of the game level; // game Play rate public: pai_snake () // initialize the game {level = 0; rate = 800; grates = 0; direction = 1; for (int I = 0; I <15; I ++) {if (I = 0 | I = 14) ma [I] = "################################## #"; // 35 else ma [I] = "#";} zz. X = 17; zz. Y = 7; snake. push_front (zz);} void draw (void) const // painting game interface {for (int I = 0; I <15; I ++) cout <ma [I] <endl; cout <"score:" <grates <"level:" <level <endl; cout <"w: Upper s: lower a: Left d: Right space: Pause "<endl;} void creat_food () // generate food {int x, y; do {x = rand () % 14 + 1; y = rand () % 34 + 1;} while (ma [x] [y]! = ''); Ma [x] [y] = '$';} void clock_system (void) // The clock system controls the game for {double Start = clock (); char ch = 'W'; bool flag; // initialize the forward direction creat_food (); while (1) {while (clock ()-Start) <rate &&! (Flag = _ kbhit (); // It is automatically read one second later or directly typed Start = clock (); if (flag) {ch = _ getch (); int direction _; if (ch = '') // pause function implementation {while (! _ Kbhit (); ch = _ getch (); Start = clock ();} else if (ch = 'R') {} switch (ch) {case 'W': ction _ = 1; break; case 'A': ction _ = 3; break; case's ': direction _ = 4; break; case 'D': ction _ = 2; break; default: direction _ = 0;} if (direction _ + direction! = 5 & direction _! = 0) direction = direction _; // If the input direction does not conflict with the current direction, change} switch (direction) {case 1: zz. X = snake. front (). x; zz. Y = snake. front (). y-1; break; case 2: zz. X = snake. front (). X + 1; zz. Y = snake. front (). y; break; case 3: zz. X = snake. front (). x-1; zz. Y = snake. front (). y; break; case 4: zz. X = snake. front (). x; zz. Y = snake. front (). Y + 1; break;} if (ma [zz. y] [zz. x] = '#' | ma [zz. y] [zz. x] = '. ') {COORD Coord; coord. X = 3; coord. Y = 8; SetConsoleCursorPosition (GetStdHandle (STD_OUTPUT_HANDLE), coord); cout <"game end"; Sleep (300); break;} else if (ma [zz. y] [zz. x] = '$') {grates ++; level = grates/10; level = min (5, level); rate = 800-level * 100; ma [snake. front (). y] [snake. front (). x] = '. '; snake. push_front (zz); ma [snake. front (). y] [snake. front (). x] = '@'; if (snake. size () == 429) {COORD coord; Coord. X = 3; coord. Y = 8; SetConsoleCursorPosition (GetStdHandle (STD_OUTPUT_HANDLE), coord); cout <""; Sleep (300); break;} creat_food ();} else {ma [snake. front (). y] [snake. front (). x] = '. '; snake. push_front (zz); ma [snake. front (). y] [snake. front (). x] = '@'; ma [snake. back (). y] [snake. back (). x] = ''; snake. pop_back () ;}system ("cls"); draw () ;}} void start (void) {draw (); COORD coord; coord. X = 13; coord. Y = 8; SetConsoleCursorPosition (GetStdHandle (STD_OUTPUT_HANDLE), coord); cout <"start by any key"; while (! _ Kbhit (); char ch = _ getch (); system ("cls"); draw (); clock_system () ;}}; int main () {retro_snake Game; game. start (); return 0 ;}

 

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.