Question: Please do task one. All instructions are written below. Code is java. Thanks! import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import javax.swing.*; /** * * ==========================================================================
Please do task one. All instructions are written below. Code is java. Thanks!
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
/**
*
* ==========================================================================
* CODING INSTRUCTIONS!
* ==========================================================================
*
*
*
* Task 0:
* compile and run SnakeGame as a Java Application.
*
* Task 1: (in class Snake)
* create a class called Snake in a separate Java source file.
* import java.awt.*;
*
* required fields (or instance variables):
* int size, direction, bodyAdd;
* Rectangle head;
* ArrayList
* ArrayList
* (note: look up Rectangle in the Java Documentation for more information)
* write a default constructor for the Snake Object
* size is 20 (the number of pixels for each Snake piece)
* direction is 8 (8 is up, 2 down, 4 left, and 6 right.. look at numpad)
* bodyAdd is 4 (4 body pieces will be initially added to the Snake
* head is a Rectangle that is 20x20 and
* is located in the approximate center of the window (i.e. coordinates (300,300))
* remember to initialize body and directionQ to new ArrayLists
* write method: public void draw(Graphics2D g)
* that will render the head of Snake ..use g.draw(head)
* and the body of Snake (use a for-loop)
* /
public class SnakeGame extends JPanel implements
KeyListener, ActionListener {
// DECLARE ALL INSTANCE VARIABLES HERE..
//private Snake snake;
//private Rectangle nibble;
private int frameCount;// used for the score
private String title = "~~ Snake Game Clone ~~ "
+ "CONTROLS: arrows = move, r = restart .. ..........";
public static final Rectangle border = new Rectangle(0,0,600,600);//size of JPanel
private Timer clock;// handles animation
private Image offScreenBuffer;// needed for double buffering graphics
private Graphics offScreenGraphics;// needed for double buffering graphics
/**
* main method needed for initialize the game window
* .. THIS METHOD SHOULD NOT BE MODIFIED! ..
*/
public static void main(String[] args) {
JFrame window = new JFrame("Snake Game Clone");
window.setBounds(100, 100, border.width + 7, border.height + 27);//inside Frame will be 600x600
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
SnakeGame game = new SnakeGame();
game.setBackground(Color.WHITE);
window.getContentPane().add(game);
window.setVisible(true);
game.init();
window.addKeyListener(game);
}
/**
* init() is needed to override the init in JApplet.
* THIS METHOD SHOULD NOT BE MODIFIED! ..
* you should write all necessary initialization code in initRound()
*/
public void init() {
System.out.println(getWidth() + " , " + getHeight());
offScreenBuffer = createImage(getWidth(), getHeight());// should be 600x600
offScreenGraphics = offScreenBuffer.getGraphics();
initRound();
clock = new Timer(100, this);// timer fires every 100 milliseconds
// ............................and invokes method actionPerformed()
// INITIALIZE ALL SOUNDS AND IMAGE VARIABLES HERE...
}
/**
* initializes all fields needed for each round of play (i.e. restart)
*/
public void initRound() {
frameCount = 0;
// YOUR CODE GOES HERE..
// initialize game Objects like Snake
}
/**
* Called automatically after a repaint request
* .. THIS METHOD SHOULD NOT BE MODIFIED! ..
* you should write all necessary rendering code in method draw()
*/
public void paint(Graphics g) {
draw((Graphics2D) offScreenGraphics);
g.drawImage(offScreenBuffer, 0, 0, this);
}
/**
* renders all objects to Graphics g (i.e. the window)
*/
public void draw(Graphics2D g) {
super.paint(g);// refresh the background
g.setColor(Color.BLACK);
g.drawString(title, 100, 20);// draw the title towards the top
g.drawString("timer: " + frameCount, 280, 100);// approximate middle
// YOUR CODE GOES HERE..
// render all game objects here
}
/**
* Called automatically when the timer fires
* this is where all the game Objects will be updated
*/
public void actionPerformed(ActionEvent e) {
//snake.move();
// YOUR CODE GOES HERE..
frameCount++;// used for the score
repaint();// needed to refresh the animation
}
/**
* handles any key pressed events and updates the player's direction by
* setting their direction to either 1 or -1 for right or left respectively
* and updates their jumping status by invoking jump()
*/
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT) {
//snake.setDirection(4);
} //else if (keyCode == KeyEvent.VK_RIGHT) {
// YOUR CODE GOES HERE..
// more else if statements for all four directions
// and any other keystrokes to handle
}
/**
* handles any key released events and restarts the game if
* the Timer is not running and user types 'r'
*/
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_R && !clock.isRunning()) {
clock.start();
initRound();
}
}
/**
* leave empty.. needed for implementing interface KeyListener
*/
public void keyTyped(KeyEvent e) {
}
}// end class SnakeGame
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
