Page 1 of 1

Problem 1: SnakeGamePanel.java Create a public class SnakeGamePanel which is a sub-class of JPanel public class SnakeGam

Posted: Tue May 24, 2022 8:00 am
by answerhappygod
Problem 1: SnakeGamePanel.java
Create a public class SnakeGamePanel which is a sub-class of
JPanel
public class SnakeGamePanel extends JPanel
This class will implement the Snake game on a JPanel. Here is the
basic setup:
The screen of the game is 320x320 pixels large. The apple’s size
is 20x20 pixels. At the beginning of the game, the snake is also
has the size of an apple.
The positions of the snake and apple are randomly selected and
cannot be the same. Their x and y coordinates have to be multiples
of 20 and in the range of [0, 300].
Paint your snake red and apple green. Paint the background blue
as shown in the demo.
The snake does not move at the beginning. As an arrow key is
pressed, the snake starts to
move in the direction of the arrow.
As the game starts, the snake moves with a constant speed of
20pixels/0.25second. The jump is precisely 20 pixels. Use a Timer
with the delay time set to be 250 milliseconds to fire an action
event periodically and then update the animation graphics in the
action listener.
In detail:
During the (delay) time between two consecutive animation
graphics updates, the player can hit multiple and different arrow
keys. Still, the last key will override all previous keys, and you
need to use the last key for graphics updates. Write a class
DirectionListener that implements the interface KeyListener to keep
track of the (final) pressed key during any delay time.
private class DirectionListener implements KeyListener
You only need to implement the method keyPressed() of this
interface and leave the implemen- tations of other methods empty.
Remember the role of this class is to obtain the direction of the
snake. Do not update the snake’s position or call the repaint()
method.
Write a class MovingListener to implement the interface
ActionListener.
Figure 1: The snake is moving to the left, before and after
eating an apple. After the snake eats an apple, another apple is
generated. A sequence is added to the tail of the snake.
private class MovingListener implements ActionListener
In implementing the actionPerformed() method, use the arrow key
obtained from the keyPressed() method to update the snake’s
position. You need to make sure that the snake cannot reverse its
direction. For example, if the snake is moving right and the player
presses the left arrow key, the snake does not change its
direction. Instead, the left arrow key is ignored, and the snake
keeps moving right. One important thing: if no arrow key is
pressed, the snake keeps going in the same direction.
In the implementation of actionPerformed(), you will need to
call repaint() and override the paintComponent(..) method:
protected void paintComponent (Graphics g)
This method will repaint the panel, as well as the snake and
apple.
After eating an apple, the snake increases its length by one
sequence (a square with the same size as the apple). Add that
sequence to the tail of the snake. Then generate a new apple by
implementing the method nextApple()
private void nextApple ()
The apple position should also be random, and cannot be located on
the body of the snake.
After eating every four apples, the game speed increases:
reducing the delay time of the Timer by 20 milliseconds. Let the
minimum delay time be 50 milliseconds (highest speed). So there
will be 11 levels in this game, corresponding to delay times 250,
230, . . . 70, 50. After reaching level 11, the speed does not
change no matter how many more apples the snake eats.
Write the method isGameOver() to check whether the game is over:
private boolean isGameOver()
The game is over when the snake hits itself or hits the panel
border. The function will return true if the game is over, and
false otherwise. Naturally, you will check whether the game is over
before calling the repaint() method. The other way around might be
ok but not encouraged since we
can assume the snake is in-compressible.
While playing the game, if the space key is hit, the game pauses
and a dialog box appears saying “Pause, continue?” with a “OK”
button. The game will resume as the space key is pressed again or
you click the OK button. The snake will continue to move in the
same direction as before the pause.
When the game is over, show a dialog box with the message “Game
Over, New Game?” and an “OK” button. You can use either
ConfirmDiaglog or OptionDiaglog. Once the play hits the button, a
new game is generated. You might need to implement a method
newGame() to set this up.
private void newGame ()
In this method, you will reset the timer’s delay time, restart the
timer, and generate a new snake
as well as a new apple.
Create a private inner-class Sequence to store the 2D
coordinates of apples and snake’s body. Class Sequence provides a
parametric constructor
public Sequence(int x, int y)
The class has two instance variables x and y which are the
coordinates of a sequence. You can set
these fields public so you can access them directly. Use
ArrayList to store the snake (its sequences):
ArrayList <Sequence > snake = new ArrayList <>()
You will learn ArrayList in detail soon. ArrayList is similar to
vector in C++ where its elements are stored contiguously in memory
and you do not need to worry about the change of the array size.
The class provides some useful methods such as get(int index),
add(Object obj), remove(int index), and size(). You can use these
methods for this project.
A little bit in detail: a snake is like a FIFO (first in first
out) data structure.
• When the snake grows by one bit, we push a sequence on top of the
stack.
• When it moves by one square, we push a sequence on top and pop
an element of the bottom
You will see that Queue or LinkedList is probably a better data
structure for this program. However, ArrayList can access data
with a constant time. So there is always a tradeoff among
these data structures. You will soon learn how to implement &
use LinkedList efficiently, but you do not know it now. ArrayList
is good enough for now.
Problem 2: SnakeGame.java
In this java file, create a static method main()
public static void main(String args[])
In this method, you will create a JFrame with name “Snake Game.”
Add a SnakeGamePanel to
this JFrame, set its size un-resizable.
We may take off up to 20% of the total marks for poor style; make
sure to name your variables
reasonably, indent properly, and comment sufficiently.