I have an worm game in java language,but i need to modify in worm classes,in draw method to change the form of head and tail of the wrom, in my case the worm head should be in the form of a triangle, while the tail should be in rectanle.
Check the draw method,head and tail are in oval form
This is my code of Worm clasess
package orginal;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.geom.Point2D;
public class Worm {
private static final int MAXPOINTS = 40;
private static final int DOTSIZE = 12;
private static final int RADIUS = DOTSIZE/2;
private static final int NUM_DIRS = 8;
private static final int N = 0;
private static final int NE = 1;
private static final int E = 2;
private static final int SE = 3;
private static final int S = 4;
private static final int SW = 5;
private static final int W = 6;
private static final int NW = 7;
private int curCompass;
private Obstacles obs;
private int nPoints;
private Point[] cells;
private int headPosn, tailPosn;
private int pWidth,pHeight;
private Point2D.Double[] incrs;
private static final int MAXPROBS = 9;;
private int probsForOffset[] = new int[MAXPROBS];
public Worm(int width, int height,Obstacles obs) {
this.pWidth = width;
this.pHeight = height;
this.obs = obs;
cells = new Point[MAXPOINTS];
headPosn = tailPosn = -1;
probsForOffset[0] = 0;
probsForOffset[1] = 0;
probsForOffset[2] = 0;
probsForOffset[3] = 1;
probsForOffset[4] = 1;
probsForOffset[5] = 2;
probsForOffset[6] = -1;
probsForOffset[7] = -1;
probsForOffset[8] = -2;
incrs = new Point2D.Double[NUM_DIRS];
incrs[N] = new Point2D.Double(0,-1);
incrs[NE] = new Point2D.Double(0.7,-0.7);
incrs[E] = new Point2D.Double(1,0);
incrs[SE] = new Point2D.Double(0.7,0.7);
incrs[S] = new Point2D.Double(0,1);
incrs[SW] = new Point2D.Double(-0.7,0.7);
incrs[W] = new Point2D.Double(-1,0);
incrs[NW] = new Point2D.Double(-0.7,-0.7);
}
private int varyBearing(){
int newOfsset = probsForOffset[(int) (Math.random() * MAXPROBS)];
return calcBearing(newOfsset);
}
private int calcBearing(int offset) {
int turn = curCompass + offset;
if (turn >= NUM_DIRS)
turn -= NUM_DIRS;
else if (turn < 0 )
turn += NUM_DIRS;
return turn;
}
private Point nextPoint(int prevPosn, int bearing) {
Point2D.Double incr = incrs[bearing];
int newX = cells[prevPosn].x + (int)(incr.x * DOTSIZE);
int newY = cells[prevPosn].y + (int)(incr.y * DOTSIZE);
if (newX + DOTSIZE < 0)
newX += pWidth;
else if (newX > pWidth)
newX -= pWidth;
if (newY + DOTSIZE < 0)
newY += pHeight;
else if (newY > pHeight)
newY -= pHeight;
return new Point(newX,newY);
}
private void newHead(int prevPosn) {
int newBearing = varyBearing();
Point newPt = nextPoint(prevPosn, newBearing);
int[] fixedOffs = {-2,2,-4};
if (obs.hits(newPt, DOTSIZE))
for (int num :fixedOffs ) {
newBearing = calcBearing(num);
newPt = nextPoint(prevPosn, newBearing);
if (!obs.hits(newPt, DOTSIZE))
break;
}
cells[headPosn] = newPt;
curCompass = newBearing;
}
public void move() {
int prevPosn = headPosn;
headPosn = (headPosn + 1) % MAXPOINTS;
if (nPoints == 0) {
tailPosn = headPosn;
curCompass = (int)(Math.random() * NUM_DIRS);
cells[headPosn] = new Point(pWidth/2,pHeight/2);
nPoints++;
}
else if (nPoints == MAXPOINTS) {
tailPosn = (tailPosn + 1) % MAXPOINTS;
newHead(prevPosn);
}
else {
newHead(prevPosn);
nPoints++;
}
}
public void draw(Graphics g) {
if (nPoints > 0) {
g.setColor(Color.black);
int i = tailPosn;
while (i != headPosn) {
g.fillOval(cells[i].x, cells[i].y, DOTSIZE, DOTSIZE);
i = (i + 1) % MAXPOINTS;
}
g.setColor(Color.red);
g.fillOval(cells[headPosn].x, cells[headPosn].y, DOTSIZE, DOTSIZE);
}
}
public boolean nearHead(int x, int y) {
if ( Math.abs(cells[headPosn].x + RADIUS - x ) <= DOTSIZE
&& Math.abs(cells[headPosn].y + RADIUS - y ) <= DOTSIZE )
return true;
return false;
}
public boolean touchedAt(int x, int y) {
int i = tailPosn;
while (i != headPosn) {
if ( Math.abs(cells[i].x + RADIUS - x ) <= DOTSIZE
&& Math.abs(cells[i].y + RADIUS - y ) <= DOTSIZE )
return true;
i = (i + 1) % MAXPOINTS;
}
return false;
}
}