Zap! Source
(Last Update 10:15 on Monday, 26 November 2001)
- // Zap! applet
- // Neil Rashbrook
- // Last modified July 15 1998
- import java.awt.*;
- public final class zap extends java.applet.Applet implements Runnable {
-
- final private static int WIDTH = 25, HEIGHT = 15;
- //final private static Color EMPTY = null, ROBOT = Color.cyan, RUBBLE = Color.green, PLAYER = Color.yellow, DEAD = Color.red;
- final private static int EMPTY = 0, RUBBLE = -1, PLAYER = -2, DEAD = -3;
- private int field[][], robotx[], roboty[];
- private int level, x, y, robots, zaps, safe, maxzaps, maxsafe, score;
- private int key = -1;
- private Thread t;
- private String player;
- private final int random(int limit) {
- return (int)(Math.random() * limit) + 1;
- }
- private void newgame() {
- level = 0;
- zaps = 1;
- safe = 1;
- maxzaps = 2;
- maxsafe = 2;
- robots = 0;
- score = 0;
- }
- private void newlevel() {
-
- level++;
- if (safe == maxsafe) safe++;
- if (zaps == maxzaps) {
- zaps++;
- safe++;
- }
- maxzaps = zaps;
- maxsafe = safe;
- field = new int[WIDTH + 2][HEIGHT + 2];
- x = random(WIDTH);
- y = random(HEIGHT);
- for (int i = x - 1; i <= x + 1; i++)
-
- for (int j = y - 1; j <= y + 1; j++)
- field[i][j] = RUBBLE;
- robots = level * 6;
- robotx = new int[robots + 1];
- roboty = new int[robots + 1];
- for (int i = 1; i < robotx.length; i++) {
-
- int x, y;
- do {
- x = random(WIDTH);
- y = random(HEIGHT);
- } while (field[x][y] != EMPTY);
- field[x][y] = i;
- robotx[i] = x;
- roboty[i] = y;
- }
- for (int i = x - 1; i <= x + 1; i++)
-
- for (int j = y - 1; j <= y + 1; j++)
- field[i][j] = EMPTY;
- field[x][y] = PLAYER;
- for (int i = 0; i < WIDTH + 2; i++) {
- field[i][0] = RUBBLE;
- field[i][HEIGHT + 1] = RUBBLE;
- }
- for (int j = 0; j < HEIGHT + 2; j++) {
- field[0][j] = RUBBLE;
- field[WIDTH + 1][j] = RUBBLE;
- }
- repaint();
- }
- public void destroy() {
- t.interrupt();
- }
- public void init() {
-
- player = getParameter("player");
- if (player == null) player = "Anonymous";
- else player = java.net.URLEncoder.encode(player);
- t = new Thread(this, "Zap");
- t.setDaemon(true);
- t.start();
- newgame();
- newlevel();
- //try {
- //requestFocus();
- //} finally {
- //return;
- //}
- }
- public boolean isFocusTraversable() {
- return true;
- }
- public synchronized boolean keyDown(Event evt, int key) {
- this.key = key;
- notifyAll();
- return false;
- }
- public void move() {
-
- int x = this.x, y = this.y, key = this.key;
- this.key = -1;
- if ((robots == 0 || field[x][y] == DEAD) && key != 10 && key != 13) return;
- switch (key) {
-
- case '1': case 1001: y++; x--; break;
- case '2': case 1005: y++; break;
- case '3': case 1003: y++; x++; break;
- case '4': case 1006: x--; break;
- case '5': case ' ': case 0: break;
- case '6': case 1007: x++; break;
- case '7': case 1000: y--; x--; break;
- case '8': case 1004: y--; break;
- case '9': case 1002: y--; x++; break;
- case 'w': case 'W': this.key = key; break;
- case 10: case 13:
- if (field[x][y] == DEAD) newgame();
- if (robots == 0) newlevel();
- return;
- case 't': case 'T': case 20:
-
- if (safe > 0) do {
- x = random(WIDTH);
- y = random(HEIGHT);
- } while (field[x][y] != EMPTY
- || field[x - 1][y - 1] > 0 || field[x - 1][y] > 0
- || field[x - 1][y + 1] > 0 || field[x][y - 1] > 0
- || field[x][y + 1] > 0 || field[x + 1][y - 1] > 0
- || field[x + 1][y] > 0 || field[x + 1][y + 1] > 0);
- else do {
- x = random(WIDTH);
- y = random(HEIGHT);
- } while (field[x][y] != EMPTY);
- if (safe > 0) safe--;
- break;
- case 'z': case 'Z': case 26:
-
- if (zaps > 0) {
-
- zaps--;
- for (int i = x - 1; i <= x + 1; i++)
-
- for (int j = y - 1; j <= y + 1; j++) {
-
- if (field[i][j] > 0) {
- robotx[field[i][j]] = -1;
- field[i][j] = RUBBLE;
- robots--;
- score += level;
- }
- }
- break;
- }
- default: return;
- }
- if (field[x][y] == RUBBLE) return;
- field[this.x][this.y] = EMPTY;
- this.x = x;
- this.y = y;
- if (field[x][y] > 0) field[x][y] = DEAD;
- else {
-
- field[x][y] = PLAYER;
- for (int i = 1; i < robotx.length; i++)
- if (robotx[i] >= 0) field[robotx[i]][roboty[i]] = EMPTY;
- for (int i = 1; i < robotx.length; i++) {
-
- if (robotx[i] >= 0) {
-
- if (robotx[i] > x) robotx[i]--;
- else if (robotx[i] < x) robotx[i]++;
- if (roboty[i] > y) roboty[i]--;
- else if (roboty[i] < y) roboty[i]++;
- switch (field[robotx[i]][roboty[i]]) {
- case PLAYER:
- field[robotx[i]][roboty[i]] = DEAD;
- case DEAD:
- break;
- case EMPTY:
- field[robotx[i]][roboty[i]] = i;
- break;
- case RUBBLE:
- robotx[i] = -1;
- robots--;
- score += level * (this.key == -1 ? 2 : 3);
- break;
- default:
- robotx[field[robotx[i]][roboty[i]]] = -1;
- field[robotx[i]][roboty[i]] = RUBBLE;
- robotx[i] = -1;
- robots -= 2;
- score += level * (this.key == -1 ? 4 : 6);
- break;
- }
- }
- }
- }
- repaint();
- return;
- }
- public synchronized void paint(Graphics g) {
-
- g.setColor(getBackground());
- g.draw3DRect(15, 15, WIDTH * 16 + 1, HEIGHT * 16 + 1, false);
- g.draw3DRect(14, 14, WIDTH * 16 + 3, HEIGHT * 16 + 3, false);
- for (int i = 1; i <= WIDTH; i++)
-
- for (int j = 1; j <= HEIGHT; j++) {
-
- if (field[i][j] != EMPTY) {
-
- g.translate(i * 16, j * 16);
- switch (field[i][j]) {
- case EMPTY: continue;
- case RUBBLE: g.setColor(Color.green); break;
- case PLAYER: g.setColor(Color.yellow); break;
- case DEAD: g.setColor(Color.red); break;
- default: g.setColor(Color.cyan); break;
- }
- g.fillOval(0, 0, 15, 15);
- g.setColor(Color.black);
- g.drawOval(0, 0, 15, 15);
- switch (field[i][j]) {
- case RUBBLE: case DEAD:
- g.fillRect(5, 5, 2, 2);
- g.fillRect(9, 5, 2, 2);
- g.drawLine(5, 12, 7, 10);
- g.drawLine(8, 10, 10, 12);
- break;
- case PLAYER:
-
- if (robots == 0) {
- g.fillRect(4, 5, 4, 2);
- g.fillRect(5, 4, 2, 4);
- g.fillRect(8, 5, 4, 2);
- g.fillRect(9, 4, 2, 4);
- g.drawLine(4, 4, 0, 8);
- g.drawLine(11, 4, 15, 8);
- g.drawLine(5, 10, 7, 12);
- g.drawLine(8, 12, 10, 10);
- } else {
- g.fillRect(5, 5, 2, 2);
- g.fillRect(9, 5, 2, 2);
- g.drawLine(5, 10, 7, 12);
- g.drawLine(8, 12, 10, 10);
- }
- break;
- default:
- g.fillRect(5, 5, 2, 2);
- g.fillRect(9, 5, 2, 2);
- g.drawLine(5, 11, 10, 11);
- break;
- }
- g.translate(i * -16, j * -16);
- }
- }
- g.drawString("Level:" + level + " Score:" + score + " Meanies:" + robots + " Zaps:" + zaps + " Teleports: " + safe, 16, (HEIGHT + 2) * 16 - 2);
- notifyAll();
- }
- public synchronized void run() {
-
- try {
-
- for (;;) {
- wait();
- if (key != -1) move();
- }
- } finally {
- return;
- }
- }
- }

38.107.191.100