📄 snakethread.java
字号:
import java.util.Random;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
public class SnakeThread extends GameCanvas implements Runnable{
Graphics g;
boolean isRun;
SnakeImg img;
Map map;
int headX = 64;
int headY = 64;
int headMoveSign;
int canpassSign;
int bodyMoveSign;
int changePointNum;
int[] changePointX = new int[100];
int[] changePointY = new int[100];
int[] changePointSign = new int[100];
long gamestarttime;
long timeSinceStart;
int x = 8;
int y = 8;
int tailX;
int tailY;
int foodX;
int foodY;
Random r = new Random();
public SnakeThread() {
super(true);
g = getGraphics();
img = new SnakeImg();
map = new Map();
start();
}
public void start(){
isRun = true;
Thread t = new Thread(this);
t.start();
}
public void run() {
setfood();
map.mBackground.setCell(x, y-3, 1);
map.mBackground.setCell(x, y-2, 1);
map.mBackground.setCell(x, y-1, 1);
while(isRun == true){
gamestarttime = System.currentTimeMillis();
paintAll();
gameOver();
if(timeSinceStart < 100){
try{
Thread.sleep(100 - timeSinceStart);
}catch(InterruptedException ie){}
}
}
}
public void paintAll(){
g.setColor(220,220,220);
g.fillRect(0, 0, 156, 156);
g.setColor(255,255,255);
g.fillRect(8, 8, 128, 128);
map.mBackground.paint(g);
img.headSprite.setPosition(headX, headY);
img.headSprite.paint(g);
bodyMove();
headMove();
img.foodSprite.paint(g);
flushGraphics();
}
public void headMove(){
if(headMoveSign == 0){
headY = headY + 8;
map.mBackground.setCell(headX/8, headY/8-1, 1);
}
else if(headMoveSign == 1){
headX = headX + 8;
map.mBackground.setCell(headX/8-1, headY/8, 1);
}
else if(headMoveSign == 2){
headY = headY - 8;
map.mBackground.setCell(headX/8, headY/8+1, 1);
}
else if(headMoveSign == 3){
headX = headX - 8;
map.mBackground.setCell(headX/8+1, headY/8, 1);
}
if(headY > 128){
headY = 8;
}
else if(headY < 8){
headY = 128;
}
else if(headX > 128){
headX = 8;
}
else if(headX < 8){
headX = 128;
}
int keyState = getKeyStates();
if((keyState & DOWN_PRESSED) != 0){
canpassSign = 0;
if(headMoveSign - canpassSign != 2){
headMoveSign = canpassSign;
changePointX[changePointNum] = headX/8;
changePointY[changePointNum] = headY/8;
changePointSign[changePointNum] = canpassSign;
changePointNum++;
}
}
else if((keyState & RIGHT_PRESSED) != 0){
canpassSign = 1;
if(headMoveSign - canpassSign != 2){
headMoveSign = canpassSign;
changePointX[changePointNum] = headX/8;
changePointY[changePointNum] = headY/8;
changePointSign[changePointNum] = canpassSign;
changePointNum++;
}
}
else if((keyState & UP_PRESSED) != 0){
canpassSign = 2;
if(headMoveSign - canpassSign != -2){
headMoveSign = canpassSign;
changePointX[changePointNum] = headX/8;
changePointY[changePointNum] = headY/8;
changePointSign[changePointNum] = canpassSign;
changePointNum++;
}
}
else if((keyState & LEFT_PRESSED) != 0){
canpassSign = 3;
if(headMoveSign - canpassSign != -2){
headMoveSign = canpassSign;
changePointX[changePointNum] = headX/8;
changePointY[changePointNum] = headY/8;
changePointSign[changePointNum] = canpassSign;
changePointNum++;
}
}
}
public void bodyMove(){
if(bodyMoveSign == 0){
if(img.headSprite.collidesWith(img.foodSprite, true)){
setfood();
}
else{
y++;
}
if(y>20){
y=5;
}
map.mBackground.setCell(x, y-4, 0);
if(x == changePointX[0] & y-4 == changePointY[0]){
bodyMoveSign = changePointSign[0];
for(int i = 0;i < changePointNum;i++){
changePointX[i] = changePointX[i+1];
changePointY[i] = changePointY[i+1];
changePointSign[i] = changePointSign[i+1];
}
changePointNum--;
}
}
else if(bodyMoveSign == 1){
if(img.headSprite.collidesWith(img.foodSprite, true)){
setfood();
}
else{
x++;
}
if(x>16){
x=1;
}
map.mBackground.setCell(x, y-4, 0);
if(x == changePointX[0] & y-4 == changePointY[0]){
bodyMoveSign = changePointSign[0];
for(int i = 0;i < changePointNum;i++){
changePointX[i] = changePointX[i+1];
changePointY[i] = changePointY[i+1];
changePointSign[i] = changePointSign[i+1];
}
changePointNum--;
}
}
else if(bodyMoveSign == 2){
if(img.headSprite.collidesWith(img.foodSprite, true)){
setfood();
}
else{
y--;
}
if(y<5){
y=20;
}
map.mBackground.setCell(x, y-4, 0);
if(x == changePointX[0] & y-4 == changePointY[0]){
bodyMoveSign = changePointSign[0];
for(int i = 0;i < changePointNum;i++){
changePointX[i] = changePointX[i+1];
changePointY[i] = changePointY[i+1];
changePointSign[i] = changePointSign[i+1];
}
changePointNum--;
}
}
else if(bodyMoveSign == 3){
if(img.headSprite.collidesWith(img.foodSprite, true)){
setfood();
}
else{
x--;
}
if(x<1){
x=16;
}
map.mBackground.setCell(x, y-4, 0);
if(x == changePointX[0] & y-4 == changePointY[0]){
bodyMoveSign = changePointSign[0];
for(int i = 0;i < changePointNum;i++){
changePointX[i] = changePointX[i+1];
changePointY[i] = changePointY[i+1];
changePointSign[i] = changePointSign[i+1];
}
changePointNum--;
}
}
}
public void setfood(){
foodX = r.nextInt(16*8) + 8;
foodY = r.nextInt(16*8) + 8;
img.foodSprite.setPosition(foodX, foodY);
}
public void gameOver(){
if(map.mBackground.getCell(headX/8, headY/8) == 1){
isRun = false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -