Source code for greedy snakes written in pure C Language

Source: Internet
Author: User
Tags sleep function

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Define N 225 struct Food {int x; int y; int yes; // 1 indicates new Food needs to appear, and 0 indicates existing Food .} Food; struct Snake {struct Food body [N]; int life; // 1 indicates that the Snake is dead, and 0 indicates that the Snake is alive. Int node; // The number of nodes of the snake. Char direction; // the direction of automatic snake movement .} Snake; int score = 0; int main () {FILE * p; int I, j, k, B; char map [16] [16], c; p = fopen ("E: \ file.txt", "r"); if (p = NULL) {printf ("error"); exit (1 );} for (I = 0; I <16; I ++) {for (j = 0; j <16; j ++) {map [I] [j] = fgetc (p);} snake. body [0]. x = 8; // Snake Head. Snake. body [0]. y = 8; snake. body [1]. x = 8; snake. body [1]. y = 7; snake. body [2]. x = 8; snake. body [2]. y = 6; snake. node = 3; // The number of nodes of the snake. Food. yes = 1; srand (time (NULL); snake. direction = 'D'; // The snake automatically runs to the right at the beginning. While (1) {if (kbhit () {c = getch (); if (snake. life = 1) break; if (c = 'W' & snake. direction! ='S ') snake. direction = 'W'; else if (c = 'A' & snake. direction! = 'D') snake. ction = 'a'; else if (c = 's' & snake. direction! = 'W') snake. ction = 's'; else if (c = 'D' & snake. direction! = 'A') snake. ction = 'D';} if (food. yes = 1) // You Need to randomly display new food. {Food. x = 1 + rand () % 14; food. y = 1 + rand () % 14; for (I = 0; I
      
        0; I --) // move forward the snake. {Snake. body [I]. x = snake. body [I-1]. x; snake. body [I]. y = snake. body [I-1]. y;} switch (snake. direction) // the direction of the snake header. {Case 'A': snake. body [0]. y-= 1; break; case 'W': snake. body [0]. x-= 1; break; case 'D': snake. body [0]. y + = 1; break; case's ': snake. body [0]. x + = 1; break;} if (food. yes = 0) // displays snakes, food, and walls. {System ("cls"); for (I = 0; I <16; I ++) {for (j = 0; j <16; j ++) {B = 1; for (k = 0; k
       
         0 & j <15 & j> 0) // food. Printf ("★"); Else if (map [I] [j] = '1') printf (" ■ "); else printf ("");}} putchar ('\ n');} Sleep (250); // Sleep function .} For (I = 3; I
        
          = 15 | snake. body [0]. x <= 0 | snake. body [0]. y> = 15 | snake. body [0]. y <= 0) {printf ("The Snake crashed into the wall! \ N "); printf (" total score: % d \ n ", score); snake. life = 1; break ;}} fclose (p); return 0 ;}
        
       
      
     
    
   
  
 
The following is file.txt:
1111111111111111100000000000000110000000000000011000000000000001100000000000000110000000000000011000000000000001100000000000000110000000000000011000000000000001100000000000000110000000000000011000000000000001100000000000000110000000000000011111111111111111

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.