📄 gamescreen.java
字号:
import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class GameScreen extends Canvas implements Runnable {
Image imgMap;
Image imgRope;
int x, y;
int rx,ry;
int frameWidth,frameHeight;
int curState=-1;
/** UP */
final static int UP_MOVE = 0;
/** DOWN */
final static int DOWN_MOVE = 1;
/** LEFT */
final static int LEFT_MOVE = 2;
/** RIGHT */
final static int RIGHT_MOVE = 3;
int curFrame;
short map[][] = { { 6, 1, 1, 1, 1, 1, 1, 6, 4, 5 },
{ 4, 6, 1, 1, 1, 1, 6, 4, 4, 5 }, { 4, 4, 6, 6, 6, 6, 4, 4, 4, 5 },
{ 4, 4, 4, 6, 6, 4, 4, 4, 4, 5 }, { 4, 4, 4, 4, 4, 4, 4, 4, 4, 5 },
{ 4, 5, 5, 4, 4, 6, 6, 4, 4, 4 }, { 4, 5, 5, 4, 4, 6, 6, 4, 4, 4 },
{ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 }, { 4, 6, 6, 6, 6, 6, 6, 4, 4, 4 },
{ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 } };
public GameScreen() {
try {
imgMap = Image.createImage("/layer.png");
imgRope=Image.createImage("/ss.png");
} catch (IOException e) {
}
frameWidth=imgRope.getWidth()/3;
frameHeight=imgRope.getHeight()/4;
new Thread(this).start();
}
protected void paint(Graphics g) {
for (int row = 0; row < map.length; row++) {
for (int col = 0; col < map[row].length; col++) {
g.setClip(x+col * 35, y+row * 35, 35, 35);
int tx, ty;
tx = (map[row][col] - 1) % 3 * 35;
ty = (map[row][col] - 1) / 3 * 35;
g.drawImage(imgMap, -tx + col * 35+x, -ty + row * 35+y, 20);
}
}
drawRope(g);
}
protected void keyPressed(int keyCode) {
int t = getGameAction(keyCode);
switch (t) {
case UP:
curState=UP_MOVE;
y++;
break;
case DOWN:
curState=DOWN_MOVE;
y--;
break;
case LEFT:
curState=LEFT_MOVE;
x++;
break;
case RIGHT:
curState=RIGHT_MOVE;
x--;
break;
default:
}
repaint();
}
public void run(){
while(true){
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
}
}
public void drawRope(Graphics g){
int fx,fy;
fx=curFrame%3*frameWidth;
fy=curFrame/3*frameHeight;
g.setClip(rx,ry, frameWidth, frameHeight);
g.drawImage(imgRope, -fx+rx, -fy+ry, 20);
move();
}
public void move() {
if (curState != -1)
curFrame++;
switch (curState) {
case UP_MOVE:
ry-=3;
if(curFrame<0||curFrame>2)
curFrame=0;
break;
case DOWN_MOVE:
ry+=3;
if(curFrame<6||curFrame>8)
curFrame=6;
break;
case LEFT_MOVE:
rx-=3;
if(curFrame<9||curFrame>11)
curFrame=9;
break;
case RIGHT_MOVE:
rx+=3;
if(curFrame<3||curFrame>5)
curFrame=3;
break;
default:
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -