/// <summary> /// Method that moves snake's tail by deleting the last bone /// </summary> /// <returns>Coordinates of the last bone that was deleted</returns> public Coordinates moveTail() { int z = tail.getBone().getDirection(); // direction last bone follow Coordinates last = tail.getBone().getCoord(); setTail(tail.getPrev()); tail.setNext(null); int d = tail.getBone().getDirection(); // direction snake moves if (d != z) {//if directioin of ex-tail and new tail are different, direction of new tail should be changed, as it used to hold corner if (d == 9) { tail.getBone().setDirection(z == 6 ? 2 : 4); } else if (d == 7) { tail.getBone().setDirection(z == 8 ? 6 : 2); } else if (d == 3) { tail.getBone().setDirection(z == 2 ? 4 : 8); } else if (d == 1) { tail.getBone().setDirection(z == 4 ? 8 : 6); } } length--; return(last); }
/// <summary> /// Method that moves snake's head by adding new Join to it. Snake is following received direction /// </summary> /// <param name="direction">direction in whitch snake is moving</param> /// <returns></returns> public Coordinates moveHead(int direction) { //creates next bone and connects it before snake's head. That imitates that head was moved Bone newBone = new Bone(direction, head.getBone().getCoord().getX(), head.getBone().getCoord().getY()); Join j = new Join(newBone, null, head); head.setPrev(j); //direction of ex-head should be changed. 8-up, 2-down, 4-left, 6-right. 1,7,3,9 - corners. //use numeric keypad to visualise direction better int z = head.getBone().getDirection(); if (direction != z) {//if direction of nex head and ex-head are different, then change diretion of ex-head to hold corner if (direction == 8) { head.getBone().setDirection(z == 4 ? 1 : 3); j.getBone().getCoord().setY(newBone.getCoord().getY() - 1); } else if (direction == 6) { head.getBone().setDirection(z == 8 ? 7 : 1); j.getBone().getCoord().setX(newBone.getCoord().getX() + 1); } else if (direction == 2) { head.getBone().setDirection(z == 4 ? 7 : 9); j.getBone().getCoord().setY(newBone.getCoord().getY() + 1); } else if (direction == 4) { head.getBone().setDirection(z == 8 ? 9 : 3); j.getBone().getCoord().setX(newBone.getCoord().getX() - 1); } } else { switch (direction) {// correction of coordinates of a new head case 2: j.getBone().getCoord().setY(newBone.getCoord().getY() + 1); break; case 4: j.getBone().getCoord().setX(newBone.getCoord().getX() - 1); break; case 6: j.getBone().getCoord().setX(newBone.getCoord().getX() + 1); break; case 8: j.getBone().getCoord().setY(newBone.getCoord().getY() - 1); break; } } setHead(j); length++; return(head.getBone().getCoord()); }
/// <summary> /// Fill the form with objects that will hold pictures during the game proccess /// </summary> private void panel1_fill() { int bsize = 30; for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { picb[i, j] = new PictureBox(); picb[i, j].Size = new Size(bsize, bsize); picb[i, j].Location = new System.Drawing.Point(bsize * i, bsize * j); picb[i, j].Image = null; picb[i, j].SizeMode = PictureBoxSizeMode.StretchImage; picb[i, j].Margin = new System.Windows.Forms.Padding(0, 0, 0, 0); picb[i, j].MouseEnter += new System.EventHandler(enterSpace); picb[i, j].MouseLeave += new System.EventHandler(leaveSpace); picb[i, j].Click += new System.EventHandler(putRock); panel1.Controls.Add(picb[i, j]); } } // put pictures of the first snake on a "gameboard" Join current = snake.getHead(); setHeadLabel(current.getBone(), images); while (current.hasNext()) { current = current.getNext(); if (current.hasNext()) { setBoneLabel(current.getBone(), images); } else { setTailLabel(current.getBone(), images); } } if (iftwo) {// put pictures of the second snake on a "gameboard" label3.Text = "Yellow 0:0 Red"; current = snake2.getHead(); setHeadLabel(current.getBone(), images2); while (current.hasNext()) { current = current.getNext(); if (current.hasNext()) { setBoneLabel(current.getBone(), images2); } else { setTailLabel(current.getBone(), images2); } } } }