HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Slitherio Single Player

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
playerslitheriosingle

Problem

I made a single player slither.io game. Here are the features:

  • You can speed up by holding down the mouse button.



  • You can eat food and grow both in length and width.



  • Only 100 food will spawn maximum (eat to generate more)



What suggestions both in game logic and in code structure do you guys have?

(there is only one class)

```
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

import javax.swing.JFrame;

public class Controller {

MyFrame frame;
ArrayList listOfDots;
ArrayList foods;
Image OSC;
PointerInfo a = MouseInfo.getPointerInfo();
int size = 10;
int speed = 10;
Random r;

public static void main(String[] args) {
Controller c = new Controller();
c.startGame();
}

public void startGame(){
frame = new MyFrame("slitherio");
listOfDots = new ArrayList();
foods = new ArrayList();
r = new Random();
listOfDots.add(new Point(100, 100));
Runner r = new Runner();
r.start();
}

class Runner extends Thread {
public void run(){
while(true){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(foods.size() 5){
n = calcCoor(last, p);
listOfDots.add(n);
if(listOfDots.size() >= size){
for(int i = 0; i i = foods.iterator();
while(i.hasNext()){
Point food = i.next();

Solution

Variables should be private when possible

MyFrame frame;
ArrayList listOfDots;
ArrayList foods;
Image OSC;
PointerInfo a = MouseInfo.getPointerInfo();
int size = 10;
int speed = 10;
Random r;


You should make variables private and final whenever possible. There are many benefits to this. The compiler will warn you when you are not using a field in your code, for example. You also want your classes to expose as little information as possible.

Think about a fixed time step for the game loop

class Runner extends Thread {
    public void run(){
        while(true){
            try {
                Thread.sleep(50);


I understand why you are doing this, but if you plan on making more games then I would start learning about a fixed time step rather than relying on Thread.sleep() and multiple threads. I found this that talks about specific ways to implement a game loop with Swing http://www.java-gaming.org/index.php?topic=24220.0

Don't use the type of data structure when naming

ArrayList listOfDots;


There's no good reason not to just call this dots. If you later change this to a Map or another structure, you won't have to go through your code and change the variable name since it would then be misleading.

Magic Numbers

if(foods.size()  5){
if(food.distance(n) < 20){
setBounds(0, 0, 900, 900);
g2.fillOval(foods.get(i).x, foods.get(i).y, 10, 10);


It's not impossible to figure out what these numbers mean when reading through the code, but the code will be more clear and more self documenting if you use explicit variables for each of these magic numbers. So things like screenWidth, screenHeight, maxFood, eatDistance, etc.

Separate game model from rendering

Currently, the logic for the slither game as well as its state are tied together with the way that you render the game and handle input. You should always try to separate your game model from the way it is rendered. You should have a Slither class that contains the information about the player position, food positions, and the rules for how the game plays. Then, each frame the rendering would look at the game model and draw things according to where they are located.

When you do this, it will be very easy for you to port the game to another graphics library, or even port the game logic to another language or game engine.

That's all for now! Nice question.

Code Snippets

MyFrame frame;
ArrayList<Point> listOfDots;
ArrayList<Point> foods;
Image OSC;
PointerInfo a = MouseInfo.getPointerInfo();
int size = 10;
int speed = 10;
Random r;
class Runner extends Thread {
    public void run(){
        while(true){
            try {
                Thread.sleep(50);
ArrayList<Point> listOfDots;
if(foods.size() < 100)
foods.add(new Point(r.nextInt(900), r.nextInt(900)));
if(last.distance(p) > 5){
if(food.distance(n) < 20){
setBounds(0, 0, 900, 900);
g2.fillOval(foods.get(i).x, foods.get(i).y, 10, 10);

Context

StackExchange Code Review Q#126911, answer score: 10

Revisions (0)

No revisions yet.