Tuesday, November 30, 2010

Create Game Snake with Netbeans

nakehq package;

import java.awt.Point;

/ **
* Snake class, a representation of a snake
*
* @ Author Haqqi
* /
public class Snake {
//*********************//
//*** Variable area ***//
//*********************//
/ / Coordinates of body
private Point [] body;
/ / Facing
private int face;

/ / Maximum snake's length
private final int MaxLength = 10;

/ / Orientation
public static final int NORTH = 0;
public static final int EAST = 1;
public static final int SOUTH = 2;
public static final int WEST = 3;

/ / Snake pixel size
public static final int SNAKE_SIZE = 10;

//************************//
//*** Constructor ***// area
//************************//
public Snake () {
body = new Point [MaxLength];
for (int i = 0; i <MaxLength; i + +) {
body [i] = new Point (20 - i, 15);
}
face = EAST;
}

//*******************//
//*** Method ***// area
//*******************//
public Point [] getBody () {
return body;
}

public void moveEast () {
if (face! = WEST)
face = EAST;
}

public void moveWest () {
if (face! = EAST)
face = WEST;
}

public void moveNorth () {
if (face! = SOUTH)
face = NORTH;
}

public void moveSouth () {
if (face! = NORTH)
face = SOUTH;
}

public void update () {
/ / Update body
for (int i = body.length - 1; i> 0; i -) {
body [i]. x = body [i-1]. x;
body [i]. y = body [i-1]. y;
}
/ / Update head
Point head = body [0];
if (face == NORTH) {
if (head.y <= 0)
head.y = Panel.AREA_HEIGHT - 1;
else
head.y--;
}
else if (face == EAST) {
if (head.x> = Panel.AREA_WIDTH - 1)
head.x = 0;
else
head.x + +;
}
else if (face == SOUTH) {
if (head.y> = Panel.AREA_HEIGHT - 1)
head.y = 0;
else
head.y + +;
}
else if (face == WEST) {
if (head.x <= 0)
head.x = Panel.AREA_WIDTH - 1;
else
head.x--;
}
}
}

/ *
* DO NOT REMOVE THIS COPYRIGHT
*
* This source code is created by Muhammad Fauzil Haqqi
* You can use and modify this source code but
* You are forbidden to change or remove this license
*
* Nick: Haqqi
* YM: xp_guitarist
* Email: fauzil.haqqi @ gmail.com
* Blog: http://fauzilhaqqi.blogspot.com
* /

snakehq package;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

/ **
* Panel class, the skeleton of the game
*
* @ Author Haqqi
* /
public class Panel extends JPanel {
//*********************//
//*** Variable area ***//
//*********************//
private Snake snake;
private Thread animation;

public static final int PANEL_WIDTH = 400;
public static final int PANEL_HEIGHT = 300;
public static final int AREA_WIDTH =
PANEL_WIDTH / Snake.SNAKE_SIZE;
public static final int AREA_HEIGHT =
PANEL_HEIGHT / Snake.SNAKE_SIZE;

//************************//
//*** Constructor ***// area
//************************//
public Panel () {
setPreferredSize (new Dimension (
PANEL_WIDTH, PANEL_HEIGHT));
snake = new Snake ();
initThread ();
addKeyListener (new KeyManager ());
setFocusable (true);
requestFocusInWindow ();
animation.start ();
}

//*******************//
//*** Method ***// area
//*******************//
@ Override
protected void paintComponent (Graphics g) {
super.paintComponent (g);
Graphics2D g2 = (Graphics2D) g;
g2.addRenderingHints (new RenderingHints (
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON));
drawSnake (g2);
}

/ / Render the snake
private void drawSnake (Graphics2D g2) {
Point [] s = snake.getBody ();
int pixel = Snake.SNAKE_SIZE;
/ / Fill the head
g2.fillOval (s [0]. x * pixel, s [0]. y * pixels,
pixel, pixel);
/ / Draw body outlines
for (int i = 0; i <s.length; i + +) {
g2.drawOval (s [i]. x * pixel, s [i]. y * pixels,
pixel, pixel);
}
}

/ / Initiate animation thread
private void initThread () {
animation = new Thread (new Runnable () {
public void run () {
while (true) {
try {
/ / Sleep a while loop for animation
Thread.sleep (300);
} Catch (InterruptedException ex) {}
snake.update ();
SwingUtilities.invokeLater (
new Runnable () {
public void run () {
repaint ();
}
});
}
}
});
}

/ / Keyboard handler of the game
private class extends KeyManager KeyAdapter {
@ Override
public void keyPressed (KeyEvent e) {
super.keyPressed (e);
int c = e.getKeyCode ();
switch (c) {
KeyEvent.VK_RIGHT case:
snake.moveEast ();
break;
KeyEvent.VK_LEFT case:
snake.moveWest ();
break;
KeyEvent.VK_UP case:
snake.moveNorth ();
break;
KeyEvent.VK_DOWN case:
snake.moveSouth ();
break;
}
}
}

/ **
* Main method Pls That Firstly run the program starts
* @ Param args the command line arguments
* /
public static void main (String [] args) {
/ / TODO code application logic here
SwingUtilities.invokeLater (new Runnable () {
public void run () {
JFrame f = new JFrame ("Snake HQ");
f.setDefaultCloseOperation (
JFrame.EXIT_ON_CLOSE);
f.add (new Panel ());
f.pack ();
f.setResizable (false);
f.setLocationRelativeTo (null);
f.setVisible (true);
}
});
}
}

0 comments:

Post a Comment