About this question
I am a self - taught coder, and have been learning Java / C# for the last 2 1/2 years (inconsistently).
I have recently created a snake game in Java with the help of an online tutorial. I have rearranged the program into separate classes & methods. I am sceptical on how good of a programmer I am, and I code messy.
I don't really want help on how to make the game better in visuals, levels, or anything like that. I am asking those who are more advanced programmers to help me with implementing programming techniques, better organisation, how to comment better, etc. Pretty much anything that will help me become a better programmer.
Also, I have commented a few things out of pure guesswork, so if it appears as if I have gotten something wrong, please correct me!
Also, any extra tips for my future projects would be much appreciated!
Game.Java
import java.awt.EventQueue;
import javax.swing.JFrame;
public class Game extends JFrame {
Game() {
add(new Board());
setResizable(false);
pack();
setTitle("Snake");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// Creates a new thread so our GUI can process itself
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new Game();
frame.setVisible(true);
}
});
}
}
Snake.Java
public class Snake {
// Stores the joints / body part locations for our snake
private final int[] x = new int[Board.getAllDots()];
private final int[] y = new int[Board.getAllDots()];
// Stores direction of our snake
private boolean movingLeft = false;
private boolean movingRight = false;
private boolean movingUp = false;
private boolean movingDown = false;
private int joints = 0; // Stores # of dots / joints the snake has (starts
// with 3)
public int getSnakeX(int index) {
return x[index];
}
public int getSnakeY(int index) {
return y[index];
}
public void setSnakeX(int i) {
x[0] = i;
}
public void setSnakeY(int i) {
y[0] = i;
}
public boolean isMovingLeft() {
return movingLeft;
}
public void setMovingLeft(boolean movingLeft) {
this.movingLeft = movingLeft;
}
public boolean isMovingRight() {
return movingRight;
}
public void setMovingRight(boolean movingRight) {
this.movingRight = movingRight;
}
public boolean isMovingUp() {
return movingUp;
}
public void setMovingUp(boolean movingUp) {
this.movingUp = movingUp;
}
public boolean isMovingDown() {
return movingDown;
}
public void setMovingDown(boolean movingDown) {
this.movingDown = movingDown;
}
public int getJoints() {
return joints;
}
public void setJoints(int j) {
joints = j;
}
public void move() {
for (int i = joints; i > 0; i--) {
// Moves the joints of the snake 'up the chain'
// Meaning, the joint of the snake all move up one
x[i] = x[(i - 1)];
y[i] = y[(i - 1)];
}
// Moves snake to the left
if (movingLeft) {
x[0] -= Board.getDotSize();
}
// To the right
if (movingRight) {
x[0] += Board.getDotSize();
}
// Down
if (movingDown) {
y[0] += Board.getDotSize();
}
// And finally up
if (movingUp) {
y[0] -= Board.getDotSize();
}
// Dotsize represents the size of the joint, so a pixel of DOTSIZE
// gets added on to the snake in that direction
}
}