一个乒乓球游戏
所有代码全部放在Game类上实现的,方块用的是矢量图实现的,因为它要和小球碰撞后消失的问题,所以只能用到Point2D类来确定位置,然后通过RectMany类来实现一组方块。
package com.j2me; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; /** * @author Administrator */ public class Main extends MIDlet implements CommandListener{ private Display dis=Display.getDisplay(this); private Command exit=new Command("exit",Command.EXIT,1); private Game g=new Game(); private Thread t; public Main(){ } public void startApp() { g.addCommand(exit); g.setCommandListener(this); t=new Thread(this.g); t.start(); dis.setCurrent(g); } public void exitApp(){ destroyApp(false); notifyDestroyed(); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable d) { if(c==exit){ this.exitApp(); } } } package com.j2me; import java.io.IOException; import javax.microedition.lcdui.*; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.game.GameCanvas; import javax.microedition.lcdui.game.LayerManager; import javax.microedition.lcdui.game.Sprite; /** * @author Administrator */ public class Game extends GameCanvas implements CommandListener ,Runnable{ private LayerManager lm; private Back back; private Sprite s_ball; private Sprite spoly; private Sprite sback; private Image poly; private Image background; private int pX,pY; private int speed=2; private int bX,bY; private int addX=4; private int addY=4; private RectMany[][]rm=new RectMany[20][10]; private Main m; private boolean over=true; private Command c=new Command("Exit", Command.EXIT, 1); public Game() { super(true); try { // Set up this canvas to listen to command events setCommandListener(this); // Add the Exit command addCommand(new Command("", Command.EXIT, 1)); poly=Image.createImage("/pole.png"); background=Image.createImage("/backGround.png"); init(); } catch(Exception e) { e.printStackTrace(); } } public void init()throws IOException{ //this.back=new Back(); /*************栏杆**************************/ spoly=new Sprite(poly); pX=this.getWidth()/2; pY=4*this.getHeight()/5; spoly.setPosition(pX, pY); spoly.setVisible(true); /*************背景************************/ sback=new Sprite(background); sback.setVisible(true); /****************小球******************/ back=new Back(); this.s_ball= this.back.getT_ball(); this.s_ball.setPosition(this.getWidth()/2,this.getHeight()/2); this.s_ball.setFrameSequence(this.back.t_ballseq001); /**************************************/ this.lm=new LayerManager(); lm.append(s_ball); lm.append(spoly); lm.append(sback); /****************方块******************/ for(int i=0;i<20;i++){ for(int j=0;j<10;j++){ rm[i][j]=new RectMany(); rm[i][j].setPoint(add1(i), add1(j));
}
} } public int add1(int x){ return x*=12; } public int add(int x){ return x+=6; } // public void /** * paint */ public void paint(Graphics g) { g.setColor(0x11ffff); for(int i=0;i<20;i++){ for(int j=0;j<10;j++){ if(rm[i][j].Alive()){ rm[i][j].huafang(g); } } } if(!over) System.out.println("**************Game over*************** "); } public void motion(){ if(s_ball.getX()+s_ball.getWidth()>getWidth()||s_ball.getX()<0){ addX=-addX; }
if(s_ball.getY()+s_ball.getHeight()>getHeight()||s_ball.getY()<0){ addY=-addY; } s_ball.move(addX, addY); s_ball.nextFrame(); } /***************拦杆控制****************/ public void polyCotrol(){ int keyState=getKeyStates(); if ((keyState & LEFT_PRESSED)!=0){ this.spoly.move(-speed, 0); } else if ((keyState & RIGHT_PRESSED)!=0){ this.spoly.move(speed, 0); } else if ((keyState & UP_PRESSED)!=0){ this.spoly.move(0, -speed); } else if ((keyState & DOWN_PRESSED)!=0){ this.spoly.move(0, speed); } } /**********************碰撞检测(精灵检测)**************************/ public boolean collition(Sprite sprite,Sprite sprite_){ return sprite.collidesWith(sprite_, true); } /**********************碰撞检测(矩形检测)**************************/ public boolean isIntersectingRect(int ax, int ay, int aw, int ah, int bx, int by, int bw, int bh) { if (by + bh < ay || // is the bottom of b above the top of a? by > ay + ah || // is the top of b below bottom of a? bx + bw < ax || // is the right of b to the left of a? bx > ax + aw) // is the left of b to the right of a? return false; else return true; } /******************碰撞检测(方块和小球检测)******************/ public void collision(){ for(int i=0;i<20;i++){ for(int j=0;j<10;j++){ if(isIntersectingRect(s_ball.getX()-2,s_ball.getY()-2,13,13,rm[i][j].X()-2,rm[i][j].Y()-2,8,8)) rm[i][j].setAlive(false); } } } public void gameover(){ if(s_ball.getY()+s_ball.getHeight()>this.getHeight()){ over=false; } } public void commandAction(Command c, Displayable d) { if(c==this.c){ m.exitApp(); } } public void run() { Graphics g=getGraphics(); while(over){ try { motion(); polyCotrol(); if(collition(spoly,s_ball)){ addY=-addY; } gameover(); collision(); this.lm.paint(g, 0, 0); flushGraphics(); repaint(); Thread.sleep(20); } catch (InterruptedException ex) { ex.printStackTrace(); } } } } package com.j2me; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.lcdui.game.*; /** * @author Administrator */ public class Back { //<editor-fold defaultstate="collapsed" desc=" Generated Fields "> private Image xball; private Sprite t_ball; public int t_ballseq001Delay = 200; public int[] t_ballseq001 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}; //</editor-fold> //<editor-fold defaultstate="collapsed" desc=" Generated Methods "> //</editor-fold> public Image getXball() throws java.io.IOException { if (xball == null) { // write pre-init user code here xball = Image.createImage("/xball.png"); } // write post-init user code here return this.xball; } public Sprite getT_ball() throws java.io.IOException { if (t_ball == null) { // write pre-init user code here t_ball = new Sprite(getXball(), 15, 15); t_ball.setFrameSequence(t_ballseq001); // write post-init user code here } return t_ball; }
public void updateLayerManagerForT_back(LayerManager lm) throws java.io.IOException { // write pre-update user code here getT_ball().setPosition(138, 183); getT_ball().setVisible(true); lm.append(getT_ball()); // write post-update user code here } } package com.j2me; class Point2D { private int x, y; private boolean alive; //int constructor Point2D() { setX(0); setY(0); } public void setPoint(int x,int y) { setX(x); setY(y); } //X property int X() { return x; } public void setX(int x) { this.x = x; } //Y property int Y() { return y; } public void setY(int y) { this.y = y; } public boolean Alive(){ return alive; } public void setAlive(boolean alive){ this.alive=alive; } } package com.j2me; import javax.microedition.lcdui.Graphics; /** * * @author Administrator */ public class RectMany extends Point2D{ public RectMany(){ setAlive(true); } public void huafang (Graphics g) { g.fillRect(X(), Y(), 10,10); } } |
图片文件:
以上是 一个乒乓球游戏 的全部内容, 来源链接: utcz.com/p/205968.html