站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2ME 专题赛马游戏源码

赛马游戏源码

摘要: 赛马游戏源码
package HorseClub;

//import javax.microedition.lcdui.*;
import java.util.Random;
import java.lang.String;

public class HorseX
{
public int m_No;
public int m_HorseAge;
public int m_HorseEx,m_HorseEndur,m_HorseHealth;
public char[] m_HorseName=new char[4];//="my horse";
// public static StringBuffer m_HorseName;//new char[4];//="my horse";
public static final String LetterTable="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public int m_Speed;
public int m_HorseState;
public int m_golds;
public int m_silvers;

public HorseX(int endur,int exp,int health,int no)
{

init(endur,exp,health,no);

}
public void init(int endur,int exp,int health,int no)
{
int randnum=0;

// rand.setSeed(no*12398147);
randnum=HorseMidlet.rand.nextInt();
m_HorseAge=Myabs(randnum%6+1);

//the "10" is the difficulty

m_HorseEndur=Myabs((randnum%20+10-20+endur))+1;
m_HorseEx=Myabs((randnum%20+10-20+exp))+1;
m_HorseHealth=Myabs((randnum%20+10-20+health))+1;
int x=m_HorseEndur;
// m_HorseName.append(LetterTable.charAt(x%25)).append(LetterTa
ble.charAt((x+5)%25)).append(LetterTable.charAt((x+17)%25));
m_HorseName[0]=LetterTable.charAt(x%25);
m_HorseName[1]=LetterTable.charAt((x+5)%25);
m_HorseName[2]=LetterTable.charAt((x+17)%25);
m_HorseState=randnum%4;
m_Speed=GetSpeed();
m_No=no;
m_golds=0;
m_silvers=0;

}

public static int Myabs(int value)
{
if(value<0)
value*=-1;
return value;
}

public static int Mysign(int value)
{
if(value<0)
return -1;
return 1;
}


//Calculate teh horse speed
public int GetSpeed()
{
int i=0;
int randnum=0;

randnum=HorseMidlet.rand.nextInt();
i=(m_HorseEx+m_HorseEndur+m_HorseHealth)/3;
i=Myabs(m_HorseEx-i)+Myabs(m_HorseEndur-i)+Myabs(m_HorseHealth-i)+1;
//i=i*rand()%3+1;

i=(m_HorseEx*4+m_HorseEndur*2+m_HorseHealth*3)*(randnum%2+1)/i;
return i;

}

}





package HorseClub;
import javax.microedition.lcdui.*;
import java.util.Random;

public class HorseCanvas extends Canvas implements Runnable, CommandListener
{
private HorseMidlet midlet;
private volatile Thread animationThread=null;

private static int canvasHeight, canvasWidth;
private HorseRacing myHorseRacing;

private Image buffer;
private Graphics graphics;

//for lot UI
private static int lot_no;
private static int lot_money;
private static int lot_select=0;//0 for lot_no
private static int sumTemp;
//images


private boolean running;
// private final int SLEEP=10;
private final int SLEEP=10;
private int ReadyToSleep=0;
private int Intro_pos=0;
private Command exit;
// private Command memory;
private Command newGame;

private Image[] imgMenu = null;
private Image[] imgDot = null;
// imgfloor=new Image[7];
private Image[] imgLot = null;
private Image[] imgTextFirstLot1 = null;
private Image[] imgTextFirstLot2 = null;

private Image[] imgTmp = null;
private Image[] imgTextSecondLot1= null;
private Image[] imgTextSecondLot2= null;
private Image[] imgTextRacingNum = null;
private Image[] imgTextAge = null;
private Image[] imgTextExp = null;
private Image[] imgTextEndur = null;
private Image[] imgTextHealth = null;
private Image[] imgTextTotalMoney = null;
private Image[] imgTextLotNum = null;
private Image[] imgTextLotMoney = null;
private Image[] imgTextOK1 = null;
private Image[] imgTextOK2 = null;

private Image[] imgTextReset1 = null;
private Image[] imgTextReset2 = null;

private Image[] imgTextBack1 = null;
private Image[] imgTextBack2 = null;


private Image[] imgWin = null;




public HorseCanvas(HorseMidlet midlet)
{
this.midlet=midlet;
canvasHeight=getHeight();
canvasWidth=getWidth();

myHorseRacing=new HorseRacing();

buffer=Image.createImage(canvasWidth, canvasHeight);
graphics=buffer.getGraphics();

exit=new Command("退出", Command.STOP,1);
// memory=new Command("Memory", Command.SCREEN,1);
newGame = new Command("新游戏",Command.SCREEN,1);

this.addCommand(exit);
// this.addCommand(memory);
this.addCommand(newGame);

this.setCommandListener(this);
sumTemp = 0;

imgMenu=new Image[1];
imgDot= new Image[1];
imgMenu=HorseMidlet.getImages("menu",1);
System.out.println("Free memory: MENU"+Runtime.getRuntime().freeMemory());

imgDot=HorseMidlet.getImages("bluedot",1);
System.out.println("Free memory: BLUEDOT"+Runtime.getRuntime().freeMemory(
));

imgLot = new Image[1];

imgLot=HorseMidlet.getImages("lothorse",1);

imgTextFirstLot1 = new Image[1];
imgTextFirstLot1 = HorseMidlet.getImages("text_firston", 1);
imgTextFirstLot2 = new Image[1];
imgTextFirstLot2 = HorseMidlet.getImages("text_firstoff", 1);

imgTextSecondLot1 = new Image[1];
imgTextSecondLot1 = HorseMidlet.getImages("text_secondon", 1);
imgTextSecondLot2 = new Image[1];
imgTextSecondLot2 = HorseMidlet.getImages("text_secondoff", 1);

imgTextRacingNum = new Image[1];
imgTextRacingNum=HorseMidlet.getImages("text_racingnum",1);

imgTextAge = new Image[1];
imgTextAge=HorseMidlet.getImages("text_age",1);

imgTextExp = new Image[1];
imgTextExp=HorseMidlet.getImages("text_exp",1);

imgTextEndur = new Image[1];
imgTextEndur=HorseMidlet.getImages("text_endur",1);


imgTextHealth = new Image[1];
imgTextHealth=HorseMidlet.getImages("text_health",1);

imgTextTotalMoney = new Image[1];
imgTextTotalMoney=HorseMidlet.getImages("text_totalmoney",1);

imgTextLotNum = new Image[1];
imgTextLotNum=HorseMidlet.getImages("text_lotnum",1);

imgTextLotMoney = new Image[1];
imgTextLotMoney=HorseMidlet.getImages("text_lotmoney",1);

imgTextOK1 = new Image[1];
imgTextOK1 = HorseMidlet.getImages("text_okon", 1);
imgTextOK2 = new Image[1];
imgTextOK2 = HorseMidlet.getImages("text_okoff", 1);

imgTextReset1 = new Image[1];
imgTextReset1 = HorseMidlet.getImages("text_reseton", 1);
imgTextReset2 = new Image[1];
imgTextReset2 = HorseMidlet.getImages("text_resetoff", 1);

imgTextBack1 = new Image[1];
imgTextBack1 = HorseMidlet.getImages("text_backon", 1);

imgTextBack2 = new Image[1];
imgTextBack2 = HorseMidlet.getImages("text_backoff", 1);

// imgWin = new Image[1];
// imgWin=HorseMidlet.getImages("win",1);






repaint();
}

public static int getCanvasHeight() {return canvasHeight;}
public static int getCanvasWidth() {return canvasWidth;}

synchronized void start()
{
running=true;
animationThread=new Thread(this);
animationThread.start();

}
public void run()
{
try
{
while (running)
{
repaint(0,0,canvasWidth, canvasHeight);
serviceRepaints();
Thread.sleep(SLEEP);
}
}
catch(InterruptedException ie)
{
System.out.println(ie.toString());
}
}

synchronized void stop()
{
running=false;
}

public void paint (Graphics g)
{
System.out.println("Free memory: "+Runtime.getRuntime().freeMemory()+" Tot
al Memory"+Runtime.getRuntime().totalMemory());


switch (HorseMidlet.gamestate) {
case HorseMidlet.GAME_STATE_START:
//draw menu


g.setColor(210,210,250);
g.fillRect(0,0,getCanvasWidth(),getCanvasHeight());
// if (sumTemp == 0)
// {

/* imgMenu=HorseMidlet.getImages("menu",1);
System.out.println("Free memory: MENU"+Runtime.getRuntime().freeM
emory());
imgDot=HorseMidlet.getImages("bluedot",1);
System.out.println("Free memory: BLUEDOT"+Runtime.getRuntime().fr
eeMemory());
sumTemp = 1;*/
// }



// g.drawImage(imgMenu[0],10,10,g.LEFT|g.TOP);
// g.drawImage(imgDot[0],30,75+HorseMidlet.substate*21,g.LEFT|g.TOP);

g.drawImage(imgMenu[0],0,10,g.LEFT|g.TOP);
g.drawImage(imgDot[0],16,75+HorseMidlet.substate*21,g.LEFT|g.TOP);
System.out.println("Free memory: ***"+Runtime.getRuntime().freeMemor
y());
g.setColor(127,0,0);

// g.drawString(Long.toString(Runtime.getRuntime().totalMemory())+"你
好",10,10,g.TOP|g.LEFT);
// g.drawString(Long.toString(Runtime.getRuntime().freeMemory()),10,30,
g.TOP|g.LEFT);

// imgMenu = null;
// imgDot = null;



break;

case HorseMidlet.GAME_STATE_INTRO:
g.setColor(210,210,250);
g.fillRect(0,0,getCanvasWidth(),getCanvasHeight());

// g.drawImage(HorseMidlet.imgIntroBG[0],10,10,g.LEFT|g.TOP);
g.drawImage(HorseMidlet.imgIntroBG[0],0,10,g.LEFT|g.TOP);

// 此处需要修改
if(Intro_pos 0)
Intro_pos++;

// g.setClip(10,10,HorseMidlet.imgIntro[0].getWidth() , HorseMidlet.i
mgIntro[0].getHeight());
// g.drawImage(HorseMidlet.imgIntro[0], 10,-Intro_pos+10, g.LEFT | g.
TOP);
// g.setClip(0, 0, getCanvasWidth(),getCanvasHeight());
g.setClip(0,0,HorseMidlet.imgIntro[0].getWidth() , HorseMidlet.imgIn
tro[0].getHeight());
g.drawImage(HorseMidlet.imgIntro[0], 0,-Intro_pos, g.LEFT | g.TOP);

// if (Intro_pos==HorseMidlet.imgIntro[0].getHeight()-getCanvasHeight
()-100)
// {
// g.drawImage(HorseMidlet.imgIntro[1], 3,getCanvasHeight()-60, g.LEF
T | g.TOP);
// }

g.setClip(0, 0, getCanvasWidth(),getCanvasHeight());

try
{
Thread.sleep(8);
repaint();
}
catch(InterruptedException ie)
{
System.out.println(ie.toString());
}



break;
case HorseMidlet.GAME_STATE_LOT:
// imgMenu = null;
// imgDot = null;
//draw lot
if(HorseMidlet.substate==0)
{
lot_no=HorseMidlet.firstlot_no;
lot_money=HorseMidlet.firstlot_money;
}
if(HorseMidlet.substate==1)
{
lot_no=HorseMidlet.secondlot_no;
lot_money=HorseMidlet.secondlot_money;
}

// Image[] imgLot=new Image[1];

// imgLot=HorseMidlet.getImages("lothorse",1);
g.setColor(105,117,184);
g.fillRect(0,0,getCanvasWidth(),getCanvasHeight());
// g.drawImage(imgLot[0],10,7,g.LEFT|g.TOP);
g.drawImage(imgLot[0],0,7,g.LEFT|g.TOP);
g.setColor(0,0,0);
// g.fillRect(10,46,40,14);
g.fillRect(0,46,40,14);
//draw the name
g.setColor(255,255,255);
// g.drawChars(myHorseRacing.horses[lot_no].m_HorseName,0,3,28,59,1);

g.drawChars(myHorseRacing.horses[lot_no].m_HorseName,0,3,18,59,1);

// Image[] imgTextFirstLot=new Image[1];
if(HorseMidlet.substate==0)
{
// imgTextFirstLot = HorseMidlet.getImages("text_firston", 1);
g.drawImage(imgTextFirstLot1[0],0,61,g.LEFT|g.TOP);
}
else
{
// imgTextFirstLot = HorseMidlet.getImages("text_firstoff", 1);
g.drawImage(imgTextFirstLot2[0],0,61,g.LEFT|g.TOP);
}

// g.drawImage(imgTextFirstLot[0],10,61,g.LEFT|g.TOP);
// g.drawImage(imgTextFirstLot[0],0,61,g.LEFT|g.TOP);

// Image[] imgTextSecondLot=new Image[1];
if(HorseMidlet.substate==1)
{
// imgTextSecondLot = HorseMidlet.getImages("text_secondon", 1);
g.drawImage(imgTextSecondLot1[0],0,74,g.LEFT|g.TOP);
}
else
{
// imgTextSecondLot = HorseMidlet.getImages("text_secondoff", 1);
g.drawImage(imgTextSecondLot2[0],0,74,g.LEFT|g.TOP);
}

// g.drawImage(imgTextSecondLot[0],10,74,g.LEFT|g.TOP);
// g.drawImage(imgTextSecondLot[0],0,74,g.LEFT|g.TOP);


//draw the horse info
g.setColor(255,180,0);
// g.fillRect(60,7,getCanvasWidth()/2+20,getCanvasHeight()/2+4);
//右上角
g.fillRect(50,7,76,getCanvasHeight()/2+4);

g.setColor(0,0,0);
// g.drawRect(60,7,getCanvasWidth()/2+20,getCanvasHeight()/2+4);
g.drawRect(50,7,76,getCanvasHeight()/2+4);

g.setColor(255,255,255);
//draw horse info text
// Image[] imgTextRacingNum=new Image[1];
// imgTextRacingNum=HorseMidlet.getImages("text_racingnum",1);
// g.drawImage(imgTextRacingNum[0],70,13,g.LEFT|g.TOP);
g.drawImage(imgTextRacingNum[0],55,13,g.LEFT|g.TOP);

// Image[] imgTextAge=new Image[1];
// imgTextAge=HorseMidlet.getImages("text_age",1);
// g.drawImage(imgTextAge[0],70,27,g.LEFT|g.TOP);
g.drawImage(imgTextAge[0],55,27,g.LEFT|g.TOP);

// Image[] imgTextExp=new Image[1];
// imgTextExp=HorseMidlet.getImages("text_exp",1);
// g.drawImage(imgTextExp[0],70,41,g.LEFT|g.TOP);
g.drawImage(imgTextExp[0],55,41,g.LEFT|g.TOP);

// Image[] imgTextEndur=new Image[1];
// imgTextEndur=HorseMidlet.getImages("text_endur",1);

// g.drawImage(imgTextEndur[0],70,55,g.LEFT|g.TOP);
g.drawImage(imgTextEndur[0],55,55,g.LEFT|g.TOP);

// Image[] imgTextHealth=new Image[1];
// imgTextHealth=HorseMidlet.getImages("text_health",1);
// g.drawImage(imgTextHealth[0],70,69,g.LEFT|g.TOP);
g.drawImage(imgTextHealth[0],55,69,g.LEFT|g.TOP);

/* g.drawString(java.lang.Integer.toString(myHorseRacing.horses[lot_no
].m_No),140,22,1);
g.drawString(java.lang.Integer.toString(myHorseRacing.horses[lot_no].
m_HorseAge),142,34,1);
g.drawString(java.lang.Integer.toString(myHorseRacing.horses[lot_no].
m_HorseEx),140,50,1);
g.drawString(java.lang.Integer.toString(myHorseRacing.horses[lot_no].
m_HorseEndur),140,64,1);
g.drawString(java.lang.Integer.toString(myHorseRacing.horses[lot_no].
m_HorseHealth),140,78,1);
*/
g.drawString(java.lang.Integer.toString(myHorseRacing.horses[lot_no].
m_No),112,22,1);
g.drawString(java.lang.Integer.toString(myHorseRacing.horses[lot_no].
m_HorseAge),112,34,1);
g.drawString(java.lang.Integer.toString(myHorseRacing.horses[lot_no].
m_HorseEx),110,50,1);
g.drawString(java.lang.Integer.toString(myHorseRacing.horses[lot_no].
m_HorseEndur),110,64,1);
g.drawString(java.lang.Integer.toString(myHorseRacing.horses[lot_no].
m_HorseHealth),110,78,1);


//draw lot info box

g.setColor(255,180,0);
// g.fillRect(10,getCanvasHeight()/2+18,getCanvasWidth()/2+20,44);
g.fillRect(0,getCanvasHeight()/2+18,87,44);

g.setColor(0,0,0);
// g.drawRect(10,getCanvasHeight()/2+18,getCanvasWidth()/2+20,44);
g.drawRect(0,getCanvasHeight()/2+18,87,44);


// Image[] imgTextTotalMoney=new Image[1];
// imgTextTotalMoney=HorseMidlet.getImages("text_totalmoney",1);
// g.drawImage(imgTextTotalMoney[0],17,92,g.LEFT|g.TOP);
g.drawImage(imgTextTotalMoney[0],2,92,g.LEFT|g.TOP);

g.setColor(0,0,0);
// g.fillRect(70,92,40,12);
g.fillRect(49,92,35,12);

// g.drawLine(10,105,10+getCanvasWidth()/2+20,105);
g.drawLine(0,105,87,105);

// Image[] imgTextLotNum=new Image[1];
// imgTextLotNum=HorseMidlet.getImages("text_lotnum",1);
// g.drawImage(imgTextLotNum[0],17,106,g.LEFT|g.TOP);
g.drawImage(imgTextLotNum[0],2,106,g.LEFT|g.TOP);

// g.fillRect(70,106,40,12);

// g.drawLine(10,119,10+getCanvasWidth()/2+20,119);
g.fillRect(49,106,35,12);
g.drawLine(0,119,87,119);

// Image[] imgTextLotMoney=new Image[1];
// imgTextLotMoney=HorseMidlet.getImages("text_lotmoney",1);
// g.drawImage(imgTextLotMoney[0],17,120,g.LEFT|g.TOP);
// g.fillRect(70,120,40,12);
g.drawImage(imgTextLotMoney[0],2,120,g.LEFT|g.TOP);
g.fillRect(49,120,35,12);

//draw lot info
g.setColor(255,255,255);
// g.drawString(java.lang.Integer.toString(HorseMidlet.mytotalmoney),8
5,103,1);

g.drawString(java.lang.Integer.toString(HorseMidlet.mytotalmoney),63,
103,1);

if(lot_select==0)
{
g.setColor(0,255,80);
}
else
{
g.setColor(255,255,255);

}
// g.drawString(java.lang.Integer.toString(lot_no+1), 85,
// 117, 1);
g.drawString(java.lang.Integer.toString(lot_no+1), 63,117, 1);

if(lot_select==1)
{
g.setColor(0,255,80);
}
else
{
g.setColor(255,255,255);
}
// g.drawString(java.lang.Integer.toString(lot_money),
// 85, 131, 1);
g.drawString(java.lang.Integer.toString(lot_money), 63, 131, 1);


// Image[] imgTextOK=new Image[1];

if(HorseMidlet.substate==2)
{
// imgTextOK = HorseMidlet.getImages("text_okon", 1);
g.drawImage(imgTextOK1[0],89,93,g.LEFT|g.TOP);

}
else
{
// imgTextOK = HorseMidlet.getImages("text_okoff", 1);
g.drawImage(imgTextOK2[0],89,93,g.LEFT|g.TOP);

}
// g.drawImage(imgTextOK[0],128,93,g.LEFT|g.TOP);
// g.drawImage(imgTextOK[0],89,93,g.LEFT|g.TOP);


// Image[] imgTextReset=new Image[1];
if(HorseMidlet.substate==3)
{
// imgTextReset = HorseMidlet.getImages("text_reseton", 1);
g.drawImage(imgTextReset1[0],89,108,g.LEFT|g.TOP);

}
else
{
// imgTextReset = HorseMidlet.getImages("text_resetoff", 1);
g.drawImage(imgTextReset2[0],89,108,g.LEFT|g.TOP);

}
// g.drawImage(imgTextReset[0],128,108,g.LEFT|g.TOP);
// g.drawImage(imgTextReset[0],89,108,g.LEFT|g.TOP);


// Image[] imgTextBack=new Image[1];
if(HorseMidlet.substate==4)
{
// imgTextBack = HorseMidlet.getImages("text_backon", 1);
g.drawImage(imgTextBack1[0],89,123,g.LEFT|g.TOP);
}
else
{
// imgTextBack = HorseMidlet.getImages("text_backoff", 1);
g.drawImage(imgTextBack2[0],89,123,g.LEFT|g.TOP);
}
// g.drawImage(imgTextBack[0],128,123,g.LEFT|g.TOP);
// g.drawImage(imgTextBack[0],89,123,g.LEFT|g.TOP);



break;

case HorseMidlet.GAME_STATE_RACING:
drawAnimation(g);
g.setColor(127,0,0);
// g.drawString(Long.toString(Runtime.getRuntime().totalMemory()),10
,10,g.TOP|g.LEFT);
// g.drawString(Long.toString(Runtime.getRuntime().freeMemory()),10,3
0,g.TOP|g.LEFT);

break;
case HorseMidlet.GAME_STATE_RACINGEND:
drawAnimation(g);
Image[] imgWin=new Image[1];
imgWin=HorseMidlet.getImages("win",1);
// g.drawImage(imgWin[0],40,20,g.LEFT|g.TOP);
g.drawImage(imgWin[0],20,20,g.LEFT|g.TOP);

int i,gold,goldi,silver,silveri,goldbonus=0,silverbonus;
gold=myHorseRacing.xHorsePos[0];
goldi=0;

for(i=1;i<8;i++)
{
if(gold { gold=myHorseRacing.xHorsePos;goldi=i;}
}
silveri=-1;
silver=-1;
for(i=0;i<7;i++)
{
if(goldi==i)continue;
if(silveri==-1||silver { silver=myHorseRacing.xHorsePos;silveri=i;}
}

//draw number
g.setColor(200,0,0);
/* g.drawChars(myHorseRacing.horses[goldi].m_HorseName,0,3,61,61,1);
g.drawString(java.lang.Integer.toString(myHorseRacing.horses[goldi].m_
No),70,64,0);

g.drawChars(myHorseRacing.horses[silveri].m_HorseName,0,3,61,113,1);
g.drawString(java.lang.Integer.toString(myHorseRacing.horses[silveri].
m_No),70,116,0);
*/
g.drawChars(myHorseRacing.horses[goldi].m_HorseName,0,3,41,61,1);
g.drawString(java.lang.Integer.toString(myHorseRacing.horses[goldi].m_No),
52,75,1);

g.drawChars(myHorseRacing.horses[silveri].m_HorseName,0,3,41,113,1);
g.drawString(java.lang.Integer.toString(myHorseRacing.horses[silveri].m_No
),52,127,1);


//calculate the bonus
if(HorseMidlet.myhorse.m_No<9)
{//bingo,Youself got the gold/silver
if(HorseMidlet.myhorse.m_No==goldi)
HorseMidlet.myhorse.m_golds++;
else
if(HorseMidlet.myhorse.m_No==silveri)
HorseMidlet.myhorse.m_silvers++;
}

if(HorseMidlet.firstlot_no==goldi)//First Lot got gold
{
if(HorseMidlet.myhorse.m_No==HorseMidlet.firstlot_no)
goldbonus=HorseMidlet.MONEY_GOLD_MUL_M*HorseMidlet.fir
stlot_money;
else
goldbonus=HorseMidlet.MONEY_GOLD_MUL*HorseMidlet.first
lot_money;
}
else
if(HorseMidlet.secondlot_no==goldi)//Second Lot got gold
{
if(HorseMidlet.myhorse.m_No==HorseMidlet.secondlot_no)
goldbonus=HorseMidlet.MONEY_GOLD_MUL_M*HorseMidlet.secondlot_m
oney;
else
goldbonus=HorseMidlet.MONEY_GOLD_MUL*HorseMidlet.secondlot_mon
ey;
}
else goldbonus=0;

if(HorseMidlet.firstlot_no==silveri)//First Lot got gold
{
if(HorseMidlet.myhorse.m_No==HorseMidlet.firstlot_no)
silverbonus=HorseMidlet.MONEY_SILVER_MUL_M*HorseMidlet
.firstlot_money;
else
silverbonus=HorseMidlet.MONEY_SILVER_MUL*HorseMidlet.f
irstlot_money;
}
else
if(HorseMidlet.secondlot_no==silveri)//Second Lot got gold
{
if(HorseMidlet.myhorse.m_No==HorseMidlet.secondlot_no)
silverbonus=HorseMidlet.MONEY_SILVER_MUL_M*HorseMidlet.secondl
ot_money;
else
silverbonus=HorseMidlet.MONEY_SILVER_MUL*HorseMidlet.secondlot
_money;
}
else silverbonus=0;

//draw the bonus
// g.drawString(java.lang.Integer.toString(goldbonus),110,75,1);
// g.drawString(java.lang.Integer.toString(silverbonus),110,127,1);
g.drawString(java.lang.Integer.toString(goldbonus),93,75,1);
g.drawString(java.lang.Integer.toString(silverbonus),93,127,1);



if(HorseMidlet.myhorse.m_No<9)
{
Random rand=new Random();
if((rand.nextInt()%100)>HorseMidlet.MATCH_WOUND_RATIO)
{
HorseMidlet.myhorse.m_HorseHealth-=rand.nextInt()%HorseMidle
t.WOUND_MAX;
}
}
HorseMidlet.mytotalmoney+=goldbonus+silverbonus;
stop();//stop the animation
break;
/*
case HorseMidlet.GAME_STATE_TRAINING:
g.setColor(255,180,0);
g.fillRect(0,0,getCanvasWidth(),getCanvasHeight());

g.drawImage(HorseMidlet.imgTrainTop[0],10,10,g.LEFT|g.TOP);
g.drawImage(HorseMidlet.imgTrainBack[0],10,26,g.LEFT|g.TOP);



g.drawImage(HorseMidlet.imgTextShort[0],10,130,g.LEFT|g.TOP);
g.drawImage(HorseMidlet.imgTextLong[0],40,130,g.LEFT|g.TOP);
g.drawImage(HorseMidlet.imgTextRst[0],70,130,g.LEFT|g.TOP);
g.drawImage(HorseMidlet.imgTextBackMenu_off[0],100,130,g.LEFT|g.TOP);


if(HorseMidlet.substate==0)
g.drawImage(HorseMidlet.imgTextShort[1],10,130,g.LEFT|g.TOP);
else if(HorseMidlet.substate==1)
g.drawImage(HorseMidlet.imgTextLong[1],40,130,g.LEFT|g.TOP);
else if(HorseMidlet.substate==2)
g.drawImage(HorseMidlet.imgTextRst[1],70,130,g.LEFT|g.TOP);
else if(HorseMidlet.substate==3)
g.drawImage(HorseMidlet.imgTextBackMenu[0],100,130,g.LEFT|g.TOP);

break;
case HorseMidlet.GAME_STATE_TRAINING_SHORT:
g.setColor(255,180,0);
g.fillRect(0,0,getCanvasWidth(),getCanvasHeight());

g.drawImage(HorseMidlet.imgTrainTop[0],10,10,g.LEFT|g.TOP);
g.drawImage(HorseMidlet.imgTrainRunning[0],10,26,g.LEFT|g.TOP)
;

ReadyToSleep++;
if(ReadyToSleep==1)
{
repaint();

}
else if(ReadyToSleep==2) {
ReadyToSleep=0;

try
{

Thread.sleep(3000);
HorseMidlet.gamestate=HorseMidlet.GAME_STATE_TRAINING;

repaint();

}
catch(InterruptedException ie)
{
System.out.println(ie.toString());
}
}


break;

case HorseMidlet.GAME_STATE_TRAINING_LONG:
g.setColor(255,180,0);
g.fillRect(0,0,getCanvasWidth(),getCanvasHeight());

g.drawImage(HorseMidlet.imgTrainTop[0],10,10,g.LEFT|g.TOP
);
g.drawImage(HorseMidlet.imgTrainRunning[0],10,26,g.LEFT|g.
TOP);
ReadyToSleep++;
if(ReadyToSleep==1)
{
repaint();

}
else if(ReadyToSleep==2) {
ReadyToSleep=0;

try
{

Thread.sleep(3000);
HorseMidlet.gamestate=HorseMidlet.GAME_STATE_TRAINING;

repaint();

}
catch(InterruptedException ie)
{
System.out.println(ie.toString());
}
}

break;
case HorseMidlet.GAME_STATE_TRAINING_REST:
g.setColor(255,180,0);
g.fillRect(0,0,getCanvasWidth(),getCanvasHeight());

g.drawImage(HorseMidlet.imgTrainTop[0],10,10,g.LEFT|g.TOP);

g.drawImage(HorseMidlet.imgTrainRest[0],10,26,g.LEFT|g.TOP);


ReadyToSleep++;
if(ReadyToSleep==1)
{
repaint();

}
else if(ReadyToSleep==2) {
ReadyToSleep=0;

try
{

Thread.sleep(3000);
HorseMidlet.gamestate=HorseMidlet.GAME_STATE_TRAINING;

repaint();

}
catch(InterruptedException ie)
{
System.out.println(ie.toString());
}
}


break;
*/
}
}

private void drawAnimation(Graphics gr)
{
Graphics g=gr;
if(this.isDoubleBuffered())
{
g=graphics;
}

try
{
myHorseRacing.setFrame();
myHorseRacing.draw(g);
gr.drawImage(buffer,0,0,g.TOP|g.LEFT);

}
catch(Exception e)
{
System.out.println("drawAnimation Error: "+e.toString());
}
}

public void commandAction(Command c, Displayable d)
{
//if any Exit key is pressed then exit
if(c==exit)
{
midlet.exitMidlet();
}
// else if(c==memory)
else if (c == newGame)
{
// midlet.startApp();
HorseMidlet.gamestate=HorseMidlet.GAME_STATE_START;
HorseMidlet.substate=0;

HorseMidlet.difficulty=10;
// HorseMidlet.gamestate=0;
HorseMidlet.substate=0;
HorseMidlet.mytotalmoney=1000;
HorseMidlet.firstlot_no=0;
HorseMidlet.firstlot_money=50;
HorseMidlet.secondlot_no=1;
HorseMidlet.secondlot_money=50;

// HorseMidlet.myhorse=new HorseX(30,40,50,9);
myHorseRacing.init();

/* imgMenu=new Image[1];
imgDot= new Image[1];

imgMenu=HorseMidlet.getImages("menu",1);
System.out.println("Free memory: MENU"+Runtime.getRuntime().freeMemory()
);
imgDot=HorseMidlet.getImages("bluedot",1);
System.out.println("Free memory: BLUEDOT"+Runtime.getRuntime().freeMemor
y());*/

stop();
// sumTemp = 1;
repaint();
// c = null;
}
}
protected void keyPressed(int keyCode)
{
// can't continue playing until the player restarts


int gameAction = getGameAction(keyCode);

switch (gameAction)
{
case FIRE:
if (HorseMidlet.gamestate==HorseMidlet.GAME_STATE_START)
{
if(HorseMidlet.substate==0)
{
HorseMidlet.gamestate=HorseMidlet.GAME_STATE_LOT;
HorseMidlet.mytotalmoney-=HorseMidlet.firstlot_money;
}
else if(HorseMidlet.substate==1)//introduce
{
HorseMidlet.gamestate=HorseMidlet.GAME_STATE_INTRO;
Intro_pos=0;
HorseMidlet.substate=0;
}
else if(HorseMidlet.substate==2)//exit
midlet.exitMidlet();

}
else if (HorseMidlet.gamestate==HorseMidlet.GAME_STATE_INTRO)
{
HorseMidlet.gamestate=HorseMidlet.GAME_STATE_START;
HorseMidlet.substate=0;

}
else if (HorseMidlet.gamestate==HorseMidlet.GAME_STATE_RACINGEND)

{
myHorseRacing.init();

HorseMidlet.gamestate=HorseMidlet.GAME_STATE_LOT;
HorseMidlet.mytotalmoney-=HorseMidlet.firstlot_money;
HorseMidlet.substate=0;

}

else if (HorseMidlet.gamestate==HorseMidlet.GAME_STATE_LOT)
{

if(HorseMidlet.substate==2)
{
HorseMidlet.gamestate=HorseMidlet.GAME_STATE_RACING;
HorseMidlet.horseruningspeed=HorseMidlet.MAX_SPEED;
HorseMidlet.screenscroll=0;
HorseMidlet.trainingtime=0;
HorseMidlet.beginwait=0;
HorseMidlet.showbeginbar=1; //show it
HorseMidlet.racingend=-1;//just begin
start();//start a animation thread;
}
if(HorseMidlet.substate<2)
HorseMidlet.substate++;
if(HorseMidlet.substate==1)
{
HorseMidlet.secondlot_money=HorseMidlet.BIT_LOT_MONEY;
HorseMidlet.mytotalmoney-=HorseMidlet.secondlot_money;
HorseMidlet.secondlot_no=HorseMidlet.firstlot_no+1;
if(HorseMidlet.secondlot_no>7)
HorseMidlet.secondlot_no=0;
}

if(HorseMidlet.substate==3)//reset
{
HorseMidlet.mytotalmoney= HorseMidlet.mytotalmoney+
HorseMidlet.firstlot_money+
HorseMidlet.secondlot_money-
HorseMidlet.BIT_LOT_MONEY;
HorseMidlet.firstlot_money=HorseMidlet.BIT_LOT_MONEY;
HorseMidlet.firstlot_no=0;
HorseMidlet.substate=0;
}
if(HorseMidlet.substate==4)//back
{
HorseMidlet.mytotalmoney= HorseMidlet.mytotalmoney+
HorseMidlet.firstlot_money+
HorseMidlet.secondlot_money;
HorseMidlet.firstlot_money=HorseMidlet.BIT_LOT_MONEY;
HorseMidlet.firstlot_no=0;
HorseMidlet.gamestate=HorseMidlet.GAME_STATE_START;
HorseMidlet.substate=0;
}

}
else if (HorseMidlet.gamestate==HorseMidlet.GAME_STATE_TRAINING)
{
if(HorseMidlet.substate==0)
HorseMidlet.gamestate=HorseMidlet.GAME_STATE_TRAINING_SHORT;
else if(HorseMidlet.substate==1)
HorseMidlet.gamestate=HorseMidlet.GAME_STATE_TRAINING_LONG;
else if(HorseMidlet.substate==2)
HorseMidlet.gamestate=HorseMidlet.GAME_STATE_TRAINING_REST;
else
{
HorseMidlet.gamestate = HorseMidlet.GAME_STATE_START;
HorseMidlet.substate=0;
}

}

sumTemp = 0;

break;

case RIGHT:
if (HorseMidlet.gamestate==HorseMidlet.GAME_STATE_LOT)
{
if (HorseMidlet.substate == 0) { //the first lot
if (lot_select == 0) {
HorseMidlet.firstlot_no++;
if (HorseMidlet.firstlot_no > 7)
HorseMidlet.firstlot_no = 0;
}
else {
if (HorseMidlet.mytotalmoney >= HorseMidlet.BIT_LOT_MONEY) {

HorseMidlet.firstlot_money += HorseMidlet.BIT_LOT_MONEY;
HorseMidlet.mytotalmoney -= HorseMidlet.BIT_LOT_MONEY;
}
}

}
if (HorseMidlet.substate == 1) { //the second lot
if (lot_select == 0) {
HorseMidlet.secondlot_no++;
if (HorseMidlet.secondlot_no == HorseMidlet.firstlot_no)
HorseMidlet.secondlot_no++;
if (HorseMidlet.secondlot_no > 7) {
HorseMidlet.secondlot_no = 0;
if (HorseMidlet.secondlot_no == HorseMidlet.firstlot_no)
HorseMidlet.secondlot_no++;

}
}
else {
if (HorseMidlet.mytotalmoney >= HorseMidlet.BIT_LOT_MONEY) {

HorseMidlet.secondlot_money += HorseMidlet.BIT_LOT_MONEY;
HorseMidlet.mytotalmoney -= HorseMidlet.BIT_LOT_MONEY;
}
}

}
}
else if (HorseMidlet.gamestate==HorseMidlet.GAME_STATE_TRAINING)
{
HorseMidlet.substate=(HorseMidlet.substate+1)%4;

}

break;

case DOWN:
if (HorseMidlet.gamestate==HorseMidlet.GAME_STATE_START)
{
HorseMidlet.substate++;
if(HorseMidlet.substate>=3)
HorseMidlet.substate=0;
// repaint();

}
else if (HorseMidlet.gamestate==HorseMidlet.GAME_STATE_LOT)
{
if(HorseMidlet.substate>=2)
{
HorseMidlet.substate++;
if(HorseMidlet.substate>4)
HorseMidlet.substate=2;
}
else
{
if(lot_select==0)
lot_select=1;
else
lot_select=0;

}
}



break;

case LEFT:

if (HorseMidlet.gamestate==HorseMidlet.GAME_STATE_LOT)
{
if (HorseMidlet.substate == 0) { //the first lot
if (lot_select == 0) {
HorseMidlet.firstlot_no--;
if (HorseMidlet.firstlot_no <0 )
HorseMidlet.firstlot_no = 7;
}
else {
if (HorseMidlet.firstlot_money > HorseMidlet.BIT_LOT_MONEY) {

HorseMidlet.firstlot_money -= HorseMidlet.BIT_LOT_MONEY;
HorseMidlet.mytotalmoney += HorseMidlet.BIT_LOT_MONEY;
}
}

}
if (HorseMidlet.substate == 1) { //the second lot
if (lot_select == 0) {
HorseMidlet.secondlot_no--;
if (HorseMidlet.secondlot_no == HorseMidlet.firstlot_no)
HorseMidlet.secondlot_no--;
if (HorseMidlet.secondlot_no <0) {
HorseMidlet.secondlot_no = 7;
if (HorseMidlet.secondlot_no == HorseMidlet.firstlot_no)
HorseMidlet.secondlot_no--;

}
}
else {
if (HorseMidlet.secondlot_money > HorseMidlet.BIT_LOT_MONEY)
{
HorseMidlet.secondlot_money -= HorseMidlet.BIT_LOT_MONEY;
HorseMidlet.mytotalmoney += HorseMidlet.BIT_LOT_MONEY;
}
}

}
}
else if (HorseMidlet.gamestate==HorseMidlet.GAME_STATE_TRAINING)
{
HorseMidlet.substate=(HorseMidlet.substate+3)%4;

}



break;

case UP:
if (HorseMidlet.gamestate==HorseMidlet.GAME_STATE_START)
{
HorseMidlet.substate--;
if(HorseMidlet.substate<0)
HorseMidlet.substate=2;
}
else if (HorseMidlet.gamestate==HorseMidlet.GAME_STATE_LOT)
{
if(HorseMidlet.substate>=2)
{
HorseMidlet.substate--;
if(HorseMidlet.substate<2)
HorseMidlet.substate=4;
}
else
{
if(lot_select==0)
lot_select=1;
else
lot_select=0;
}

}
repaint();

break;

default:
break;
}
repaint();
}


}









package HorseClub;


import javax.microedition.lcdui.*;

public class RaceGround{

private static int imageWidth, imageHeight;
private static int tileWidth, tileHeight;
private static int imageSkyWidth, imageSkyHeight;
private static int tileSkyWidth, tileSkyHeight;
private static int canvasWidth, canvasHeight;
private Image[] images;
private final String BACKGROUND_NAME="raceground";
private final int BACKGROUND_FRAMES=1;
private final String SKY_NAME="racesky";
private final int SKY_FRAMES=1;

private Image[] imgTile=new Image[BACKGROUND_FRAMES];
private Image[] imgSkyTile=new Image[SKY_FRAMES];

private static Image imgBackground;
private static Image imgSky;
private static int xBackground, yBackground,xSky;
int yStart=30;
int ySkyStart=0;


public RaceGround()
{

// load the images from the .jar
imgTile=getImages(BACKGROUND_NAME,BACKGROUND_FRAMES);
imgSkyTile=getImages(SKY_NAME,SKY_FRAMES);

// set the canvas measurements to local class variables.
canvasWidth=(HorseCanvas.getCanvasWidth());
canvasHeight=(HorseCanvas.getCanvasHeight());

//prepare the background paras
// all character frame images are assumed
// to be the same height and width
tileWidth=imgTile[0].getWidth();
tileHeight=imgTile[0].getHeight();

// set the background image dimensions
imageWidth=((canvasWidth/tileWidth)*tileWidth)+(tileWidth*2);
imageHeight=tileHeight;//((canvasWidth/tileHeight)*tileHeight)+tileHeight;



//prepare the sky paras
tileSkyWidth=imgSkyTile[0].getWidth();
tileSkyHeight=imgSkyTile[0].getHeight();

// set the background image dimensions
imageSkyWidth=((canvasWidth/tileSkyWidth)*tileSkyWidth)+(tileSkyWidth*2);

imageSkyHeight=tileSkyHeight;


init();

}
public void init()
{
xBackground=0;
yBackground=-tileHeight;
xSky=0;
ySkyStart=0;
createImage();
createSkyImage();
setImage();


}
public Image getImage(){return imgBackground;}
public Image getSkyImage(){return imgSky;}


public void draw(Graphics g)
{
g.drawImage(getImage(),xBackground,yStart,g.LEFT|g.TOP);
g.drawImage(getSkyImage(),xSky,ySkyStart,g.LEFT|g.TOP);
}

public void setPosition(int offset)
{
//move the background to provide animation of moving landscape.
boolean reset=false;

// if the image exceeds the x-axis then reset it
// and set the starting point for the next row in the image array
// set the redraw flag for redrawing the image
xBackground=xBackground-offset;
if(xBackground<=-tileWidth)
{
xBackground=0;
reset=true;
}
xSky=xSky-1;
if(xSky<=-tileSkyWidth)
{
xSky=0;
reset=true;
}


// if the images exceeds the y-axis then reset it
// and set the stating point for the next column in the image array
// set the redraw flag for redrawing the image
//if(++yBackground==0)
//{
// yBackground=-tileHeight;
// if(--xStart<0)
// {
// xStart=9;
// }
// reset=true;
//}

// redraw the background image if reset=true
if(reset)
{
// setImage();
}
}

private static void createImage()
{
// allocate memory to store the onscreen background image.
// assuming all tiles are the same size, make the buffer
// the screen size plus one row and column.
imgBackground=Image.createImage(imageWidth, imageHeight);
}

private static void createSkyImage()
{
// allocate memory to store the onscreen background image.
// assuming all tiles are the same size, make the buffer
// the screen size plus one row and column.
imgSky=Image.createImage(imageSkyWidth, imageSkyHeight);
}


public void setImage()
{
Graphics g = imgBackground.getGraphics();
Graphics gSky = imgSky.getGraphics();

int currentWidth=0;
int currentHeight=(imageHeight-tileHeight);

// draw the tiles within the bounds of the allocated image buffer
// draw from the starting from the current position within the image array
.
while (currentHeight>=0)
{
while (currentWidth {
g.drawImage(imgTile[0],currentWidth,currentHeight,g.LEFT | g.TOP);
currentWidth+=tileWidth;
}

currentWidth=0;
currentHeight-=tileHeight;
}

//Title the Sky

currentWidth=0;
currentHeight=(imageSkyHeight-tileSkyHeight);

while (currentHeight>=0)
{
while (currentWidth {
gSky.drawImage(imgSkyTile[0],currentWidth,currentHeight,g.LEFT | g.TOP);

currentWidth+=tileSkyWidth;
}

currentWidth=0;
currentHeight-=tileSkyHeight;
}


}

private Image[] getImages(String imgName, int frames)
{
return HorseMidlet.getImages(imgName, frames);
}
}

↑返回目录
前一篇: J2ME学习(一)——J2ME开发工具和厂商SDK介绍
后一篇: Mophun &