服务器之家

服务器之家 > 正文

C++结构体数组实现贪吃蛇

时间:2021-08-23 15:03     来源/作者:oyxy2019

本文实例为大家分享了C++结构体数组实现贪吃蛇的具体代码,供大家参考,具体内容如下

代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
 
using namespace std;
 
const int h=50,w=50,MaxLen=400;
void gotoxy(short y,short x)//光标移动函数
{
 COORD pos={x,y};
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
struct node
{
 int x,y;
};
node s[MaxLen];
node food;
int dir,len;
int Map[h+5][w+5];
int Time[7]={0},level;//building。。。
 
 
void FoodCreate()
{
 srand ((unsigned)time(NULL));//时间作随机数种子,避免伪随机
 while(1)
 {
 food.x=rand()%h,food.y=rand()%w;
 if(food.x>0&&food.y>0&&Map[food.x][food.y]==0)break;
 }
 gotoxy(food.x,food.y),printf("@");
 gotoxy(h+1,0);
}
 
void init()
{
 system("cls");
 for(int i=0;i<=h;i++)//画图
 {
 for(int j=0;j<=w;j++)
 {
 if(i==0||j==0||i==h||j==w)Map[i][j]=1,printf("#");
 else Map[i][j]=0,printf(" ");
 }
 printf("\n");
 }
 
 len=2; //初始化蛇
 dir=0;
 s[1].x=12,s[1].y=4;
 s[len].x=12,s[len].y=3;
 Map[s[1].x][s[1].y]=Map[s[len].x][s[len].y]=1;
 gotoxy(s[1].x,s[1].y),printf("*");
 gotoxy(s[len].x,s[len].y),printf("*");
 gotoxy(h+1,0);
 
 FoodCreate();
}
 
int move()
{
 node next=s[1];
 switch(dir)
 {
 case 0:next.y++;break;
 case 1:next.x--;break;
 case 2:next.y--;break;
 case 3:next.x++;break;
 }
 if(Map[next.x][next.y])return 0; //下一步GG
 if(next.x==food.x&&next.y==food.y) //下一步遇到食物
 {
 len++;
 FoodCreate(); 
 }
 else   //下一步是空白就将尾部覆盖
 {
 gotoxy(s[len].x,s[len].y),printf(" ");
 Map[s[len].x][s[len].y]=0;
 }
 
 gotoxy(next.x,next.y),printf("*");
 gotoxy(h+1,0);
 Map[next.x][next.y]=1;
 for(int i=len;i>1;i--)s[i]=s[i-1];
 s[1]=next;
 Sleep(100);  //Sleep放在最后比较顺滑//速度在这儿调
 return 1;
}
 
void GameOver()
{
 for(int i=1;i<=3;i++)
 {
 gotoxy(s[1].x,s[1].y);
 printf(" ");
 Sleep(300);
 gotoxy(s[1].x,s[1].y);
 printf("*");
 Sleep(300);
 }
 gotoxy(h+1,0);
 printf("GameOver\n");
 printf("Press any key to continue...");
}
void Welcome()
{
 printf("为了您的游戏体验,请先调整控制台字体和布局(记得不要忘了默认设置):\n");
 printf("右键白色框->属性->字体 选择点阵字体并调整字体大小为8×8\n");
 printf("再选择布局设置窗口大小,推荐60×60\n\n");
 printf("WASD控制方向\n");
 
 printf("\n任意键进入贪吃蛇皮...");
 getch();
 
}
int main()
{
 Welcome();
 init();
 while(1)
 {
 if(kbhit())
 {
 char ch=getch();
 int temp=dir;
 switch(ch)
 {
 case 'd':temp=0;break;
 case 'w':temp=1;break;
 case 'a':temp=2;break;
 case 's':temp=3;break;
 }
 if((temp+dir)%2)dir=temp;//如果方向不冲突
 }
 if(move()==0)
 {
 GameOver();
 getch();
 init();
 }
 }
 
}

运行效果:

C++结构体数组实现贪吃蛇

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_45572675/article/details/104671481

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
返回顶部