当前位置:网站首页>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);
边栏推荐
- Spolicy request case
- Show default image when wechat applet image cannot be displayed
- 2021 windows penetration of "Cyberspace Security" B module of Shandong secondary vocational group (analysis)
- 挡不住了,纯国产PC已就位,美国的软硬件体系垄断正式被破
- Study notes at the end of summer vacation
- Use of tabbarcontroller
- Alibaba cloud technology expert haochendong: cloud observability - problem discovery and positioning practice
- Uniapp common error [wxml file compilation error]./pages/home/home Wxml and using MySQL front provided by phpstudy to establish an independent MySQL database and a detailed tutorial for independent da
- 解释一下自动装箱和自动拆箱?
- MySQL的逻辑架构
猜你喜欢
I finished watching this video on my knees at station B
在Blazor 中自定义权限验证
MySQL的逻辑架构
Sqoop【环境搭建 01】CentOS Linux release 7.5 安装配置 sqoop-1.4.7 解决警告并验证(附Sqoop1+Sqoop2最新版安装包+MySQL驱动包资源)
2021年山东省中职组“网络空间安全”B模块windows渗透(解析)
Solve proxyerror: CONDA cannot proceed due to an error in your proxy configuration
SSG framework Gatsby accesses the database and displays it on the page
Fuzzy PID control of motor speed
服务发现原理分析与源码解读
Alibaba cloud technology expert haochendong: cloud observability - problem discovery and positioning practice
随机推荐
Common errors when starting projects in uniapp ---appid
云原生(三十六) | Kubernetes篇之Harbor入门和安装
Fuzzy PID control of motor speed
Unstoppable, pure domestic PCs have been in place, and the monopoly of the U.S. software and hardware system has been officially broken
解决ProxyError: Conda cannot proceed due to an error in your proxy configuration.
数通基础-STP原理
Principle analysis and source code interpretation of service discovery
Uniapp error 7 < Map >: marker ID should be a number
Flask framework beginner-03-template
解释一下自动装箱和自动拆箱?
编写一个在bash / shell 和 PowerShell中均可运行的脚本
面试突击68:为什么 TCP 需要 3 次握手?
MySQL function
【荧光字效果】
Wechat H5 payment on WAP, for non wechat browsers
[datawhale] [machine learning] Diabetes genetic risk detection challenge
Interview shock 68: why does TCP need three handshakes?
Basic knowledge of website design
Wechat applet learning notes 2
SQL优化的魅力!从 30248s 到 0.001s