📄 cgame.java~26~
字号:
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import java.util.*;
import javax.microedition.media.control.*;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class CGame extends Canvas implements Runnable, Constance {
int keyPressed;
int newnodeX;
int newnodeY;
int tickcount;
Player currentPlayer;
Player p_bg;
Player p_eat;
Player p_die;
int[] nodeX = new int[MAX_NODE];
int[] nodeY = new int[MAX_NODE];
int currentNode = 0;
int direction;
int volume = 50;
Random Ran = new Random();
public CGame() {
try {
setFullScreenMode(true);
randomXY();
p_bg = Manager.createPlayer("".getClass().getResourceAsStream(
"/bg.mid"), "audio/midi");
p_eat = Manager.createPlayer("".getClass().getResourceAsStream(
"/eat.wav"), "audio/x-wav");
p_die = Manager.createPlayer("".getClass().getResourceAsStream(
"/die.wav"), "audio/x-wav");
snd_play(p_bg, -1);
(new Thread(this)).start();
} catch (Exception e) {
e.printStackTrace();
}
}
void snd_play(Player p, int loop){
try{
if(currentPlayer != null)
currentPlayer.stop();
currentPlayer = p;
currentPlayer.realize();
currentPlayer.prefetch();
VolumeControl vol = (VolumeControl) currentPlayer.getControl("VolumeControl");
vol.setLevel(volume);
currentPlayer.setLoopCount(loop);
currentPlayer.start();
}catch(Exception e){
e.printStackTrace();
}
}
void randomXY() {
newnodeX = Math.abs(Ran.nextInt() % (SCR_WIDTH / 5)) * 5;
newnodeY = Math.abs(Ran.nextInt() % (SCR_HEIGHT / 5)) * 5;
}
public void paint(Graphics g) {
updateSnake();
updateVolume();
drawBG(g);
drawSnake(g);
drawNewNode(g);
keyPressed = 0;
}
void drawBG(Graphics g) {
g.setClip(0, 0, SCR_WIDTH, SCR_HEIGHT);
g.setColor(0x00ff00);
g.fillRect(0, 0, 240, 320);
}
void drawSnake(Graphics g) {
for (int i = 0; i <= currentNode; i++) {
g.setColor(0);
g.fillRect(nodeX[i], nodeY[i], NODE_LENGTH, NODE_LENGTH);
}
}
void drawNewNode(Graphics g) {
if (tickcount % 6 < 3){
g.setColor(0);
g.fillRect(newnodeX, newnodeY, NODE_LENGTH, NODE_LENGTH);
}
}
void updateSnake() {
try{
rearrangeSnake();
switch (direction) {
case DIR_RIGHT:
nodeX[0] += NODE_LENGTH;
break;
case DIR_LEFT:
nodeX[0] -= NODE_LENGTH;
break;
case DIR_UP:
nodeY[0] -= NODE_LENGTH;
break;
case DIR_DOWN:
nodeY[0] += NODE_LENGTH;
break;
}
if (hitMyself() || nodeX[0] < 0 || nodeX[0] > SCR_WIDTH ||
nodeY[0] < 0 ||
nodeY[0] > SCR_HEIGHT)
die();
changeDir(keyPressed);
if (getNextX() == newnodeX && getNextY() == newnodeY) {
snd_play(p_eat, 1);
addNode(newnodeX, newnodeY);
randomXY();
}
}catch(Exception e){
e.printStackTrace();
}
}
void updateVolume(){
if(keyPressed == Canvas.KEY_NUM1)
volume -= 5;
if(keyPressed == Canvas.KEY_NUM3)
volume += 5;
if(volume < 0)
volume = 0;
if(volume > 100)
volume = 100;
}
void rearrangeSnake() {
for (int i = currentNode; i > 0; i--) {
nodeX[i] = nodeX[i - 1];
nodeY[i] = nodeY[i - 1];
}
}
void changeDir(int key) {
switch (key) {
case Canvas.RIGHT:
direction = DIR_RIGHT;
break;
case Canvas.LEFT:
direction = DIR_LEFT;
break;
case Canvas.UP:
direction = DIR_UP;
break;
case Canvas.DOWN:
direction = DIR_DOWN;
break;
}
}
void addNode(int x, int y) {
currentNode++;
if (currentNode == MAX_NODE)
currentNode = MAX_NODE - 1;
rearrangeSnake();
nodeX[0] = x;
nodeY[0] = y;
}
int getNextX() {
switch (direction) {
case DIR_RIGHT:
return nodeX[0] + NODE_LENGTH;
case DIR_LEFT:
return nodeX[0] - NODE_LENGTH;
default:
return nodeX[0];
}
}
int getNextY() {
switch (direction) {
case DIR_UP:
return nodeY[0] - NODE_LENGTH;
case DIR_DOWN:
return nodeY[0] + NODE_LENGTH;
default:
return nodeY[0];
}
}
boolean hitMyself() {
for (int i = 1; i < currentNode; i++) {
if (nodeX[0] == nodeX[i] && nodeY[0] == nodeY[i])
return true;
}
return false;
}
void die() {
try{
p_die.start();
currentNode = 0;
nodeX[0] = 0;
nodeY[0] = 0;
direction = -1;
}catch(Exception e){
e.printStackTrace();
}
}
public void run() {
try {
while (true) {
Thread.sleep(100);
repaint();
tickcount++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void keyPressed(int code) {
keyPressed = getGameAction(code);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -