【C语言】学习笔记,贪吃蛇小游戏

请添加图片描述
运行环境:Linux,用到ncurse

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

#define UP     1
#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()%20;
	int y =rand()%20;
	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>0 && hang<= 18){
			for(lie = 0;lie<=20;lie++){
				if(lie == 0 || lie==20){
					printw("|");
					}else if(hasSnakeNode(hang,lie)){
					printw("[]");
					}
				
				else	{
					printw("  ");
					}
				}
			printw("\n");
		}*/
			if(hang == 19){
				for(lie = 0;lie<20;lie++){
					printw("--");
				}
					printw("\n");
					printw("by Wang ChengXV,food.hang==%d,food.lie==%d",food.hang,food.lie);
			}
		
	}
    
}
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 delNode(){
	struct Snake *p;
	p = head;
	head = head->next;
	free(p);
}

int ifSnakeDie()
{
	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{ 
				delNode();
				}
			if(ifSnakeDie()){

				initSnake();
			}

}

void refreshJieMian()
{
	while(1){
		moveSnake();
		gamePic();
		refresh();
		usleep(100000);
          }

}

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;
	pthread_t t2;

	initNcurse();
	initSnake();
	gamePic();
	pthread_create(&t1,NULL,refreshJieMian,NULL);
        pthread_create(&t2,NULL,changeDir,NULL);
	while(1);
        
	getch();
	endwin();
	return 0;
}
  • 13
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的贪吃游戏的 C 语言代码: ``` #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <time.h> #define MAP_WIDTH 20 #define MAP_HEIGHT 20 #define SNAKE_MAX_LENGTH 100 int map[MAP_WIDTH][MAP_HEIGHT]; int snakeX[SNAKE_MAX_LENGTH], snakeY[SNAKE_MAX_LENGTH]; int snakeLength = 0; int score = 0; int foodX = 0, foodY = 0; int direction = 1; // 1: up, 2: right, 3: down, 4: left void initMap() { for (int i = 0; i < MAP_WIDTH; i++) { for (int j = 0; j < MAP_HEIGHT; j++) { if (i == 0 || i == MAP_WIDTH - 1 || j == 0 || j == MAP_HEIGHT - 1) { map[i][j] = 1; } else { map[i][j] = 0; } } } } void printMap() { system("cls"); for (int i = 0; i < MAP_WIDTH; i++) { for (int j = 0; j < MAP_HEIGHT; j++) { if (map[i][j] == 1) { printf("#"); } else if (map[i][j] == 2) { printf("*"); } else if (map[i][j] == 3) { printf("O"); } else { printf(" "); } } printf("\n"); } printf("Score: %d\n", score); } void generateFood() { srand((unsigned int)time(NULL)); int x, y; do { x = rand() % (MAP_WIDTH - 2) + 1; y = rand() % (MAP_HEIGHT - 2) + 1; } while (map[x][y] != 0); foodX = x; foodY = y; map[foodX][foodY] = 2; } void initSnake() { int x = MAP_WIDTH / 2, y = MAP_HEIGHT / 2; snakeX[0] = x; snakeY[0] = y; snakeLength = 1; map[x][y] = 3; } void updateSnake() { int x = snakeX[0], y = snakeY[0]; switch (direction) { case 1: y--; break; case 2: x++; break; case 3: y++; break; case 4: x--; break; } if (x == foodX && y == foodY) { snakeLength++; score += 10; generateFood(); } else { int tailX = snakeX[snakeLength - 1], tailY = snakeY[snakeLength - 1]; map[tailX][tailY] = 0; for (int i = snakeLength - 1; i > 0; i--) { snakeX[i] = snakeX[i - 1]; snakeY[i] = snakeY[i - 1]; } snakeX[0] = x; snakeY[0] = y; } if (map[x][y] == 1 || map[x][y] == 3) { printf("Game over!\n"); exit(0); } map[x][y] = 3; } void handleInput() { if (_kbhit()) { int ch = _getch(); if (ch == 'w' || ch == 'W') { if (direction != 3) { direction = 1; } } else if (ch == 'd' || ch == 'D') { if (direction != 4) { direction = 2; } } else if (ch == 's' || ch == 'S') { if (direction != 1) { direction = 3; } } else if (ch == 'a' || ch == 'A') { if (direction != 2) { direction = 4; } } } } int main() { initMap(); generateFood(); initSnake(); while (1) { printMap(); updateSnake(); handleInput(); Sleep(100); } return 0; } ``` 希望能帮到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值