Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The practical application of C language-150809124

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

#include

#include//basic type definition. Support type definition functions. User interface functions graphical device interface functions.

#include //corresponding actions generated by the user pressing the keyboard (console)

#include

#include //date and time header file

#define LEN 30

#define WID 25

int Snake[LEN][WID] = {0}; //Array elements represent snake parts

char Sna_Hea_Dir = 'a';//Record the movement direction of the snake head

int Sna_Hea_X, Sna_Hea_Y;//Record the position of the snake head

int Snake_Len = 3;//Record length of snake

clock_t Now_Time;//Record the current time for automatic movement

int Wait_Time ;//Record the time interval for automatic movement

int Eat_Apple = 1;//Eating apples means 1

int Level ;

int All_Score = -1;

int Apple_Num = -1;

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); //Get the handle of the standard output

//Handles: flags different objects in the application and different instances of objects of the same kind for easy manipulation,

void gotoxy(int x, int y)//Set cursor position

{

COORD pos = {x,y}; //Defines the coordinates POS of a character on the console screen

SetConsoleCursorPosition(hConsole, pos); //function to position cursor position

}

void Hide_Cursor()//Hide Cursor Fixed Function

{

CONSOLE_CURSOR_INFO cursor_info = {1, 0};

SetConsoleCursorInfo(hConsole, &cursor_info);

}

void SetColor(int color)//Set color

{

SetConsoleTextAttribute(hConsole, color);

//is API function format for setting font color and background color: SetConsoleTextAttribute(handle, color);

}

void Print_Snake()//Print snake heads and snake necks and snake tails

{

int iy, ix, color;

for(iy = 0; iy

< WID; ++iy) for(ix = 0; ix < LEN; ++ix) { if(Snake[ix][iy] == 1)//蛇头 { SetColor(0xf); //oxf代表分配的内存地址 setcolor:34行自定义设置颜色的函数 gotoxy(ix*2, iy); printf("※"); } if(Snake[ix][iy] == 2)//蛇的脖子 { color = rand() + 1; //rand()函数是产生随机数的一个随机函数。C语言里还有 srand()函数等。 //头文件:stdlib.h if(color == 14) color -= rand() % 13 + 1; //变色 SetColor(color); gotoxy(ix*2, iy); printf("■"); } if(Snake[ix][iy] == Snake_Len) { gotoxy(ix*2, iy); SetColor(0xe); printf("≈"); } } } void Clear_Snake()//擦除贪吃蛇 { int iy, ix; for(iy = 0; iy < WID; ++iy) for(ix = 0; ix < LEN; ++ix) { gotoxy(ix*2, iy); if(Snake[ix][iy] == Snake_Len) printf(" "); } } void Rand_Apple()//随机产生苹果 { int ix, iy; do { ix = rand() % LEN; iy = rand() % WID; }while(Snake[ix][iy]); Snake[ix][iy] = -1; gotoxy(ix*2, iy); printf("⊙"); Eat_Apple = 0; } void Game_Over()//蛇死掉了 { gotoxy(30, 10); printf("Game Over"); Sleep(3000); system("pause >

nul");

exit(0);

}

void Move_Snake()//Move the Snake

{

int ix, iy;

for(ix = 0; ix

< LEN; ++ix)//先标记蛇头 for(iy = 0; iy < WID; ++iy) if(Snake[ix][iy] == 1) { switch(Sna_Hea_Dir)//根据新的蛇头方向标志蛇头 { case 'w': if(iy == 0) Game_Over(); else Sna_Hea_Y = iy - 1; Sna_Hea_X = ix; break; case 's': if(iy == (WID -1)) Game_Over(); else Sna_Hea_Y = iy + 1; Sna_Hea_X = ix; break; case 'a': if(ix == 0) Game_Over(); else Sna_Hea_X = ix - 1; Sna_Hea_Y = iy; break; case 'd': if(ix == (LEN - 1)) Game_Over(); else Sna_Hea_X = ix + 1; Sna_Hea_Y = iy; break; default: break; } } if(Snake[Sna_Hea_X][Sna_Hea_Y]!=1&&Snake[Sna_Hea_X][Sna_Hea_Y]!=0&&Snake[Sna_Hea_X][Sna_Hea_Y]!=-1) Game_Over(); if(Snake[Sna_Hea_X][Sna_Hea_Y] < 0)//吃到苹果 { ++Snake_Len; Eat_Apple = 1; } for(ix = 0; ix < LEN; ++ix)//处理蛇尾 for(iy = 0; iy < WID; ++iy) { if(Snake[ix][iy] >

0)

{

if(Snake[ix][iy] != Snake_Len)

Snake[ix][iy] += 1;

else

Snake[ix][iy] = 0;

}

}

Snake[Snake_Hea_X][Snake_Hea_Y] = 1;//process snake heads

}

void Get_Input()//Controls the movement direction of snakes

{

if(kbhit())

{

switch(getch())

{

case 87:

Sna_Hea_Dir = 'w';

break;

case 83:

Sna_Hea_Dir = 's';

break;

case 65:

Sna_Hea_Dir = 'a';

break;

case 68:

Sna_Hea_Dir = 'd';

break;

default:

break;

}

}

if(clock() - Now_Time >= Wait_Time)

{

Clear_Snake();

Move_Snake();

Print_Snake();

Now_Time = clock();

}

}

void Init()//initialize

{

system("title");

system("mode con: cols=80 lines=25");

Hide_Cursor();

gotoxy(61, 4);

printf("You Score:");

gotoxy(61, 6);

printf("You Level:");

gotoxy(61, 8);

printf("The Lenght:");

gotoxy(61, 10);

printf("The Speed:");

gotoxy(61, 12);

printf("Apple Num:");

int i;

for(i = 0; i < Snake_Len; ++i)//generate snake

Snake[10+i][15] = i+1;

int iy, ix;//print snake

for(iy = 0; iy < WID; ++iy)

for(ix = 0; ix < LEN; ++ix)

{

if(Snake[ix][iy])

{

SetColor(Snake[ix][iy]);

gotoxy(ix*2, iy);

printf("■");

}

}

}

Use C language statements can not only do some interesting Mini games can also complete some practical problems in life, clear the relationship is very important, first complete the definition and then the relationship input, and finally you can run the program to achieve their desired purpose, C language appropriate practical, I want to learn it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report