MyGameCanvas.java
上传用户:pyly5030
上传日期:2021-09-17
资源大小:174k
文件大小:6k
- import java.io.IOException;
- import java.util.*;
- import javax.microedition.lcdui.Command;
- import javax.microedition.lcdui.CommandListener;
- import javax.microedition.lcdui.Displayable;
- import javax.microedition.lcdui.Font;
- import javax.microedition.lcdui.Graphics;
- import javax.microedition.lcdui.Image;
- import javax.microedition.lcdui.game.GameCanvas;
- import javax.microedition.lcdui.game.LayerManager;
- import javax.microedition.lcdui.game.Sprite;
- import javax.microedition.lcdui.game.TiledLayer;
- public class MyGameCanvas extends GameCanvas implements Runnable{
- private static final int MILLS_PER_TICK = 50;
- private Thread t;
- private boolean isPlay;
- private long delay;
- private int currentX,currentY;
- private int width;
- private int height;
- private LayerManager layerManager;
- private MyPlayerSprite player;
- private TiledLayer terrain;
- private int animatedIdx;
- private boolean switchTile = true;
- private int bgY = -320;
- private Vector bullets;
- private Image bulletImage;
- private int gameCount;
- private int bulletSpeed = 5;
- private boolean flag = true;
-
- private Command startCommand = new Command("Start", Command.OK, 1);
- private Command pauseCommand = new Command("Pause", Command.EXIT, 1);
- private Command exitCommand = new Command("Exit", Command.OK, 2);
-
- protected MyGameCanvas() throws Exception {
- super(true);
- width = getWidth();
- height = 320;//getHeight();
- currentX = width/2;
- currentY = height/2;
- delay = 30;
- player = new MyPlayerSprite(Image.createImage("/xiaoji.png"), 30, 30, width, height);
- player.startPosition();
- terrain = getBackground();
- bullets = new Vector();
- bulletImage = Image.createImage("/bullet.png");
- layerManager = new LayerManager();
- layerManager.append(player);
- layerManager.append(terrain);
- this.setFullScreenMode(true);
- this.start();
- }
- public void run() {
- Graphics g = getGraphics();
- Thread currentThread = Thread.currentThread();
- long startTime = System.currentTimeMillis();
- try{
- while(currentThread == t){
- if(isPlay){
- input();
- drawScreen(g);
- if(gameCount > 30)
- gameCount = 0;
- else
- gameCount++;
- Thread.sleep(delay);
- }
- long useTime = System.currentTimeMillis() - startTime;
- if(useTime < MILLS_PER_TICK){
- synchronized(this){
- wait(MILLS_PER_TICK - useTime);
- }
- }else{
- currentThread.yield();
- }
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- }
-
- public void start(){
- isPlay = true;
- t = new Thread(this);
- t.start();
- }
-
- public void drawScreen(Graphics g){
- Font font = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN,Font.SIZE_SMALL);
- g.setColor(0x000000);
- g.fillRect(0, 0, width, height);
- g.setColor(0x4169E1);
- g.setFont(font);
- //player.nextFrame();
- //player.setPosition(currentX, currentY);
- //用静态图片索引2和3轮换
- if(switchTile)
- terrain.setAnimatedTile(animatedIdx, 2);
- else
- terrain.setAnimatedTile(animatedIdx, 3);
- //switchTile = !switchTile;
-
- //layerManager.setViewWindow(0, bgY, 240, 320); //这样的缺点是让与layerManager有关的对象也会滚动
- terrain.setPosition(0,bgY);
- bgY+=2;
- if(bgY>0)
- bgY = -320;
- layerManager.paint(g,0,0);
- //bullet
- for(int i=0;i<bullets.size();i++){
- for (int j = 0; j < 2; ++j) {
- Sprite bullet = (Sprite) bullets.elementAt(i);
- bullet.move(0, -3);
- if (bullet.getY() < 0) {
- bullets.removeElementAt(i);
- layerManager.remove(bullet);
- i--;
- break;
- }
- }
- }
-
- g.drawString(currentX+"/"+currentY, 180, 10, g.TOP|g.LEFT);
- flushGraphics();
- }
-
- private void input(){
- int keyStatus = getKeyStates();
- if(keyStatus == LEFT_PRESSED){
- //currentX = Math.max(0, currentX-moveSpeed);
- player.setFrame(1);
- player.moveLeft();
- }
- else if(keyStatus == RIGHT_PRESSED){
- //currentX = Math.min(width-30,currentX+moveSpeed);
- player.setFrame(2);
- player.moveRight();
- }
- else if(keyStatus == UP_PRESSED){
- //currentY = Math.max(0, currentY-moveSpeed);
- player.setFrame(3);
- player.moveUp();
- }
- else if(keyStatus == DOWN_PRESSED){
- //currentY = Math.min(height-30, currentY+moveSpeed);
- player.setFrame(4);
- player.moveDown();
- }
- else if(keyStatus == FIRE_PRESSED){
-
- }else {
- if(flag)
- player.setFrame(0);
- else
- player.setFrame(1);
- }
- if(gameCount % bulletSpeed != 0){
- return;
- }else{
- Sprite bullet = player.fire(bulletImage);
- flag = !flag;
- bullets.addElement(bullet);
- layerManager.insert(bullet, 1);
- }
- }
- public void paint(Graphics g) {
- //添加响应命令及监听器
- this.addCommand(pauseCommand);
- this.addCommand(exitCommand);
- //this.setCommandListener(this);
- super.paint(g);
- }
-
- /**
- * 创建背景
- * @throws IOException
- */
- protected TiledLayer getBackground() throws IOException {
- Image image = Image.createImage("/di.jpg");
- TiledLayer bglayer = new TiledLayer(6, 16, image, 40, 40);
- int[] map = { 2, 3, 6, 7, 8, 7, 6, 8, 3, 6, 7, 8, 7, 6, 8, 4
- , 3, 6, 7, 8, 7, 6, 8, 4, 3, 6, 7, 8, 7, 6, 8, 4
- , 3, 6, 7, 8, 7, 6, 8, 4, 3, 6, 7, 8, 7, 6, 8, 4
- , 2, 3, 6, 7, 8, 7, 6, 8, 3, 6, 7, 8, 7, 6, 8, 4
- , 3, 6, 7, 8, 7, 6, 8, 4, 3, 6, 7, 8, 7, 6, 8, 4
- , 3, 6, 7, 8, 7, 6, 8, 4, 3, 6, 7, 8, 7, 6, 8, 4};
- for (int i = 0; i < map.length; i++) {
- int col = i % 6;
- int row = (i - col)/6;
- bglayer.setCell(col, row, map[i]);
- }
- animatedIdx = bglayer.createAnimatedTile(2); //设置初始的静态图片索引
- bglayer.setCell(1, 1, animatedIdx);
- return bglayer;
-
- }
- }