Snake Game in Java
Need help writing a snake game in java. Need a snake class, foodclass, wall class, a game class to run those classes, and a testerclass to test that everything works. There’s no need to implementany kinds of GUI, the classes just needs to contain the basic logicof a snake game.
Answer
code:-
snakegame.java
package com.snakegame;
import java.awt.EventQueue;
import javax.swing.JFrame;
public class Snakegame extends JFrame {
public Snakegame() {
initUI();
}
private void initUI() {
add(new Board());
setResizable(false);
pack();
setTitle(“Snake”);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
EventQueue.invokeLater(() -> {
JFrame ex = new Snake();
ex.setVisible(true);
});
}
}
2) code:-
boardgame.java
package com.snakegame;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import