package snake; import java.awt.*; import java.applet.*; import javax.swing.ImageIcon; import javax.swing.JLabel; public class Snake extends Applet implements Runnable { Image dot[] = new Image[400]; Image offI; Graphics offG; int x[] = new int[400]; int y[] = new int[400]; int rtemp = 1; int speed; int z; int n; int score = 0; int delay = -1; String directory = System.getProperty("user.dir"); Button easy = new Button("Easy"); Button medium = new Button("Medium"); Button hard = new Button("Hard"); JLabel dif; ImageIcon difImage = new ImageIcon(directory+"\\images\\difImage.png"); String direction; boolean go[] = new boolean[400]; boolean isPaused = true; boolean gameOver = true; Thread setTime; Panel buttons; public void init() { setSize(500, 500); setBackground(new Color(50,200,50)); buttons = new Panel(); buttons.setLayout(new GridLayout(4,1,50,50)); buttons.setBackground(new Color(50,200,50)); dif = new JLabel(difImage); buttons.add(dif); buttons.add(easy); buttons.add(medium); buttons.add(hard); add(buttons); for (z=0 ; z < 400 ; z++) { dot[z] = Toolkit.getDefaultToolkit().getImage(directory+"\\images\\dot.png"); } } public void update(Graphics g) { Dimension d = this.size(); if(offI == null) { offI = createImage(d.width, d.height); offG = offI.getGraphics(); } offG.clearRect(0, 0, d.width, d.height); paint(offG); g.drawImage(offI, 0, 0, null); delay--; } public void paint(Graphics g) { g.setColor(Color.black); if(!isPaused) { g.setFont(new Font("Verdana", 1, 28)); g.setColor(Color.BLACK); String scoreString = "Score "+score+""; g.drawString(scoreString, 320, 495); String line = "-------------------------------------------------"; g.drawString(line, -5,465); } if(!gameOver) { for (z=0; z<=n; z++) { g.drawImage(dot[z],x[z],y[z],this); } } } public void run() { for(z=4; z<400; z++) { go[z]=false; } for(z=0; z<4; z++) { go[z]=true; x[z]=91; y[z]=91; } n=3; score=0; buttons.move(600, 600); direction = ""; locateRandom(4); while(true) { if (!isPaused) { if ((x[0]==x[n]) && (y[0]==y[n])) { go[n]=true; locateRandom((n+1)); score+=10; } for(z=399 ; z>0 ; z--) { if (go[z]) { x[z] = x[(z-1)]; y[z] = y[(z-1)]; if ((z>4)&&(x[0]==x[z]) && (y[0]==y[z])) { gameOver = true; gameOver(); } } } if(direction.equals("left")) { x[0]-=10; } if(direction.equals("right")) { x[0]+=10; } if(direction.equals("up")) { y[0]-=10; } if(direction.equals("down")) { y[0]+=10; } } if(y[0]>450 && !gameOver) { y[0]=500; gameOver=true; gameOver(); break; } if(y[0]<1 && !gameOver) { y[0]=1; gameOver=true; gameOver(); break; } if(x[0]>500 && !gameOver) { x[0]=500; gameOver=true; gameOver(); break; } if(x[0]<1 && !gameOver) { x[0]=1; gameOver=true; gameOver(); break; } repaint(); try { setTime.sleep(speed); } catch(InterruptedException e){} } } public void locateRandom(int turn) { rtemp=(int)(Math.random()*50); x[turn]=((rtemp*10)+1) ; rtemp=(int)(Math.random()*40); y[turn]=((rtemp*10)+1); n++; } public boolean keyDown(Event e, int key) { if(delay<0) { if (key == Event.LEFT && !gameOver && !isPaused) { if(!direction.equals("right")) { direction="left"; delay=1; } isPaused = false; } if (key == Event.RIGHT && !gameOver && !isPaused) { if(!direction.equals("left")) { direction="right"; delay=1; } isPaused = false; } if (key == Event.UP && !gameOver && !isPaused) { if(!direction.equals("down")) { direction="up"; delay=1; } isPaused = false; } if (key == Event.DOWN && !gameOver && !isPaused) { if(!direction.equals("up")) { direction="down"; delay=1; } isPaused = false; } if(key == Event.ENTER && !gameOver) { if(isPaused) { isPaused = false; } else { isPaused = true; } direction=""; } return true; } return true; } public boolean action(Event event, Object obj) { String difficulty = (String) obj; if(difficulty.equals("Easy")) { speed=60; isPaused = false; gameOver = false; buttons.move(0,0); setTime = new Thread(this); setTime.start(); return true; } if(difficulty.equals("Medium")) { speed=35; isPaused = false; gameOver = false; buttons.move(0,0); setTime = new Thread(this); setTime.start(); return true; } if(difficulty.equals("Hard")) { speed=10; isPaused = false; gameOver = false; buttons.move(0,0); setTime = new Thread(this); setTime.start(); return true; } return false; } public void gameOver() { if(gameOver) { direction = ""; isPaused = true; buttons.move(175,5); gameOver = false; isPaused = true; setTime.stop(); } } }