U E D R , A S I H C RSS

Mobile Java Study/Snake Bite/Spec3 Source

상규

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

class SnakeCell {
	public int x;
	public int y;

	public SnakeCell(int x, int y) {
		this.x = x;
		this.y = 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 xRange;
	private final int yRange;

	private Vector cellVector;
	private int headIndex;
	private int direction;

	public Snake(int length, int xRange, int yRange) {
		this.xRange = xRange;
		this.yRange = yRange;

		cellVector = new Vector();

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

		headIndex = 0;
		direction = Snake.RIGHT;
	}

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

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

	public int getDirection() {
		return direction;
	}

	public void setDirection(int direction) {
		this.direction = direction;
	}

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

		head = getSnakeCell(0);
		if(head.x < 0 || head.x > xRange - 1
		|| head.y < 0 || head.y > yRange - 1)
			return false;

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

		return true;
	}

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

		currentHead.x = prevHead.x;
		currentHead.y = prevHead.y;

		if(direction == Snake.LEFT)
			currentHead.x--;
		else if(direction == Snake.RIGHT)
			currentHead.x++;
		else if(direction == Snake.UP)
			currentHead.y--;
		else if(direction == Snake.DOWN)
			currentHead.y++;

		return checkGameOver();
	}
};

class SnakeBiteCanvas extends Canvas implements Runnable {
	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(10, 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.x,
			boardInnerY + snakeCellWidth * cell.y,
			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);
		}
	}

	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 void run() {
		try {
			for(;;) {
				if(!pause && !gameOver) {
					if(snake.go() == false) {
						this.removeCommand(pauseCommand);
						this.addCommand(restartCommand);
						gameOver = true;
					}
					repaint();

					Thread.sleep(100);
				}
			}
		} catch(InterruptedException e) {
			e.printStackTrace();
		}
	}
};

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() {
		new Thread(canvas).start();
		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 implements Runnable {
    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 int currentDirection;
    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));
        }
        
        currentDirection = RIGHT;
        
        isGameOver = false;
    }
    
    public void changeSnakeHeadAndTeil() {
        snakes.addElement((SnakeCell)snakes.elementAt(0));
        snakes.removeElementAt(0);
    }
     
    public void keyPressed(int keyCode) {
        int gameAction = getGameAction(keyCode);
        moveSnake(gameAction);
    }
    
    public void moveSnake(int ga) {
        ((SnakeCell)snakes.elementAt(0)).x = ((SnakeCell)snakes.elementAt(3)).x;
        ((SnakeCell)snakes.elementAt(0)).y = ((SnakeCell)snakes.elementAt(3)).y;
        if(isMoveLeft(ga)) {
            ((SnakeCell)snakes.elementAt(0)).x -= pathLength;
            currentDirection = LEFT;
        } else if(isMoveRight(ga)) {
            ((SnakeCell)snakes.elementAt(0)).x += pathLength;
            currentDirection = RIGHT;
        } else if(isMoveUp(ga)) {
            ((SnakeCell)snakes.elementAt(0)).y -= pathLength;
            currentDirection = UP;
        } else if(isMoveDown(ga)) {
            ((SnakeCell)snakes.elementAt(0)).y += pathLength;
            currentDirection = DOWN;
        }
        changeSnakeHeadAndTeil();
        repaint();
    }
    
    public boolean isMoveUp(int ga) {
        return ga == UP && currentDirection != DOWN && ((SnakeCell)snakes.elementAt(snakeLength - 1)).y > boardY;
    }
    
    public boolean isMoveDown(int ga) {
        return ga == DOWN && currentDirection != UP && ((SnakeCell)snakes.elementAt(snakeLength - 1)).y < boardHeight + boardY - snakeRect * 2;
    }
      
    public boolean isMoveLeft(int ga) {
        return ga == LEFT && currentDirection != RIGHT && ((SnakeCell)snakes.elementAt(snakeLength - 1)).x > boardX;
    }
    
    public boolean isMoveRight(int ga) {
        return ga == RIGHT && currentDirection != LEFT && ((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 void run() {
        while(true) {
        	try {
        		Thread.sleep(100);
        		this.moveSnake(currentDirection);
        		this.repaint();
                } catch (Exception e) {
            }
        }
    }
}

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) {
                new Thread(boardCanvas).start();
            display.setCurrent(boardCanvas);
        }
    }
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:46
Processing time 0.0176 sec