用C语言写个最简单的贪吃蛇

Posted landaliming

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用C语言写个最简单的贪吃蛇相关的知识,希望对你有一定的参考价值。

一、思路

  1. 定义二维数组window表示窗口;
  2. 定义BLANK、BORDER、SNAKE、FOOD 分别表示:空白、边框、蛇身、食物;
  3. 定义并初始化: 蛇头,蛇尾,食物;
  4. 显示初始窗口;
  5. 循环getchar,确定move方向,并使用fifo保存move方向,用于移动蛇尾;
  6. 计算新的蛇头,判断新蛇头为空白则移动蛇尾;为食物则不移动蛇尾,并重新生成食物;其他情况则直接结束游戏;
  7. 显示蛇头;
  8. 显示窗口。

二、最终效果

lim@TIM:~/code/game$ ./main 
OOOOOOOOOOOOOOOO
O          *   O
O    DDD       O
O      D       O
O      D       O
O      D  D    O
O      DDDD    O
O              O
O              O
O              O
O              O
O              O
O              O
O              O
O              O
OOOOOOOOOOOOOOOO

三、代码

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <memory.h>

#define XLEN 16
#define YLEN 16

enum 
    BLANK = ' ',    // 空白
    BORDER = 'O',   // 边框
    SNAKE = 'D',    // 蛇身
    FOOD = '*',     // 食物
;

typedef struct point 
    int x;
    int y;
point;

typedef struct queue

    char buf[256];
    char in;
    char out;
queue;

void queue_init(queue *q)

    q->in = 0;
    q->out = 0;


void queue_push(queue *q, char c)

    q->buf[q->in++] = c;


char queue_pop(queue *q)

    return q->buf[q->out++];


static char window[XLEN][YLEN] = 0;
static point head;
static point tail;
static point food;
static queue action_queue;


point get_rand_point() 
    struct point node;
    do
    
        node.x = rand() % (XLEN - 2) + 1;
        node.y = rand() % (YLEN - 2) + 1;
     while (window[node.x][node.y] != BLANK);
    
    return node;


void window_init()

    memset(window, BLANK, sizeof(window));

    for (int x = 0; x < XLEN; ++x) 
        window[x][0] = BORDER;
        window[x][YLEN - 1] = BORDER;
    

    for (int y = 0; y < YLEN; ++y) 
        window[0][y] = BORDER;
        window[XLEN - 1][y] = BORDER;
    

    head = get_rand_point();
    tail = head;
    food = get_rand_point();
    window[head.x][head.y] = SNAKE;
    window[food.x][food.y] = FOOD;


void window_show()

    for (int y = 0; y < YLEN; ++y) 
        for (int x = 0; x < XLEN; ++x) 
            printf("%c", window[x][y]);
                
        printf("\\n");
        


int move(point *node, char c) 

    switch (c) 
    case 'a':
        node->x -= 1;
        break;
    case 'd':
        node->x += 1;
        break;
    case 'w':
        node->y -= 1;
        break;
    case 's':
        node->y += 1;
        break;

    default:
        return -1;
    

    return 0; 


int main(int argc, char *argv[])

    srand((unsigned int)time(0));
    window_init();
    window_show(window, XLEN, YLEN);

    queue_init(&action_queue);
    char c = 0;
    while ((c = getchar()) != EOF) 
        if (move(&head, c))
            continue;  
        
        queue_push(&action_queue, c);

        switch (window[head.x][head.y]) 
        case BORDER:
        case SNAKE:
            goto gameover;
        case FOOD:
            food = get_rand_point();
            window[food.x][food.y] = FOOD;
            break;
         case BLANK:
            window[tail.x][tail.y]= BLANK;
            move(&tail, queue_pop(&action_queue));
            break;
        
        
        window[head.x][head.y]= SNAKE;

        printf("\\r\\033[%dA", YLEN + 1);
        window_show((int **)window, XLEN, YLEN); 
    

gameover:
    printf("game over\\n");

以上是关于用C语言写个最简单的贪吃蛇的主要内容,如果未能解决你的问题,请参考以下文章

用C语言写个最简单的贪吃蛇

用C语言写个最简单的贪吃蛇

c语言 贪吃蛇 程序

c语言 贪吃蛇 程序

C语言课程设计,贪吃蛇应该怎么做?

C语言实现贪吃蛇小游戏!(超简单详细)详细思路+源码分享