public class snake extends JPanel implements ActionListener { //declare variables used in the beginning. by making them private, they can be seen only by the classes they belong to //sets the screen width to be 400 private int screenWidth = 400; //sets the screen height to be 400 private int screenHeight = 400; //sets the size of each dot to be 10 private int dotSize = 10; //sets the max number of dots that can be on the screen (400*400/10*10) private int maxDots = 1600; //used to randomize a dot to appear on screen private int randomPosition; //speed of the game private int delay = 200; //creates an array of all of the x coordinates of the snake that can be on screen private int x [] = new int [maxDots]; //creates an array of all of the y coordinates of the snake that can be on the screen private int y [] = new int [maxDots]; //creates variable for the dots on snake private int dots; //the x coordinate for the current edible dot private int edibleDotX; //the y coordinate for the current edible dot private int edibleDotY; //boolean for the left direction of the snake private boolean left = false; //boolean for the right direction of the snake private boolean right = false; //boolean for the up direction of the snake set to true as it starts going up private boolean up = true; //boolean for the down direction of the snake private boolean down = false; //boolean for whether the game should continue running or end private boolean inGame = true; //add a timer object private Timer timer; //image variable for the dots. accessible only by the class that needs it private Image dotSprite; //image variable for snake head. accessible only by the class that needs it private Image snakeHead; private Image snakeBody; //initializes the screen. this method is public because everything is displayed here public snake () { JFrame screen = new JFrame ("Snake"); screen.setVisible (true); screen.setBackground (Color.black); screen.setSize (screenWidth, screenHeight); screen.setResizable (false); loadImages (); Initialize (); } private void loadImages () { ImageIcon ish = null; try { ish = new ImageIcon (ImageIO.read(snake.class.getResource("/snakehead.png"))); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } snakeHead = ish.getImage (); ImageIcon id = null; try { id = new ImageIcon (ImageIO.read(snake.class.getResource ("/dot.png"))); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } dotSprite = id.getImage (); ImageIcon isb = null; try { isb = new ImageIcon (ImageIO.read(snake.class.getResource ("/snakebody.png"))); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } snakeBody = isb.getImage (); } private void Initialize () { dots = 1; for (int z = 0; z < dots; z++) { x[z] = 50 - z * 10; y[z] = 50; } randomEdibleDot (); timer = new Timer (delay, this); timer.start (); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); doDrawing(g); } private void doDrawing(Graphics g) { if (inGame = true) { g.drawImage(dotSprite, edibleDotX, edibleDotY, this); for (int z = 0; z < dots; z++) { if (z == 0) { g.drawImage(snakeHead, x[z], y[z], this); } else { g.drawImage(snakeBody, x[z], y[z], this); } } Toolkit.getDefaultToolkit().sync(); g.dispose(); } else { gameOver(); } } private void gameOver () { JOptionPane .showMessageDialog( null, "Game Over"); } public static void main(String[] args) { // TODO Auto-generated method stub snake screenShow = new snake (); } private void randomEdibleDot () { //randomly positions a dot in a new place on the board. int r = (int) (Math.random() * randomPosition); edibleDotX = ((r * dotSize)); r = (int) (Math.random() * randomPosition); edibleDotY = ((r * dotSize)); } private void checkDot() { if ((x[0] == edibleDotX) && (y[0] == edibleDotY)) { dots++; randomEdibleDot(); } } private void moving () { for (int z = dots; z > 0; z--) { x[z] = x[(z - 1)]; y[z] = y[(z - 1)]; } if (left) { x[0] -= dotSize; } if (right) { x[0] += dotSize; } if (up) { y[0] -= dotSize; } if (down) { y[0] += dotSize; } } private void checkDotCollision () { for (int z = dots; z > 0; z--) { if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) { inGame = false; } } if (y[0] > screenHeight) { inGame = false; } if (y[0] < 0) { inGame = false; } if (x[0] > screenWidth) { inGame = false; } if (x[0] < 0) { inGame = false; } } @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub if (inGame = true) { checkDot(); checkDotCollision(); moving(); } repaint(); } private class TAdapter extends KeyAdapter { @Override public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if ((key == KeyEvent.VK_LEFT) && (!right)) { left = true; up = false; down = false; } if ((key == KeyEvent.VK_RIGHT) && (!left)) { right = true; up = false; down = false; } if ((key == KeyEvent.VK_UP) && (!down)) { up = true; right = false; left = false; } if ((key == KeyEvent.VK_DOWN) && (!up)) { down = true; right = false; left = false; } } } }
So I'm intending to recreate Snake. There aren't any errors so it's just the logic I think. I think it's in how my components and stuff are done. But I have no idea what I'm doing wrong. So I put all of my code here minus the import statements. Can anyone figure out what I'm doing wrong?