C语言小项目-贪吃蛇第2天方向移动

1. 显示贪吃蛇身子的一个节点(140.10)

  • snake4.c
#include <curses.h>

struct Snake
{
	int line;
	int list;
	struct Snake *Nest;
};

struct Snake node1={2,2,NULL};

void initNcurse()
{
	initscr();	
	keypad(stdscr,1);
}

void gamePic()
{
	int line;
	int list;

	for(line=0;line<20;line++){
		if(line==0){
			for(list=0;list<20;list++){
				printw("--");
			}
			printw("\n");
		}
		if(line>=0 && line<=19){
			for(list=0;list<=20;list++){
				if(list==0 || list==20){
					printw("|");
				}else if(node1.line==line && node1.list==list){
					printw("[]");
				}
				else{
					printw("  ");
				}
			}
		  	printw("\n");
		}
		if(line==19){
			for(list=0;list<20;list++){
				printw("--");
			}
			printw("\nBy Jessie\n");
		}
		
	}

}

int main()
{
	initNcurse();
	gamePic();

	getch();
	endwin();

	return 0;
}

2. 显示贪吃蛇完整身子(141.11)

  • snake5.c
#include <curses.h>

struct Snake
{
	int line;
	int list;
	struct Snake *next;
};

struct Snake node1={2,2,NULL};
struct Snake node2={2,3,NULL};
struct Snake node3={2,4,NULL};

int hasSnakeNode(int line,int list)
{
	if(node1.line==line && node1.list==list){
		return 1;
	}
	return 0;
}

void initNcurse()
{
	initscr();	
	keypad(stdscr,1);
}

void gamePic()
{
	int line;
	int list;

	for(line=0;line<20;line++){
		if(line==0){
			for(list=0;list<20;list++){
				printw("--");
			}
			printw("\n");
		}
		if(line>=0 && line<=19){
			for(list=0;list<=20;list++){
				if(list==0 || list==20){
					printw("|");
				}else if(hasSnakeNode(line,list)){
					printw("[]");
				}
				else{
					printw("  ");
				}
			}
		  	printw("\n");
		}
		if(line==19){
			for(list=0;list<20;list++){
				printw("--");
			}
			printw("\nBy Jessie\n");
		}
		
	}

}

int main()
{
	initNcurse();
	node1.next=&node2;
	node2.next=&node3;

	gamePic();

	getch();
	endwin();

	return 0;
}
  • snake6.c
#include <curses.h>

struct Snake
{
	int line;
	int list;
	struct Snake *next;
};

struct Snake node1={2,2,NULL};
struct Snake node2={2,3,NULL};
struct Snake node3={2,4,NULL};
struct Snake node4={2,5,NULL};

int hasSnakeNode(int line,int list)
{
	struct Snake *p=&node1;
	while(p != NULL){
		if(p->line==line && p->list==list){
			return 1;
		}
		p=p->next;
	}
	return 0;
}

void initNcurse()
{
	initscr();	
	keypad(stdscr,1);
}

void gamePic()
{
	int line;
	int list;

	for(line=0;line<20;line++){
		if(line==0){
			for(list=0;list<20;list++){
				printw("--");
			}
			printw("\n");
		}
		if(line>=0 && line<=19){
			for(list=0;list<=20;list++){
				if(list==0 || list==20){
					printw("|");
				}else if(hasSnakeNode(line,list)){
					printw("[]");
				}
				else{
					printw("  ");
				}
			}
		  	printw("\n");
		}
		if(line==19){
			for(list=0;list<20;list++){
				printw("--");
			}
			printw("\nBy Jessie\n");
		}
		
	}

}

int main()
{
	initNcurse();
	node1.next=&node2;
	node2.next=&node3;
	node3.next=&node4;

	gamePic();

	getch();
	endwin();

	return 0;
}

3. 显示贪吃蛇完整身子改进(142.12)

链表动态添加蛇的节点

  • snake7.c
#include <curses.h>
#include <stdlib.h>

struct Snake
{
	int line;
	int list;
	struct Snake *next;
};

struct Snake *head;
struct Snake *tail;

int hasSnakeNode(int line,int list)
{
	struct Snake *p=head;
	while(p != NULL){
		if(p->line==line && p->list==list){
			return 1;
		}
		p=p->next;
	}
	return 0;
}

void initNcurse()
{
	initscr();	
	keypad(stdscr,1);
}

void gamePic()
{
	int line;
	int list;

	for(line=0;line<20;line++){
		if(line==0){
			for(list=0;list<20;list++){
				printw("--");
			}
			printw("\n");
		}
		if(line>=0 && line<=19){
			for(list=0;list<=20;list++){
				if(list==0 || list==20){
					printw("|");
				}else if(hasSnakeNode(line,list)){
					printw("[]");
				}
				else{
					printw("  ");
				}
			}
		  	printw("\n");
		}
		if(line==19){
			for(list=0;list<20;list++){
				printw("--");
			}
			printw("\nBy Jessie\n");
		}
		
	}

}

void addNode()
{
	struct Snake *new=(struct Snake*)malloc(sizeof(struct Snake));
	new->line=tail->line;
	new->list=tail->list+1;
	new->next=NULL;

	tail->next=new;
	tail=new;
}

void initSnake()
{
	head=(struct Snake*)malloc(sizeof(struct Snake));
	tail=(struct Snake*)malloc(sizeof(struct Snake));
	head->line=2;
	head->list=2;
	head->next=NULL;

	tail=head;
	
	addNode();
	addNode();
	addNode();
}

int main()
{
	initNcurse();
	initSnake();

	gamePic();

	getch();
	endwin();

	return 0;
}

4. 贪吃蛇左右移动

  • snake8.c
#include <curses.h>
#include <stdlib.h>

struct Snake
{
	int line;
	int list;
	struct Snake *next;
};

struct Snake *head;
struct Snake *tail;

int hasSnakeNode(int line,int list)
{
	struct Snake *p=head;
	while(p != NULL){
		if(p->line==line && p->list==list){
			return 1;
		}
		p=p->next;
	}
	return 0;
}

void initNcurse()
{
	initscr();	
	keypad(stdscr,1);
}

void gamePic()
{
	int line;
	int list;

	move(0,0);
	for(line=0;line<20;line++){
		if(line==0){
			for(list=0;list<20;list++){
				printw("--");
			}
			printw("\n");
		}
		if(line>=0 && line<=19){
			for(list=0;list<=20;list++){
				if(list==0 || list==20){
					printw("|");
				}else if(hasSnakeNode(line,list)){
					printw("[]");
				}
				else{
					printw("  ");
				}
			}
		  	printw("\n");
		}
		if(line==19){
			for(list=0;list<20;list++){
				printw("--");
			}
			printw("\nBy Jessie\n");
		}
		
	}

}

void addNode()
{
	struct Snake *new=(struct Snake*)malloc(sizeof(struct Snake));
	new->line=tail->line;
	new->list=tail->list+1;
	new->next=NULL;

	tail->next=new;
	tail=new;
}

void initSnake()
{
	head=(struct Snake*)malloc(sizeof(struct Snake));
	tail=(struct Snake*)malloc(sizeof(struct Snake));
	head->line=2;
	head->list=2;
	head->next=NULL;

	tail=head;
	
	addNode();
	addNode();
	addNode();
	addNode();
}

void deleNode()
{
	struct Snake *p=head;
	head=head->next;
	free(p);
}
void moveSnake()
{
	addNode();
	deleNode();
}

int main()
{
	initNcurse();
	initSnake();
	gamePic();
	while(1){
		int con=getch();
		if(con==KEY_RIGHT){
			moveSnake();
			gamePic();
		}
	}

	getch();
	endwin();

	return 0;
}

5. 贪吃蛇不想活了撞墙找死(144.14)

  • snake9.c
#include <curses.h>//ncurse的头文件
#include <stdlib.h>//malloc的头文件

struct Snake
{
	int line;
	int list;
	struct Snake *next;
};

struct Snake *head=NULL;
struct Snake *tail=NULL;

void initNcurse()//初始化,开始运行,接受功能键
{
	initscr();	//ncurse界面的初始化函数
	keypad(stdscr,1);//从标准的stdscr中接受功能键,1代表是接受
}

void addNode()//增加节点
{
	struct Snake *new=(struct Snake*)malloc(sizeof(struct Snake));
	new->line=tail->line;
	new->list=tail->list+1;
	new->next=NULL;

	tail->next=new;
	tail=new;
}

void initSnake()//初始化蛇的位置和节点个数
{
	struct Snake *p;
	while(head != NULL){
		p=head;
		head=head->next;
		free(p);
	}
	head=(struct Snake*)malloc(sizeof(struct Snake));
	tail=(struct Snake*)malloc(sizeof(struct Snake));
	head->line=1;
	head->list=1;
	head->next=NULL;

	tail=head;
	
	addNode();
	addNode();
	addNode();
	addNode();
}

int hasSnakeNode(int line,int list)//定位蛇的起始位置
{
	struct Snake *p=head;
	while(p != NULL){
		if(p->line==line && p->list==list){
			return 1;
		}
		p=p->next;
	}
	return 0;
}

void gamePic()//打印游戏地图
{
	int line;
	int list;

	move(0,0);//改变光标位置为原型
	for(line=0;line<20;line++){
		if(line==0){
			for(list=0;list<20;list++){
				printw("--");
			}
			printw("\n");
		}
		if(line>=0 && line<=19){
			for(list=0;list<=20;list++){
				if(list==0 || list==20){
					printw("|");
				}else if(hasSnakeNode(line,list)){
					printw("[]");
				}
				else{
					printw("  ");
				}
			}
		  	printw("\n");
		}
		if(line==19){
			for(list=0;list<20;list++){
				printw("--");
			}
			printw("\nBy Jessie\n");
		}
		
	}

}

void deleNode()//删除节点
{
	struct Snake *p=head;
	head=head->next;
	free(p);
}
void moveSnake()//控制蛇移动的函数
{
	addNode();
	deleNode();
	if(tail->line==0||tail->line==20||tail->list==0||tail->list==20){
		initSnake();
	}
}

int main()//主函数
{
	initNcurse();//nurse界面初始化,开始运行,接受功能键
	initSnake();//初始化蛇的位置和节点个数
	gamePic();//打印游戏地图
	while(1){
		int con=getch();
		if(con==KEY_RIGHT){
			moveSnake();//蛇移动
			gamePic();//刷新地图
		}
	}

	getch();
	endwin();

	return 0;
}

6. 贪吃蛇脱缰向右自行游走(145.15)

  • snake10.c
#include <curses.h>
#include <stdlib.h>//malloc的头文件
#include <unistd.h>//usleep的头文件
struct Snake
{
	int line;
	int list;
	struct Snake *next;
};

struct Snake *head=NULL;
struct Snake *tail=NULL;

void initNcurse()//初始化,开始运行,接受功能键
{
	initscr();	//ncurse界面的初始化函数
	keypad(stdscr,1);//从标准的stdscr中接受功能键,1代表是接受
}

void addNode()//增加节点
{
	struct Snake *new=(struct Snake*)malloc(sizeof(struct Snake));
	new->line=tail->line;
	new->list=tail->list+1;
	new->next=NULL;

	tail->next=new;
	tail=new;
}

void initSnake()//初始化蛇的位置和节点个数
{
	struct Snake *p;
	while(head != NULL){
		p=head;
		head=head->next;
		free(p);
	}
	head=(struct Snake*)malloc(sizeof(struct Snake));
	tail=(struct Snake*)malloc(sizeof(struct Snake));
	head->line=1;
	head->list=1;
	head->next=NULL;

	tail=head;
	
	addNode();
	addNode();
	addNode();
	addNode();
}

int hasSnakeNode(int line,int list)//定位蛇的起始位置
{
	struct Snake *p=head;
	while(p != NULL){
		if(p->line==line && p->list==list){
			return 1;
		}
		p=p->next;
	}
	return 0;
}

void gamePic()//打印游戏地图
{
	int line;
	int list;

	move(0,0);//改变光标位置为原型
	for(line=0;line<20;line++){
		if(line==0){
			for(list=0;list<20;list++){
				printw("--");
			}
			printw("\n");
		}
		if(line>=0 && line<=19){
			for(list=0;list<=20;list++){
				if(list==0 || list==20){
					printw("|");
				}else if(hasSnakeNode(line,list)){
					printw("[]");
				}
				else{
					printw("  ");
				}
			}
		  	printw("\n");
		}
		if(line==19){
			for(list=0;list<20;list++){
				printw("--");
			}
			printw("\nBy Jessie\n");
		}
		
	}

}

void deleNode()//删除节点
{
	struct Snake *p=head;
	head=head->next;
	free(p);
}
void moveSnake()//控制蛇移动的函数
{
	addNode();
	deleNode();
	if(tail->line==0||tail->line==20||tail->list==0||tail->list==20){
		initSnake();
	}
}

int main()//主函数
{
	initNcurse();//nurse界面初始化,开始运行,接受功能键
	initSnake();//初始化蛇的位置和节点个数
	gamePic();//打印游戏地图
	while(1){
			moveSnake();//蛇移动
			gamePic();//刷新地图
			refresh();//刷新
			usleep(100000);//100毫秒
	}

	getch();
	endwin();

	return 0;
}

7.贪吃蛇方向移动和刷新界面一起实现面临的问题(146.16)

  • snake11.c
#include <curses.h>//ncurse的头文件
#include <stdlib.h>//malloc的头文件
#include <unistd.h>//usleep的头文件
struct Snake
{
	int line;
	int list;
	struct Snake *next;
};

struct Snake *head=NULL;
struct Snake *tail=NULL;

void initNcurse()//初始化,开始运行,接受功能键
{
	initscr();	//ncurse界面的初始化函数
	keypad(stdscr,1);//从标准的stdscr中接受功能键,1代表是接受
}

void addNode()//增加节点
{
	struct Snake *new=(struct Snake*)malloc(sizeof(struct Snake));
	new->line=tail->line;
	new->list=tail->list+1;
	new->next=NULL;

	tail->next=new;
	tail=new;
}

void initSnake()//初始化蛇的位置和节点个数
{
	struct Snake *p;
	while(head != NULL){
		p=head;
		head=head->next;
		free(p);
	}
	head=(struct Snake*)malloc(sizeof(struct Snake));
	tail=(struct Snake*)malloc(sizeof(struct Snake));
	head->line=1;
	head->list=1;
	head->next=NULL;

	tail=head;
	
	addNode();
	addNode();
	addNode();
	addNode();
}

int hasSnakeNode(int line,int list)//定位蛇的起始位置
{
	struct Snake *p=head;
	while(p != NULL){
		if(p->line==line && p->list==list){
			return 1;
		}
		p=p->next;
	}
	return 0;
}

void gamePic()//打印游戏地图
{
	int line;
	int list;

	move(0,0);//改变光标位置为原型
	for(line=0;line<20;line++){
		if(line==0){
			for(list=0;list<20;list++){
				printw("--");
			}
			printw("\n");
		}
		if(line>=0 && line<=19){
			for(list=0;list<=20;list++){
				if(list==0 || list==20){
					printw("|");
				}else if(hasSnakeNode(line,list)){
					printw("[]");
				}
				else{
					printw("  ");
				}
			}
		  	printw("\n");
		}
		if(line==19){
			for(list=0;list<20;list++){
				printw("--");
			}
			printw("\nBy Jessie\n");
		}
		
	}

}

void deleNode()//删除节点
{
	struct Snake *p=head;
	head=head->next;
	free(p);
}
void moveSnake()//控制蛇移动的函数
{
	addNode();
	deleNode();
	if(tail->line==0||tail->line==20||tail->list==0||tail->list==20){
		initSnake();
	}
}

int main()//主函数
{
	initNcurse();//nurse界面初始化,开始运行,接受功能键
	initSnake();//初始化蛇的位置和节点个数
	gamePic();//打印游戏地图
	while(1){
			moveSnake();//蛇移动
			gamePic();//刷新地图
			refresh();//刷新
			usleep(100000);//100毫秒
	}
	while(1){
		int key=getch();
		switch(key){
			case KEY_DOWN:
				printw("DOWN\n");
				break;
			case KEY_UP:
				printw("UP\n");
				break;
			case KEY_LEFT:
				printw("LEFT\n");
				break;
			case KEY_RIGHT:
				printw("RIGHT\n");
				break;
		}
	}
	getch();
	endwin();

	return 0;
}

8. Linux线程概念引入及编程实现(147.17)

#include <stdio.h>
#include <pthread.h>
void* thread( void *arg )
{
    printf( "This is a thread and arg = %d.\n", *(int*)arg);
    *(int*)arg = 0;
    return arg;
}
int main( int argc, char *argv[] )
{
    pthread_t th;
    int ret;
    int arg = 10;
    int *thread_ret = NULL;
    ret = pthread_create( &th, NULL, thread, &arg );
    if( ret != 0 ){
        printf( "Create thread error!\n");
        return -1;
    }
    printf( "This is the main process.\n" );
    pthread_join( th, (void**)&thread_ret );
    printf( "thread_ret = %d.\n", *thread_ret );
    return 0;
}
  • tmp.c
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

void* func1()
{
	while(1){
		printf("this is func 1\n");
		sleep(1);
	}
}

void* func2()
{
	while(1){
		printf("this is func 2\n");
		sleep(1);
	}
}

int main()
{
	pthread_t th1;
	pthread_t th2;

	pthread_create( &th1, NULL, func1, NULL );
	pthread_create( &th2, NULL, func2, NULL );
	
	while(1);
	return 0;
}

9. 使用线程解决16节中提到问题(148.18)

  • snake12.c
#include <curses.h>//ncurse的头文件
#include <stdlib.h>//malloc的头文件
#include <unistd.h>//usleep的头文件
#include <pthread.h>//pthread线程的头文件

struct Snake
{
	int line;
	int list;
	struct Snake *next;
};

struct Snake *head=NULL;
struct Snake *tail=NULL;
int key;

void initNcurse()//初始化,开始运行,接受功能键
{
	initscr();	//ncurse界面的初始化函数
	keypad(stdscr,1);//从标准的stdscr中接受功能键,1代表是接受
}

void addNode()//增加节点
{
	struct Snake *new=(struct Snake*)malloc(sizeof(struct Snake));
	new->line=tail->line;
	new->list=tail->list+1;
	new->next=NULL;

	tail->next=new;
	tail=new;
}

void initSnake()//初始化蛇的位置和节点个数
{
	struct Snake *p;
	while(head != NULL){
		p=head;
		head=head->next;
		free(p);
	}
	head=(struct Snake*)malloc(sizeof(struct Snake));
	tail=(struct Snake*)malloc(sizeof(struct Snake));
	head->line=1;
	head->list=1;
	head->next=NULL;

	tail=head;
	
	addNode();
	addNode();
	addNode();
	addNode();
}

int hasSnakeNode(int line,int list)//定位蛇的起始位置
{
	struct Snake *p=head;
	while(p != NULL){
		if(p->line==line && p->list==list){
			return 1;
		}
		p=p->next;
	}
	return 0;
}

void gamePic()//打印游戏地图
{
	int line;
	int list;

	move(0,0);//改变光标位置为原型
	for(line=0;line<20;line++){
		if(line==0){
			for(list=0;list<20;list++){
				printw("--");
			}
			printw("\n");
		}
		if(line>=0 && line<=19){
			for(list=0;list<=20;list++){
				if(list==0 || list==20){
					printw("|");
				}else if(hasSnakeNode(line,list)){
					printw("[]");
				}
				else{
					printw("  ");
				}
			}
		  	printw("\n");
		}
		if(line==19){
			for(list=0;list<20;list++){
				printw("--");
			}
			printw("\nBy Jessie,key=%d\n",key);
		}
		
	}

}

void deleNode()//删除节点
{
	struct Snake *p=head;
	head=head->next;
	free(p);
}
void moveSnake()//控制蛇移动的函数
{
	addNode();
	deleNode();
	if(tail->line==0||tail->line==20||tail->list==0||tail->list==20){
		initSnake();
	}
}

void* refreshInt()
{
	while(1){
			moveSnake();//蛇移动
			gamePic();//刷新地图
			refresh();//刷新
			usleep(100000);//100毫秒
	}
}

void* changeDir()
{
	while(1){
		key=getch();
		switch(key){
			case KEY_DOWN:
				printw("DOWN\n");
				break;
			case KEY_UP:
				printw("UP\n");
				break;
			case KEY_LEFT:
				printw("LEFT\n");
				break;
			case KEY_RIGHT:
				printw("RIGHT\n");
				break;
		}
	}
}

int main()//主函数
{
	pthread_t t1;
	pthread_t t2;

	initNcurse();//nurse界面初始化,开始运行,接受功能键
	initSnake();//初始化蛇的位置和节点个数
	gamePic();//打印游戏地图
	
	pthread_create( &t1, NULL, refreshInt, NULL );
	pthread_create( &t2, NULL, changeDir, NULL );

	while(1);

	getch();
	endwin();

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 很高兴能帮助你。贪吃游戏使用C语言编写非常容易,因为C语言具有可读性,可扩展性,可移植性和可维护性,使它成为最适合编写游戏的编程语言之一。你可以使用C语言的绘图函数来绘制游戏的界面,并使用用户输入函数来控制移动。 ### 回答2: 贪吃是一个经典的游戏,可以用C语言编写。首先,我们需要定义的数据结构。一条通常由一个链表来表示,每个结点代表的身体的一个部分,每个结点包含身体的位置(x和y坐标)。除了的身体,我们还需要定义的长度和方向。 接下来,我们需要创建一个游戏界面,在界面上定义移动方向键,并显示的身体以及食物。游戏的核心逻辑是移动。当吃到食物时,它会变长,并生成新的食物。当碰到自己的身体或者碰到边界时,游戏结束。 为了实现游戏的交互性,我们需要使用C语言的输入输出函数进行键盘输入和屏幕输出。我们可以使用控制台相关的函数来实现控制台界面的绘制和刷新。 游戏的主要循环是通过检测键盘输入来控制方向并更新的位置,然后判断是否吃到了食物、撞到了自己或边界,最后更新游戏界面并刷新屏幕。 编写这个贪吃游戏需要一定的编程基础和逻辑思维能力。需要注意处理边界、碰撞、身体长度等各种情况,并为游戏增加适当的难度和可玩性。 总结来说,通过使用C语言的数据结构、输入输出函数和控制台相关的函数,我们可以编写一个简单而又有趣的贪吃游戏。编写游戏不仅能提高我们的编程能力,还能带来无穷的乐趣。 ### 回答3: 贪吃是一款经典的游戏,下面我将详细介绍如何使用C语言来编写一个贪吃游戏。 首先,我们需要使用C语言来建立游戏画面。可以使用C语言的图形库,如graphics.h,或者使用其他第三方库,如SDL。这些库可以帮助我们绘制出游戏界面和身。 接下来,我们需要定义贪吃的身体和食物。身可以使用链表或者数组来表示,每个节点或者元素代表的一节身体。食物则可以随机生成,让去吃。通过不断更新身的位置和方向,我们可以实现移动。 然后,我们需要处理玩家的输入。使用C语言中的键盘输入函数,我们可以捕捉到玩家按下的键,并根据按键来控制方向。比如,当玩家按下方向键时,我们可以更新移动方向。 在游戏进行中,需要注意和食物的碰撞检测。当头与食物重合时,我们需要将食物的位置移动到其他地方,并在的尾部增加一个节点,以延长的身体。同时,如果头与身的其他节点重合或者撞到边界,游戏结束。 最后,我们需要实现游戏循环。通过不断的更新的位置,重新绘制游戏画面,并处理玩家的输入,我们可以创建一个无限循环,让贪吃游戏持续进行。直到游戏结束,显示得分并给出重新开始的选项。 总结起来,通过合理地使用C语言的图形库和相关函数,我们可以编写一个简单而有趣的贪吃游戏。这个游戏可以通过控制移动来获取食物,同时需要避免撞到边界或者身,提高玩家的操作和反应能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值