package snakeB_04; import java.applet.*; import java.awt.Color; import java.awt.Event; import java.awt.Graphics; import java.awt.Point; import java.util.ArrayList; /** * @author Traffyk * @website http://www.traffyk.com */ public class Snake extends Applet implements Runnable { private static final long serialVersionUID = -7779152480459644178L; protected Thread esecutore; protected int d, x, y, tempX, tempY, verso, size, punteggio, record; protected String nome; protected ArrayList snake = new ArrayList(); // protected Button bPlay = new Button("Start!"); public void init() { /* add(bPlay); bPlay.setVisible(false);*/ d = 50; x = y = d / 2 + 1; verso = 0; record = 0; punteggio = 0; snake.add(new Point(x, y)); snake.add(new Point(x, y - 1)); snake.add(new Point(x, y - 2)); nome = new String("Snake"); creaPunto(); } public void start() { if (esecutore == null) { esecutore = new Thread(this); esecutore.start(); } } public void run() { while (true) { // repaint(); // ridisegna la finestra dell'applet pause(50); // pausa (in millisecondi) switch (verso) { case 1: muovi("DOWN"); break; case 2: muovi("UP"); break; case 3: muovi("LEFT"); break; case 4: muovi("RIGHT"); break; default: } } } public void pause(int time) { try { Thread.sleep(time); } catch (InterruptedException e) { } } public void stop() { // arresto dell'applet, medoto richiamato quando la pagina che // contiene l'applet viene abbandonata. Puo' arrestare le // attivita' in background (thread) avviati dall'applet if (esecutore != null) { esecutore.interrupt(); esecutore = null; } if (snake != null) snake = null; verso = 0; } public void destroy() { if (snake != null) snake = null; verso = 0; if (esecutore != null) { esecutore.interrupt(); esecutore = null; } } public void muovi(String s) { if (s.equals("DOWN")) { if (verso != 2) { verso = 1; if (y < d - 1) y++; else y = 0; } } else if (s.equals("UP")) { if (verso != 1) { verso = 2; if (y > 0) --y; else y = d - 1; } } else if (s.equals("LEFT")) { if (verso != 4) { verso = 3; if (x > 0) --x; else x = d - 1; } } else if (s.equals("RIGHT")) { if (verso != 3) { verso = 4; if (x < d - 1) ++x; else x = 0; } } if (snakeCrash()) { if (puntoPreso()) { snake.add(new Point(snake.get(snake.size() - 1).x, snake.get(snake.size() - 1).y)); creaPunto(); punteggio += 9; if (record < punteggio) record = punteggio; } repaint(); } } public boolean keyDown(Event evt, int key) { if (key == Event.DOWN) muovi("DOWN"); else if (key == Event.UP) muovi("UP"); else if (key == Event.LEFT) muovi("LEFT"); else if (key == Event.RIGHT) muovi("RIGHT"); return true; } public boolean keyUp(Event evt, int key) { // repaint(); return true; } public void paint(Graphics g) { g.setColor(Color.blue); if (getSize().height <= getSize().width) size = getSize().height; else size = getSize().width; g.drawRect(0, 0, size - 1, size - 1); for (int i = 0; i < snake.size(); i++) { g.fillRect(snake.get(i).x * (size / d), snake.get(i).y * (size / d), size / d - 1, size / d - 1); } quadro(g); } public int random() { int x = (int) (Math.random() * (d - 1)); return x; } public void creaPunto() { boolean risp = false; do { tempX = random(); tempY = random(); for (int i = 0; i < snake.size(); i++) { if (snake!=null) if (snake.get(i).x == tempX && snake.get(i).y == tempY) { risp = true; break; } } } while (risp); } public void reset() { snake = null; snake = new ArrayList(); x = y = d / 2 + 1; verso = 0; punteggio = 0; snake.add(new Point(x, y)); snake.add(new Point(x, y - 1)); snake.add(new Point(x, y - 2)); creaPunto(); } public void quadro(Graphics g) { g.setColor(Color.red); g.fillRect(tempX * (size / d), tempY * (size / d), size / d, size / d); g.setColor(Color.black); g.drawString(nome + ": " + punteggio + " (Max: " + record + ")", 0, 10); } public boolean snakeCrash() { for (int i = snake.size() - 1; i > 0; i--) { if (x == snake.get(i).x && y == snake.get(i).y) { reset(); return false; } snake.get(i).move(snake.get(i - 1).x, snake.get(i - 1).y); if (i == 1) snake.get(0).move(x, y); } return true; } public boolean puntoPreso() { if(x == tempX && y == tempY) return true; return false; } /* public boolean action(Event evt, Object arg) { if (evt.target instanceof Button) { Button b = (Button) evt.target; if (b == bPlay) { muovi("DOWN"); bPlay.setVisible(false); } } return true; }*/ }