当前位置:网站首页>Reproduce the snake game in C language (I) build pages and construct snakes
Reproduce the snake game in C language (I) build pages and construct snakes
2022-07-26 10:06:00 【SingleDog_ seven】
A small still snake is displayed on the screen . For two dimensional arrays canvas[High][Width] The corresponding element of , The value is 0 Output space , The value is -1 Output border #, The value is 1 Output snakehead @, Value is greater than 1 The positive number of the output snake body *. stay startup() Function to initialize the snake head in the middle of the canvas (canvas[High/2][Width/2]=1;), The snake head generates... In turn to the left 4 A snake (for(i=1;i<=4;i++) canvas[High/2][Width/2-i]=i+1;) The element values are 2、3、4、5.
#include<stdio.h>
#include<stdlib.h> // Contains C、C++ The most commonly used system function of language
#include<conio.h> // It is mainly the input and output of files and standard console
#include<windows.h> // Control page
#define High 20 // Game screen size : high 20. wide 30
#define Width 30
//0 Is a space ,-1 Is border #,1 For the snake head @, Greater than 1 For the snake
int canvas[High][Width]={0}; // Two dimensional array Store elements
void gotoxy(int x,int y) // Move the cursor to (x,y); Included in conio.h
{
HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(handle,pos);
}gotoxy(int x,int y) Is the position of the fixed cursor .
void startup() // Data initialization
{
int i,j;
// Initialize border
for(i=0;i<High;i++)
{
canvas[i][0] = -1;
canvas[i][Width-1] =-1; // Yes i Conduct 20 Secondary external circulation
}
for(j=0;j<Width;j++)
{
canvas[0][j]=-1;
canvas[High-1][j] = -1; // Yes j Conduct 30 Secondary external circulation
}
canvas[High/2][Width/2] =1; // Snake head position
for(i=1;i<=4;i++) // stay @ The left side of the output three * As a snake
canvas[High/2][Width/2-i] = i+1; // Initialize the snake body
}
Here is the construction of snake and game page as required .
void show() // display frame
{
gotoxy(0,0); // Move the cursor to the origin position , Clear the screen
int i,j;
for (i=0;i<High;i++)
{
for(j=0;j<Width;j++)
{
if(canvas[i][j]==0) // The unassigned , The assignment is 0
printf(" "); // Output space
else if(canvas[i][j]==-1)
printf("#"); // Output border
else if(canvas[i][j]==1)
printf("@"); // Output snakehead
else if(canvas[i][j]>1)
printf("*"); // Output snake body
}
printf("\n");
}
}Use show Functions and if Conditional statements output the page and the snake .
void updateWithoutTnput() // User independent updates
{
}
void updateWithInput() // User related updates
{
}
The above two statements are convenient to identify the subsequent updates of the game , Does not affect the integrity of the program .
int main() // The main function
{
startup(); // call startup function
while(1)
{
show();
updateWithoutTnput(); // User independent updates
updateWithInput(); // User related updates
}
return 0;
} The results are shown in the figure .
##############################
# #
# #
# #
# #
# #
# #
# #
# #
# #
# ****@ #
# #
# #
# #
# #
# #
# #
# #
# #
##############################notes : If the cursor flashes , You can do it again main Add the following function to the function to hide the cursor :
CONSOLE_CURSOR_INFO cci;
cci.bVisible = FALSE;
cci.dwSize = sizeof(cci);
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);边栏推荐
- The combination of officially issued SSL certificate and self signed certificate realizes website two-way authentication
- 苹果独占鳌头,三星大举复兴,国产手机在高端市场颗粒无收
- Netease cloud UI imitation -- & gt; sidebar
- 数通基础-网络基础知识
- 2021 windows penetration of "Cyberspace Security" B module of Shandong secondary vocational group (analysis)
- AirTest
- Flask框架初学-04-flask蓝图及代码抽离
- Transform between tree and array in JS (hide the children field if the child node of the tree is empty)
- Rowselection emptying in a-table
- 解释一下自动装箱和自动拆箱?
猜你喜欢
![[MySQL database] a collection of basic MySQL operations - the basis of seeing (adding, deleting, modifying, and querying)](/img/a7/b3bb6f584dff0eb9b49e81e5b9dade.png)
[MySQL database] a collection of basic MySQL operations - the basis of seeing (adding, deleting, modifying, and querying)

Mysql5.7.25 master-slave replication (one-way)

SSG framework Gatsby accesses the database and displays it on the page

30分钟彻底弄懂 synchronized 锁升级过程

服务发现原理分析与源码解读

R language ggplot2 visualization: align the legend title to the middle of the legend box in ggplot2 (default left alignment, align legend title to middle of legend)

Apple dominates, Samsung revives, and domestic mobile phones fail in the high-end market

Installation and use of cocoapods

Vs Code configures go locale and successfully installs go related plug-ins in vscode problem: Tools failed to install

SQL优化的魅力!从 30248s 到 0.001s
随机推荐
Use of tabbarcontroller
Session based recommendations with recurrent neural networks
Installation and use of cocoapods
AR model in MATLAB for short-term traffic flow prediction
AirTest
Customize permission validation in blazor
In the same CONDA environment, install pytroch first and then tensorflow
Tableviewcell highly adaptive
网易云UI模仿--&gt;侧边栏
JS table auto cycle scrolling, mouse move in pause
Meeting OA project (III) -- my meeting (meeting seating and submission for approval)
The combination of officially issued SSL certificate and self signed certificate realizes website two-way authentication
The use of MySQL in nodejs
苹果独占鳌头,三星大举复兴,国产手机在高端市场颗粒无收
Uniapp "no mobile phone or simulator detected, please try again later" and uniapp custom components and communication
Mysql5.7.25 master-slave replication (one-way)
Cloud native (36) | introduction and installation of harbor in kubernetes
Production of a-modal drag function in antui
解释一下自动装箱和自动拆箱?
2022年中科磐云——服务器内部信息获取 解析flag