⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 footballgamecanvas.java

📁 netbean下开发的
💻 JAVA
字号:
/*
 * FootballGameCanvas.java
 *
 * Created on 2007年6月3日, 下午10:16
 */

package hello;

import java.util.Timer;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.GameCanvas;
import java.io.IOException;
import javax.microedition.lcdui.game.*;
import javax.microedition.media.*;
import javax.microedition.media.control.ToneControl;
import java.io.InputStream;
import javax.microedition.lcdui.game.Sprite;
import java.lang.Thread;
/**
 *
 * @author  FXB
 * @version
 */
public class FootballGameCanvas extends Canvas implements CommandListener,Runnable{

    /**
     * constructor
     */
    //如果人没有跳起来,人对球的作用力,可以为负
    private float peopleforce = 0 ;
    //人的脚离屏幕低端的高度
    private int peopleheight = 10;
     //帧的刷新时间间隔
    private int timeDelay;    
//private Image FootballImage;
    private Image footballImage;
    private Image paopleImage;
    private HelloMidlet Midlet;

    private float peaklength =4f;    //人的跳跃时的初时两帧的间隔
    
    private  float maxHeigh = 160;  //人对球加正力(就是给球加上一定的速度)的最高距离
    private float BL = 20;  //人高度与人对球的力的比例

    private Sprite Football ;   //足球精灵
    private Sprite WallRight;
    private Sprite WallLeft;
    private Sprite People;      //人的精灵
    private Sprite Sky;
    private Sprite Grass;
    private Image backGround;
        //屏幕的高和宽,为了尽量适应各种屏幕宽度
    private int height;
    private int width;
        //绘制图形的类型,主要用来避免有些图象的重复绘制
    //private int PaintType = 0;      //0为初始化时,绘制所有画布对象到初时位置
                                     //1为对足球进行重新绘制
                                     //2为对足球和人都进行重新绘制(因为人有可能不动,但足球却是每时每刻都在动)    
    
    private int PeopleDirection = 0;    //标记人的走向,以便刷新人精灵,1为左,2为右
    private double GSpeed =0.3;        //重力加速度的大小
    private double Resistance_air = 0.1; //空气阻力的大小(实际上就是考虑空气对两帧的距离的影响)
    private int PeopleSpd = 3;
    
    private Command ExitCmd = new Command("Exit", Command.EXIT, 1);
    private Command PauseCmd = new Command("Pause",Command.STOP,1);
    
    private FootballClass FootballManage; //定义一个足球的实例,主要用来通过控制精灵的对齐点来操纵足球精灵的动作
                                    //(注意:足球被顶高的力度也包含在这个类中)
    private PeopleClass PeopleManage; //定义一个人的实例,用来通过键盘的输入来判断和控制人的运动
    
    
    public FootballGameCanvas(HelloMidlet Midlet) {
        //初始化程序对象//
        
        //每一帧之间的时延
        this.timeDelay = 10;
        this.Midlet = Midlet;
        //System.out.println((float)this.Resistance_air);
        //得到屏幕的高度和宽度
        this.height = this.getHeight();
        this.width = this.getWidth();

        try {
            footballImage = Image.createImage("/hello/fb.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        try {
            //载入各种精灵,并对其进行相应的设置
            this.Football = new Sprite(footballImage,footballImage.getWidth()/6,footballImage.getHeight());
            this.Football.setPosition(width/2-10,height/2-10);
           // System.out.println(width/2-10);
           // System.out.println(height/2-10);
            this.Football.defineReferencePixel(this.Football.getWidth()/2, this.Football.getHeight()/2);
            
            this.WallRight = new Sprite(Image.createImage("/hello/wall.png"));
            this.WallRight.setPosition((10-this.WallRight.getWidth()),-10);
            this.WallLeft = new Sprite(Image.createImage("/hello/wall.png"));
            this.WallLeft.setPosition(this.width-10,-10);
            
            this.paopleImage = Image.createImage("/hello/pp.png");
            
            this.People = new Sprite(this.paopleImage,this.paopleImage.getWidth()/2,this.paopleImage.getHeight()); 
            this.People.setPosition(width/2,(height-this.People.getHeight()-this.peopleheight));
            this.People.defineReferencePixel(this.People.getWidth()/2, this.People.getWidth()/2);
            this.People.setRefPixelPosition(this.People.getRefPixelX(),
                    height-this.People.getHeight()/2-this.peopleheight);
            this.Grass = new Sprite(Image.createImage("/hello/grass.png"));
            this.Grass.setPosition(0,height-5);
            this.Sky = new Sprite(Image.createImage("/hello/sky.png"));
            this.Sky.setPosition(0,0);
            this.backGround = Image.createImage("/hello/bg.png");
            
            // Set up this canvas to listen to command events
            setCommandListener(this);   //监听按键
            
            //添加各种画布按键
            this.addCommand(ExitCmd);   
            this.addCommand(PauseCmd);

        } catch(Exception e) {
            e.printStackTrace();
            //System.out.println("Can't Load fb.png");
        }
        
        //建立足球精灵处理实例,方便以后对足球运动的管理
        FootballManage = new FootballClass(this.Football, this.Football.getRefPixelX(), this.Football.getRefPixelY(),
                this.GSpeed,(float)this.Resistance_air,this.timeDelay,this);
        
        //建立人精灵的处理实例,方便以后对人的运动的控制管理
        this.PeopleManage = new PeopleClass(this.People,this.PeopleSpd);
        
        Thread t = new Thread(this);    //  建立画布的新线程
        t.start();                      //启动线程
    }
    
    /**
     * paint
     */
    public void paint(Graphics g) {
//        g.drawString("Sample Text",0,0,Graphics.TOP|Graphics.LEFT);
        g.drawImage(backGround, 0, 0, Graphics.TOP|Graphics.LEFT);
        Football.paint(g);
        WallRight.paint(g);
        WallLeft.paint(g);
        this.People.paint(g);
        Grass.paint(g);
        Sky.paint(g);
    }
//    private int a=0;
//    private int b=0;
    private int roll = 0;
    private int TimeM = 0;
    
    public void run(){

            //this.PaintType = 0;
       while(true){
//        this.Football.setRefPixelPosition(a,b);
//        a++;
//        b++;
//           if(this.PeopleDirection != 0){
//               this.repaint();
//               this.PeopleDirection =0;
//           }
           
           
           /*此处实现人的轨迹为连续的而不是跳跃的*/
       if(this.People.getX()>10 &&this.PeopleDirection == 1){
           this.PeopleManage.moveleft();
            
            if(this.TimeM * this.timeDelay >40){
            this.People.nextFrame();
            this.TimeM = 0;
            }
            this.TimeM++;
//           roll+=2;
       }
       else
           if( this.People.getX()<(this.width-this.People.getWidth()-10) && this.PeopleDirection == 2){
                this.PeopleManage.moveright();
                
            if(this.TimeM * this.timeDelay >40){
            this.People.prevFrame();
            this.TimeM = 0;
            }
            this.TimeM++;
            }


//        if(roll > this.PeopleSpd){
//           this.PeopleDirection=0;
//           roll = 0;
//        }
        /*此处为功能的结束处*/  

       //this.FootballManage.test();
        
        //检测是否有碰撞,因为碰撞只可能是一种类型,所以,利用条件语句,首先对碰撞几率比较大的情况进行检测
        //注意:!!!可以考虑增加一个条件判断,只有足球到达一定的范围内才进行碰撞检测
            if(this.FootballManage.isStrike(this.People,4)){
                        System.out.println(this.People.getRefPixelY());
                                            System.out.println(height-this.People.getHeight()/2-this.peopleheight);
                    if(this.People.getRefPixelY() == height-this.People.getHeight()/2-this.peopleheight){
                        System.out.println("rrrrrrrrrrrrrrrrrrrrrrrrrr");
                        this.FootballManage.setForce(this.peopleforce);
                    }else
                    {this.FootballManage.setForce((this.maxHeigh -(this.height-this.People.getRefPixelY()))/this.BL );}

                    this.Updata();
            }   //与人头的碰撞检测
            else
            if(this.FootballManage.isStrike(this.Grass,3)){this.Updata();}    //与地面进行碰撞检测
                    else
                    if(this.FootballManage.isStrike(this.WallRight,2)){this.Updata();}    //与右侧墙进行碰撞检测
                        else                
                        if(this.FootballManage.isStrike(this.WallLeft,1)){this.Updata();} //与左侧墙的碰撞检测
                            else {
                                this.FootballManage.strikeNo();     //计算下一步的足球精灵的位置
                                this.Updata();
                            }
//                                if(this.FootballManage.isStrike(this.Sky,0)){
//                                    this.Updata();   //与天花板进行碰撞检测
//                                }
              
       //这里使人能弹跳起来,并且落在地上静止
       if(this.peak==1){
           this.PeopleManage.moveup();
           if(this.People.collidesWith(this.Grass,true)){

               this.People.setRefPixelPosition(this.People.getRefPixelX(),
                       (height-this.People.getHeight()/2-this.peopleheight));
               
               this.PeopleManage.setPeopleYY((height-this.People.getHeight()/2-this.peopleheight),
                       (height-this.People.getHeight()/2-this.peopleheight));
               this.peak = 0;
           }
       }
       
       
        //此时,所有的碰撞可能都检测完毕,
        if(!this.FootballManage.state){
            this.Midlet.exitMIDlet();
        }
       

        
       }

    }

    public void Updata(){
        this.FootballManage.displayFootball();
        

        
        this.repaint();
        try {
            Thread.sleep(timeDelay);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
    
    /**
     * Called when a key is pressed.
     */
    private boolean circle = true;
    private int peak=0;
    protected  void keyPressed(int keyCode) {
        int action = getGameAction(keyCode);

	    switch (action) { 
	    case Canvas.LEFT:
                this.PeopleDirection = 1;
                
		break;
	    case Canvas.RIGHT:

                this.PeopleDirection = 2;

                break;
	    case Canvas.DOWN:
		break;
	    case Canvas.UP:
                if(this.peak ==0){
                this.peak =1;
                this.PeopleManage.setPeopleYY((float)this.People.getRefPixelY(),
                        (float)this.People.getRefPixelY()+this.peaklength);
                }
		break;
		// case 0: // Ignore keycode that don't map to actions.
	    default:
		return;
            }
    }
    /**
     * Called when a key is released.
     */

    protected  void keyReleased(int keyCode) {
        
                int action = getGameAction(keyCode);

	    switch (action) { 
	    case Canvas.LEFT:
                this.PeopleDirection = 0;
                
		break;
	    case Canvas.RIGHT:
                this.PeopleDirection = 0;
                break;
	    case Canvas.DOWN:
		break;
	    case Canvas.UP:
                
		break;
		// case 0: // Ignore keycode that don't map to actions.
	    default:
		return;
            }
        
    }
    
    /**
     * Called when a key is repeated (held down).
     */

    protected  void keyRepeated(int keyCode) {
        System.out.println("按下键changshijian了!!!");


    }
    
    /**
     * Called when the pointer is dragged.
     */
    protected  void pointerDragged(int x, int y) {
    }
    
    /**
     * Called when the pointer is pressed.
     */
    protected  void pointerPressed(int x, int y) {
    }
    
    /**
     * Called when the pointer is released.
     */
    protected  void pointerReleased(int x, int y) {
    }
    
    /**
     * Called when action should be handled
     */
    public void commandAction(Command command, Displayable displayable) {
        if(command == this.ExitCmd) {
            
            this.Midlet.exitMIDlet();
        }else if(command == this.PauseCmd) {
            
        }
    }
    public int  getmyHeight(){
        return this.height;
    }
    public int getmyWidth(){
        return this.width;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -