public MainFrame() { JTextField widthField = new JTextField(); widthField.setColumns(2); JTextField heightField = new JTextField(); heightField.setColumns(2); JCheckBox boundaryCheck = new JCheckBox(); Object[] dataIn = {"Width", widthField, "Height", heightField, "Boundaries?", boundaryCheck}; JOptionPane op = new JOptionPane( dataIn, JOptionPane.QUESTION_MESSAGE, JOptionPane.DEFAULT_OPTION, null, null); JDialog dialog = op.createDialog(op, "Settings"); dialog.setVisible(true); endAtWall = boundaryCheck.isSelected(); int width = Integer.parseInt(widthField.getText()); int height = Integer.parseInt(heightField.getText()); board = new boolean[width][height]; super.setSize(width * 15 + 80, height * 15 + 40); super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); start(); placeExtra(); gp = new GamePanel(board, images, snake); mainPanel = new JPanel(new GridBagLayout()); GridBagConstraints mc = new GridBagConstraints(); mc.anchor = GridBagConstraints.NORTHWEST; mc.gridx = 0; mc.gridy = 0; mainPanel.add(gp, mc); mc.gridx = 1; sp = new ScorePanel(score); mainPanel.add(sp, mc); super.add(mainPanel); super.setVisible(true); this.addKeyListener( new KeyListener() { public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_RIGHT) { snake[0].setDirection(4); } else if (e.getKeyCode() == KeyEvent.VK_LEFT) { snake[0].setDirection(3); } else if (e.getKeyCode() == KeyEvent.VK_DOWN) { snake[0].setDirection(1); } else if (e.getKeyCode() == KeyEvent.VK_UP) { snake[0].setDirection(2); } } public void keyReleased(KeyEvent e) {} public void keyTyped(KeyEvent e) {} }); t.schedule( new TimerTask() { public void run() { int[] loc = { snake[snake.length - 1].getLocationx(), snake[snake.length - 1].getLocationy() }; // old coordinates of last snake cell for (int i = snake.length - 1; i > 0; i--) { snake[i] = new Snake( snake[i - 1].getDirection(), snake[i - 1].getLocationx(), snake[i - 1].getLocationy()); } // makes each cell of the snake equal to the one ahead so that the very first cell is // empty if (endAtWall) { switch (snake[1].getDirection()) { // assigns the first cell of the snake case 1: snake[0] = new Snake( snake[1].getDirection(), snake[1].getLocationx(), snake[1].getLocationy() + 1); break; case 2: snake[0] = new Snake( snake[1].getDirection(), snake[1].getLocationx(), snake[1].getLocationy() - 1); break; case 3: snake[0] = new Snake( snake[1].getDirection(), snake[1].getLocationx() - 1, snake[1].getLocationy()); break; case 4: snake[0] = new Snake( snake[1].getDirection(), snake[1].getLocationx() + 1, snake[1].getLocationy()); break; } } else { switch (snake[1].getDirection()) { // assigns the first cell of the snake case 1: if (snake[1].getLocationy() + 1 == board[0].length) { snake[0] = new Snake(snake[1].getDirection(), snake[1].getLocationx(), 0); } else { snake[0] = new Snake( snake[1].getDirection(), snake[1].getLocationx(), snake[1].getLocationy() + 1); } break; case 2: if (snake[1].getLocationy() - 1 == -1) { snake[0] = new Snake( snake[1].getDirection(), snake[1].getLocationx(), board[0].length - 1); } else { snake[0] = new Snake( snake[1].getDirection(), snake[1].getLocationx(), snake[1].getLocationy() - 1); } break; case 3: if (snake[1].getLocationx() - 1 == -1) { snake[0] = new Snake( snake[1].getDirection(), board.length - 1, snake[1].getLocationy()); } else { snake[0] = new Snake( snake[1].getDirection(), snake[1].getLocationx() - 1, snake[1].getLocationy()); } break; case 4: if (snake[1].getLocationx() + 1 == board.length) { snake[0] = new Snake(snake[1].getDirection(), 0, snake[1].getLocationy()); } else { snake[0] = new Snake( snake[1].getDirection(), snake[1].getLocationx() + 1, snake[1].getLocationy()); } break; } } if (snake[0].getLocationx() == extraSnake.getLocationx() && snake[0].getLocationy() == extraSnake.getLocationy()) { // if the head hits the extra Snake[] temp = new Snake[snake.length]; for (int i = 0; i < snake.length; i++) { temp[i] = new Snake( snake[i].getDirection(), snake[i].getLocationx(), snake[i].getLocationy()); } snake = new Snake[snake.length + 1]; for (int i = 0; i < temp.length; i++) { snake[i] = new Snake( temp[i].getDirection(), temp[i].getLocationx(), temp[i].getLocationy()); } // copied snake array back to snake with 1 more space for the extra on the tail switch (snake[snake.length - 2].getDirection()) { case 1: snake[snake.length - 1] = new Snake( snake[snake.length - 2].getDirection(), snake[snake.length - 2].getLocationx(), snake[snake.length - 2].getLocationy() - 1); break; case 2: snake[snake.length - 1] = new Snake( snake[snake.length - 2].getDirection(), snake[snake.length - 2].getLocationx(), snake[snake.length - 2].getLocationy() + 1); break; case 3: snake[snake.length - 1] = new Snake( snake[snake.length - 2].getDirection(), snake[snake.length - 2].getLocationx() + 1, snake[snake.length - 2].getLocationy()); break; case 4: snake[snake.length - 1] = new Snake( snake[snake.length - 2].getDirection(), snake[snake.length - 2].getLocationx() - 1, snake[snake.length - 2].getLocationy()); break; } extra = false; if (snake[snake.length - 1].getLocationx() == -1) { snake[snake.length - 1].setLocation( board.length - 1, snake[snake.length - 1].getLocationy()); } if (snake[snake.length - 1].getLocationy() == -1) { snake[snake.length - 1].setLocation( snake[snake.length - 1].getLocationx(), board[0].length - 1); } board[snake[snake.length - 1].getLocationx()][ snake[snake.length - 1].getLocationy()] = false; score++; sp.refresh(score); placeExtra(); } boolean gameOver = false; for (int i = 1; i < snake.length; i++) { if (snake[0].getLocationx() == snake[i].getLocationx() && snake[0].getLocationy() == snake[i].getLocationy()) { endGame(); gameOver = true; } } if (endAtWall) { if (snake[0].getLocationx() == board.length || snake[0].getLocationx() == -1 || snake[0].getLocationy() == -1 || snake[0].getLocationy() == board[0].length) { endGame(); gameOver = true; } } if (!gameOver) { // if the game isnt over, refresh gp.Refresh(board, images, snake, loc, extraSnake, MainFrame.this); } } }, 1000, 100); }