U E D R , A S I H C RSS

Mobile Java Study/Snake Bite/Spec2 Source

상규

~cpp 
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;

class SnakeCell {
	public int snakeCellX;
	public int snakeCellY;

	public SnakeCell(int x, int y) {
		snakeCellX = x;
		snakeCellY = y;
	}
};

class Snake {
	public static final int LEFT = 1;
	public static final int RIGHT = 2;
	public static final int UP = 3;
	public static final int DOWN = 4;

	private final int snakeCellXRange;
	private final int snakeCellYRange;

	private Vector snakeCellVector;
	private int snakeHeadIndex;
	private int snakeDirection;

	public Snake(int length, int xRange, int yRange) {
		snakeCellXRange = xRange;
		snakeCellYRange = yRange;

		snakeCellVector = new Vector();

		for(int i = length ; i >= 1 ; i--)
			snakeCellVector.addElement(new SnakeCell(i, 1));

		snakeHeadIndex = 0;
		snakeDirection = Snake.RIGHT;
	}

	public int length() {
		return snakeCellVector.size();
	}

	public SnakeCell getSnakeCell(int index) {
		return (SnakeCell)snakeCellVector.elementAt((snakeHeadIndex + index) % length());
	}

	public int getDirection() {
		return snakeDirection;
	}

	public void setDirection(int direction) {
		snakeDirection = direction;
	}

	public boolean checkGameOver() {
		SnakeCell head;
		SnakeCell cell;

		head = getSnakeCell(0);
		if(head.snakeCellX < 0 || head.snakeCellY > snakeCellXRange - 1
		|| head.snakeCellY < 0 || head.snakeCellY > snakeCellYRange - 1)
			return false;

		int length = length();
		for(int i = 1 ; i < length ; i++) {
			cell = getSnakeCell(i);
			if(cell.snakeCellX == head.snakeCellX && cell.snakeCellY == head.snakeCellY)
				return false;
		}

		return true;
	}

	public boolean go() {
		SnakeCell prevHead = getSnakeCell(0);
		snakeHeadIndex = (snakeHeadIndex + length() - 1) % length();
		SnakeCell currentHead = getSnakeCell(0);

		currentHead.snakeCellX = prevHead.snakeCellX;
		currentHead.snakeCellY = prevHead.snakeCellY;

		if(snakeDirection == Snake.LEFT)
			currentHead.snakeCellX--;
		else if(snakeDirection == Snake.RIGHT)
			currentHead.snakeCellX++;
		else if(snakeDirection == Snake.UP)
			currentHead.snakeCellY--;
		else if(snakeDirection == Snake.DOWN)
			currentHead.snakeCellY++;

		return checkGameOver();
	}
};

class SnakeBiteCanvas extends Canvas {
	private final int boardWallWidth = 4;
	private final int snakeCellWidth = 4;

	private final int canvasWidth;
	private final int canvasHeight;

	private final int boardX;
	private final int boardY;
	private final int boardWidth;
	private final int boardHeight;

	private final int boardInnerX;
	private final int boardInnerY;
	private final int boardInnerWidth;
	private final int boardInnerHeight;

	private Snake snake;

	private boolean pause;
	private boolean drawAll;
	private boolean printScore;
	private boolean gameOver;

	public Command pauseCommand;
	public Command restartCommand;
	
	public SnakeBiteCanvas() {
		canvasWidth = getWidth();
		canvasHeight = getHeight();

		boardWidth = canvasWidth - 6 - (canvasWidth - 6 - boardWallWidth * 2) % snakeCellWidth;
		boardHeight = boardWidth / 10 * 8 - (boardWidth / 10 * 8 - boardWallWidth * 2) % snakeCellWidth;
		boardX = (canvasWidth - boardWidth) / 2;
		boardY = (canvasHeight - boardHeight) /2;
		
		boardInnerX = boardX + boardWallWidth;
		boardInnerY = boardY + boardWallWidth;
		boardInnerWidth = boardWidth - boardWallWidth * 2;
		boardInnerHeight = boardHeight - boardWallWidth * 2;
		
		snake = new Snake(5, boardInnerWidth / snakeCellWidth, boardInnerHeight / snakeCellWidth);

		pause = true;
		drawAll = true;
		printScore = false;
		gameOver = false;
	}

	public void drawBoard(Graphics g) {
		g.setColor(0xFFFFFF);
		g.fillRect(0, 0, canvasWidth, canvasHeight);

		g.setColor(0x000000);
		g.fillRect(boardX, boardY, boardWidth, boardHeight);
	}

	public void clearBoard(Graphics g) {
		g.setColor(0xFFFFFF);
		g.fillRect(boardInnerX - 1, boardInnerY - 1, boardInnerWidth + 2, boardInnerHeight + 2);
	}

	public void drawSnakeCell(Graphics g, SnakeCell cell) {
		g.fillRect(boardInnerX + snakeCellWidth * cell.snakeCellX,
			boardInnerY + snakeCellWidth * cell.snakeCellY,
			snakeCellWidth, snakeCellWidth);
	}

	public void paint(Graphics g) {
		if(drawAll) {
			drawBoard(g);
			drawAll = false;
		}

		clearBoard(g);

		int length = snake.length();
		SnakeCell cell;
		g.setColor(0x000000);
		for(int i = 0; i < length ; i++) {
			cell = snake.getSnakeCell(i);
			drawSnakeCell(g, cell);
		}

		if(printScore) {
		}

		if(gameOver) {
			g.drawString("Game Over!!", canvasWidth / 2, canvasHeight, Graphics.HCENTER | Graphics.BOTTOM);
		}
	}
	
	public void keyPressed(int keyCode) {
		if(!pause && !gameOver) {
			int gameAction = getGameAction(keyCode);
			if(gameAction == Canvas.LEFT && snake.getDirection() != Snake.RIGHT)
				snake.setDirection(Snake.LEFT);
			else if(gameAction == Canvas.RIGHT && snake.getDirection() != Snake.LEFT)
				snake.setDirection(Snake.RIGHT);
			else if(gameAction == Canvas.UP && snake.getDirection() != Snake.DOWN)
				snake.setDirection(Snake.UP);
			else if(gameAction == Canvas.DOWN && snake.getDirection() != Snake.UP)
				snake.setDirection(Snake.DOWN);
			else
				return;

			if(snake.go() == false) {
				this.removeCommand(pauseCommand);
				this.addCommand(restartCommand);
				gameOver = true;
			}
		
			repaint();
		}
	}

	public void start() {
		pause = false;
	}

	public void pause() {
		pause = true;
	}

	public void restart() {
		snake = new Snake(5, boardInnerWidth / snakeCellWidth, boardInnerHeight / snakeCellWidth);

		pause = true;
		drawAll = true;
		printScore = false;
		gameOver = false;

		repaint();
	}
};

public class SnakeBite extends MIDlet implements CommandListener {
	private Display display;
	private SnakeBiteCanvas canvas;
	private Command startCommand;
	private Command pauseCommand;
	private Command restartCommand;
	private Command exitCommand;
	
	public SnakeBite() {
		display = Display.getDisplay(this);
		canvas = new SnakeBiteCanvas();
		startCommand = new Command("Start", Command.SCREEN, 1);
		pauseCommand = new Command("Pause", Command.SCREEN, 1);
		restartCommand = new Command("Restart", Command.SCREEN, 1);
		exitCommand = new Command("Exit", Command.EXIT, 1);

		canvas.pauseCommand = pauseCommand;
		canvas.restartCommand = restartCommand;
		
		canvas.addCommand(startCommand);
		canvas.addCommand(exitCommand);
		canvas.setCommandListener(this);
	}
	
	public void startApp() {
		display.setCurrent(canvas);
	}
	
	public void pauseApp() {
	}
	
	public void destroyApp(boolean unconditional) {
		display = null;
		canvas = null;
		exitCommand = null;
	}
	
	public void commandAction(Command c, Displayable d) {
		if(c == startCommand) {
			canvas.removeCommand(startCommand);
			canvas.addCommand(pauseCommand);
			canvas.start();
		}
		else if(c == pauseCommand) {
			canvas.removeCommand(pauseCommand);
			canvas.addCommand(startCommand);
			canvas.pause();
		}
		else if(c == restartCommand) {
			canvas.removeCommand(restartCommand);
			canvas.addCommand(startCommand);
			canvas.restart();
		}
		else if(c == exitCommand) {
			destroyApp(true);
			notifyDestroyed();
		}
	}
};

재동

~cpp 
import java.io.*;
import java.util.*;
import javax.microedition.midlet.*; 
import javax.microedition.lcdui.*; 

class StartCanvas extends Canvas { 
    private String startString; 
    private final int BACKGROUND_COLOR = 0xFFFFFF; 
    private final int FOREGROUND_COLOR = 0x000000; 
    private Image image; 
     
    public StartCanvas() { 
        try {
        	image = Image.createImage("/start.png"); 
        } 
        catch(Exception e) {
        	System.out.println("그림이 없음"); 
        } 
    } 
    public void paint(Graphics g) {
    	g.setColor(BACKGROUND_COLOR); 
	    g.fillRect(g.getClipX(),g.getClipY(),g.getClipWidth(),g.getClipHeight()); 
	    g.setColor(FOREGROUND_COLOR); 
	    g.drawImage(image,this.getWidth()/2,this.getHeight()/2,g.HCENTER|g.VCENTER); 
    } 
} 

class SnakeCell {
	public int x;
	public int y;
	public SnakeCell(int x, int y) {
		this.x = x;
		this.y = y;
	}
}

class BoardCanvas extends Canvas { 
    private int snakeHeadX; 
    private int snakeHeadY; 
    private int pathLength; 
    private final int snakeRect;
    private int snakeLength;
    private Vector snakes;
    
    private final int BACKGROUND_COLOR = 0xFFFFFF; 
    private final int FOREGROUND_COLOR = 0x0000FF; 

    private final int boardX; 
    private final int boardY; 
    private final int boardWidth;
    private final int boardHeight;
    
    private boolean isGameOver;
    
    public BoardCanvas() { 
        snakeHeadX = 20; 
        snakeHeadY = 30; 
        pathLength = 4; 
        snakeRect = 4;
        snakeLength = 4;
        
        boardX = 10; 
        boardY = 20; 
        boardWidth = this.getWidth() - boardX * 2; 
        boardHeight = this.getHeight() - boardY;
        
        snakes = new Vector();
        
        for(int i = 0;i < snakeLength;i++) {
			snakes.addElement(new SnakeCell(boardX + (i+1) * snakeRect, boardY + snakeRect));
        }
        
        isGameOver = false;
    }
    
    public void changeSnakeHeadAndTeil() {
    	snakes.addElement((SnakeCell)snakes.elementAt(0));
    	snakes.removeElementAt(0);
    }
    
    public void keyPressed(int keyCode) { 
        int gameAction = getGameAction(keyCode);
        
        ((SnakeCell)snakes.elementAt(0)).x = ((SnakeCell)snakes.elementAt(3)).x;
        ((SnakeCell)snakes.elementAt(0)).y = ((SnakeCell)snakes.elementAt(3)).y;
        
        if(isMoveLeft(gameAction))
            ((SnakeCell)snakes.elementAt(0)).x -= pathLength;
        else if(isMoveRight(gameAction))
            ((SnakeCell)snakes.elementAt(0)).x += pathLength;
        else if(isMoveUp(gameAction))
            ((SnakeCell)snakes.elementAt(0)).y -= pathLength;
        else if(isMoveDown(gameAction)) 
            ((SnakeCell)snakes.elementAt(0)).y += pathLength;
        changeSnakeHeadAndTeil();
        repaint();
    }
    
    public boolean isMoveUp(int ga) {
        return ga == Canvas.UP && ((SnakeCell)snakes.elementAt(snakeLength - 1)).y > boardY;
    }
    
    public boolean isMoveDown(int ga) { 
        return ga == Canvas.DOWN && ((SnakeCell)snakes.elementAt(snakeLength - 1)).y < boardHeight + boardY - snakeRect * 2; 
    } 
     
    public boolean isMoveLeft(int ga) { 
        return ga == Canvas.LEFT && ((SnakeCell)snakes.elementAt(snakeLength - 1)).x > boardX; 
    } 
    public boolean isMoveRight(int ga) { 
        return ga == Canvas.RIGHT && ((SnakeCell)snakes.elementAt(snakeLength - 1)).x < boardWidth + boardX - snakeRect * 2; 
    } 
    
    public void paint(Graphics g) {
	    g.setColor(BACKGROUND_COLOR); 
	    g.fillRect(g.getClipX(),g.getClipY(),g.getClipWidth(),g.getClipHeight()); 
	    g.setColor(FOREGROUND_COLOR); 
	    g.drawRect(boardX-2,boardY-2,boardWidth,boardHeight);
	    
	    for(int i = 0;i < snakeLength;i++) {
	    	g.fillRect(((SnakeCell)snakes.elementAt(i)).x,
	    		((SnakeCell)snakes.elementAt(i)).y, snakeRect, snakeRect);
	    }		
    } 
} 
 
public class SnakeBite extends MIDlet implements CommandListener { 
    private Display display; 
    private BoardCanvas boardCanvas; 
    private StartCanvas startCanvas; 
     
    private Command exitCommand; 
    private Command startCommand; 
     
    public SnakeBite() { 
        display = Display.getDisplay(this); 
         
        exitCommand = new Command("Exit", Command.EXIT, 1); 
        startCommand = new Command("Start", Command.SCREEN, 2); 
         
        startCanvas = new StartCanvas(); 
        boardCanvas = new BoardCanvas(); 
         
        startCanvas.addCommand(startCommand); 
        startCanvas.addCommand(exitCommand); 
        startCanvas.setCommandListener(this); 
         
        boardCanvas.addCommand(exitCommand); 
        boardCanvas.setCommandListener(this); 
    } 
    public void startApp() {
        display.setCurrent(startCanvas); 
    } 
    public void pauseApp() { 
    } 
    public void destroyApp(boolean unconditional) { 
        display = null; 
        boardCanvas = null; 
        startCanvas = null; 
        exitCommand = null; 
        startCommand = null; 
    }
    
    public void commandAction(Command c, Displayable d) { 
        if(c == exitCommand) { 
            destroyApp(true); 
            notifyDestroyed(); 
        } 
        else if(c == startCommand) { 
            display.setCurrent(boardCanvas); 
        } 
    } 
} 
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2009-05-27 07:09:19
Processing time 0.0939 sec