U E D R , A S I H C RSS

Mobile Java Study/Snake Bite/Final Source

상규

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

class SplashCanvas extends Canvas {
	public void paint(Graphics g) {
		g.setColor(0xFFFFFF);
		g.fillRect(0, 0, getWidth(), getHeight());

		try {
			Image splashImage = Image.createImage("/Splash.png");
			g.drawImage(splashImage, getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
		} catch(IOException e) {
			e.printStackTrace();
		}
	}
};

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;
	
	private boolean growing;

	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;
		
		growing = false;
	}

	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(head.x == cell.x && head.y == cell.y)
				return false;
		}

		return true;
	}

	public boolean go() {
		SnakeCell prevHead = getSnakeCell(0);
		SnakeCell currentHead;
		if(growing) {
			currentHead = new SnakeCell(prevHead.x, prevHead.y);
			cellVector.insertElementAt(currentHead, headIndex);

			growing = false;
		}
		else {
			headIndex = (headIndex + length() - 1) % length();
			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();
	}
	
	public void grow() {
		growing = true;
	}
};

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 final int snakeXRange;
	private final int snakeYRange;

	private Snake snake;
	
	private int appleX;
	private int appleY;
	
	private int level;

	private boolean pause;
	private boolean drawBoard;
	private boolean printLevel;
	private boolean gameOver;

	private int delayTime;

	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;
		
		snakeXRange = boardInnerWidth / snakeCellWidth;
		snakeYRange = boardInnerHeight / snakeCellWidth;

		newGame();
	}

	public void newGame() {
		snake = new Snake(5, snakeXRange, snakeYRange);
		
		newApple();
		
		level = 1;

		pause = true;
		drawBoard = true;
		printLevel = true;
		gameOver = false;

		delayTime = 200;

		new Thread(this).start();
	}
	
	public void newApple() {
		Random random = new Random();
		int length = snake.length();
		SnakeCell cell;
		boolean ok;
		for(;;) {
			appleX = Math.abs(random.nextInt()) % snakeXRange;
			appleY = Math.abs(random.nextInt()) % snakeYRange;

			ok = true;
			for(int i = 0 ; i < length ; i++) {
				cell = snake.getSnakeCell(i);
				if(appleX == cell.x && appleY == cell.y) {
					ok = false;
					break;
				}
			}
			if(ok)
				return;
		}
	}

	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 drawCell(Graphics g, int x, int y) {
		g.fillRect(boardInnerX + snakeCellWidth * x,
			boardInnerY + snakeCellWidth * y,
			snakeCellWidth, snakeCellWidth);
	}

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

		clearBoard(g);

		g.setColor(0x000000);
		
		int length = snake.length();
		SnakeCell cell;
		for(int i = 0; i < length ; i++) {
			cell = snake.getSnakeCell(i);
			drawCell(g, cell.x, cell.y);
		}
		
		drawCell(g, appleX, appleY);

		if(printLevel) {
			g.setColor(0xFFFFFF);
			g.fillRect(0, 0, canvasWidth, boardY);

			g.setColor(0x000000);
			g.drawString("Level : " + level, canvasWidth / 2, 0, Graphics.HCENTER | Graphics.TOP);

			printLevel = false;
		}

		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);
			int direction = snake.getDirection();
			if(gameAction == Canvas.LEFT && direction != Snake.RIGHT)
				snake.setDirection(Snake.LEFT);
			else if(gameAction == Canvas.RIGHT && direction != Snake.LEFT)
				snake.setDirection(Snake.RIGHT);
			else if(gameAction == Canvas.UP && direction != Snake.DOWN)
				snake.setDirection(Snake.UP);
			else if(gameAction == Canvas.DOWN && direction != Snake.UP)
				snake.setDirection(Snake.DOWN);
		}
	}

	public void start() {
		pause = false;
	}

	public void pause() {
		pause = true;
	}

	public void restart() {
		newGame();
		repaint();
	}

	public void run() {
		SnakeCell head;
		try {
			for(;;) {
				if(!pause) {
					if(snake.go() == false) {
						gameOver = true;
						repaint();

						this.removeCommand(pauseCommand);
						this.addCommand(restartCommand);

						return;
					}
					
					head = snake.getSnakeCell(0);
					if(head.x == appleX && head.y == appleY) {
						level++;
						printLevel = true;

						delayTime-=3;

						snake.grow();
						newApple();
					}
					
					repaint();

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

public class SnakeBite extends MIDlet implements CommandListener {
	private Display display;

	private SplashCanvas splashCanvas;
	private SnakeBiteCanvas snakeBiteCanvas;

	private Command continueCommand;
	private Command startCommand;
	private Command pauseCommand;
	private Command restartCommand;
	private Command exitCommand;
	
	public SnakeBite() {
		display = Display.getDisplay(this);

		splashCanvas = new SplashCanvas();
		snakeBiteCanvas = new SnakeBiteCanvas();

		continueCommand = new Command("Continue", Command.SCREEN, 1);
		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);

		splashCanvas.addCommand(continueCommand);
		splashCanvas.addCommand(exitCommand);
		splashCanvas.setCommandListener(this);

		snakeBiteCanvas.pauseCommand = pauseCommand;
		snakeBiteCanvas.restartCommand = restartCommand;
		
		snakeBiteCanvas.addCommand(startCommand);
		snakeBiteCanvas.addCommand(exitCommand);
		snakeBiteCanvas.setCommandListener(this);
	}
	
	public void startApp() {
		display.setCurrent(splashCanvas);
	}
	
	public void pauseApp() {
	}
	
	public void destroyApp(boolean unconditional) {
		display = null;
		snakeBiteCanvas = null;
		startCommand = null;
		pauseCommand = null;
		restartCommand = null;
		exitCommand = null;
	}
	
	public void commandAction(Command c, Displayable d) {
		if(c == continueCommand) {
			display.setCurrent(snakeBiteCanvas);
		}
		else if(c == startCommand) {
			snakeBiteCanvas.removeCommand(startCommand);
			snakeBiteCanvas.addCommand(pauseCommand);

			snakeBiteCanvas.start();
		}
		else if(c == pauseCommand) {
			snakeBiteCanvas.removeCommand(pauseCommand);
			snakeBiteCanvas.addCommand(startCommand);

			snakeBiteCanvas.pause();
		}
		else if(c == restartCommand) {
			snakeBiteCanvas.removeCommand(restartCommand);
			snakeBiteCanvas.addCommand(startCommand);

			snakeBiteCanvas.restart();
		}
		else if(c == exitCommand) {
			destroyApp(true);
			notifyDestroyed();
		}
	}
};

재동

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

class StartCanvas extends Canvas {
    private String startString;
    private final int BACKGROUND_COLOR = 0xFFFFFF;
    private Image image;
    
    public StartCanvas() {
        try {
        	image = Image.createImage("/start.png");
        }
        catch(Exception e) {
        	System.out.println("No Image");
        }
    }
    
    public void paint(Graphics g) {
        g.setColor(BACKGROUND_COLOR);
        g.fillRect(g.getClipX(),g.getClipY(),g.getClipWidth(),g.getClipHeight());
        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 final int cellRect;
    private int snakeLength;
    private int currentDirection;
    private Vector snakes;
    
    private Random random;
    private int appleX;
    private int appleY;
    private int score;
    private int gameSpeed;    
    
    private final int boardX;
    private final int boardY;
    private int boardWidth;
    private int boardHeight;
    private int boardWall;
    
    private boolean isGameOver;
    private Command restartCommand;
    
    private final int BACKGROUND_COLOR = 0xFFFFFF;
    private final int WALL_COLOR = 0x000000;
    private final int SNAKE_COLOR = 0x0000FF;
    private final int APPLE_COLOR = 0xFF0000;
    
    public BoardCanvas() {
        cellRect = 4;  
        boardX = 10;
        boardY = 20;
        boardWidth = this.getWidth() - boardX * 2;
        boardHeight = this.getHeight() - boardY - 2;
		boardWall = 2;	
        snakes = new Vector();
        random = new Random();
        newGame();
    }
    
    public void newGame() {
    	currentDirection = RIGHT;
    	score = 0;
        gameSpeed = 50;
    	newApple();
        isGameOver = false;
        
        snakes.removeAllElements();
        snakeLength = 4;
        for(int i = 0;i < snakeLength;i++) {
        	snakes.addElement(new SnakeCell(boardX + (i+1) * cellRect, boardY + cellRect));
        }
    }
    
    public void newApple() {
        appleX = (Math.abs(random.nextInt()) % ((boardWidth-1) / cellRect)) * cellRect + boardX;
        appleY = (Math.abs(random.nextInt()) % ((boardHeight-1) / cellRect)) * cellRect + boardY;
	}
	
	public void growSnake() {
		snakeLength++;
		snakes.insertElementAt(new SnakeCell(((SnakeCell)snakes.elementAt(0)).x,((SnakeCell)snakes.elementAt(0)).y),0);
	}
	
    public void chaeckEatApple() {
    	if(appleX == ((SnakeCell)snakes.elementAt(snakeLength - 1)).x && appleY == ((SnakeCell)snakes.elementAt(snakeLength - 1)).y) {
    		newApple();
    		score += 10;
    		if(score % 3 == 0) {
    			growSnake();
    			if(gameSpeed > 10)
    				gameSpeed -= 2;
    		}
    	}	
    }
    
    public void changeSnakeHeadToTeil() {
        snakes.addElement((SnakeCell)snakes.elementAt(0));
        snakes.removeElementAt(0);
    }
     
    public void keyPressed(int keyCode) {
    	if(isGameOver == false) {
	        int gameAction = getGameAction(keyCode);
	        moveSnake(gameAction);
		}
    }
    
    public void moveSnakeHeadToTail() {
    	((SnakeCell)snakes.elementAt(0)).x = ((SnakeCell)snakes.elementAt(snakeLength - 1)).x;
        ((SnakeCell)snakes.elementAt(0)).y = ((SnakeCell)snakes.elementAt(snakeLength - 1)).y;
    }
    
    public void moveSnake(int ga) {
        moveSnakeHeadToTail();
        if(isMoveLeft(ga)) {
            ((SnakeCell)snakes.elementAt(0)).x -= cellRect;
            currentDirection = LEFT;
        } else if(isMoveRight(ga)) {
            ((SnakeCell)snakes.elementAt(0)).x += cellRect;
            currentDirection = RIGHT;
        } else if(isMoveUp(ga)) {
            ((SnakeCell)snakes.elementAt(0)).y -= cellRect;
            currentDirection = UP;
        } else if(isMoveDown(ga)) {
            ((SnakeCell)snakes.elementAt(0)).y += cellRect;
            currentDirection = DOWN;
        }
        changeSnakeHeadToTeil();
        chaeckEatApple();
        repaint();
    }
    
    public boolean isMoveUp(int ga) {
    	if (((SnakeCell)snakes.elementAt(0)).y < boardY - 1) {
    		isGameOver = true;
    	}
        return ga == UP && currentDirection != DOWN && ((SnakeCell)snakes.elementAt(0)).y >= boardY;
    }
    
    public boolean isMoveDown(int ga) {
    	if (((SnakeCell)snakes.elementAt(0)).y > boardHeight + boardY - cellRect * 2 + 1) {
    		isGameOver = true;
    	}
        return ga == DOWN && currentDirection != UP && ((SnakeCell)snakes.elementAt(0)).y <= boardHeight + boardY - cellRect * 2;
    }
    
    public boolean isMoveLeft(int ga) {
    	if (((SnakeCell)snakes.elementAt(0)).x < boardX - 1) {
    		isGameOver = true;
    	}
        return ga == LEFT && currentDirection != RIGHT && ((SnakeCell)snakes.elementAt(0)).x >= boardX;
    }
    
    public boolean isMoveRight(int ga) {
    	if (((SnakeCell)snakes.elementAt(0)).x > boardWidth + boardX - cellRect + 1) {
    		isGameOver = true;
    	}
        return ga == RIGHT && currentDirection != LEFT && ((SnakeCell)snakes.elementAt(0)).x <= boardWidth + boardX - cellRect;
    }
     
    public void paint(Graphics g) {
    	cleanBackGround(g);
    	paintWall(g);
    	paintScore(g);
        paintSnake(g);
        paintApplae(g);
        
        if(isGameOver == true) {
        	g.drawString("Game Over", getWidth()/2, getHeight()/2, Graphics.TOP|Graphics.HCENTER);
        }
	}
	
	public void cleanBackGround(Graphics g) {
		g.setColor(BACKGROUND_COLOR);
        g.fillRect(g.getClipX(),g.getClipY(),g.getClipWidth(),g.getClipHeight());
	}
	
	public void paintScore(Graphics g) {
		g.drawString("Score: "+score, boardX, 2, Graphics.TOP|Graphics.LEFT);
	}
	
	public void paintWall(Graphics g) {
		g.setColor(WALL_COLOR);
        g.drawRect(boardX-boardWall,boardY-boardWall,boardWidth+boardWall*2,boardHeight+boardWall);
	}
	
    public void paintApplae(Graphics g) {
    	g.setColor(APPLE_COLOR);
        g.fillArc(appleX, appleY, cellRect, cellRect, 0, 360);
    }
    
    public void paintSnake(Graphics g) {
    	g.setColor(SNAKE_COLOR);
    	for(int i = 0;i < snakeLength;i++) {
            g.fillRect(((SnakeCell)snakes.elementAt(i)).x,
                    ((SnakeCell)snakes.elementAt(i)).y, cellRect, cellRect);
        }
    }
    
    public void run() {
        while(true) {
        	try {
        		if(isGameOver == false) {
	        		Thread.sleep(gameSpeed);
	        		this.moveSnake(currentDirection);
	        		this.repaint();
	        	}
	        	else {
	        		this.addCommand(restartCommand);
	        	}
        	} catch (Exception e) {
            }
        }
    }
    
    public void setRestartCommand(Command restartCommand) {
    	this.restartCommand = restartCommand;
    }
}

public class SnakeBite extends MIDlet implements CommandListener {
    private Display display;
    private BoardCanvas boardCanvas;
    private StartCanvas startCanvas;
    
    private Command exitCommand;
    private Command startCommand;
    private Command restartCommand;
    public SnakeBite() {
        display = Display.getDisplay(this);
        
        exitCommand = new Command("Exit", Command.EXIT, 1);
        startCommand = new Command("Start", Command.SCREEN, 1);
        restartCommand = new Command("Restart", Command.SCREEN, 1);
        
        startCanvas = new StartCanvas();
        boardCanvas = new BoardCanvas();
        boardCanvas.setRestartCommand(restartCommand);
        
        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);
        } else if(c == restartCommand) {
        	boardCanvas.newGame();
            display.setCurrent(boardCanvas);
            boardCanvas.removeCommand(restartCommand);
        }
    }
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:46
Processing time 0.0233 sec