用C语言在ubantu中编写贪吃蛇小游戏(linux系统)
1.展示
2.想明白做出来贪吃蛇要做什么?(3个大纲)
①游戏初始化
②启动游戏
③结束游戏
3.创建这些文件,并写入代码,Linux创建文件的命令
touch main.c,e而后vim main.c编译。(每一个.c文件对应一个.h文件.demo1.c为测试文件不用创建)
①main.c写入
include "main.h"
#include <stdio.h>
int main(int argc,char *argv[])
{
//游戏元素的初始化
init_game();
//启动游戏
start_game();
//退出游戏
end_game();
return 0;
}
②main.h写入
//防止重入的机制
#ifndef __MAIN_H__
#define __MAIN_H__
#include "game.h"
#endif
~
③game.c写入
//用数据结构队列
#include "game.h"
char (*bg)[BG_SIZE] = NULL;
Queue *snake;
//背景
void bg_print(void)
{
for(int i=0;i<BG_SIZE;i++)
{
for(int j = 0;j<BG_SIZE;j++)
{
putchar(bg[i][j]);
//printf("%d",bg[i][j]);
putchar(' ');
}
putchar('\n');
}
}
//背景边框
void bg_frame_set(char frame)
{
for(int i=0;i<BG_SIZE;i++)
bg[0][i] = bg[BG_SIZE-1][i] = bg[i][0] = bg[i][BG_SIZE-1] = frame;
}
void bg_all_set(char chr)
{
for(int i=0;i<BG_SIZE;i++)
for(int j=0;j<BG_SIZE;j++)
bg[i][j] = chr;
}
void bg_init(void)
{
//背景图空间创建
bg = malloc(sizeof(char)*BG_SIZE*BG_SIZE);
//初始化所有字符
bg_all_set(' ');
//背景的边框(*)
bg_frame_set('*');
//bg_print();
}
void init_game(void)
{
//背景初始化
bg_init();
//初始化的蛇
snake = create_queue();
push(snake,BG_SIZE/2,1);
push(snake,BG_SIZE/2,2);
push(snake,BG_SIZE/2,3);
//食物的初始化
srand(time(NULL));
//按键初始化(非阻塞)
int flags;
if(flags = fcntl(0, F_GETFL, 0) < 0)
{
perror("fcntl");
return ;
}
flags |= O_NONBLOCK;
if(fcntl(0, F_SETFL, flags) < 0)
{
perror("fcntl");
return ;
}
//其他
}
//随着长度的增加,速度也在增加
int check_level(int num)
{
int level = num/10;
if(level > 5)
level = 5;
return 300000-50000*level;
}
//游戏开始
void start_game()
{
int food[2];//存放食物的坐标
int food_flag = 0;//存放食物是否被吃掉
int station = 4;//1 --上 2 -- 下 3 ---- 左 4 ----- 右
//printf("%d---------------------------------\n",num_queue(snake));
//sleep(3);
while(1)
{
// 画背景(蛇/背景/事物)
bg_init();
unsigned short res0 = find_queue(snake,num_queue(snake));
int s_head_x = res0>>8 & 0xff;
int s_head_y = res0>>0 & 0xff;
for(int i = 1;i<=num_queue(snake);i++){
unsigned short res = find_queue(snake,i);
if(i != num_queue(snake))
{
if((res>>8 & 0xff) == s_head_x && (res>>0 & 0xff) == s_head_y)
return ;
else
bg[res>>8 & 0xff][res>>0 & 0xff] = 'o';
}
else
{
bg[res>>8 & 0xff][res>>0 & 0xff] = '+';
}
//printf("%d -- %d\n",res>>8 & 0xff,res & 0xff);
}
//sleep(10);
while(!food_flag)
{
int line = rand()%(BG_SIZE-2)+1;
int row = rand()%(BG_SIZE-2)+1;
if(bg[line][row] == ' ')
{
food_flag = 1;
food[0] = line;
food[1] = row;
break;
}
}
bg[food[0]][food[1]] = '@';
system("clear");//如果没有这行代码,看到的是好多框
printf("长度: %d 速度:%d\n",num_queue(snake),num_queue(snake)/10);
bg_print();
//等待时间
usleep(check_level(num_queue(snake)));
//接收用户输入
char chr = getch();
if(chr != -1)
{
// 修改内容
//printf("------------------%c-------------------\n",chr)
if((chr == 'w' || chr == 'W') && station != 2)
station = 1;
else if(chr == 's' && station != 1)
station = 2;
else if(chr == 'a' && station != 4)
station = 3;
else if(chr == 'd' && station != 3)
station = 4;
else if(chr == 'q')
break;
}
//pop(snake);
unsigned short res1 = find_queue(snake,num_queue(snake));
int line1 = res1>>8 & 0xff;
int row1 = res1>>0 & 0xff;
switch(station)
{
case 1:
push(snake,line1-1,row1);
break;
case 2:
push(snake,line1+1,row1);
break;
case 3:
push(snake,line1,row1-1);
break;
case 4:
push(snake,line1,row1+1);
}
//判断(是否存活/食物更新)
if(line1 == food[0] && row1 == food[1])
food_flag = 0;
else
pop(snake);
putchar(bg[line1][row1]);
if(line1 == 0 || line1 == BG_SIZE-1 || row1 == 0 || row1 == BG_SIZE-1)
break;
}
}
void end_game(void)
{
free(bg);
delete_queue(snake);
}
④game.h写入
#ifndef __GAME_H__
#define __GAME_H__
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "queue.h"
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#define BG_SIZE 25//贪吃蛇边框要不断修改,所以设计一个宏,只改一个参数就可以完成对这以常量整体的修改
//贪吃蛇游戏初始化
void init_game(void);
void start_game(void);
void end_game(void);
void bg_init(void);
void bg_print(void);
void bg_frame_set(char);
#endif
⑤queue.c写入
#include "queue.h"
Queue *create_queue(void)
{
Queue *head = malloc(sizeof(Queue));
head->num = 0;
head->first = NULL;
head->end = NULL;
return head;
}
void delete_queue(Queue *head)
{
while(head->num != 0)
pop(head);
free(head);
}
int num_queue(Queue *head)
{
return head->num;
}
int find_queue(Queue *head,int num)
{
Node *buf = head->first;
if(num<=0 || num >head->num)
return 0;
for(int i = 0;i<num-1;i++)
buf = buf->next;
int res = ((unsigned char)buf->line)<<8|((unsigned char)buf->row);
return res;
}
unsigned short pop(Queue *head)
{
unsigned short res = 0;
if(head->num == 0)
return res;
else if(head->num == 1)
{
res=((unsigned char)head->first->line)<<8|((unsigned char)head->first->row);
free(head->first);
head->num = 0;
head->first = head->end = NULL;
return res;
}
else
{
res=((unsigned char)head->first->line)<<8|((unsigned char)head->first->row);
Node *buf = head->first->next;
free(head->first);
head->first = buf;
head->num--;
return res;
}
}
void push(Queue *head,int line,int row)
{
//构建节点
Node *node = malloc(sizeof(Node));
node->line = line,
node->row = row;
node->next = NULL;
if(head->first == NULL && head->end == NULL)
head->first = head->end = node;
else
{
head->end->next = node;
head->end = node;
}
head->num ++;
}
⑥queue.h写入
#ifndef __QUEUE_H__
#define __QUEUE_H__
#include <stdlib.h>
typedef struct node
{
int line;
int row;
struct node *next;
}Node;
typedef struct queue
{
//queue的属性信息
int num;
//queue的指针
struct node *first;
struct node *end;
}Queue;
Queue *create_queue(void);
void delete_queue(Queue *head);
int num_queue(Queue *head);
unsigned short pop(Queue *head);
void push(Queue *head,int line,int row);
int find_queue(Queue *head,int num);
#endif
4,按照图片中内容的顺序输入,即可玩贪吃蛇
(贪吃蛇控制键wasd)