Skip to main content
added 38 characters in body
Source Link
Reinderien
  • 71.1k
  • 5
  • 76
  • 256

I am a beginner at programming. I made this snake game in JavaFX to practice and improve my coding skills. I tried to make my code clean, but I'm not sure if it is, or if it is well-organized. I also tried to make the design object oriented-oriented. So I would like to ask your opinion on my code's structure and clarity. I would appreciate any advice you give me to improve.

This is what the game looks like:

  1. SnakeGameSnakeGame is the main class that is responsible for putting the game together and starting, restarting, ending the game, and taking user input.

  2. GameBoardGameBoard extends PanePane. It is responsible for the snake and the fruit objects. It has an inner class, SnakeMovementSnakeMovement, that extends TimerTaskTimerTask. This task invokes the methods for checking for collisions, checking if the snake ate a fruit, and invokes the moveSnake()moveSnake() method in the Snake class.

  3. SnakeSnake class extends ArrayListArrayList<Rectangle>. It has the methods for creating the snake, moving the snake, and adding a new head to the snake when a fruit is eaten.

  4. FruitFruit class extends RectangleRectangle. It has the methods to get a random empty tile and to spawn the fruit in an empty tile.

Edit:

#root {
    -fx-background-color: black;
}

#scoreBar {
    -fx-alignment: center;
    -fx-padding: 10px;
    -fx-border-style: solid;
    -fx-border-width: 0px 0px 2px 0px;
    -fx-border-color: white;
}

#scoreIndicator {
    -fx-text-fill: white;
    -fx-font-size: 15px;
    -fx-font-family: monospace;
}

#replay {
    -fx-background-color: transparent;
    -fx-text-fill: white;
    -fx-font-size: 15px;
    -fx-font-family: monospace;
}
#root {
    -fx-background-color: black;
}

#scoreBar {
    -fx-alignment: center;
    -fx-padding: 10px;
    -fx-border-style: solid;
    -fx-border-width: 0px 0px 2px 0px;
    -fx-border-color: white;
}

#scoreIndicator {
    -fx-text-fill: white;
    -fx-font-size: 15px;
    -fx-font-family: monospace;
}

#replay {
    -fx-background-color: transparent;
    -fx-text-fill: white;
    -fx-font-size: 15px;
    -fx-font-family: monospace;
}

I am a beginner at programming. I made this snake game in JavaFX to practice and improve my coding skills. I tried to make my code clean, but I'm not sure if it is, or if it is well-organized. I also tried to make the design object oriented. So I would like to ask your opinion on my code's structure and clarity. I would appreciate any advice you give me to improve.

This is what the game looks like

  1. SnakeGame is the main class that is responsible for putting the game together and starting, restarting, ending the game, and taking user input.

  2. GameBoard extends Pane. It is responsible for the snake and the fruit objects. It has an inner class, SnakeMovement, that extends TimerTask. This task invokes the methods for checking for collisions, checking if the snake ate a fruit, and invokes the moveSnake() method in the Snake class.

  3. Snake class extends ArrayList. It has the methods for creating the snake, moving the snake, and adding a new head to the snake when a fruit is eaten.

  4. Fruit class extends Rectangle. It has the methods to get a random empty tile and to spawn the fruit in an empty tile.

Edit:

#root {
    -fx-background-color: black;
}

#scoreBar {
    -fx-alignment: center;
    -fx-padding: 10px;
    -fx-border-style: solid;
    -fx-border-width: 0px 0px 2px 0px;
    -fx-border-color: white;
}

#scoreIndicator {
    -fx-text-fill: white;
    -fx-font-size: 15px;
    -fx-font-family: monospace;
}

#replay {
    -fx-background-color: transparent;
    -fx-text-fill: white;
    -fx-font-size: 15px;
    -fx-font-family: monospace;
}

I am a beginner at programming. I made this snake game in JavaFX to practice and improve my coding skills. I tried to make my code clean, but I'm not sure if it is, or if it is well-organized. I also tried to make the design object-oriented. So I would like to ask your opinion on my code's structure and clarity. I would appreciate any advice you give me to improve.

This is what the game looks like:

  1. SnakeGame is the main class that is responsible for putting the game together and starting, restarting, ending the game, and taking user input.

  2. GameBoard extends Pane. It is responsible for the snake and the fruit objects. It has an inner class, SnakeMovement, that extends TimerTask. This task invokes the methods for checking for collisions, checking if the snake ate a fruit, and invokes the moveSnake() method in the Snake class.

  3. Snake class extends ArrayList<Rectangle>. It has the methods for creating the snake, moving the snake, and adding a new head to the snake when a fruit is eaten.

  4. Fruit class extends Rectangle. It has the methods to get a random empty tile and to spawn the fruit in an empty tile.

#root {
    -fx-background-color: black;
}

#scoreBar {
    -fx-alignment: center;
    -fx-padding: 10px;
    -fx-border-style: solid;
    -fx-border-width: 0px 0px 2px 0px;
    -fx-border-color: white;
}

#scoreIndicator {
    -fx-text-fill: white;
    -fx-font-size: 15px;
    -fx-font-family: monospace;
}

#replay {
    -fx-background-color: transparent;
    -fx-text-fill: white;
    -fx-font-size: 15px;
    -fx-font-family: monospace;
}
Added snake_game.css.
Source Link
Abeer
  • 63
  • 4

Edit:

snake_game.css

#root {
    -fx-background-color: black;
}

#scoreBar {
    -fx-alignment: center;
    -fx-padding: 10px;
    -fx-border-style: solid;
    -fx-border-width: 0px 0px 2px 0px;
    -fx-border-color: white;
}

#scoreIndicator {
    -fx-text-fill: white;
    -fx-font-size: 15px;
    -fx-font-family: monospace;
}

#replay {
    -fx-background-color: transparent;
    -fx-text-fill: white;
    -fx-font-size: 15px;
    -fx-font-family: monospace;
}

Edit:

snake_game.css

#root {
    -fx-background-color: black;
}

#scoreBar {
    -fx-alignment: center;
    -fx-padding: 10px;
    -fx-border-style: solid;
    -fx-border-width: 0px 0px 2px 0px;
    -fx-border-color: white;
}

#scoreIndicator {
    -fx-text-fill: white;
    -fx-font-size: 15px;
    -fx-font-family: monospace;
}

#replay {
    -fx-background-color: transparent;
    -fx-text-fill: white;
    -fx-font-size: 15px;
    -fx-font-family: monospace;
}
Tweeted twitter.com/StackCodeReview/status/1537087548660584452
added 11 characters in body
Source Link
Abeer
  • 63
  • 4
Source Link
Abeer
  • 63
  • 4
Loading