private void restart() { this.gameStatus = Snake.STATUS_RUNNING; this.foodNumber = 0; for (int j = 0; j < listPieces.Count; j++) { Destroy ((GameObject)listPieces [j]); } this.listPieces.Clear (); SNFood.getInstance ().reset (); SNCell cell = null; for (int i = 0; i < rows; i++) { for (int j = 0; j < colums; j++) { cell = new SNCell (); cell.reset(); } } direction = Vector3.down; initializeHead (); addInitialPieces (); }
public void generate() { if (this.enabled) return; for (int j = 0; j < 5; j++) { int r = (int)Mathf.Floor (Random.Range (1, snake.rows - 1)); int c = (int)Mathf.Floor (Random.Range (1, snake.colums - 1)); SNCell cell = snake.getCell (r, c); if (cell.runningPiece == null) { cell.hasFood = true; this.myCell = cell; this.transform.position = cell.getCenter (); this.enabled = true; return; } } }
private void setupGame() { //get width and heigh of screen in Vector3 p1 = Camera.main.ViewportToWorldPoint(new Vector3(0,0,6)); Vector3 p2 = Camera.main.ViewportToWorldPoint(new Vector3(1,0,6)); snakeScreenWidth = Vector3.Distance(p1, p2); p2 = Camera.main.ViewportToWorldPoint(new Vector3(0,1,6)); snakeScreenHeight = Vector3.Distance(p1, p2) ; colums = (int)Mathf.Floor( (snakeScreenWidth-1.0f) / cellWidth); rows= (int)Mathf.Floor((snakeScreenHeight-1.0f) / cellHeight); gameWidth = colums * cellWidth; gameHeight= rows * cellHeight; startX = snakeScreenWidth/2-(snakeScreenWidth - gameWidth) / 2; startY = snakeScreenHeight/2-(snakeScreenHeight - gameHeight) / 2; startX *= -1; startY *= -1; Debug.Log ("snakeScreenWidth:"+snakeScreenWidth); Debug.Log ("snakeScreenHeight:"+snakeScreenHeight); Debug.Log ("gameWidth:"+gameWidth); Debug.Log ("gameHeight:"+gameHeight); Debug.Log ("startXstartX:"+startX); Debug.Log ("startXstartyy:"+startY); arrCells = new SNCell[rows,colums]; SNCell cell = null; for (int i = 0; i < rows; i++) { for (int j = 0; j < colums; j++) { cell = new SNCell (); cell.x = startX + j * cellWidth; cell.y = startY + i * cellHeight; cell.width = cellWidth; cell.height = cellHeight; cell.row = i; cell.column = j; arrCells [i,j] = cell; } } drawGrid (); addInitialPieces (); }
public void reset() { this.enabled = false; this.myCell = null; }
public void eaten() { this.enabled = false; this.myCell.hasFood = false; this.myCell = null; }