Apple Spawning Inside Snake

问题描述 投票:0回答:2

我对团结非常陌生,我做了一场蛇游戏。但是,苹果有时会在蛇内产卵。我假设这需要if语句,例如if CreateRandomApple = TailNode,然后重试,但是我不确定如何编写该语句。这是我的蛇和苹果代码。

    void PlacePlayer()
    {
        playerObj = new GameObject("Player");
        SpriteRenderer playerRender = playerObj.AddComponent<SpriteRenderer>();
        playerSprite = CreateSprite(playerColor);
        playerRender.sprite = (playerSprite);
        playerRender.sortingOrder = 1;
        playerNode = GetNode(3, 3);

        PlacePlayerObject(playerObj, playerNode.worldPosition);
        playerObj.transform.localScale = Vector3.one * 1.2f;

        tailParent = new GameObject("tailParent");
    }


    void CreateApple()
    {
        appleObj = new GameObject("Apple");
        SpriteRenderer appleRenderer = appleObj.AddComponent<SpriteRenderer>();
        appleRenderer.sprite = CreateSprite(appleColor);
        appleRenderer.sortingOrder = 1;
        RandomlyPlacedApple();
    }

    #endregion

    #region Update

    void MoveTail()
    {
        Node prevNode = null;

        for (int i = 0; i < tail.Count; i++)
        {
            SpecialNode p = tail[i];
            availbleNodes.Add(p.node);

            if (i == 0)
            {
                prevNode = p.node;
                p.node = playerNode;
            }
            else
            {
                Node prev = p.node;
                p.node = prevNode;
                prevNode = prev;
            }

            availbleNodes.Remove(p.node);
            PlacePlayerObject(p.obj, p.node.worldPosition);
        }
    }
    #endregion

    #region Utilities


    bool isTailNode(Node n)
    {
        for (int i = 0; i < tail.Count; i++)
        {
            if(tail[i].node == n)
            {
                return true;
            }
        }
        return false;
    }

    void PlacePlayerObject(GameObject obj, Vector3 pos)
    {
        pos += Vector3.one * .5f;
        obj.transform.position = pos;
    }

    void RandomlyPlacedApple()
    {
        int ran = Random.Range(0, availbleNodes.Count);
        Node n = availbleNodes[ran];
        PlacePlayerObject(appleObj, n.worldPosition);
        appleNode = n;
    }

    Node GetNode(int x, int y)
    {
        if (x < 0 || x > MaxWidth - 1 || y < 0 || y > MaxHeight - 1)
            return null;
        return grid[x, y];
    }

    SpecialNode CreateTailNode(int x, int y)
    {
        SpecialNode s = new SpecialNode();
        s.node = GetNode(x, y);
        s.obj = new GameObject();
        s.obj.transform.parent = tailParent.transform;
        s.obj.transform.position = s.node.worldPosition;
        s.obj.transform.localScale = Vector3.one * .95f;
        SpriteRenderer r = s.obj.AddComponent<SpriteRenderer>();
        r.sprite = (playerSprite);
        r.sortingOrder = 1;

        return s;
    }


    #endregion

}`

这是脚本的简化版本。谢谢

c# visual-studio unity3d
2个回答
0
投票

由于具有isTailNode函数,您应该能够连续生成新的Apple节点并检查它们是否为尾节点。如果它们是您生成新的。这可以使用while循环来实现。

void RandomlyPlacedApple()
{
    int ran = Random.Range(0, availbleNodes.Count);
    while(isTailNode(availbleNodes[ran]))
    {
        ran = Random.Range(0, availbleNodes.Count);
    }
    PlacePlayerObject(appleObj, availbleNodes[ran].worldPosition);
    appleNode = availbleNodes[ran];
}

0
投票

检查是否在ez内

if(apple.location == snake.location){ changeAppleLocation() }

© www.soinside.com 2019 - 2024. All rights reserved.