/*
* ファイル名: Food
* 説明 : 食物類
* 製造会社:大連日光
*/
package greedSnake;
import java.util.Random;
import com.nttdocomo.ui.Graphics;
public class Food {
private int x;
private int y;
private static Random rnd = new Random();
private Food(int x, int y) {
this.x = x;
this.y = y;
}
public static Food createRandomFood(int width, int height) {
return createFood(getRandomInt(width), getRandomInt(height));
}
private static int getRandomInt(int limit) {
return Math.abs(rnd.nextInt() % limit);
}
private static Food createFood(int x, int y) {
return new Food(x, y);
}
/**
* Get the x coordinate of the base
* location of this instance.
*/
public int getX() {
return x;
}
/**
* Get the y coordinate of the base
* location of this instance.
*/
public int getY() {
return y;
}
/** Paint this instance onto the specified Graphics context. */
public synchronized void render(Graphics g) {
int FoodColor =ThreeDColor.red;
Grid.draw3DRect(g,getX(),getY(),FoodColor);
}
}
/*
* ファイル名: GreedSnake
* 説明 : 主運行類
* 製造会社:大連日光
*/
package greedSnake;
import com.nttdocomo.io.ConnectionException;
import com.nttdocomo.ui.*;
public class GreedSnake extends IApplication implements Runnable {
public static final int GRID_WIDTH = 20;
public static final int GRID_HEIGHT = 24;
public static final int NODE_SIZE = 10;
private Grid grid ;
private Food food ;
private Snake snake;
private int lvl;
private int score;
private int level;
private int count;
private int countMove;
private int timeInterval;
private int countTime;
private int speedChangeRate;
private int intSelectedIndex;
private int selectGameScreen;
private boolean running;
private boolean paused;
private boolean isCycle;
private AudioPresenter audiopresenter;
// ----------------------------------------
/** Set up game. */
public void start() {
loadSound();
selectGameScreen = 0;
isCycle = false;
grid = new Grid(GRID_WIDTH, GRID_HEIGHT, this);
Display.setCurrent(grid);
grid.setselectGameScreen(selectGameScreen);
}
private void reset() {
snake = new Snake();
running = false;
paused = false;
score = 0;
level = 0;
count = 0;
countTime = 0;
countMove = 6;
speedChangeRate = 20;
switch (intSelectedIndex) {
case 0 :
timeInterval = 400;
break;
case 1 :
timeInterval = 200;
break;
case 2 :
timeInterval = 100;
break;
}
}
private void GameScreen() {
reset();
grid.reset();
selectGameScreen = 1;
isCycle = false;
grid.setselectGameScreen(selectGameScreen);
Thread runner = new Thread(this);
runner.start();
}
/** Runnable interface. */
public void run() {
long startTimeMillis = System.currentTimeMillis();
grid.addSnake(snake);
food = grid.addFood();
grid.repaint();
for (;;) {
if (isCycle) {
break;
}
if (food == null) {
food = grid.addFood();
}
try {
Thread.sleep(timeInterval);
} catch (Exception e) {
//break;
}
if (!paused) {
if (!running) {
snake.move();
Node head = snake.getHead();
if (grid.overlapsWith(head)
|| (snake.isHeadInSnake(head))) {
running = true;
continue;
}
if ((head.x == food.getX()) && (head.y == food.getY())) {
int scoreGet = (10000 - 200 * countMove) / timeInterval;
score += scoreGet > 0 ? scoreGet : 0;
countMove = 0;
snake.eatFood(food);
grid.reduceFood(food);
food = null;
audiopresenter.play();
if (++count % 5 == 0) {
speedUp();
level++;
}
} else {
countMove++;
}
grid.repaint();
} else {
grid.setGameOver();
running = true;
grid.repaint();
continue;
}
if (System.currentTimeMillis() - startTimeMillis > 1000) {
startTimeMillis = System.currentTimeMillis();
countTime++;
}
}else{
grid.repaint();
}
}
}
/** Key event handler. */
public void keyPressed(int key) {
switch (selectGameScreen) {
case 0 : // タイトル
if (key == Display.KEY_SELECT || key == Display.KEY_SOFT2) {
if (grid.getLvl(lvl) == 0) {
intSelectedIndex = 0;
}
if (grid.getLvl(lvl) == 1) {
intSelectedIndex = 1;
}
if (grid.getLvl(lvl) == 2) {
intSelectedIndex = 2;
}
GameScreen();
}
if (key == Display.KEY_SOFT1) {
terminate();
}
if (key == Display.KEY_DOWN) {
lvl = grid.getLvl(lvl);
lvl++;
if (lvl > 2) {
lvl = 2;
}
grid.setLvl(lvl);
grid.repaint();
}
if (key == Display.KEY_UP) {
lvl = grid.getLvl(lvl);
lvl--;
if (lvl < 0) {
lvl = 0;
}
grid.setLvl(lvl);
grid.repaint();
}
break;
case 1 : // ゲーム中
if (key == Display.KEY_SELECT) {
changePauseState();
}
if (key == Display.KEY_SOFT1) {
isCycle = true;
selectGameScreen = 0;
grid.setselectGameScreen(selectGameScreen);
grid.setLvl(lvl);
grid.repaint();
}
if (key == Display.KEY_SOFT2) {
isCycle = false;
newGame();
}
if (key == Display.KEY_LEFT) {
snake.setDirection(Snake.LEFT);
}
if (key == Display.KEY_RIGHT) {
snake.setDirection(Snake.RIGHT);
}
if (key == Display.KEY_DOWN) {
snake.setDirection(Snake.DOWN);
}
if (key == Display.KEY_UP) {
snake.setDirection(Snake.UP);
}
break;
}
}
private void newGame() {
grid.reset();
reset();
grid.addSnake(snake);
food = grid.addFood();
}
private void speedUp() {
timeInterval -= speedChangeRate;
}
public int getTime() {
return countTime;
}
public int getScore() {
return score;
}
/** Get the current level of the game. */
public int getLevel() {
return level;
}
public void changePauseState() {
paused = !paused;
grid.setGamePause();
}
private void loadSound() {
MediaSound mediaSound;
audiopresenter = AudioPresenter.getAudioPresenter();
try {
mediaSound = MediaManager.getSound("resource:///getitem.mld");
mediaSound.use();
audiopresenter.setSound(mediaSound);
} catch (ConnectionException connectionexception) {
System.out.println("loadSound() error!");
}
}
}
/*
* ファイル名: Grid
* 説明 : 画面描写
* 製造会社:大連日光
*/
package greedSnake;
import com.nttdocomo.io.ConnectionException;
import com.nttdocomo.ui.*;
/**
* This class represents the grid box that catches
* the pieces dropping from above. If a dropped piece
* fills up one or more horizontal lines in the grid,
* these lines will be removed from it.
*/
public class Grid extends Canvas {
private int width;
private int height;
private GreedSnake listener;
private Snake snake;
private Food food;
private int lvl;
private Image image;
private String pad = "0000";
private Font score_font;
private Font game_font;
private boolean game_pause;
private boolean game_over;
private int selectGameScreen;
/**
* Create a new instance of this class.
* @param witdth the width (in tiles) of this grid
* @param height the height (in tiles) of this grid
* @param score the score object to keep track of the score
* @param listener a reference to the owning tetris object
*/
public Grid(int width, int height, GreedSnake listener) {
this.width = width;
this.height = height;
this.listener = listener;
reset();
score_font = Font.getFont(Font.FACE_MONOSPACE | Font.SIZE_SMALL);
game_font =
Font.getFont(
Font.FACE_PROPORTIONAL | Font.SIZE_LARGE | Font.STYLE_BOLD);
MediaImage mimage =
MediaManager.getImage("resource:///greedsnake02.gif");
// MediaImage mimage = MediaManager.getImage("resource:///background.gif");
try {
mimage.use();
} catch (ConnectionException ce) {
System.out.println(ce);
} catch (UIException uie) {
System.out.println(uie);
}
image = mimage.getImage();
}
/** Remove all pieces from this instance */
public void reset() {
food = null;
snake = null;
game_over = false;
game_pause = false;
lvl = 0;
}
public void setGameOver() {
snake = null;
food = null;
game_over = true;
}
public void setGamePause() {
game_pause = !game_pause;
}
/** Get the width (in tiles) of this instance. */
private int getGridWidth() {
return width;
}
/** Get the height (in tiles) of this instance. */
private int getGridHeight() {
return height;
}
/** Add the specified piece to this instance. */
public Food addFood() {
int gs = GreedSnake.GRID_WIDTH;
for (;;) {
this.food = Food.createRandomFood(gs, gs);
Node node = new Node(food.getX(), food.getY());
if (snake.isInSnake(node)) {
continue;
} else {
break;
}
}
return this.food;
}
public void addSnake(Snake snake) {
this.snake = snake;
}
/**
* Returns <code>true</code> if the specified
* piece overlaps with any tile in this instance,
* <code>false</code> otherwise.
*/
public boolean overlapsWith(Node node) {
if ((node.x < 0)
|| (node.x > getGridWidth() - 1)
|| (node.y < 0)
|| (node.y > getGridHeight() - 1)) {
return true;
}
return false;
}
public void reduceFood(Food food) {
this.food = null;
}
private String format(int n) {
String raw = "" + n;
return pad.substring(0, 4 - raw.length()) + raw;
}
public int getLvl(int lvl) {
return lvl;
}
public void setLvl(int lvl) {
this.lvl = lvl;
}
public void setselectGameScreen(int selectGameScreen) {
this.selectGameScreen = selectGameScreen;
switch(selectGameScreen){
case 0: // タイトル
setSoftLabel(Frame.SOFT_KEY_1, "終了");
setSoftLabel(Frame.SOFT_KEY_2, "????");
break;
case 1: // ゲーム中
setSoftLabel(Frame.SOFT_KEY_1, "戻る");
setSoftLabel(Frame.SOFT_KEY_2, "再開");
break;
}
}
/** Paint this instance onto the specified graphics context. */
public synchronized void paint(Graphics g) {
g.lock();
switch (selectGameScreen) {
case 0: // タイトル
g.setColor(Graphics.getColorOfName(Graphics.GREEN));
g.fillRect(0, 0, getWidth(), getHeight());
String drawSt="Welcome to GreedSnake!";
textOut(g,drawSt,25,40);
textOut(g,"初級", 30, 80);
textOut(g,"中級", 30, 100);
textOut(g,"高級", 30, 120);
textOut(g, ">", 10, 80 + lvl * 20);
Font font = Font.getFont(Font.FACE_PROPORTIONAL | Font.SIZE_TINY | Font.STYLE_ITALIC);
g.setFont(font);
g.drawString("-- Product by 大連日光 --", 40, 220);
break;
case 1: // ゲーム中
g.drawImage(image, 0, 0);
g.setColor(Graphics.getColorOfName(Graphics.BLACK));
g.fillRect(200, 0, getWidth(), getHeight());
if (food != null) {
food.render(g);
}
if (snake != null) {
snake.render(g);
}
g.setColor(Graphics.getColorOfName(Graphics.SILVER));
g.setFont(score_font);
g.drawString("LEVEL", 203, 40);
g.drawString(String.valueOf(listener.getLevel()), 220, 60);
g.drawString("SCORE", 202, 100);
g.drawString(format(listener.getScore()), 208, 120);
g.drawString("TIME", 206, 160);
g.drawString(format(listener.getTime()), 208, 180);
if (game_over) {
g.setColor(Graphics.getColorOfName(Graphics.WHITE));
g.fillRect(15, 108, 170, 24);
g.setColor(Graphics.getColorOfName(Graphics.RED));
g.setFont(game_font);
g.drawString("GAME OVER", 18, 130);
}
if (game_pause && !game_over) {
g.setColor(Graphics.getColorOfName(Graphics.WHITE));
g.fillRect(52, 108, 96, 24);
g.setColor(Graphics.getColorOfName(Graphics.RED));
g.setFont(game_font);
g.drawString("PAUSE", 55, 130);
}
break;
}
g.unlock(true);
}
public static void draw3DRect(Graphics g, int x, int y, int color) {
int ts = GreedSnake.NODE_SIZE - 1;
x = x * GreedSnake.NODE_SIZE;
y = y * GreedSnake.NODE_SIZE;
g.setColor(color);
g.fillRect(x, y, ts, ts);
g.setColor(ThreeDColor.brighter(color));
g.drawLine(x, y, x, y + ts);
g.drawLine(x, y, x + ts, y);
g.setColor(ThreeDColor.darker(color));
g.drawLine(x + ts, y + 1, x + ts, y + ts);
g.drawLine(x, y + ts, x + ts, y + ts);
}
// 文字列描画(影付き)
public void textOut(Graphics g, String st, int x, int y)
{
Font font =Font.getFont(Font.SIZE_SMALL | Font.FACE_MONOSPACE | Font.STYLE_BOLD);
g.setFont(font);
// Xが0以下の場合、センタリングする
if(x < 0){
x = (getWidth() - font.getBBoxWidth(st)) / 2;
}
g.setColor(Graphics.getColorOfName(Graphics.GRAY));
g.drawString(st, x + 1, y + 1);
g.setColor(Graphics.getColorOfName(Graphics.GRAY));
g.drawString(st, x, y + 1);
g.setColor(Graphics.getColorOfName(Graphics.YELLOW));
g.drawString(st, x, y);
}
/** Process key events. */
public void processEvent(int type, int param) {
if (type == Display.KEY_PRESSED_EVENT) {
listener.keyPressed(param);
}
}
}
/*
* ファイル名: Node
* 説明 : ノード類
* 製造会社:大連日光
*/
package greedSnake;
public class Node {
public int x;
public int y;
/** Create a new instance of this class. */
public Node(int x, int y) {
this.x = x;
this.y = y;
}
}
/*
* ファイル名: Snake
* 説明 : 蛇の模型
* 製造会社:大連日光
*/
package greedSnake;
import java.util.Random;
import java.util.Vector;
import java.util.Enumeration;
import com.nttdocomo.ui.Graphics;
public class Snake {
public final static int UP = 1;
public final static int DOWN = 3;
public final static int LEFT = 2;
public final static int RIGHT = 4;
private int direction;
private Vector snakeData = null;
private static Random rnd = new Random();
/** Private internal constructor. */
public Snake() {
snakeData = new Vector();
Node node = new Node(4, 0);
snakeData.addElement(node);
node = new Node(3, 0);
snakeData.addElement(node);
node = new Node(2, 0);
snakeData.addElement(node);
node = new Node(1, 0);
snakeData.addElement(node);
node = new Node(0, 0);
snakeData.addElement(node);
setDirection(Snake.DOWN);
}
public int getDirection() {
return direction;
}
public void setDirection(int dir) {
if (direction % 2 != dir % 2) {
direction = dir;
}
}
public Node getHead() {
return (Node) snakeData.elementAt(0);
}
public Node getNextHead() {
Node node = (Node) snakeData.elementAt(0);
node = new Node(node.x, node.y);
switch (this.direction) {
case Snake.UP :
node.y--;
break;
case Snake.DOWN :
node.y++;
break;
case Snake.LEFT :
node.x--;
break;
case Snake.RIGHT :
node.x++;
break;
}
return node;
}
public void move() {
Node node = (Node) snakeData.elementAt(0);
Node newNode = this.getNextHead();
for (int i = snakeData.size() - 1; i > 0; i--) {
Node node1 = (Node) snakeData.elementAt(i);
Node node2 = (Node) snakeData.elementAt(i - 1);
node1.x = node2.x;
node1.y = node2.y;
}
node.x = newNode.x;
node.y = newNode.y;
}
public void addNode() {
Node node = (Node) snakeData.lastElement();
node = new Node(node.x, node.y);
snakeData.addElement(node);
}
public void render(Graphics g) {
int SnakeColor = ThreeDColor.green;
Enumeration e = snakeData.elements();
while (e.hasMoreElements()) {
Node node = (Node) e.nextElement();
Grid.draw3DRect(g,node.x,node.y,SnakeColor);
}
}
public void eatFood(Food food) {
addNode();
}
public boolean isInSnake(Node node) {
for (int i = 0; i < snakeData.size(); i++) {
Node nodeInSnake = (Node) snakeData.elementAt(i);
if ((nodeInSnake.x == node.x) && (nodeInSnake.y == node.y)) {
return true;
}
}
return false;
}
public boolean isHeadInSnake(Node node) {
for (int i = 1; i < snakeData.size(); i++) {
Node nodeInSnake = (Node) snakeData.elementAt(i);
if ((nodeInSnake.x == node.x) && (nodeInSnake.y == node.y)) {
return true;
}
}
return false;
}
}
/*
* ファイル名: ThreeDColor
* 説明 : 3D Color
* 製造会社:大連日光
*/
package greedSnake;
import com.nttdocomo.ui.Graphics;
/*
* Provide functionality required for pseudo-3D
* color highlighting and shadowing.
*/
public class ThreeDColor {
/** Light Gray */
public static int lightGray = Graphics.getColorOfRGB(0xc0, 0xc0, 0xc0);
/** Blue */
public static int blue = Graphics.getColorOfRGB(0x00, 0x00, 0xff);
/** Red */
public static int red = Graphics.getColorOfRGB(0xff, 0x00, 0x00);
/** Yellow */
public static int yellow = Graphics.getColorOfRGB(0xff, 0xff, 0x00);
/** Green */
public static int green = Graphics.getColorOfRGB(0x00, 0xc0, 0x00);
/**
* For a given color, return a shade suitable
* for highlighting.
*/
public static int brighter(int color) {
if (color == lightGray) {
return Graphics.getColorOfRGB(0xff, 0xff, 0xff);
}
if (color == blue) {
return Graphics.getColorOfRGB(0x00, 0xb2, 0xb2);
}
if (color == red) {
return Graphics.getColorOfRGB(0xff, 0xaf, 0xaf);
}
if (color == yellow) {
return Graphics.getColorOfRGB(0xff, 0xff, 0xaf);
}
if (color == green) {
return Graphics.getColorOfRGB(0xaf, 0xff, 0x00);
}
return -1;
}
/**
* For a given color, return a shade suitable
* for shadowing.
*/
public static int darker(int color) {
if (color == lightGray) {
return Graphics.getColorOfRGB(0x80, 0x80, 0x80);
}
if (color == blue) {
return Graphics.getColorOfRGB(0x40, 0x40, 0x40);
}
if (color == red) {
return Graphics.getColorOfRGB(0xb2, 0x00, 0x00);
}
if (color == yellow) {
return Graphics.getColorOfRGB(0xc8, 0xc8, 0x00);
}
if (color == green) {
return Graphics.getColorOfRGB(0x00, 0xb2, 0x00);
}
return -1;
}
}