/ * Requirements to improve: Print () function * / # include
#include
INT score = 0; char a [] = "9999"; void * ball; int maxx; unsigned int size;
INT Head = 1; / * 0, 1, 2, 3 * / I = 3, J = 0, II = 0, JJ = 0; int LENGTH = 3; int x, y; / * snake head sign * / int Add = 1; / * Food Sign * / INT TII, TJJ; / * Front Snake Tail Coordinate * / Void Start_Image () {setLinesTyle (Solid_Line, 1, 2); SetColor (Green); Line (39, 39, 39, 441); Line (39, 441, 441, 441); LINE (441, 441, 441, 39); line (441, 39, 39, 39);
} void Draw_Image (int x, int y) {INT i = x * 400 / n 40; int J = Y * 400 / N 40; setfillstyle (1, 4); bar (j, i, j 400 / N, i 400 / n);} void eraser_image (int x, int y) {INT i = x * 400 / n 40; int J = Y * 400 / N 40; setFillStyle (1,0); bar (J, I, J 400 / N, I 400 / N);
Void Tu () {/ * tuxing jiemian * / maxx = getmaxx (); size = imageSize (210, 390, 450, 420); ball = malloc (size); setFillStyle (1, 10); BAR (160, 170, 473, 253); setfillstyle (1, 9); bar (163,173,470,250); setcolor (13); outtextxy (200,200, "Welcome to Greedy Snake Game"); setcolor (14); outtextxy (250,220, "Developer: Wu siyuan"); outtextxy (290,240, "2003.5"); outtextxy (210, 390, "Press any key to contunue ..."); GetImage (210, 390, 450, 420, ball);
While (! kbhit ()) {/ * No keyboard keys * / Putimage (210, 390, ball, xor_put); delay (30000);} cleardevice ();} void print () / * Output * / {/ * Convert to graphics mode * / int A, b; / * start_image (); * /
For (a = 0; a
Void snake () / * begin * / {Int a, b; for (a = 0; a A [i] [j] = 5; A [i-1] [j] = 4; A [i-2] [j] = 3; A [i-3] [j] = 2;} void del_tail ) / * Snake tail * / {int Number = a [ii] [jj]; TII = II; tjj = jj; / * Record front snake tail * / While (1) {IF (a [ii] [jj-1] == Number 1 || a [ii] [jj-1] == 2 && number == 30) {jj -; break;} IF (a [ii] [jj 1] == Number 1 || a [ii] [jj 1] == 2 && number == 30) {JJ ; Break;} IF (a [II-1] [JJ] == Number 1 || A [II-1] [JJ] == 2 && number == 30) {ii -; Break;} IF (a [ii 1] [jj] == Number 1 || a [ii 1] [jj] == 2 && number == 30) {II ; Break;} } Void Down () {head = 1; if (a [i 1] [j]> 1 && a [i 1] [j]! = 99) {Printf ("I hit myself!"); exit (1); } I ; IF (IE> 30) IE = 2; a [i] [j] = IE; IE ; del_tail (); a [tii] [tj] = 0; } void up () {head = 0; if (a [i-1] [j]> 1 && a [i-1] [j]! = 99) {Printf ("i hit myself!"); exit (1) } I -; IF (IE> 30) IE = 2; a [i] [j] = IE; IE ; del_tail (); a [Tii] [TJJ] = 0; } void left () {head = 2; IF (a [i] [j-1]> 1 && a [i] [j-1]! = 99) {Printf ("i hit myself!"); exit (1) } J -; IF (IE> 30) IE = 2; a [i] [j] = IE; IE ; del_tail (); A [Tii] [TJJ] = 0; } void right () {head = 3; IF (a [i] [j 1]> 1 && a [i] [j 1]! = 99) {Printf ("i hit myself!"); exit (1) } J ; IF (IE> 30) IE = 2; a [i] [j] = IE; IE ; del_tail (); a [tii] [TJJ] = 0;} void test () / * test is reached Boundary * / {IF (i <0 || i> N-1 || j <0 || j> n-1) {printf ("out of bound! Game over."); Exit (1);}} void Move () / * Current mobile * / {switch (head) {case 0: Up (); break; case 1: down (); break; case 2: left (); break; case 3: right (); break; / * DEFAULT: Printf ("Direction error! / n"); * /} } int keyboard () {/ * Judging whether there is a keyboard input // has a return value 0, 1, 2, 3 // int K = keyboard; // Decision direction is effective direction // 无效 无效 则 不If the direction is valid, change the value of the HEAD * / INT A, Key, H; for (a = 0; a <2; a ) IF (Bioskey (1)) Break; key = bioskey (0); Switch (key) {/ * up * / case 18432: h = 0; break; / * down * / case 20480: h = 1; Break; / * left * / case 19200: h = 2; Break; / * Right * / Case 19712: h = 3; Break; / * esc * / case 283: Printf ("You Press the ESC Key!"); Exit (1); break; / * default: h = 999; * / } Return H;} void add_point () / * is used to generate food * / { While (1) {x = random (100)% n; y = random (100)% N; IF (a [x] [y] == 0) Break;} a [x] [y] = 99; / * The logo of the food is 5 * / Add = 0; / * Add food sign clear 0 * / } void score () { SetColor (9); SetTextStyle (3, 0, 4); Outtextxy (450, 40, "Score:"); SetColor (Red); Outtextxy (450, 200, "Speed:"); } void Number_char (int x) { INT A0, A1, A2, A3; IF (x> 9999) {a [0] = '9'; A [1] = '9'; A [2] = '9'; a [3] = '9 ';} Else {a0 = x / 1000; a [0] = A0 48; A1 = (X-A0 * 1000) / 100; a [1] = a1 48; A2 = (X-A0 * 1000-A1 * 100) / 10; A [2] = A2 48; A3 = (X-A0 * 1000-A1 * 100-A2 * 10); A [3] = A3 48; A [4] = '/ 0';}}} Void snake_get () / * When you eat food * / {if (i == x && j == y) {Length ; A [Tii] [TJJ] = a [ii] [jj] -1; / * snake tail length * / II = Tii; jj = tjj; / * Snake redefinition * / add = 1; / * Food sign 1 * / setfillstyle (1, black); bar (520, 100, 650, 140); / * Clear score * / setColor (3); Number_char (score = 100); OutTextxy (550, 100, a); } } Void main () {int GET_HEAD; / * Define keyboard input * / int GD = Detect, GM; CLRSCR (); / * RegibgiobjDriver (public name) * / initgraph (& GD, & GM, "C: // BGI"); Setbkcolor (Black); Tu (); snake (); / * Initialization * / start_image (); score (); / * Define the score interface * / While (1) {while (Bioskey (1) == 0) / * Is there a keyboard input * / { IF (add) add_point (); / * Can you increase food * / PRINT (); move (); snake_get (); / * Do you eat food (TEST) * / test (); delay (time);} get_head = keyboard (); / * choose * / While (1) {/ * judges whether the resulting coordinates are legal * / if (get_head == 0 && head == 1) Break; if (get_head == 1 && head == 0) Break; if (get_head == 2 && head == 3) BREAK ; If (GET_HEAD == 3 && Head == 2) Break; Else {head = GET_HEAD; Break;} / * Legal to Head assignment * /} } }