(C语言贪吃蛇)15.贪吃蛇吃食物

news/2024/10/8 22:40:12 标签: c语言, 开发语言, linux, C语言贪吃蛇, 嵌入式

目录

前言

注意事项⚠️

效果预览

 实现方法

运行效果

新的问题🙋

最终效果 

总结


前言

        我们上一节实现了解决了贪吃蛇不合理走位的情况,不理解的再回去看看(传送门:解决贪吃蛇不合理走位),那么贪吃蛇自然是要吃食物的啊,所以我们本节来实现贪吃蛇吃食物的效果

注意事项⚠️

        贪吃蛇食物关心的是位置及符号,位置同样可以使用贪吃蛇节点结构体,食物显示为“##”。

效果预览

        

我们用“##”来表示食物,贪吃蛇的头节点碰到食物就吃掉食物。

 实现方法

        我们创建一个结构体struct Snake food(我们先来测试一下食物能不能成功生成,所以我们赋予了食物坐标的初始值)。

struct Snake food;
void initfood()
{
    int x = 4;
    int y = 5;

    food.lie = x;
    food.lie = y;
}

我们创造出了食物节点,那么要显示这个节点呢,我们需要修改gamePic()函数,增加判断,该节点位置是否为食物节点,是的话就打印“##”:

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;    
}

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 == 19)
        {
            for(lie = 0;lie < 20;lie ++)
               {
                   printw("--");
               }
               printw("\n");
               printw("by beiweiqiuAC,%d\n",key);
        }
    }

}

我们在上面的函数中新增了一个判断函数hasfood(),是用来判断这个节点是否时食物节点,这个函数其实非常熟悉,因为我们判断蛇身节点的函数跟这个函数可以说是大同小异吧

先来看一下判断蛇身节点的函数:

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;    
}

我们稍微修改下就变成了hasfood()函数,非常简单哈:

int hasfood(int i,int j)
{

    if(food.hang == i && food.lie == j) return 1;
    return 0;    
}

运行效果

        运行测试一下这个程序:

可以看到食物已经出现在屏幕上了。

新的问题🙋

        既然是食物,那贪吃蛇应该如何吃掉呢?

在这个贪吃蛇里面,贪吃蛇的蛇头实际上是链表的尾节点,那么尾节点的坐标和食物的坐标一样,然后消除就代表吃食物了。

最终效果 

#include <curses.h>
#include <stdlib.h>
#include <unistd.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()
{
    static int x = 11;
    static int y = 6;

    food.hang = x;
    food.lie = y;

    x += 2;
    y += 2;
}

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 == 19)
        {
            for(lie = 0;lie < 20;lie ++)
               {
                   printw("--");
               }
               printw("\n");
               printw("by beiweiqiuAC,%d\n",key);
        }
    }

}

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 deleNode()
{
 // struct Snake * p;
 // p = head;
    head = head ->next;
 // free(p);
}

void moveSnake()
{
    addNode();
    if(hasfood(tail->hang,tail->lie))
    {
        initfood();
    }else{
        deleNode();
    }
    

    if(tail ->hang == 0 || tail->lie == 0 || tail->hang == 20 || tail ->lie == 20)
    {
        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 0402:
                    turn(DOWN);
                    break;
            case 0403:
                    turn(UP);
                    break;
            case 0404:
                    turn(LEFT);
                    break;
            case 0405:
                    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;
}

 

        但其实这个程序还是有点小bug,食物到后面就跑到地图外面了,我们下一节实现食物随机出现的效果。

总结

        我们本节实现了贪吃蛇吃食物的效果,吃一个食物身体就伸长一个节点,但还是有点小bug,我们下一节来解决📨。


http://www.niftyadmin.cn/n/5694893.html

相关文章

【Vue】Vue2(2)

文章目录 1 数据代理1.1 回顾Object.defineproperty方法1.2 何为数据代理1.3 Vue中的数据代理 2 事件处理2.1 事件的基本使用2.2 事件修饰符2.3 键盘事件 1 数据代理 1.1 回顾Object.defineproperty方法 <!DOCTYPE html> <html><head><meta charset&quo…

Crypto虐狗记---”你“和小鱼(五)

前言&#xff1a;剧情五 提示&#xff1a; 一种食物&#xff1f; 一种食物——培根&#xff1a;&#xff08;A B 也暗示是培根加密&#xff09; cyberpeace{attackanddefenceworldisinteresting} 密码学笔记——培根密码 - ILK - 博客园 (cnblogs.com)

当x趋于零时,零乘以无穷的极限等于多少

当x趋于零时&#xff0c;零乘以无穷的极限是未定义。‌ 在数学中&#xff0c;0乘以无穷大&#xff08;0 ∞&#xff09;是一个未定义的表达式&#xff0c;因为它涉及到两个相互矛盾的概念&#xff1a;0乘以任何有限数都等于0&#xff0c;而无穷大乘以任何非零数都应该是无穷大…

聊聊Mysql的MVCC

1 什么是MVCC&#xff1f; MVCC&#xff0c;是Multiversion Concurrency Control的缩写&#xff0c;翻译过来是多版本并发控制&#xff0c;和数据库锁一样&#xff0c;他也是一种并发控制的解决方案。 我们知道&#xff0c;在数据库中&#xff0c;对数据的操作主要有2种&#…

C++——STL简介

目录 一、什么是STL 二、STL的版本 三、STL的六大组件 没用的话..... 不知不觉两个月没写博客了&#xff0c;暑假后期因为学校的事情在忙&#xff0c;开学又在准备学校的java免修&#xff0c;再然后才继续开始学C&#xff0c;然后最近打算继续写博客沉淀一下最近学到的几周…

MFC工控项目实例二十三模拟量输入设置界面

承接专栏《MFC工控项目实例二十二主界面计数背景颜色改变》 1、在SenSet.h文件中添加代码 #include "BtnST.h" #include "ShadeButtonST.h"/ // SenSet dialogclass SenSet : public CDialog { // Construction public:SenSet(CWnd* pParent NULL); //…

【Docker】Docker 实践与应用举例

目录 1. Docker 基本概念2. Docker 安装2.1 安装步骤2.2 验证安装 3. Docker 实践3.1 创建第一个 Docker 容器3.2 管理容器3.3 创建 Dockerfile3.4 数据卷 4. 应用举例4.1 Web 应用部署4.2 数据库容器化4.3 多容器应用 总结参考文献 Docker 是一个开源平台&#xff0c;用于自动…

倪师学习笔记-天纪-01

一、概要 介绍课程内容&#xff0c;介绍部分概念 二、具体内容 1、天纪内容 天机道&#xff1a;看象&#xff0c;使用斗数等工具人间道&#xff1a;看卦&#xff0c;使用易经地脉道&#xff1a;看风水地理 2、神 神与形对应&#xff0c;形是神的实例&#xff0c;神是形的…