예제 #1
0
 private void CopySnakeBody()
 {
     snakeBodyTemp.Clear();
     foreach (var item in snakeBody)
     {
         SnakeCell snakeCells = new SnakeCell();
         snakeCells.Coord      = item.Coord;
         snakeCells.SpriteName = item.SpriteName;
         snakeBodyTemp.Add(snakeCells);
     }
 }
예제 #2
0
    public void AddSnakeBody(Vector2 coord, string spriteName = "圆形")
    {
        //if (CreateFoodEvent != null)
        //{
        //    CreateFoodEvent(gameController);
        //}

        SnakeCell snakeCell = new SnakeCell();

        snakeCell.Coord      = coord;
        snakeCell.SpriteName = spriteName;
        snakeBody.Add(snakeCell);
    }
예제 #3
0
    private void ResetCells()
    {
        lCells.Clear();
        lSnakeCells.Clear();

        int r = Screen.height / Cell.isize - 2;
        int c = Screen.width / Cell.isize - 2;

        for (int i = 0; i < r; i++)
        {
            lCells.Add(new List <Cell>());
            for (int j = 0; j < c; j++)
            {
                if (i == 0 || j == 0 || i == r - 1 || j == c - 1)
                {
                    WallCell wc = new WallCell();
                    wc.ix = j;
                    wc.iy = i;
                    lCells[i].Add(wc);
                }
                else
                {
                    EmptyCell wc = new EmptyCell();
                    wc.ix = j;
                    wc.iy = i;
                    lCells[i].Add(wc);
                }
            }
        }

        for (int i = 0; i < 5; i++)
        {
            int       ix = 5 + i;
            int       iy = 5;
            SnakeCell sc = new SnakeCell();
            sc.ix = ix;
            sc.iy = iy;
            lSnakeCells.Insert(0, sc);
            lCells[iy][ix] = sc;
        }

        RandomOneFood();

        bGameOver = false;

        iDirectX = 1;
        iDirectY = 0;
        moveSep  = 1;
    }
예제 #4
0
    void Start()
    {
        cells = new List <SnakeCell>();

        for (int x = -15; x <= 15; x++)
        {
            for (int y = -10; y <= 10; y++)
            {
                cell = new SnakeCell(x, y);
                cells.Add(cell);
            }
        }

        Debug.Log(cells.Count);
        isReady = true;
    }
예제 #5
0
        private void move()
        {
            GridCell prevHeadPos = snakeCells.First.Value.pos;
            GridCell nextHeadPos = calculateNextGridCell(prevHeadPos, nextDirection);

            prevDirection = nextDirection;
            SnakeCell last = snakeCells.Last.Value;

            snakeCells.RemoveLast();
            snakeCells.AddFirst(last);
            last.pos = nextHeadPos;
            playerController.gridController.placeInCell(last.gameObject, nextHeadPos);

            if (playerController.playerMovementEvent != null)
            {
                playerController.playerMovementEvent(nextHeadPos);
            }
        }
예제 #6
0
    void getsnake()
    {
        snake             = new List <SnakeCell> ();
        snakehead         = new SnakeCell();
        snakehead.targetx = snakestartposx;
        snakehead.targetz = snakestartposz;
        snakeheadolddir   = snakehead.dir = snakestartdir;
        snakehead.getposfromdir();
        if (snakehead.prevx >= 0 && snakehead.prevz >= 0 && snakehead.prevx < width && snakehead.prevz < height)
        {
            board [snakehead.prevx, snakehead.prevz] |= (1 << 7);
        }
        snakehead.gobj          = Instantiate(Resources.Load("SnakeHead") as GameObject);
        snakehead.tran          = snakehead.gobj.transform;
        snakehead.tran.position = new Vector3(snakehead.posx + leftbound, snakey, snakehead.posz + downbound);
        snakehead.tran.rotation = dir_rotation [snakehead.dir];
        snake.Add(snakehead);
        int i;

        for (i = 0; i < 4; i++)
        {
            snakebody         = new SnakeCell();
            snaketail         = snake [snake.Count - 1];
            snakebody.targetx = snaketail.prevx;
            snakebody.targetz = snaketail.prevz;
            snakebody.dir     = snaketail.dir;
            snakebody.getposfromdir();
            if (snakebody.prevx >= 0 && snakebody.prevz >= 0 && snakebody.prevx < width && snakebody.prevz < height)
            {
                board [snakebody.prevx, snakebody.prevz] |= (1 << 7);
            }
            snakebody.gobj          = Instantiate(Resources.Load("SnakeBody") as GameObject);
            snakebody.tran          = snakebody.gobj.transform;
            snakebody.gobj.name     = "snakebody_" + snake.Count.ToString();
            snakebody.tran.position = new Vector3(snakebody.posx + leftbound, snakey, snakebody.posz + downbound);
            snakebody.tran.rotation = dir_rotation [snakebody.dir];
            snake.Add(snakebody);
        }
    }
예제 #7
0
    public void InitData()
    {
        snakeBody.Clear();

        SnakeCell snakeCell = new SnakeCell();

        snakeCell.Coord      = new Vector2(3, 7);
        snakeCell.SpriteName = "正方形";
        snakeBody.Add(snakeCell);

        SnakeCell snakeCell2 = new SnakeCell();

        snakeCell2.Coord      = new Vector2(3, 6);
        snakeCell2.SpriteName = "圆形";
        snakeBody.Add(snakeCell2);

        SnakeCell snakeCell3 = new SnakeCell();

        snakeCell3.Coord      = new Vector2(3, 5);
        snakeCell3.SpriteName = "圆形";
        snakeBody.Add(snakeCell3);

        chessBoard.SetSpriteImage(snakeBody);
    }
예제 #8
0
    void movesnake()
    {
        int i, k;

        snakehead = snake [0];
        float cur_movelength = Time.deltaTime * movelength;
        float reach_rate     = snakehead.move(cur_movelength);

        if (reach_rate > 0.5)
        {
            if (reach_rate > 0.8)
            {
                if (eraseblue)
                {
                    for (i = 0; i < bluecnt; i++)
                    {
                        Destroy(boardgobj [bluelist [prevblueindex] [i].x, bluelist [prevblueindex] [i].z]);
                        boardgobj [bluelist [prevblueindex] [i].x, bluelist [prevblueindex] [i].z] = null;
                        if ((board [bluelist [prevblueindex] [i].x, bluelist [prevblueindex] [i].z] & (1 << 5)) != 0)
                        {
                            board [bluelist [prevblueindex] [i].x, bluelist [prevblueindex] [i].z] -= (1 << 5);
                        }
                        if ((board [bluelist [prevblueindex] [i].x, bluelist [prevblueindex] [i].z] & (1 << 10)) != 0)
                        {
                            board [bluelist [prevblueindex] [i].x, bluelist [prevblueindex] [i].z] -= (1 << 10);
                        }
                        if (bluecnt > 1)
                        {
                            int index = (bluelist [prevblueindex] [i].x * height + bluelist [prevblueindex] [i].z) * 4;
                            for (k = 0; k < 4; k++)
                            {
                                uvs [index + k] = srcpos [board [bluelist [prevblueindex] [i].x, bluelist [prevblueindex] [i].z] & 7, k];
                            }
                        }
                        belongbluelist [bluelist [prevblueindex] [i].x, bluelist [prevblueindex] [i].z] = -1;
                    }
                    if (bluecnt > 1)
                    {
                        mh.uv = uvs;
                    }
                    bluelist[prevblueindex].Clear();
                    eatenblue [prevblueindex] = true;
                    prevblueindex             = -1;
                    if (snake.Count < 20)
                    {
                        newsnaketail               = new SnakeCell();
                        oldsnaketail               = snake [snake.Count - 1];
                        newsnaketail.targetx       = newsnaketail.prevx = oldsnaketail.prevx;
                        newsnaketail.targetz       = newsnaketail.prevz = oldsnaketail.prevz;
                        newsnaketail.dir           = oldsnaketail.dir;
                        newsnaketail.posx          = oldsnaketail.posx;
                        newsnaketail.posz          = oldsnaketail.posz;
                        newsnaketail.gobj          = Instantiate(Resources.Load("SnakeBody") as GameObject);
                        newsnaketail.tran          = newsnaketail.gobj.transform;
                        newsnaketail.gobj.name     = "snakebody_" + snake.Count.ToString();
                        newsnaketail.tran.position = new Vector3(newsnaketail.posx + leftbound, snakey, newsnaketail.posz + downbound);
                        newsnaketail.tran.rotation = dir_rotation [newsnaketail.dir];
                        snake.Add(newsnaketail);
                    }
                    createblue();
                    eraseblue = false;
                    if (bluecnt > 1)
                    {
                        UIClick.score += bluecnt * factor * 10;
                        if (factor < 99)
                        {
                            factor++;
                        }
                    }
                    else
                    {
                        UIClick.score += 500;
                    }
                    updateui();
                    eatennum++;
                    updatebar();
                    if (eatennum == needtoeat)
                    {
                        win = true;
                    }
                    bluecnt = 0;
                }
                if (eraseother)
                {
                    Destroy(boardgobj [snakehead.targetx, snakehead.targetz]);
                    boardgobj [snakehead.targetx, snakehead.targetz] = null;
                    eraseother = false;
                }
            }
            if (!judged)
            {
                if (snakehead.targetx >= 0 && snakehead.targetz >= 0 && snakehead.targetx < width && snakehead.targetz < height)
                {
                    if ((board [snakehead.targetx, snakehead.targetz] & (1 << 7)) != 0)
                    {
                        // bite itself
                        Sunlight.light_strength = 3f;
                        gameover = true;
                        return;
                    }
                    if ((board [snakehead.targetx, snakehead.targetz] & (1 << 4)) != 0)
                    {
                        // hit big obstacle
                        Sunlight.light_strength = 1.3f;
                        int cutlen = snake.Count >> 1;
                        for (i = 0; i < cutlen; i++)
                        {
                            snaketail = snake [snake.Count - 1];
                            if ((board [snaketail.prevx, snaketail.prevz] & (1 << 7)) != 0)
                            {
                                board [snaketail.prevx, snaketail.prevz] -= (1 << 7);
                            }
                            Destroy(snaketail.gobj);
                            snake.Remove(snaketail);
                            if (snake.Count < 3)
                            {
                                Sunlight.light_strength = 3f;
                                gameover = true;
                                return;
                            }
                        }
                        snakehead.dir = (snakehead.dir + 3) & 3;
                        snakehead.gettarget();
                        if ((board [snakehead.targetx, snakehead.targetz] & ((1 << 7) | (1 << 4))) == 0)
                        {
                            snakehead.posx = snakehead.prevx * 0.5f + snakehead.targetx * 0.5f;
                            snakehead.posz = snakehead.prevz * 0.5f + snakehead.targetz * 0.5f;
                            snakenextdir   = 0;
                            lockleft       = lockright = false;
                        }
                        else
                        {
                            snakehead.dir = (snakehead.dir + 2) & 3;
                            snakehead.gettarget();
                            if ((board [snakehead.targetx, snakehead.targetz] & ((1 << 7) | (1 << 4))) == 0)
                            {
                                snakehead.posx = snakehead.prevx * 0.5f + snakehead.targetx * 0.5f;
                                snakehead.posz = snakehead.prevz * 0.5f + snakehead.targetz * 0.5f;
                                snakenextdir   = 0;
                                lockleft       = lockright = false;
                            }
                            else
                            {
                                snakehead.dir = (snakehead.dir + 1) & 3;
                                snakehead.gettarget();
                                if ((board [snakehead.targetx, snakehead.targetz] & ((1 << 7) | (1 << 4))) == 0)
                                {
                                    snakehead.posx = snakehead.prevx * 0.5f + snakehead.targetx * 0.5f;
                                    snakehead.posz = snakehead.prevz * 0.5f + snakehead.targetz * 0.5f;
                                    snakenextdir   = 0;
                                    lockleft       = lockright = false;
                                }
                                else
                                {
                                    snakehead.dir = (snakehead.dir + 2) & 3;
                                    snakehead.gettarget();
                                    Sunlight.light_strength = 3f;
                                    gameover = true;
                                    return;
                                }
                            }
                        }
                    }
                    if ((board [snakehead.targetx, snakehead.targetz] & (1 << 3)) != 0)
                    {
                        // hit small obstacle
                        Sunlight.light_strength = 0.9f;
                        int cutlen = 2;
                        for (i = 0; i < cutlen; i++)
                        {
                            snaketail = snake [snake.Count - 1];
                            if ((board [snaketail.prevx, snaketail.prevz] & (1 << 7)) != 0)
                            {
                                board [snaketail.prevx, snaketail.prevz] -= (1 << 7);
                            }
                            Destroy(snaketail.gobj);
                            snake.Remove(snaketail);
                            if (snake.Count < 3)
                            {
                                Sunlight.light_strength = 3f;
                                gameover = true;
                                return;
                            }
                        }
                        boardgobj[snakehead.targetx, snakehead.targetz].transform.position = new Vector3(snakehead.targetx + leftbound + 0.08f, -10f, snakehead.targetz + downbound - 0.08f);
                        board [snakehead.targetx, snakehead.targetz] -= (1 << 3);
                    }
                    if ((board [snakehead.targetx, snakehead.targetz] & (1 << 5)) != 0 && (board [snakehead.targetx, snakehead.targetz] & (1 << 10)) == 0)
                    {
                        if (belongbluelist [snakehead.targetx, snakehead.targetz] != prevblueindex)
                        {
                            bluecnt = 0;
                            if (prevblueindex != -1)
                            {
                                resetblue(prevblueindex);
                            }
                            prevblueindex = belongbluelist [snakehead.targetx, snakehead.targetz];
                        }
                        bluecnt++;
                        boardgobj [snakehead.targetx, snakehead.targetz].transform.position = new Vector3(boardgobj [snakehead.targetx, snakehead.targetz].transform.position.x, -10, boardgobj [snakehead.targetx, snakehead.targetz].transform.position.z);
                        board [snakehead.targetx, snakehead.targetz] |= (1 << 10);
                        if (bluecnt == bluelist [prevblueindex].Count)
                        {
                            eraseblue = true;
                        }
                        if (bluelist [prevblueindex].Count > 1)
                        {
                            UIClick.score += factor * 10;
                        }
                    }
                    else
                    {
                        if ((board [snakehead.targetx, snakehead.targetz] & (1 << 11)) != 0)
                        {
                            UIClick.score += 2000;
                            eraseother     = true;
                        }
                        else if ((board [snakehead.targetx, snakehead.targetz] & (1 << 12)) != 0)
                        {
                            UIClick.life++;
                            UIClick.score        += 100;
                            stageandlifetext.text = "Stage: " + UIClick.level.ToString() + "\nLife: " + UIClick.life.ToString();
                            eraseother            = true;
                        }
                        bluecnt = 0;
                        if (prevblueindex != -1)
                        {
                            resetblue(prevblueindex);
                        }
                        prevblueindex = -1;
                    }
                    updateui();
                }
                judged = true;
            }
            // Enter the next block, judge whether the head hits something
            if (reach_rate >= 0.99f)
            {
                UIClick.score++;
                stepcnt++;
                int curclockid = stepcnt * 8 / maxstep;
                if (curclockid > 7)
                {
                    curclockid = 7;
                }
                if (curclockid == 7)
                {
                    Sunlight.light_strength = 0.7f;
                }
                int prevclockid = (stepcnt - 1) * 8 / maxstep;
                if (curclockid > prevclockid)
                {
                    clockimg.sprite = sprt [curclockid];
                }
                if (stepcnt >= maxstep)
                {
                    Sunlight.light_strength = 3f;
                    gameover = true;
                    return;
                }
                if (snakehead.prevx >= 0 && snakehead.prevz >= 0 && snakehead.prevx < width && snakehead.prevz < height)
                {
                    if ((board [snakehead.prevx, snakehead.prevz] & (1 << 7)) != 0)
                    {
                        board [snakehead.prevx, snakehead.prevz] -= (1 << 7);
                    }
                }
                snakehead.posx = snakehead.prevx = snakehead.targetx;
                snakehead.posz = snakehead.prevz = snakehead.targetz;
                if (snakehead.prevx >= 0 && snakehead.prevz >= 0 && snakehead.prevx < width && snakehead.prevz < height)
                {
                    board [snakehead.prevx, snakehead.prevz] |= (1 << 7);
                }
                snakehead.tran.position = new Vector3(snakehead.posx + leftbound, snakey, snakehead.posz + downbound);
                for (i = 1; i < snake.Count; i++)
                {
                    snakebodyprev = snake [i - 1];
                    snakebody     = snake [i];
                    if (snakebody.prevx >= 0 && snakebody.prevz >= 0 && snakebody.prevx < width && snakebody.prevz < height)
                    {
                        if ((board [snakebody.prevx, snakebody.prevz] & (1 << 7)) != 0)
                        {
                            board [snakebody.prevx, snakebody.prevz] -= (1 << 7);
                        }
                    }
                    snakebody.posx = snakebody.prevx = snakebody.targetx;
                    snakebody.posz = snakebody.prevz = snakebody.targetz;
                    if (snakebody.prevx >= 0 && snakebody.prevz >= 0 && snakebody.prevx < width && snakebody.prevz < height)
                    {
                        board [snakebody.prevx, snakebody.prevz] |= (1 << 7);
                    }
                    snakebody.targetx = snakebodyprev.prevx;
                    snakebody.targetz = snakebodyprev.prevz;
                    if (snakebody.targetx == snakebody.prevx + 1)
                    {
                        snakebody.dir = 0;
                    }
                    else if (snakebody.targetz == snakebody.prevz + 1)
                    {
                        snakebody.dir = 1;
                    }
                    else if (snakebody.targetx == snakebody.prevx - 1)
                    {
                        snakebody.dir = 2;
                    }
                    else
                    {
                        snakebody.dir = 3;
                    }
                    snakebody.tran.position = new Vector3(snakebody.posx + leftbound, snakey, snakebody.posz + downbound);
                    snakebody.tran.rotation = dir_rotation [snakebody.dir];
                }
                snakeheadolddir = snakehead.dir;
                if (snakenextdir != 0)
                {
                    snakehead.dir += snakenextdir;
                    if (snakehead.dir == 4)
                    {
                        snakehead.dir = 0;
                    }
                    else if (snakehead.dir == -1)
                    {
                        snakehead.dir = 3;
                    }
                    snakenextdir = 0;
                }
                if (snakehead.dir == 0)
                {
                    snakehead.targetx++;
                }
                else if (snakehead.dir == 1)
                {
                    snakehead.targetz++;
                }
                else if (snakehead.dir == 2)
                {
                    snakehead.targetx--;
                }
                else
                {
                    snakehead.targetz--;
                }
                judged = false;
                updateui();
                return;
            }
        }
        snakehead.tran.position = new Vector3(snakehead.posx + leftbound, snakey, snakehead.posz + downbound);
        for (i = 1; i < snake.Count; i++)
        {
            snakebody = snake [i];
            snakebody.move(cur_movelength);
            snakebody.tran.position = new Vector3(snakebody.posx + leftbound, snakey, snakebody.posz + downbound);
        }
        if (snakehead.dir != snakeheadolddir)
        {
            if (snakehead.dir == snakeheadolddir + 1 || snakehead.dir == snakeheadolddir - 3)
            {
                snakehead.tran.rotation = Quaternion.Euler(0f, -90 * snakeheadolddir + 180 - 90 * Mathf.Min(1, reach_rate * 1.0f), 90f);
            }
            else
            {
                snakehead.tran.rotation = Quaternion.Euler(0f, -90 * snakeheadolddir + 180 + 90 * Mathf.Min(1, reach_rate * 1.0f), 90f);
            }
        }
        else
        {
            snakehead.tran.rotation = Quaternion.Euler(0f, -90 * snakeheadolddir + 180, 90f);
        }
    }
예제 #9
0
 public SnakeCell(int x, int y, SnakeCell nextCell)
     : this(nextCell)
 {
     X = x;
     Y = y;
 }
예제 #10
0
 public SnakeCell(SnakeCell nextCell)
 {
     _nextCell = nextCell;
 }
예제 #11
0
 public SnakeCell()
 {
     _nextCell = null;
 }
예제 #12
0
    void Update()
    {
        if (bGameOver)
        {
            return;
        }
        if (!bStarting)
        {
            return;
        }

        if (Input.GetKeyDown(KeyCode.UpArrow) && iDirectY == 0 && !bDirectionLock)
        {
            Debug.Log("up");
            iDirectX       = 0;
            iDirectY       = -1;
            bDirectionLock = true;
        }
        if (Input.GetKeyDown(KeyCode.DownArrow) && iDirectY == 0 && !bDirectionLock)
        {
            Debug.Log("down");
            iDirectX       = 0;
            iDirectY       = 1;
            bDirectionLock = true;
        }
        if (Input.GetKeyDown(KeyCode.LeftArrow) && iDirectX == 0 && !bDirectionLock)
        {
            Debug.Log("left");
            iDirectX       = -1;
            iDirectY       = 0;
            bDirectionLock = true;
        }
        if (Input.GetKeyDown(KeyCode.RightArrow) && iDirectX == 0 && !bDirectionLock)
        {
            Debug.Log("right");
            iDirectX       = 1;
            iDirectY       = 0;
            bDirectionLock = true;
        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (bGameOver)
            {
                bGameOver = false;
                ResetCells();
            }
            else if (!bStarting)
            {
                bStarting = true;
                ResetCells();
            }
            else
            {
                bPause = !bPause;
            }
        }

        if (bPause)
        {
            return;
        }

        deltaSec += Time.deltaTime;
        if (deltaSec < moveSep)
        {
            return;
        }
        deltaSec -= moveSep;

        Vector2 lastPos = Vector2.zero;

        foreach (SnakeCell sc in lSnakeCells)
        {
            if (lastPos.x == 0 || lastPos.y == 0)
            {
                lastPos.y = sc.iy + iDirectY;
                lastPos.x = sc.ix + iDirectX;
            }
            if (lCells[(int)lastPos.y][(int)lastPos.x].IsBlock)
            {
                bGameOver = true;
                break;
            }
            if (lCells[(int)lastPos.y][(int)lastPos.x] is FoodCell)
            {
                SnakeCell c = new SnakeCell();
                c.ix = (int)lastPos.x;
                c.iy = (int)lastPos.y;
                lSnakeCells.Insert(0, c);
                lCells[(int)lastPos.y][(int)lastPos.x] = c;

                RandomOneFood();

                moveSep -= moveSep * 0.07f;
                break;
            }

            Vector2 pos;
            pos.x = sc.ix;
            pos.y = sc.iy;
            lCells[sc.iy][sc.ix] = new EmptyCell();
            sc.ix = (int)lastPos.x;
            sc.iy = (int)lastPos.y;
            lCells[sc.iy][sc.ix] = sc;
            lastPos = pos;
        }
        bDirectionLock = false;
    }