View Game: ApoBlockLock4k

ApoBlockLock4k (http://www.apo-games.de/apoBlockLock4k/)

By ApoGames, submitted on Dec 2, 2012
Min JRE version: 1.6
2015 downloads.
This game was submitted in the Java4K 2013 Competition.
Source code available! Show source code

(Click on game for focus)

Sorry, your Browser doesn't support Applets
You need Java installed to play this game.
You may use the button below to get it.

Get Java


Description

ApoBlockLock4k is based on the sliding block puzzle "Rushhour" invented by Nob Yoshigahara.

Instructions

Play with the mouse.

The goal of the game is to get a blue block out of a the grid by dragging the other blocks out of its way with the mouse.

User Comments

No comments have been made.

You need be logged in to write comments. If you need to register click here.

Source code

Note: We recommend you copy+paste this to a better editor. Some special character encoding may have been lost.

* All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ import java.applet.Applet; import java.awt.Color; import java.awt.Event; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; public class C extends Applet implements Runnable { private final String[] levels = new String[] { "000000"+ "000000"+ "100000"+ "000000"+ "000000"+ "000000", "400003"+ "300300"+ "010000"+ "000000"+ "200040"+ "005000", "000300"+ "000000"+ "210000"+ "025000"+ "002400"+ "400000", "000000"+ "000000"+ "010300"+ "040003"+ "020000"+ "004000", "200500"+ "000003"+ "100020"+ "500000"+ "002040"+ "400400", "300300"+ "000000"+ "010000"+ "002500"+ "000002"+ "005000", "400302"+ "300030"+ "010002"+ "050000"+ "200040"+ "000040", "400200"+ "400033"+ "010300"+ "402000"+ "200000"+ "000500", "024022"+ "000200"+ "010002"+ "004000"+ "000200"+ "000000", "004040"+ "000203"+ "102020"+ "330000"+ "004040"+ "000400", "000403"+ "004020"+ "102200"+ "400040"+ "402500"+ "400500", "024040"+ "000240"+ "100032"+ "350000"+ "002002"+ "000000", "050022"+ "020000"+ "003102"+ "400000"+ "200240"+ "040000", "402040"+ "400003"+ "310000"+ "050000"+ "000240"+ "400040", "200500"+ "040200"+ "102003"+ "000000"+ "002400"+ "000500", "240003"+ "003000"+ "100000"+ "000500"+ "000020"+ "500000", "404020"+ "002003"+ "020100"+ "300400"+ "000240"+ "040040", "402000"+ "000040"+ "221022"+ "004000"+ "002040"+ "400000", "040400"+ "404033"+ "331000"+ "002200"+ "000040"+ "040400", "404023"+ "204000"+ "023100"+ "000500"+ "000000"+ "400000", "250000"+ "004040"+ "102000"+ "400300"+ "500022"+ "500000", "402300"+ "400000"+ "310000"+ "050000"+ "004000"+ "500000", "002400"+ "000020"+ "021000"+ "004020"+ "050000"+ "000000", "340300"+ "000000"+ "010000"+ "002500"+ "000002"+ "005000", "402300"+ "300000"+ "010000"+ "050000"+ "000000"+ "000500", "002500"+ "200340"+ "010000"+ "020040"+ "204002"+ "050000", "005003"+ "002400"+ "000100"+ "002240"+ "000040"+ "005000", "002400"+ "020000"+ "201020"+ "040000"+ "500020"+ "400000", "402040"+ "400003"+ "310020"+ "050000"+ "020240"+ "000040", "020500"+ "200230"+ "010002"+ "250000"+ "002002"+ "000400", }; /** * p[0] == X-Wert Maus * p[1] == Y-Wert Maus * p[2] == dragged X-Wert Maus * p[3] == dragged Y-Wert Maus * p[4] == pressed X-Wert Maus * p[5] == pressed Y-Wert Maus * p[6] == aktuelles Level * p[7] == aktuelle Moveanzahl * p[8] == change X Mausbewegung * p[9] == change Y Mausbewegung * p[10] == welche Entität X angeklickt * p[11] == welche Entität Y angeklickt * p[12] == Maus gedrückt * p[13] == Maus losgelassen */ private final int[] p = new int[14]; private final int[][] level = new int[6][6]; public void start() { new Thread(this).start(); } public void run() { // setSize(400, 400); // für den AppletViewer BufferedImage background = new BufferedImage(400,400,BufferedImage.TYPE_INT_RGB); Graphics2D g = background.createGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, 400, 400); g.setColor(new Color(120, 120, 120, 180)); for (int x = 50; x < background.getWidth() - 50; x++) { if (Math.random() * 100 > 30) { g.fillOval(x + (int)(Math.random() * 3), 50 + (int)(Math.random() * 3), (int)(Math.random()*4 + 1), (int)(Math.random()*4 + 1)); } if (Math.random() * 100 > 30) { g.fillOval(x + (int)(Math.random() * 3), background.getHeight() - 50 - 5 + (int)(Math.random() * 3), (int)(Math.random()*4 + 1), (int)(Math.random()*4 + 1)); } } for (int y = 50; y < background.getHeight() - 50; y++) { if (Math.random() * 100 > 30) { g.fillOval(50 + (int)(Math.random() * 3), y + (int)(Math.random() * 3), (int)(Math.random()*4 + 1), (int)(Math.random()*4 + 1)); } if (Math.random() * 100 > 30) { g.fillOval(background.getWidth() - 50 - 5 + (int)(Math.random() * 3), y + (int)(Math.random() * 3), (int)(Math.random()*4 + 1), (int)(Math.random()*4 + 1)); } } g.setColor(Color.WHITE); g.fillRect(300, 150, 100, 50); g.setColor(new Color(120, 120, 120, 180)); for (int x = 350; x < background.getWidth(); x++) { if (Math.random() * 100 > 30) { g.fillOval(x + (int)(Math.random() * 3), 148 + (int)(Math.random() * 3), (int)(Math.random()*4 + 1), (int)(Math.random()*4 + 1)); } if (Math.random() * 100 > 30) { g.fillOval(x + (int)(Math.random() * 3), 198 + (int)(Math.random() * 3), (int)(Math.random()*4 + 1), (int)(Math.random()*4 + 1)); } } for (int x = 110; x < background.getWidth() - 110; x++) { if (Math.random() * 100 > 30) { g.fillOval(x + (int)(Math.random() * 3), 365 + (int)(Math.random() * 3), (int)(Math.random()*4 + 1), (int)(Math.random()*4 + 1)); g.fillOval(x + (int)(Math.random() * 3), 395 + (int)(Math.random() * 3), (int)(Math.random()*4 + 1), (int)(Math.random()*4 + 1)); } } for (int y = 365; y < background.getHeight(); y++) { if (Math.random() * 100 > 30) { g.fillOval(110 + (int)(Math.random() * 3), y + (int)(Math.random() * 3), (int)(Math.random()*4 + 1), (int)(Math.random()*4 + 1)); g.fillOval(290 + (int)(Math.random() * 3), y + (int)(Math.random() * 3), (int)(Math.random()*4 + 1), (int)(Math.random()*4 + 1)); g.fillOval(240 + (int)(Math.random() * 3), y + (int)(Math.random() * 3), (int)(Math.random()*4 + 1), (int)(Math.random()*4 + 1)); g.fillOval(160 + (int)(Math.random() * 3), y + (int)(Math.random() * 3), (int)(Math.random()*4 + 1), (int)(Math.random()*4 + 1)); } } BufferedImage[] images = new BufferedImage[9]; images[0] = new BufferedImage(100,50,BufferedImage.TYPE_INT_ARGB); images[1] = new BufferedImage(50,100,BufferedImage.TYPE_INT_ARGB); images[2] = new BufferedImage(50,150,BufferedImage.TYPE_INT_ARGB); images[3] = new BufferedImage(100,50,BufferedImage.TYPE_INT_ARGB); images[4] = new BufferedImage(150,50,BufferedImage.TYPE_INT_ARGB); for (int i = 0; i < 5; i++) { g = images[i].createGraphics(); g.setColor(new Color(215,215,215)); if (i == 0) { g.setColor(new Color(128,140,159)); } else if ((i == 1) || (i == 3)) { g.setColor(new Color(180,180,180)); } g.fillRect(2, 2, images[i].getWidth() - 5, images[i].getHeight() - 5); g.setColor(new Color(120, 120, 120, 180)); for (int x = 0; x < images[i].getWidth(); x++) { if (Math.random() * 100 > 30) { g.fillOval(x + (int)(Math.random() * 3), 0 + (int)(Math.random() * 3), (int)(Math.random()*3 + 1), (int)(Math.random()*3 + 1)); } if (Math.random() * 100 > 30) { g.fillOval(x + (int)(Math.random() * 3), images[i].getHeight() - 5 + (int)(Math.random() * 3), (int)(Math.random()*3 + 1), (int)(Math.random()*3 + 1)); } } for (int y = 0; y < images[i].getHeight(); y++) { if (Math.random() * 100 > 30) { g.fillOval(0 + (int)(Math.random() * 3), y + (int)(Math.random() * 3), (int)(Math.random()*3 + 1), (int)(Math.random()*3 + 1)); } if (Math.random() * 100 > 30) { g.fillOval(images[i].getWidth() - 5 + (int)(Math.random() * 3), y + (int)(Math.random() * 3), (int)(Math.random()*3 + 1), (int)(Math.random()*3 + 1)); } } } images[5] = new BufferedImage(40,20,BufferedImage.TYPE_INT_ARGB); images[6] = new BufferedImage(40,20,BufferedImage.TYPE_INT_ARGB); images[7] = new BufferedImage(40,20,BufferedImage.TYPE_INT_ARGB); images[8] = new BufferedImage(40,20,BufferedImage.TYPE_INT_ARGB); for (int i = 5; i < 9; i++) { g = images[i].createGraphics(); g.setColor(new Color(120, 120, 120, 180)); if ((i == 6) || (i == 8)) { g.setColor(new Color(200, 200, 0, 180)); } for (int x = 0; x < images[6].getWidth() - 4; x++) { if (Math.random() * 100 > 20) { if (i <= 6) { g.fillOval(x + (int)(Math.random() * 1), x/2 + images[6].getHeight()/2 + (int)(Math.random() * 1), (int)(Math.random()*2 + 1), (int)(Math.random()*2 + 1)); g.fillOval(x + (int)(Math.random() * 1), -x/2 + images[6].getHeight()/2 + (int)(Math.random() * 1), (int)(Math.random()*2 + 1), (int)(Math.random()*2 + 1)); } else { g.fillOval(images[6].getWidth() - 4 - x + (int)(Math.random() * 1), x/2 + images[6].getHeight()/2 + (int)(Math.random() * 1), (int)(Math.random()*2 + 1), (int)(Math.random()*2 + 1)); g.fillOval(images[6].getWidth() - 4 - x + (int)(Math.random() * 1), -x/2 + images[6].getHeight()/2 + (int)(Math.random() * 1), (int)(Math.random()*2 + 1), (int)(Math.random()*2 + 1)); } } } } // Graphische Grundlagen für das Double Buffering BufferedImage screen = new BufferedImage(400,400,BufferedImage.TYPE_INT_RGB); g = screen.createGraphics(); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Graphics2D appletGraphics = (Graphics2D)getGraphics(); // Variablen zum Zeitmessen und genau Timen wann geupdatet werden soll long lastTime = System.nanoTime(); long think = lastTime; this.level[0][0] = -1; p[10] = -1; // Game loop. while (true) { long now = System.nanoTime(); long delta = now - lastTime; think += delta; // Update / think // Wenn 10 ms vergangen sind, dann denke nach while (think >= 10000000L) { think -= 10000000L; if (this.level[0][0] == -1) { if (p[6] >= levels.length) { p[6] = 0; } if (p[6] < 0) { p[6] = levels.length - 1; } String l = levels[p[6]]; for (int y = 0; y < 6; y++) { for (int x = 0; x < 6; x++) { this.level[y][x] = Integer.valueOf(l.substring(y * 6 + x, y * 6 + x + 1)); } } p[7] = 0; p[13] = 0; p[10] = -1; p[11] = -1; p[4] = -1; p[5] = -1; } else { if ((p[13] > 0) && (p[10] >= 0)) { int newX = p[10]; int newY = p[11]; if (p[8] != 0) { newX = p[10] * 50 + p[8]; newX = (newX + 25)/50; } else { newY = p[11] * 50 + p[9]; newY = (newY + 25)/50; } p[13] = 0; if ((p[10] != newX) || (p[11] != newY)) { p[7] += 1; level[newY][newX] = level[p[11]][p[10]]; level[p[11]][p[10]] = 0; p[10] = -1; p[11] = -1; p[4] = -1; p[5] = -1; if (level[2][5] == 1) { p[6] += 1; level[0][0] = -1; think += 10000000L; } } } else if ((p[4] > 110) && (p[4] < 160) && (p[5] > 365) && (p[5] < 395)) { p[6] -= 1; level[0][0] = -1; think += 10000000L; p[4] = -1; p[5] = -1; } else if ((p[4] > 240) && (p[4] < 290) && (p[5] > 365) && (p[5] < 395)) { p[6] += 1; level[0][0] = -1; think += 10000000L; p[4] = -1; p[5] = -1; } else if ((p[4] > 160) && (p[4] < 240) && (p[5] > 360) && (p[5] < 395)) { level[0][0] = -1; think += 10000000L; p[4] = -1; p[5] = -1; } else if ((p[4] > 50) && (p[4] < 350) && (p[5] > 50) && (p[5] < 350)) { boolean[][] bFree = new boolean[6][6]; int xV = (p[4] - 50) / 50; int yV = (p[5] - 50) / 50; for (int y = 0; y < 6; y++) { for (int x = 0; x < 6; x++) { if (level[y][x] != 0) { if (level[y][x] == 1) { if ((y == yV) && ((x == xV) || (x + 1 == xV))) { xV = x; yV = y; } bFree[y][x] = true; bFree[y][x+1] = true; } else if (level[y][x] == 4) { if ((y == yV) && ((x == xV) || (x + 1 == xV))) { xV = x; yV = y; } bFree[y][x] = true; bFree[y][x+1] = true; } else if (level[y][x] == 5) { if ((y == yV) && ((x == xV) || (x + 1 == xV) || (x + 2 == xV))) { xV = x; yV = y; } bFree[y][x] = true; bFree[y][x+1] = true; bFree[y][x+2] = true; } else if (level[y][x] == 2) { if ((x == xV) && ((y == yV) || (y + 1 == yV))) { xV = x; yV = y; } bFree[y][x] = true; bFree[y+1][x] = true; } else if (level[y][x] == 3) { if ((x == xV) && ((y == yV) || (y + 1 == yV) || (y + 2 == yV))) { xV = x; yV = y; } bFree[y][x] = true; bFree[y+1][x] = true; bFree[y+2][x] = true; } } } } p[10] = xV; p[11] = yV; p[8] = 0; p[9] = 0; if ((p[2] > 0) && (p[2] < 400) && (p[3] > 0) && (p[3] < 400)) { int changeDraggedX = p[2] - xV * 50 - 50; int changeDraggedY = p[3] - yV * 50 - 50; int changePressedX = p[4] - xV * 50 - 50; int changePressedY = p[5] - yV * 50 - 50; if ((level[yV][xV] == 2) || (level[yV][xV] == 3)) { int size = 2; if (level[yV][xV] == 3) { size = 3; } int min = 0; int max = 350; if (changeDraggedY < changePressedY) { for (int i = yV - 1; i >= 0; i--) { if (bFree[i][xV]) { min = (i + 1) * 50; break; } } } else { for (int i = yV + size; i < 6; i++) { if (bFree[i][xV]) { max = (i + 1) * 50; break; } } } if ((yV * 50 + changeDraggedY - changePressedY > min) && ((yV + size + 1) * 50 + (changeDraggedY - changePressedY) < max)) { p[9] = p[3] - p[5]; } else if (yV * 50 + changeDraggedY - changePressedY <= min) { p[9] = -yV * 50 + min; } else if ((yV + size + 1) * 50 + (changeDraggedY - changePressedY) >= max) { p[9] = max - (yV + size + 1) * 50; } } else if (level[yV][xV] > 0) { int size = 2; if (level[yV][xV] == 5) { size = 3; } int min = 0; int max = 350; if (level[yV][xV] == 1) { max = 400; } if (changeDraggedX < changePressedX) { for (int i = xV - 1; i >= 0; i--) { if (bFree[yV][i]) { min = (i + 1) * 50; break; } } } else { for (int i = xV + size; i < 6; i++) { if (bFree[yV][i]) { max = (i + 1) * 50; break; } } } if ((xV * 50 + changeDraggedX - changePressedX > min) && ((xV + size + 1) * 50 + (changeDraggedX - changePressedX) < max)) { p[8] = p[2] - p[4]; } else if (xV * 50 + changeDraggedX - changePressedX <= min) { p[8] = -xV * 50 + min; } else if ((xV + size + 1) * 50 + (changeDraggedX - changePressedX) >= max) { p[8] = max - (xV + size + 1) * 50; } } } } else { p[10] = -1; p[11] = -1; } } p[12] = 0; p[13] = 0; } lastTime = now; // Renderabschnitt // Hintergrund malen g.drawImage(background, 0, 0, null); for (int y = 0; y < 6; y++) { for (int x = 0; x < 6; x++) { if (level[y][x] > 0) { int changeX = 0; int changeY = 0; if ((x == p[10]) && (y == p[11])) { changeX = p[8]; changeY = p[9]; } g.drawImage(images[level[y][x] - 1], 50 + x * 50 + changeX, y * 50 + 50 + changeY, null); } } } g.setColor(new Color(120, 120, 120)); g.setFont(g.getFont().deriveFont(20f).deriveFont(1)); String s = "ApoBlockLock4k"; int w = g.getFontMetrics().stringWidth(s); g.drawString(s, 200 - w/2, 40); try { g.setFont(g.getFont().deriveFont(15f).deriveFont(1)); s = "level: "+String.valueOf((int)(p[6] + 1))+" / "+String.valueOf(levels.length); g.drawString(s, 10, 390); s = "moves: "+String.valueOf((int)(p[7])); w = g.getFontMetrics().stringWidth(s); g.drawString(s, 390 - w, 390); } catch (Exception ex) {} if ((p[0] > 160) && (p[0] < 240) && (p[1] > 365) && (p[1] < 395)) { g.setColor(Color.YELLOW.darker()); } s = "reset"; w = g.getFontMetrics().stringWidth(s); g.drawString(s, 200 - w/2, 388); if ((p[0] > 110) && (p[0] < 160) && (p[1] > 365) && (p[1] < 395)) { g.drawImage(images[6], 123, 373, null); } else { g.drawImage(images[5], 123, 373, null); } if ((p[0] > 240) && (p[0] < 290) && (p[1] > 365) && (p[1] < 395)) { g.drawImage(images[8], 245, 373, null); } else { g.drawImage(images[7], 245, 373, null); } // Render das Ganze auf den Bildschirm appletGraphics.drawImage(screen, 0, 0, null); try { Thread.sleep(10); } catch (Exception e) { /** nicht schön aber selten */ } ; if (!isActive()) { return; } } } public boolean handleEvent(Event e) { switch (e.id) { case Event.KEY_PRESS: break; case Event.KEY_ACTION: case Event.KEY_RELEASE: break; case Event.MOUSE_DOWN: // mouse button pressed p[2] = e.x; p[3] = e.y; p[4] = e.x; p[5] = e.y; p[12] = 1; p[13] = 0; break; case Event.MOUSE_UP: // mouse button released p[12] = 0; p[13] = 1; p[4] = -1; p[5] = -1; break; case Event.MOUSE_MOVE: p[0] = e.x; p[1] = e.y; break; case Event.MOUSE_DRAG: p[2] = e.x; p[3] = e.y; break; case Event.MOUSE_ENTER: case Event.MOUSE_EXIT: p[2] = e.x; p[3] = e.y; p[12] = 0; p[13] = 1; p[4] = -1; p[5] = -1; break; } return false; } }