提交 586cf6d2 编写于 作者: 6 631f057a7fde637fa39bbe51

update src

上级 e63fb81c
#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.
先完成此消息的编辑!
想要评论请 注册