贪吃蛇游戏的制作

liunx系统下用c语言做的贪吃蛇游戏

我在学习c语言编程之后,对于链表和结构体的用法还不太清楚,因而通过来制作这样一个小游戏来加强自己对这两个知识点的应用,同时,因为c语言的一些缺陷,所以我们还得用上ncurses的图形界面库,并使用liunx系统编程中的线程来进行编程,废话不多说,上源码。

#include<stdio.h>
#include<curses.h>
#include<stdlib.h>
#include<pthread.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()%21;
	int y=rand()%21;
	food.hang=x;
	food.lie=y;
}

void addnode()
{
	struct snake *new;
	new=(struct snake *)malloc(sizeof(struct snake));
	new->hang=tail->hang;
	new->lie=tail->lie+1;
	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=(struct snake *)malloc(sizeof(struct snake));
	dir=RIGHT;
	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->hang=1;
	head->lie=1;
	head->next=NULL;
	tail=head;
	initfood();
	addnode();
	addnode();
	addnode();
}

void deletenode()
{
	struct snake *p=(struct snake*)malloc(sizeof(struct snake));
	p=head;
	head=head->next;
	free(p);
}

int judge3()
{
	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(judge2(tail->hang,tail->lie)){
		initfood();
		}else{
	deletenode();
		}
	if(judge3()){
		initsnake();
		}
	
}

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 judge2(int i,int j)
{
	if(food.hang==i&&food.lie==j){
		return 1;
		}else{
	return 0;
		}
}

int judge(int a,int b)
{
	struct snake *p;
	p=(struct snake*)malloc(sizeof(struct snake));
	p=head;
	while(p!=NULL){
		if(p->hang==a&&p->lie==b){
			return 1;
			}
		p=p->next;
	}	
	return 0;
}

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

void gameCreat()
{
	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(judge(hang,lie)){
					printw("[]");
						}else if(judge2(hang,lie)){
						printw("##");
						}
						else{
						printw("  ");  
							}
			}
			printw("\n");
		}
		if(hang==19){
			for(lie=0;lie<20;lie++){
				printw("--");
				}
			printw("\n");
			printw("by the jiangwencai");
		}
	}
}

void* refreshInterface()
{
	while(1){
		movesnake();
		gameCreat();
		refresh();
		usleep(150000);
	}	
}

int main()
{
	pthread_t t1;

	pthread_t t2;	

	initNcurse();

	initsnake();

	gameCreat();

	pthread_create(&t1,NULL,refreshInterface,NULL);

	pthread_create(&t2,NULL,changeDir,NULL);

	while(1);

	getch();

	endwin();
	
	return 0;
}

主要应用的是链表和结构体,当然像c语言的基础例如函数调用就不在赘述了,当然这里引入了线程的概念,这是为了实现两个while(1)循环同时运行而采用,另外还应用了ncurses库相关的打印函数,因为这个图形库的输入输出函数要比c标准库的函数好用,不用按回车就能响应,从而提高游戏的可玩性,现在能给大家看看编译时和运行成功后的样子。

编译时:gcc f.c -lncurse -lpthread(一定要链上ncurses和线程的库不然编译时会报错,因为有些函数编译器会不认识)

运行程序后的界面:
在这里插入图片描述
因为截图不能体现动态,大家可以自行运行体验游戏。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Python和Pygame制作的经典贪吃游戏的示例代码: ```python import pygame import random # 初始化游戏 pygame.init() # 定义颜色 BLACK = (0, 0, 0) WHITE = (255, 255, 255) GREEN = (0, 255, 0) RED = (255, 0, 0) # 设置屏幕尺寸 screen_width = 800 screen_height = 600 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("贪吃游戏") # 定义的初始位置和大小 snake_block_size = 20 snake_speed = 15 font_style = pygame.font.SysFont(None, 50) score_font = pygame.font.SysFont(None, 35) def our_snake(snake_block_size, snake_list): for x in snake_list: pygame.draw.rect(screen, GREEN, [x[0], x[1], snake_block_size, snake_block_size]) def message(msg, color): mesg = font_style.render(msg, True, color) screen.blit(mesg, [screen_width / 6, screen_height / 3]) def game_loop(): game_over = False game_close = False # 初始化的位置和移动方向 x1 = screen_width / 2 y1 = screen_height / 2 x1_change = 0 y1_change = 0 # 初始化的身体 snake_List = [] Length_of_snake = 1 # 初始化食物的位置 foodx = round(random.randrange(0, screen_width - snake_block_size) / 20.0) * 20.0 foody = round(random.randrange(0, screen_height - snake_block_size) / 20.0) * 20.0 while not game_over: while game_close: screen.fill(BLACK) message("游戏结束!按Q退出或C重新开始", RED) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_q: game_over = True game_close = False if event.key == pygame.K_c: game_loop() for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: x1_change = -snake_block_size y1_change = 0 elif event.key == pygame.K_RIGHT: x1_change = snake_block_size y1_change = 0 elif event.key == pygame.K_UP: y1_change = -snake_block_size x1_change = 0 elif event.key == pygame.K_DOWN: y1_change = snake_block_size x1_change = 0 # 判断是否撞墙 if x1 >= screen_width or x1 < 0 or y1 >= screen_height or y1 < 0: game_close = True x1 += x1_change y1 += y1_change screen.fill(BLACK) pygame.draw.rect(screen, RED, [foodx, foody, snake_block_size, snake_block_size]) snake_Head = [] snake_Head.append(x1) snake_Head.append(y1) snake_List.append(snake_Head) if len(snake_List) > Length_of_snake: del snake_List[0] for x in snake_List[:-1]: if x == snake_Head: game_close = True our_snake(snake_block_size, snake_List) pygame.display.update() # 判断是否吃到食物 if x1 == foodx and y1 == foody: foodx = round(random.randrange(0, screen_width - snake_block_size) / 20.0) * 20.0 foody = round(random.randrange(0, screen_height - snake_block_size) / 20.0) * 20.0 Length_of_snake += 1 # 设置游戏速度 pygame.time.Clock().tick(snake_speed) pygame.quit() game_loop() ``` 你可以将以上代码保存为一个名为`snake_game.py`的文件,并在终端中运行该文件,即可开始游戏

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值