This example shows a prototypical snake game developed in java swings
by considering small buttons on the screen as pixels. The mazes are
generated randomly and snake has to travel without touching any maze.
if (!param.startsWith("KEY_PRESSED")) { return true; // ignoring key released
}
switch (ke.getKeyCode()) {
case KeyEvent.VK_LEFT:
System.out.println("left"); if (!("right".equals(direction))) {
direction = "left";
}// end if
return true; case KeyEvent.VK_RIGHT:
System.out.println("right"); if (!("left".equals(direction))) {
direction = "right";
}// end if
return true; case KeyEvent.VK_UP:
System.out.println("up"); if (!("down".equals(direction))) {
direction = "up";
}// end if
return true; case KeyEvent.VK_DOWN:
System.out.println("down"); if (!("up".equals(direction))) {
direction = "down";
}// end if
return true;
}// end switch
return false;
}// end method
});
//Build game panel
buildGamePanel();
//Start snake
startSnake();
}
private void startSnake() {
//the tail pixel is left in white, other are blue for (int i = 0; i < snake_length-1; i++) { this.but[snake[i][0]][snake[i][1]].setBackground(Color.BLUE);
}// end for
}// end sync try {
Thread.sleep(50);
} catch (Exception e) {
e.printStackTrace();
}
}// end while
scoreflag = false;
setWallFlag(false);
}// end run
}; // end thread
snakeThread.start();
}// end method
private void changePosition(String dir) {
int hx = snake[0][0]; int hy = snake[0][1];
//starting the from the while pixel at tail
//move all pixel positions to their previous pixel for (int i = snake_length - 1; i > 0; i--) {
snake[i][0] = snake[i - 1][0];
snake[i][1] = snake[i - 1][1];
}// moving positions to right in array
if ("left".equals(direction)) {
hy--;
}// end if
if ("right".equals(direction)) {
hy++;
}// end if
if ("up".equals(direction)) {
hx--;
}// end if
if ("down".equals(direction)) {
hx++;
}// end if
if (hx < 0)
hx = 63; if (hx > 63)
hx = 0; if (hy < 0)
hy = 63; if (hy > 63)
hy = 0;
snake[0][0] = hx;
snake[0][1] = hy;
}// end method
private void paintSnake(int x, int y, Color c) { this.but[x][y].setBackground(c);
}// end method
private void generateWalls() {
Runnable ra = new Runnable() { public void run() {
Random r = new Random();
Random s = new Random();
int x = 0, y = 0; boolean flag = getWallFlag(); while (flag) {
x = Math.abs(r.nextInt()) % 47 + 2;
y = Math.abs(s.nextInt()) % 53 + 2;
Color wallcol = getRandomWallColor(); for (int i = 0; i < mage_length; i++) {
if (x % 2 == 0) {
Snake.this.but[x][y + i].setBackground(wallcol);
} else {
Snake.this.but[x + i][y].setBackground(wallcol);
}
}// end for
if (x % 2 == 0) {
Snake.this.but[x][y + i].setBackground(Color.WHITE);
} else {
Snake.this.but[x + i][y].setBackground(Color.WHITE);
}
}// end for
flag = getWallFlag();
}// end while
}// end run
}; // end thread
for (int i = 0; i < 16; i++) {
Thread wa = new Thread(ra);
wa.start();
}// end for
}// end method
private Color getRandomWallColor() {
Random r = new Random(); int val = Math.abs(r.nextInt()) % COLORS.length; if (val < COLORS.length) { return COLORS[val];
} return Color.CYAN;
}// end method
//Any other color means collided with wall private boolean gameOver(int x, int y) { if (but[x][y].getBackground().equals(Color.BLUE)
|| but[x][y].getBackground().equals(Color.WHITE)) { return false;
} return true;
}// end method
private void scoreWatcher() {
Thread sc = new Thread() {
Random r = new Random();
public void run() {
while (scoreflag) { int val = Math.abs(r.nextInt()) % 9;
scoreval += val;
score.setText("score :: " + scoreval); try {
Thread.sleep(400);
} catch (Exception e) {
e.printStackTrace();
}
}
}
};// end thread
sc.start();
}// end method
private synchronized boolean getWallFlag() { return wallflag;
}// end method
System.out.println("Going to restart");
startSnake();
}// end method
public static void main(String a[]) {
Snake b = new Snake("BE THE CODER - Snake Game");
b.setSize(450, 380);
b.setLocation(50, 50);
b.setVisible(true);
}// end main