贪吃蛇游戏(C语言+Linux)

        为了加深自己对于C语言的掌握和理解,写个比较复杂的贪吃蛇小游戏。贪吃蛇代码量大概有300行左右,基本运用了C语言的常见的知识点(指针,链表,结构体,函数封装与调等),对于自己的C语言能力的加强和逻辑思考能力的提升有较大的帮助。

#include <curses.h>
#include <stdlib.h>
#include <pthread.h>

#define UP  	 1
#define DOWN 	-1
#define LEFT  	 2
#define RIGHT  	-2

struct Snake
{
	int row;
	int column;
	struct Snake *next;
};

struct Snake *head = NULL;
struct Snake *tail = NULL;
int key;
int direction;
int score;
int gameCnt = 1;
int totalScore;
struct Snake food;

void initCurse()
{
	initscr();
	keypad(stdscr, 1);
	noecho();
}

int traversalSnakeFood(int i, int j)
{
	if (food.row == i && food.column == j){
		return 1;	
	}

	return 0;
}

int traversalSnakeNode(int i, int j)
{
	struct Snake *p;
	p = head;
	while (p != NULL){
		if (p->row == i && p->column == j){
			return 1;
		}
		p = p->next;
	}
	
	return 0;
}

void gameMap()
{
	int i, j;
	int row = 20;
	int column = 20;

	move(0, 0);
	
	for (i=0; i<row; i++){
		if (i == 0){
			for (j=0; j<column; j++){
				printw("--");
			}
			printw("\n");	
		}
		if (i>=0 && i<=row-1){
			for (j=0; j<column+1; j++){
				if (j == 0 || j == column){
					 printw("|");
				}
				else if (traversalSnakeFood(i, j)){
					printw("$$");
				}
				else if (traversalSnakeNode(i, j)){
					printw("[]");
				}
				else{
					printw("  ");
				}
			}
			printw("\n");
		}
		if (i == row-1){
			for (j=0; j<column; j++){
                                printw("--");
                        }
                        printw("\n");
			printw("Your current score is: %d \n", score);
			printw("Your game count: %d \n", gameCnt);
			printw("Your total score is: %d \n", totalScore);
			printw("By NUIST WF\n");
			printw("Your control direction: ");
		}
	}
}

void addSnakeTailNode()
{
	struct Snake *new;
	new = (struct Snake*)malloc(sizeof(struct Snake));

	switch (direction){
		case UP:
			new->row = tail->row - 1;
			new->column = tail->column;
			break;
		case DOWN:
			new->row = tail->row + 1;
			new->column = tail->column;
			break;
		case LEFT:
			new->row = tail->row;
			new->column = tail->column - 1;
			break;
		case RIGHT:
			new->row = tail->row;
			new->column = tail->column + 1;
			break;
	}
	new->next = NULL;

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

void deleteSnakeHeadNode()
{
	struct Snake *p;
	p = head;

	head = head->next;

	free(p);
}

void initSnakeFood()
{
	int x = rand()%20;
	int y = rand()%20;

	food.row = x;
	food.column = y;
}

void initSnakeNode()
{
	direction = RIGHT;

	initSnakeFood();	

	struct Snake *p;
	while (head != NULL){
		p = head;
		head = head->next;
		free(p);
	}		
	
	head = (struct Snake*)malloc(sizeof(struct Snake));
	
	head->row = 1;
	head->column = 1;
	head->next = NULL;
	
	tail = head;
	
	addSnakeTailNode();
	addSnakeTailNode();
}

int snakeDie()
{
	struct Snake *p;	
	p = head;
	while (p->next != NULL){
		if ((tail->row == p->row) && (tail->column == p->column)){
			return 1;
		}
		p = p->next;
	}
	
	if (tail->row < 0 || tail->row == 20 || tail->column == 0 || tail->column == 20){
		return 1;
	}

	return 0;
}

void moveSnake()
{	
	addSnakeTailNode();

	if ((tail->row == food.row) && (tail->column == food.column)){
		score++;
		totalScore++;
		initSnakeFood();
	}
	else{
		deleteSnakeHeadNode();
	}
	
	if (snakeDie()){
		initSnakeNode();
		gameCnt++;
		score = 0;	
	}
}

void turn(int tempdirection)
{
	if (abs(direction) != abs(tempdirection)){
		direction = tempdirection;
	}
}

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

void* refreshInterface()
{
	while (1){
		moveSnake();
		gameMap();
		refresh();
		usleep(200000);
	}
}

int main()
{
	pthread_t mythread1, mythread2;

	initCurse();
	gameMap();
	initSnakeNode();

	pthread_create(&mythread1, NULL, directionMove, NULL);
	pthread_create(&mythread2, NULL, refreshInterface, NULL);
	
	while(1);
	endwin();
	return 0;
}

结果展示:(贪吃蛇视频可看)

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Linux再对我好一点

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值