Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Help me with java game programming

  1. #1
    Junior Member
    Join Date
    Sep 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help me with java game programming

    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;
    }




    }
    Last edited by MrPortable; September 16th, 2018 at 08:44 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,322
    Thanks
    53
    Thanked 2,393 Times in 2,347 Posts

    Default Re: Help me with java game programming

    head should be in the form of a triangle, while the tail should be in rectangle
    What are the current shapes that the program draws?
    Where does the code draw the head and the tail? I don't see any comments saying where it is being done or what shape they should be.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me with java game programming

    Check the draw method,head and tail are in oval form

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,322
    Thanks
    53
    Thanked 2,393 Times in 2,347 Posts

    Default Re: Help me with java game programming

    Ok, the draw method calls the Graphics class's fillOval method. If you want a different shape, look at the Graphics class's other methods.
    Are there other methods that can be used for the shapes you want?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Where To Start? : Java Game Programming
    By TripleChickenJumpman in forum Java Theory & Questions
    Replies: 5
    Last Post: January 23rd, 2014, 12:17 PM
  2. java game programming problem 1
    By swikot in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 1st, 2014, 10:27 AM
  3. question - java game programming
    By haimdorlevi in forum Java Theory & Questions
    Replies: 1
    Last Post: April 30th, 2013, 03:44 AM
  4. android programming vs game programming using java
    By vgoel38 in forum Android Development
    Replies: 4
    Last Post: September 8th, 2012, 05:48 PM
  5. Java Programming - Dice Game Help!
    By blackvelvet in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 19th, 2012, 11:47 AM

Tags for this Thread