Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Lion Long
1024云IDE应用挑战赛
提交
586cf6d2
1024云IDE应用挑战赛
项目概览
Lion Long
/
1024云IDE应用挑战赛
与 Fork 源项目一致
Fork自
Cloud IDE / 1024云IDE应用挑战赛
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
1024云IDE应用挑战赛
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
586cf6d2
编写于
11月 10, 2022
作者:
6
631f057a7fde637fa39bbe51
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update src
上级
e63fb81c
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
198 addition
and
0 deletion
+198
-0
src/main.c
src/main.c
+198
-0
未找到文件。
src/main.c
0 → 100644
浏览文件 @
586cf6d2
#include<curses.h>
#include<stdlib.h>
#define up 1 //1与-1的目的是使用abs()函数防止一上一下
#define down -1
#define left 2
#define right -2
struct
snake
{
//创建一个结构体
int
hang
;
int
lie
;
struct
snake
*
next
;
};
struct
snake
*
head
=
null
;
//全局定义一个头和尾
struct
snake
*
tail
=
null
;
int
key
;
//定义一个按键的整形变量
int
dir
;
//定义一个方向的整形变量
struct
snake
food
;
void
initfood
(){
//定义一个食物## 可以随机生成
int
x
=
rand
()
%
19
;
int
y
=
rand
()
%
19
;
food
.
hang
=
x
;
food
.
lie
=
y
;
}
void
initncurse
(){
initscr
();
keypad
(
stdscr
,
1
);
noecho
();
}
int
hassnakenode
(
int
i
,
int
j
){
//显示蛇身体
struct
snake
*
p
;
p
=
head
;
while
(
p
!=
null
){
if
(
p
->
hang
==
i
&&
p
->
lie
==
j
){
return
1
;
}
p
=
p
->
next
;
}
return
0
;
}
int
hasfood
(
int
i
,
int
j
){
//有食物
if
(
food
.
hang
==
i
&&
food
.
lie
==
j
){
return
1
;
}
return
0
;
}
void
gamepic
(){
//游戏图形化展示
int
hang
;
int
lie
;
move
(
0
,
0
);
for
(
hang
=
0
;
hang
<
20
;
hang
++
){
if
(
hang
==
0
){
for
(
lie
=
0
;
lie
<
20
;
lie
++
){
printw
(
"--"
);
}
printw
(
"
\n
"
);
}
if
(
hang
>=
0
&&
hang
<=
19
){
for
(
lie
=
0
;
lie
<=
20
;
lie
++
){
if
(
lie
==
0
||
lie
==
20
){
printw
(
"|"
);
}
else
if
(
hassnakenode
(
hang
,
lie
)){
printw
(
"[]"
);
}
else
if
(
hasfood
(
hang
,
lie
)){
printw
(
"##"
);
}
else
{
printw
(
" "
);
}
}
printw
(
"
\n
"
);
}
if
(
hang
==
19
){
for
(
lie
=
0
;
lie
<
20
;
lie
++
){
printw
(
"--"
);
}
printw
(
"
\n
"
);
printw
(
"by ricko"
);
}
}
}
void
addnode
(){
//加头并且方向
struct
snake
*
new
=
(
struct
snake
*
)
malloc
(
sizeof
(
struct
snake
));
new
->
next
=
null
;
switch
(
dir
){
case
up
:
new
->
hang
=
tail
->
hang
-
1
;
new
->
lie
=
tail
->
lie
;
break
;
case
down
:
new
->
hang
=
tail
->
hang
+
1
;
new
->
lie
=
tail
->
lie
;
break
;
case
left
:
new
->
hang
=
tail
->
hang
;
new
->
lie
=
tail
->
lie
-
1
;
break
;
case
right
:
new
->
hang
=
tail
->
hang
;
new
->
lie
=
tail
->
lie
+
1
;
break
;
}
tail
->
next
=
new
;
tail
=
new
;
}
void
initsnake
(){
//初始化蛇
struct
snake
*
p
;
dir
=
right
;
while
(
head
!=
null
){
p
=
head
;
head
=
head
->
next
;
free
(
p
);
}
initfood
();
head
=
(
struct
snake
*
)
malloc
(
sizeof
(
struct
snake
));
head
->
hang
=
1
;
head
->
lie
=
1
;
head
->
next
=
null
;
tail
=
head
;
addnode
();
addnode
();
addnode
();
addnode
();
}
void
delenode
(){
//删除最后节点
struct
snake
*
p
;
p
=
head
;
head
=
head
->
next
;
free
(
p
);
}
int
ifsnakedie
(){
//在撞到边界以及自己迟到自己的时候会输出一个1让自己复活
struct
snake
*
p
;
p
=
head
;
if
(
tail
->
hang
<
0
||
tail
->
lie
==
0
||
tail
->
hang
==
20
||
tail
->
lie
==
20
){
return
1
;
}
while
(
p
->
next
!=
null
){
if
(
p
->
hang
==
tail
->
hang
&&
p
->
lie
==
tail
->
lie
){
return
1
;
}
p
=
p
->
next
;
}
return
0
;
}
void
movesnake
(){
//蛇的移动
addnode
();
if
(
hasfood
(
tail
->
hang
,
tail
->
lie
)){
//如果吃到食物就不删除最后的节点
initfood
();
}
else
{
delenode
();
}
if
(
ifsnakedie
()){
initsnake
();
}
}
void
refreshjiemian
(){
//刷新界面 线程
while
(
1
){
movesnake
();
gamepic
();
refresh
();
usleep
(
150000
);
//刷新频率
}
}
void
turn
(
int
direction
){
//防止方向键按了上又按下
if
(
abs
(
dir
)
!=
abs
(
direction
)){
dir
=
direction
;
}
}
void
changedir
(){
//改变方向
while
(
1
){
key
=
getch
();
switch
(
key
){
case
key_down
:
turn
(
down
);
break
;
case
key_up
:
turn
(
up
);
break
;
case
key_left
:
turn
(
left
);
break
;
case
key_right
:
turn
(
right
);
break
;
}
}
}
int
main
(){
pthread_t
t1
;
//定义线程1
pthread_t
t2
;
initncurse
();
//初始化ncurse
initsnake
();
//初始化蛇
gamepic
();
//初始化界面
pthread_create
(
&
t1
,
null
,
refreshjiemian
,
null
);
//启动线程里面的函数
pthread_create
(
&
t2
,
null
,
changedir
,
null
);
while
(
1
);
//线程3
getch
();
endwin
();
return
0
;
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录