📄 engine.java
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.lang.*;
import java.io.*;
import javax.microedition.io.*;
import java.util.*;
public class Engine implements Runnable
{
public final static int TITLE = 0;
public final static int PLAY = 1;
public final static int OVER = 2;
public final static int MENU = 3;
public final static int PROGRESS = 4;
public final static int HELP = 5;
public final static int NONETWORK = 6;
public static ConfigStorage conf;
public static ConfigStorage[] confs = new ConfigStorage[3];
private Ball[] ball = new Ball[2];
private Capsule[] capsule;
private Brick brick;
private NormalObject bricks[];
private NormalObject oldBricks[];
private Paddle paddle;
private RandomG randomG = new RandomG();
private boolean plategenerateMove = true;
private Screen screen;
private int score;
private int nextScore;
private int rank;
private String Url4_= new String();
public static String internetRank = "";
private int level;
private int availableLevels;
private int lives;
private int availableLives;
public static int state;
public static String noNetwork= "No Network";
private int key;
private boolean paused;
private long lastKeyPress;
public boolean levelStarted = false;
private boolean done;
private boolean getWin;
private boolean left;
private boolean right;
private Menu menu;
public int count = 0;
public int brickCount = 0;
public int brickNum = 0;
public int ballNumber = 0;
public int paddleNumber = 0;
public int capsuleNumber = 0;
public static int currentNum = 0;
public static int totalNum = 10;
public int row = 0;
public int column = 0;
public long bonusTime = 3000;
public long bonusbeginTime;
public int currentCapsule = -1;
private HttpRequester net;
public HighScore hs;
public static boolean upload = false;
public static boolean downloadLevel = false;
private String Url1 = "http://cat.cs.mu.oz.au:9080/gameserver/score?cmd=getfile&fname=yww_config1.txt";
private String Url2 = "http://cat.cs.mu.oz.au:9080/gameserver/score?cmd=getfile&fname=yww_config2.txt";
private String Url3 = "http://cat.cs.mu.oz.au:9080/gameserver/score?cmd=getfile&fname=yww_config3.txt";
private String Url4 = "http://cat.csse.unimelb.edu.au:9080/ss/score?team=yww&score=";
private static final int LAST_KEY_DELTA = 70000;
public Engine(Screen screen)
{
int i;
int x = 0;
menu = new Menu();
menu.setEngine(this);
this.screen = screen;
screen.setEngine(this);
//state = PROGRESS;
//screen.repaint();
//currentNum = 1;
//screen.repaint();
conf = new ConfigStorage();
conf.processDoc("/config1.txt");
confs[0] = new ConfigStorage();
confs[0].processDoc("/config1.txt");
//System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&7");
confs[1] = new ConfigStorage();
confs[1].processDoc("/config2.txt");
confs[2] = new ConfigStorage();
confs[2].processDoc("/config3.txt");
//currentNum = 5;
screen.brick_1 = scale(screen.brick_1, (int)(Screen.width * conf.brick_width), (int)(Screen.height * conf.brick_height));
screen.brick_2 = scale(screen.brick_2, (int)(Screen.width * conf.brick_width), (int)(Screen.height * conf.brick_height));
hs = new HighScore(0);
//currentNum = 6;
//screen.repaint();
hs.processDoc("/score.txt");
//currentNum = 10;
////screen.repaint();
//for (int tnnd = 0; tnnd < 1000; tnnd++)
//{
//}
//state = TITLE;
availableLevels = 3;
availableLives = 3;
level = 0;
state = TITLE;
done = false;
lastKeyPress = System.currentTimeMillis();
//System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
net = new HttpRequester(Url1, Url2, Url3, Url4);
Thread t = new Thread(net);
t.start();
//System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
//uploadscore = new HttpRequester(uploadUrl);
//Thread u = new Thread(uploadscore);
//u.start();
//up = new Upload();
//Thread u = new Thread(up);
//u.start();
Thread runner = new Thread(this);
runner.start();
}
private void reset()
{
lives = availableLives;
score = 0;
rank = -1;
nextScore = hs.Score[9];
startLevel();
}
private void startLevel()
{
changeLevel(level);
restartLevel();
}
private void restartLevel()
{
if (state == PLAY)
{
paused = true;
}
levelStarted = true;
paddle.setPos((int)(Screen.width * conf.ball_initpos_width - paddle.cx / 2), Screen.height - (Screen.fontHeight + 3) - paddle.cy);
ball[0].setPos((int)(Screen.width * conf.ball_initpos_width), (int)(Screen.height * conf.ball_initpos_height));
screen.repaint();
screen.serviceRepaints();
}
public void initialBricks()
{
brickCount = brickNum = conf.brick_position.length;
System.out.println("brickCount=" + brickCount+"level = "+ level);
capsule = new Capsule[brickNum];
bricks = new NormalObject[brickNum];
for (int x = 0; x < brickNum; x++)
{
bricks[x] = new Brick((int)(conf.brick_position[x][0] * Screen.width), (int)(Screen.height * conf.brick_position[x][1]), (int)(Screen.width * conf.brick_width), (int)(Screen.height * conf.brick_height), 1, 1, 1, 0);
}
}
public int changeRatioToPixW(double ratio)
{
return (int)(Screen.width*ratio);
}
public int changeRatioToPixH(double ratio)
{
return (int)(Screen.height*ratio);
}
private void changeLevel(int level)
{
if (level == 0)
conf = confs[0];
else if (level == 1)
conf = confs[1];
else if (level == 2)
conf = confs[2];
ballNumber = 0;
capsuleNumber = 0;
brickNum = 0;
paddleNumber = 0;
paddle = new Paddle("/j6.png", changeRatioToPixW(conf.paddles[0].shape_width), changeRatioToPixH(conf.paddles[0].shape_height), 20, 100, 0.0f, 0.0f, 0);
paddleNumber++;
ball[0] = new Ball("/j5.png", (int)(Screen.width * conf.ball_initpos_width), (int)(Screen.height * conf.ball_initpos_height), (float)(conf.ball_initspeed_width), (float)(conf.ball_initspeed_height), 0);
ballNumber++;
initialBricks();
switch (level)
{
case 0:
for (int x = 0; x < brickNum; x++)
{
int temp = randomBrick(0.1);
int tmp = randomGen(0.9);
//System.out.println(randomGen(0.9));
((Brick)bricks[x]).set(conf.brick_onhit_score, tmp, temp, temp % 3);
}
break;
case 1:
for (int x = 0; x < brickNum; x++)
{
int temp = randomBrick(0.5);
int tmp = randomGen(0.4);
((Brick)bricks[x]).set(conf.brick_onhit_score,tmp, temp, temp % 3);
}
break;
case 2:
for (int x = 0; x < brickNum; x++)
{
int temp = randomBrick(0.9);
int tmp = randomGen(0.1);
((Brick)bricks[x]).set(conf.brick_onhit_score,tmp, temp, temp % 3);
}
break;
}
}
public void keyPressed(int keycode, int gameAction)
{
key = gameAction;
lastKeyPress = System.currentTimeMillis();
if (state == OVER || state == TITLE || state == HELP|| state == NONETWORK)
{
state = MENU;
}
else if (state == PLAY && (key == Canvas.LEFT || key == Canvas.RIGHT
|| key == Canvas.FIRE))
{
paused = false;
}
}
public void keyReleased(int keycode, int gameAction)
{
key = 0;
}
public void getState(EngineState engineState)
{
engineState.bricks = bricks;
engineState.ball = ball;
engineState.capsule = capsule;
engineState.paddle = paddle;
engineState.state = state;
engineState.score = score;
engineState.nextScore = nextScore;
engineState.rank = rank;
engineState.lives = lives;
engineState.menu = menu;
engineState.currentNum=currentNum;
engineState.totalNum=totalNum;
engineState.brickCount = brickCount;
engineState.ballNumber = ballNumber ;
engineState.paddleNumber = paddleNumber;
engineState.capsuleNumber = capsuleNumber;
engineState.brickNum = brickNum;
engineState.levelStarted = levelStarted;
}
protected void startGame(int level)
{
this.level = level;
state = PLAY;
paused = false;
reset();
}
public void setLastKeyPressed(long time)
{
lastKeyPress = time;
}
public void stop()
{
done = true;
}
public void setPaused(boolean pause)
{
paused = pause;
}
public void platemoveLimit()
{
if (paddle.vx < 0 && paddle.x <= 0)
paddle.vx *= paddle.attenuation;
else if (paddle.x + paddle.cx >= Screen.width && paddle.vx > 0)
paddle.vx *= paddle.attenuation;
}
public void ballmoveLimit(int i)
{
if (ball[i].vx < 0 && ball[i].x <= 0)
ball[i].vx *= -1.0f;
else if (ball[i].x + ball[i].cx >= Screen.width && ball[i].vx > 0)
ball[i].vx *= -1.0f;
if (ball[i].vy < 0 && ball[i].y <= 0)
ball[i].vy *= -1.0f;
else if (ball[i].vy > 0 && ball[i].y + ball[i].cy >= Screen.height - (Screen.fontHeight + 3))
{
if (ballNumber == 1)
{
if (state == PLAY) lives--;
if (lives <= 0)
{
StringBuffer temp = new StringBuffer();
temp.append(Url4);
temp.append(score);
Url4_=temp.toString();
net.setUrl4(Url4_);
System.out.println("************************" + Url4_);
upload = true;
//for (int tnnd = 0; tnnd < 200000; tnnd++) ;
state = PROGRESS;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -