A snake game written in the curses Library

Source: Internet
Author: User

The Snake Game written in the curses library is a beginner in C language. It may be very spam and the code is messy. If you are just learning programming, you can check it out. I hope you can give me some advice.
# Include <stdio. h>
# Include <curses. h>
# Include <stdlib. h>
# Include <time. h>

Struct Food {
Int x;
Int y;
Int exist;
};
Struct Snake {
Int x [100];
Int y [100];
Int node;
};

Struct Food food;
Struct Snake;
Int t = 300000;
Int point = 0;

// Draw the Main Window
Int win (void)
{
Int I;
For (I = 0; I <20; I ++)
{
Move (I, 0 );
Addstr ("+ ");
Move (I, 50 );
Addstr ("+ ");
}
For (I = 0; I <= 50; I ++)
{
Move (0, I );
Addstr ("+ ");
Move (20, I );
Addstr ("+ ");
}
}
// Draw the score window
Int socrewin (void)
{
Int I;
For (I = 0; I <5; I ++)
{
Move (I, 52 );
Addstr ("+ ");
Move (I, 62 );
Addstr ("+ ");
}
For (I = 52; I <= 62; I + = 1)
{
Move (0, I );
Addstr ("+ ");
Move (5, I );
Addstr ("+ ");
}
}
// Menu options
Int menu (void)
{
Int ch;
Move (3, 5 );
Addstr ("a. Star ");
Move (5, 5 );
Addstr ("q. Exit ");
Ch = getch ();
Switch (ch)
{
Case:
Return;
Case q:
Return q;

}
}
// Game Operation
Int gamerun (void)
{
Int ch, I;
I = 0;
Snake. x [0] = 10;
Snake. y [0] = 25;
Snake. node = 1;
Nodelay (stdscr, TRUE );
Food_init ();
While (I! =-1)
{
If (pd () = 1)
{
Food_init ();
}
Snake ke_win (snake. x [0], snake. y [0]);
Food_win (food. x, food. y );
If (game_win () =-1)
Break;
Ch = getch ();
If (key (ch ))! = 0)
I = key (ch );
Switch (I)
{
Case 1:
Snake. x [0] --;
Break;
Case 2:
Snake. x [0] ++;
Break;
Case 3:
Snake. y [0] --;
Break;
Case 4:
Snake. y [0] ++;
Break;
}
Coo ();

}
Move (10, 20 );
Addstr ("Game over ");
Refresh ();
Sleep (5 );
Return 0;
}
// Food Initialization
Int food_init (void)
{
Srand (int) time (0 ));
Food. x = rand () % 20;
If (food. x = 0)
Food. x = 1;
Food. y = rand () % 50;
If (food. y = 0)
Food. y = 1;
Food_win (food. x, food. y );
}
// Draw game graphics
Int game_win (void)
{
Int I;
If (snake. x [0] = 0 | snake. x [0] = 20 | snake. y [0] = 0 | snake. y [0] = 50)
Return-1;
For (I = 0; I <= snake. node; I ++)
{
Move (snake. x [I], snake. y [I]);
Addstr ("O ");
}
Move (LINES-1, 0 );
Return 0;
}

// Keyboard signal capture
Int key (ch)
{
If (ch = KEY_UP)
Return 1;
If (ch = KEY_DOWN)
Return 2;
If (ch = KEY_LEFT)
Return 3;
If (ch = KEY_RIGHT)
Return 4;
If (ch = q)
Return-1;
Return 0;
}
// Judge
Int pd (void)
{
If (food. x = snake. x [0])
{
If (food. y = snake. y [0])
{
Snake. node ++;
Point ++;
Return 1;
}
}
Return 0;
}
// Score
Int Score ()
{
Move (2, 55 );
Addstr ("Score ");
Move (3, 57 );
Printw ("% d", point );
}
// Graphic drawing of food
Int food_win (int x, int y)
{
Move (x, y );
Addstr ("$ ");
Return 0;
}
// Refresh the screen
Int coo (void)
{
Clear ();
Win ();
Socrewin ();
Score ();
Usleep (t );
Refresh ();
Return 0;
}

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.