import java.applet.*; import java.awt.event.*; import java.awt.*; import javax.swing.*; //we need the applet methods and the MouseMotionListener interface //(used for the human controlled paddle) public class snakeMain extends Applet implements KeyListener, ActionListener { // a font used to display the score Font newFont = new Font("sansserif", Font.BOLD, 20); //The image is going to be double buffered to avoid flicker Graphics bufferGraphics; //the Image will contain everything drawn on bufferGraphics Image offscreen; //variables used to set the width and height of the applet final int WIDTH = 500, HEIGHT = 500, BORDER = 100, GRIDSIZE = 10, INITIALLENGTH = 3, INITIALSNAKEX = 5, INITIALSNAKEY = 5; private boolean gameOver = false; private int snakeLength = INITIALLENGTH; private int snakeX = INITIALSNAKEX, snakeY = INITIALSNAKEY; private int snakeUpDown = -1, snakeLeftRight = 0; private int frameCounter = 0; //declare the array int[][] grid = new int[GRIDSIZE][GRIDSIZE]; //I am going to use a timer to do a certain list of tasks //every 15 milliseconds (67 FPS) Timer time = new Timer(15, this); public void init() { //set the applet to be 500*300 setSize(WIDTH, HEIGHT); gameOver = false; grid[snakeY][snakeX] = snakeLength; //mouseMotionListener allows the player to control their paddle addKeyListener(this); setBackground(Color.black); //create offscreen image to draw on offscreen = createImage(WIDTH, HEIGHT); bufferGraphics = offscreen.getGraphics(); //this is the game loop time.start(); } //every 15 milliseconds the timer triggers the actionPerformed method public void actionPerformed(ActionEvent arg0) { if(gameOver == true) { //after the game needs to end we stop the timer, time.stop(); repaint(); } else { frameCounter = frameCounter + 1; if (frameCounter == 10) { snakeX = snakeX + snakeLeftRight; snakeY = snakeY + snakeUpDown; if (snakeX < 0) { snakeX = GRIDSIZE; } if (snakeX > GRIDSIZE) { snakeX = 0; } if (snakeY < 0) { snakeY = GRIDSIZE; } if (snakeY > GRIDSIZE) { snakeY = 0; } for (int i = 0; i <= GRIDSIZE; i++) { for (int j = 0; j <= GRIDSIZE; j++) { if (grid[i][j] > 0) { grid[i][j]--; } } } grid[snakeY][snakeX] = snakeLength; frameCounter = 0; } else { } //repaints the applet repaint(); } } public void checkCollision() { } public void paint(Graphics g) { // first clear off the image bufferGraphics.clearRect(0,0,WIDTH,HEIGHT); //Now draw the snake in white bufferGraphics.setColor(Color.white); //placeholder to test graphics bufferGraphics.fillRect(10,10,100,100); bufferGraphics.drawString(" " + frameCounter, 150, 15); for (int i = 0; i <= GRIDSIZE; i++) { for (int j = 0; j <= GRIDSIZE; j++) { if(grid[i][j] > 0) { bufferGraphics.fillRect(((i*10) + BORDER),((j*10) + BORDER),10,10); } } } //finally draw the offscreen image to the applet g.drawImage(offscreen,0,0,this); //this line makes sure all the monitors are up to date before proceeding Toolkit.getDefaultToolkit().sync(); } public void update(Graphics g) { paint(g); } public void keyPressed(KeyEvent e) { } public void keyTyped(KeyEvent e) { //this is placeholder } public void keyReleased(KeyEvent e) { //this is placeholder } }