Question

I HAVE THE FOLLOWING CODE IN JAVA FOR A SNAKE GAME, I WANT SOMEONE TO EXPLAIN...

I HAVE THE FOLLOWING CODE IN JAVA FOR A SNAKE GAME, I WANT SOMEONE TO EXPLAIN EACH PART OF THE CODE, WHAT'S THE FUNCTION OF EACH PART

THANK YOU

import java.awt.Color;

import java.awt.Graphics;

public class Apple {

private int xCoor, yCoor, width, height;

public Apple(int xCoor, int yCoor, int tileSize) {

this.xCoor = xCoor;

this.yCoor = yCoor;

width = tileSize;

height = tileSize;

}

public void tick() {

}

public void draw(Graphics g) {

g.setColor(Color.RED);

g.fillRect(xCoor * width , yCoor * height, width, height);

}

public int getxCoor() {

return xCoor;

}

public void setxCoor(int xCoor) {

this.xCoor = xCoor;

}

public int getyCoor() {

return yCoor;

}

public void setyCoor(int yCoor) {

this.yCoor = yCoor;

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find commented code below::

Apple.java

package graphichal;

import java.awt.Color;
import java.awt.Graphics;

//creating class Apple
public class Apple {

   //added instance variable for x,y coordinates and width/height of apple
   private int xCoor, yCoor, width, height;

   //constructor for apple class
   public Apple(int xCoor, int yCoor, int tileSize) {

       //setting members by parameters
       this.xCoor = xCoor;

       this.yCoor = yCoor;

       width = tileSize;

       height = tileSize;

   }

   public void tick() {

   }

   //this function will be used to draw apple using parameter graphics
   public void draw(Graphics g) {

       //set color of apple to red
       g.setColor(Color.RED);

       //draw filled rectangle at position x,y with width and height
       g.fillRect(xCoor * width , yCoor * height, width, height);

   }

   //getter of x coordinate
   public int getxCoor() {

       return xCoor;

   }
   //setter of x coordinate
   public void setxCoor(int xCoor) {

       this.xCoor = xCoor;

   }
   //getter of y coordinate
   public int getyCoor() {

       return yCoor;

   }
   //setter of y coordinate
   public void setyCoor(int yCoor) {

       this.yCoor = yCoor;

   }

}

Add a comment
Know the answer?
Add Answer to:
I HAVE THE FOLLOWING CODE IN JAVA FOR A SNAKE GAME, I WANT SOMEONE TO EXPLAIN...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I HAVE THE FOLLOWING CODE IN JAVA FOR A SNAKE GAME, I WANT SOMEONE TO EXPLAIN...

    I HAVE THE FOLLOWING CODE IN JAVA FOR A SNAKE GAME, I WANT SOMEONE TO EXPLAIN EACH PART OF THE CODE, WHAT'S THE FUNCTION OF EACH PART THANK YOU import java.awt.Color; import java.awt.Graphics; public class BodyPart { private int xCoor, yCoor, width, height; public BodyPart(int xCoor, int yCoor, int tileSize) { this.xCoor = xCoor; this.yCoor = yCoor; width = tileSize; height = tileSize; } public void tick() { } public void draw(Graphics g) { g.setColor(Color.BLACK); g.fillRect(xCoor * width, yCoor *...

  • Java Program The goal of this assignment is to develop an event-based game. You are going...

    Java Program The goal of this assignment is to develop an event-based game. You are going to develop a Pong Game. The game board is a rectangle with a ball bouncing around. On the right wall, you will have a rectangular paddle (1/3 of the wall height). The paddle moves with up and down arrows. Next to the game board, there will be a score board. It will show the count of goals, and count of hits to the paddle....

  • Lab Description: Using graphics, polygons, and arrays, draw the tree shown below. You can change the...

    Lab Description: Using graphics, polygons, and arrays, draw the tree shown below. You can change the tree anyway you like to make it your own. Sample Data Sec below Files Needed:: GraphicaRunner.java Tree.java Sample Output: import java.awt.Graphics; import java.awt.color; import java.awt.Polygon; import java.awt.Font; import java.awt.Canvas; public class Tree extends Canvas public Tree() setBackground (Color.WHITE) public void paint ( Graphics window) window. setColor (Color.RED); window.setFont (new Font("TAHOMA" ,Font.BOLD,12) window.drawstring("Lab14h Tree Lab", 50, 50) tree(window); IONaA , Font.BOL.D,12) public void tree(Graphics window)...

  • please help me to creating UML class diagrams. Snake.java package graphichal2; import java.awt.Color; import java.awt.Font; import...

    please help me to creating UML class diagrams. Snake.java package graphichal2; import java.awt.Color; import java.awt.Font; import java.awt.Frame; import java.awt.Graphics; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.Random; enum Dir{L, U, R, D}; class Food { public static int FOODSTYLE = 1; private int m = r.nextInt(Yard.WIDTH / Yard.B_WIDTH); private int n = r.nextInt(Yard.HEIGHT / Yard.B_WIDTH - 30/Yard.B_WIDTH) + 30/Yard.B_WIDTH;// 竖格 public static Random r = new Random(); public int getM() { return m; } public void setM(int m)...

  • (JAVA NetBeans) Write programs in java Example 10.21-24 //Animal.java /** Animal class * Anderson. Franceschi */...

    (JAVA NetBeans) Write programs in java Example 10.21-24 //Animal.java /** Animal class * Anderson. Franceschi */ import java.awt.Graphics; public abstract class Animal { private int x; // x position private int y; // y position private String ID; // animal ID /** default constructor * Sets ID to empty String */ public Animal( ) { ID = ""; } /** Constructor * @param rID Animal ID * @param rX x position * @param rY y position */ public Animal( String...

  • I have to create a java graphics program which will draw 10 rectangles and 10 ellipses...

    I have to create a java graphics program which will draw 10 rectangles and 10 ellipses on the screen. These shapes are to be stored in an arrayList. I have to do this using 6 different class files. I am not sure whether I successfully created an ellipse in the Oval Class & also need help completing the print Class. I need someone to check whether my Oval Class is correct (it successfully creates an ellipse with x, y, width,...

  • Hi, for my Java class I have built a number guessing game. I need to separate...

    Hi, for my Java class I have built a number guessing game. I need to separate my code into two classes and incorporate a try-catch statement. Any help would be appreciated! Here is my code. import javax.swing.JOptionPane; import javax.swing.UIManager; import java.awt.Color; import java.awt.color.*; import java.util.Random; public class game { public static void main (String [] args) { UIManager.put("OptionPane.backround", Color.white); UIManager.put("Pandelbackround", Color.white); UIManager.put("Button.background", Color.white); Random nextRandom = new Random(); int randomNum = nextRandom.nextInt(1000); boolean playerCorrect = false; String keyboardInput; int playerGuess...

  • JAVA JAR HELP...ASAP I have the code that i need for my game Connect 4, but...

    JAVA JAR HELP...ASAP I have the code that i need for my game Connect 4, but i need to create a jar file . the instructions are below. It has to pass some parameters. I am really confused on doing so.l I dont know what to do next. Can someone help me and give detailed descritiopm opn how you ran the jar file in CMD import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Connect4 {               ...

  • Java question. I am creating a clock program in Java GUI, but it has some problem....

    Java question. I am creating a clock program in Java GUI, but it has some problem. First, The time of the clock is not correct. It has to point at the current time. Second, the position of the clock character is wrong. Please fix it for me please. import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.util.Date; import javax.swing.JPanel; import javax.swing.WindowConstants; public class Clock extends javax.swing.JFrame { public int hour; public int min; public int sec; ClockDial cd; public Clock() {...

  • This is a Clock program in Java. Can you fix my Java program error, and add function to display c...

    This is a Clock program in Java. Can you fix my Java program error, and add function to display current time currently? import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.util.Date; import javax.swing.JPanel; import javax.swing.WindowConstants; public class Clock extends javax.swing.JFrame {    public int hour;    public int min;    public int sec;    ClockDial cd;    public Clock() {        setSize(510, 530);        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);        cd = new ClockDial(this);        getContentPane().add(cd);        Date curr = new Date();        String time = curr.toString();        hour = Integer.parseInt(time.substring(11, 13));        min = Integer.parseInt(time.substring(14, 16));        sec...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT