I always wanted to write a game and play it on my own. I finally made up my mind today to write the most classic casual game-snake. Of course, I also learned from other people's programs, however, the entire code is different from other people's. Just go to the Code:
# Include <conio. h> # include <iostream> # include <vector> # include <time. h> using namespace STD; # define row 22 # define Col 22 struct point {char ch; int X; int y ;}; void refresh (vector <point> vec1, vector <point> vec2, point PT, int grade, int speed); void move (vector <point> & VEC, int Dir, int touch, bool & flag ); point produce_food (vector <point> VEC); bool isgameover (vector <point> vec1, vector <point> vec2); void main () {Co Ut <Endl; cout <"\ t" <"the game is about to begin! "; Int start = clock (); While (clock ()-start <= 1000); For (INT I = 3; I> = 0; I --) {start = clock (); While (clock ()-start <= 1000); System ("CLS"); cout <Endl; cout <"\ t" <I <Endl;} Point pt; vector <point> snake; int length = 1, head = 3, tail = 0; for (INT I = 0; I <3; I ++) {pt. ch = '*'; PT. X = 1; PT. y = I + 1; snake. push_back (PT);} PT. ch = '#'; PT. X = 1; PT. y = 4; snake. push_back (PT); vector <point> env; For (INT I = 0; I <row; I ++) {for (Int J = 0; j <Col; j ++) {if (I = 0 | I = row-1) {pt. ch = '-'; PT. X = I; PT. y = J; ENV. push_back (PT);} if (I! = 0 & I! = Row-1) {If (j = 0 | j = col-1) {pt. ch = '|'; PT. X = I; PT. y = J; ENV. push_back (PT);} else {pt. ch = ''; PT. X = I; PT. y = J; ENV. push_back (PT) ;}}} int direction = 77; int speed = 500, grade = 1; int timeover = 1; int touch = 0; point food; srand (time (0); Food = produce_food (ENV); bool flag = true; while (1) {touch = 0; Start = clock (); while (timeover = (clock ()-start <= speed ))&&! Kbhit (); If (timeover) {getch (); Direction = getch ();} If (food. X = (snake. end ()-1)-> X & food. y = (snake. end ()-1)-> Y) {touch = 1; length ++; food = produce_food (ENV);} If (length >=8) {length-= 8; grade ++; speed-= 50;} Move (snake, direction, touch, flag); If (isgameover (ENV, snake) & flag) {refresh (ENV, snake, food, grade, speed) ;}else {cout <"game over" <Endl; exit (1) ;}} void refresh (vector <point> vec1, vector <point> vec2, point PT, Int grade, int speed) {system ("CLS"); For (vector <point >:: iterator ite2 = vec2.begin (); ite2 <vec2.end (); ite2 ++) {for (vector <point >:: iterator ite1 = vec1.begin (); ite1 <vec1.end (); ite1 ++) {If (ite1-> X = ite2-> X & ite1-> Y = ite2-> Y) ite1-> CH = ite2-> CH ;}} for (vector <point >:: iterator ite = vec1.begin (); ite <vec1.end (); ite ++) {If (ITE-> X = pt. X & ite-> Y = pt. y) ite-> CH = '$'; cout <ite-> CH <''; if (ITE-> X = 4 & ite-> Y = col-1) c Out <"level:" <Grade; If (ITE-> X = 6 & ite-> Y = col-1) cout <"interval: "<speed; If (ITE-> X = 8 & ite-> Y = col-1) cout <" Developer: Dapeng Dai "; if (ITE-> Y = col-1) cout <Endl ;}} void move (vector <point> & VEC, int Dir, int touch, bool & flag) {point pt; vector <point >:: iterator ite = Vec. end ()-1; Switch (DIR) {Case 77: ite-> CH = '*'; PT. X = (ITE-> X); PT. y = (ITE-> Y) + 1; PT. ch = '#'; For (vector <point>: iterator it = Vec. begin (); It <Vec. end (); It ++) {if (IT-> X = pt. X & IT-> Y = pt. Y) Flag = false;} Vec. push_back (PT); If (! Touch) Vec. erase (VEC. begin (); break; Case 75: ite-> CH = '*'; PT. X = (ITE-> X); PT. y = (ITE-> Y)-1; PT. ch = '#'; For (vector <point>: iterator it = Vec. begin (); It <Vec. end (); It ++) {If (IT-> X = pt. X & IT-> Y = pt. y) Flag = false;} Vec. push_back (PT); If (! Touch) Vec. erase (VEC. begin (); break; Case 72: ite-> CH = '*'; PT. X = (ITE-> X)-1; PT. y = (ITE-> Y); PT. ch = '#'; For (vector <point>: iterator it = Vec. begin (); It <Vec. end (); It ++) {If (IT-> X = pt. X & IT-> Y = pt. y) Flag = false;} Vec. push_back (PT); If (! Touch) Vec. erase (VEC. begin (); break; case 80: ite-> CH = '*'; PT. X = (ITE-> x) + 1; PT. y = (ITE-> Y); PT. ch = '#'; For (vector <point>: iterator it = Vec. begin (); It <Vec. end (); It ++) {If (IT-> X = pt. X & IT-> Y = pt. y) Flag = false;} Vec. push_back (PT); If (! Touch) Vec. erase (VEC. begin (); break; default: break;} Point produce_food (vector <point> VEC) {int x = 0, y = 0; do {x = rand () % (Row-2) + 1; y = rand () % (Col-2) + 1;} while (VEC. begin () + (row * x + y)-> CH! = ''); Point pt; PT. X = x; PT. y = y; PT. ch = '$'; return pt;} bool isgameover (vector <point> vec1, vector <point> vec2) {If (vec2.end ()-1) -> X = 0 | (vec2.end ()-1)-> X = row-1 | (vec2.end ()-1) -> Y = 0 | (vec2.end ()-1)-> Y = col-1) {return false;} return true ;}The program has been written for a long time, but it can run perfectly.
I hope you will give me more advice.