?? footballgamecanvas.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;
//屏幕的高和寬,為了盡量適應(yīng)各種屏幕寬度
private int height;
private int width;
//繪制圖形的類型,主要用來避免有些圖象的重復(fù)繪制
//private int PaintType = 0; //0為初始化時,繪制所有畫布對象到初時位置
//1為對足球進行重新繪制
//2為對足球和人都進行重新繪制(因為人有可能不動,但足球卻是每時每刻都在動)
private int PeopleDirection = 0; //標記人的走向,以便刷新人精靈,1為左,2為右
private double GSpeed =0.3; //重力加速度的大小
private double Resistance_air = 0.1; //空氣阻力的大?。▽嶋H上就是考慮空氣對兩幀的距離的影響)
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) {
//初始化程序?qū)ο?/
//每一幀之間的時延
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 {
//載入各種精靈,并對其進行相應(yīng)的設(shè)置
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); //監(jiān)聽按鍵
//添加各種畫布按鍵
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;
// }
/*此處實現(xiàn)人的軌跡為連續(xù)的而不是跳躍的*/
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;
// }
/*此處為功能的結(jié)束處*/
//this.FootballManage.test();
//檢測是否有碰撞,因為碰撞只可能是一種類型,所以,利用條件語句,首先對碰撞幾率比較大的情況進行檢測
//注意:?。。】梢钥紤]增加一個條件判斷,只有足球到達一定的范圍內(nèi)才進行碰撞檢測
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();} //與右側(cè)墻進行碰撞檢測
else
if(this.FootballManage.isStrike(this.WallLeft,1)){this.Updata();} //與左側(cè)墻的碰撞檢測
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;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -