Godot C# 实现简单【贪吃蛇 】Demo【上】

Godot C# 实现贪吃蛇 Demo

1.启动godot并且新建项目

2.创建一个Area2D节点作为蛇头(SnakeHead)【快捷键Ctrl + N,Ctrl + A】

1.给Area2D添加Sprite2D节点
2.给Sprite2D添加一个图像并调整大小(是蛇头)
3.给Area2D添加一个CollisionShape2D
4.保存场景【Ctrl + S】

在这里插入图片描述

3.创建一个新的Area2D节点作为蛇身(SnakeBody)

根据一下场景树添加节点并且保存场景

在这里插入图片描述

4.创建一个新的Node2D 【控制蛇的移动和身体增长】

按一下场景书添加节点

在这里插入图片描述
【主:添加食物之前timer用来测试蛇身体增长效果的出发的条件】

将Timer按照以下图片设置

在这里插入图片描述

添加输入映射

根据以下步骤操作

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Player添加一个名为Player.cs的脚本
using Godot;
using System;
using System.Collections.Generic;

public partial class Player : Node2D
{
    // 将蛇头打包进来
    [Export]
    private PackedScene thePlayerHead { get; set; }

    // 将蛇身打包进来
    [Export]
    private PackedScene thePlayerBody { get; set; }

    // 记录蛇头的参量
    private Area2D theHead;

    // 记录蛇身的列表
    private List<Area2D> theBodyList = new List<Area2D>();

    // 控制蛇移动的参量
    private Vector2 _direction = Vector2.Up;

    private const float GridSize = 48f;     //每次移动的距离
    private float _timer = 0f;      //临时时间变量
    private const float MoveInterval = 0.2f;    //参考时间变量


    //准备函数
    public override void _Ready()
    {
        // 实例化蛇头
        theHead = thePlayerHead.Instantiate<Area2D>();
        // 调整蛇头的位置
        theHead.Position = new Vector2(640, 360);
        // 讲述人头添加到蛇身列表的第一个元素位置
        theBodyList.Add(theHead);
        // 将蛇头添加到Player场景中
        AddChild(theHead);
    }

    public override void _Process(double delta)
    {
        // 控制蛇移动函数的调用
        _timer += (float)delta;
        if (_timer >= MoveInterval)
        {
            _timer = 0;
            MoveSnake();
        }

        // 根据玩家输入控制蛇移动方向
        ControlDirection();
    }

    // 蛇移动函数
    private void MoveSnake()
    {
        Vector2 previousPosition = theHead.Position;    //记录蛇头的位置,并且每一次循环中根据蛇头的移动调整蛇身的移动
        theHead.Position += _direction * GridSize;      //蛇头移动

        // 通过循环控制蛇身跟随蛇头移动
        for (int i = theBodyList.Count - 1; i > 0; i--)
        {
            // 后一个蛇身节点跟随前一个节点移动(第一个蛇身节点是蛇头)
            Vector2 temp = theBodyList[i].Position;
            theBodyList[i].Position = previousPosition;
            previousPosition = temp;
        }
    }

    // 控制蛇身的移动并且防止反方向移动
    private void ControlDirection()
    {
        if (Input.IsActionJustPressed("Move_Up") && _direction != Vector2.Down)
        {
            _direction = Vector2.Up;
        }
        else if (Input.IsActionJustPressed("Move_Down") && _direction != Vector2.Up)
        {
            _direction = Vector2.Down;
        }
        else if (Input.IsActionJustPressed("Move_Right") && _direction != Vector2.Left)
        {
            _direction = Vector2.Right;
        }
        else if (Input.IsActionJustPressed("Move_Left") && _direction != Vector2.Right)
        {
            _direction = Vector2.Left;
        }
    }

    // 吃到食物增长身体
    // 暂时没有添加食物,根据timer控制[每过一秒钟身体增长一个节点]
    private void GrowBody()
    {
        GD.Print("[Signal]  GrowBody ");
        Area2D theBody = thePlayerBody.Instantiate<Area2D>();
        theBody.Position = theBodyList[theBodyList.Count - 1].Position;
        AddChild(theBody);
        theBodyList.Add(theBody);
    }
}

完成

  • 14
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值