void addLength(int size) // add a length to the snake in pixels { BodySegment trailing = bodySegments[bodySegments.Count - 1]; // retrieve the last body segment since we're building off the tail of the snake // get the buildup angle double buildAngle = trailing.velocity.Degrees * Math.PI / 180 + Math.PI; // flip it around // get the change in each dimension int dx = (int)(size * Math.Cos(buildAngle)); int dy = (int)(size * Math.Sin(buildAngle)); // add the new dimensions trailing.grow(dx, dy); }
// temporary constructor public ContinuousSnake(int x, int y, CoreForm _mainForm, Vector _velocity, double size, bool red = false) { // assign all variables this.Size = (int)size; this.mainForm = _mainForm; this.red = red; // find angle to build the body in rads double angle = _velocity.Degrees * Math.PI / 180 + Math.PI; // add PI radians to flip the angle around // since the build must occur in opposite direction of velocity int[] coords = new int[] { x, y }; // coords in proper form to be used BodySegment segment = new BodySegment(0, 0, coords, _velocity.Clone(), mainForm); // set size to zero for now int dx = (int)(BodyPart.SIZE * size * Math.Cos(angle)); // get change in each direction for the new size int dy = (int)(BodyPart.SIZE * size * Math.Sin(angle)); // add negative because coords are reversed on screen // whichever is zero will be default width if (dx == 0) { dx = BodyPart.SIZE; } else { dy = BodyPart.SIZE; } segment.grow(dx, dy); if (_velocity.Degrees == 180 || _velocity.Degrees == 90) // make the snake head at proper coords { snakeHead = new BodyPart(segment.X, segment.Y, _velocity.Clone(), mainForm); } else if (_velocity.Degrees == 0) { snakeHead = new BodyPart((int)(segment.X + BodyPart.SIZE * size - BodyPart.SIZE), segment.Y, _velocity.Clone(), mainForm); } else if (_velocity.Degrees == 0) { snakeHead = new BodyPart(segment.X, (int)(segment.Y + BodyPart.SIZE * size - BodyPart.SIZE), _velocity.Clone(), mainForm); } FixHead(); // fix head orientation based on velocity // add the segment to the bodySegments list to keep track of it bodySegments.Add(segment); // set the timer event for preventing too fast player responses interval.Elapsed += new ElapsedEventHandler(intervalFunc); // do the red colour thing if (red) { // make the segment red if the red flag is set bodySegments[0].picBox.BackColor = Color.Red; bodySegments[0].picBox.Image = null; } }