📄 gameplayer.java
字号:
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
////////////////////
//游戏精灵类
// 游戏中玩家控制的精灵类,跳跃,移动,死亡,受伤等效果
///////////////////
public class GamePlayer extends Sprite
{
public int down;
public static boolean isOK,isDie,isGoU;
private final int Length = 10;
private int threadNum,currentFrame,thread;
private boolean isEnd;
public GamePlayer(Image img,int w,int h)
{
super(img,w,h);
down = -1;
threadNum = 0;
thread = 0;
isOK =false;
isDie = false;
isEnd = true;
isGoU = false;
currentFrame = 0;
this.defineCollisionRectangle(2,0,this.getWidth() - 4 ,this.getHeight());
int seq[] = {0,0,0,1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,8,9,9,9,9,8,8,8,8,9,9,9,9,10};
this.setFrameSequence(seq);
}
public void moveLeft()
{
if (!isDie)
{
if (this.getFrame() > 8 && this.getFrame() < 17)
{
this.nextFrame();
}
else
{
this.setFrame(9);
}
this.move(-2, 0);
}
}
public void moveRight()
{
if (!isDie)
{
if (this.getFrame() < 8)
{
this.nextFrame();
}
else
{
this.setFrame(0);
}
this.move(2, 0);
}
}
int jump[] = {0,0,0,0,0,0,1,0,0,0,0,0,0,1
,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1
,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1
,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1
,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1
,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1
,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,
0,0,0,0,0,0,0,0,0,0,0,1};
public void moveJump()
{
if (!isDie)
{
for (int i = 0; i < jump.length; i++)
{
this.move(0,-jump[i]);
if (i == 0)
{
down = -1;
MyGame.isJump = false;
}
}
}
}
public void moveDown()
{
this.move(0,down);
down += 1;
if(down >= 5)
{
down = 5;
}
}
public void Up()
{
if (!isDie)
{
if (this.getFrame() > 17 && this.getFrame() < 23)
{
this.nextFrame();
}
else
{
this.setFrame(18);
}
this.move(0,-2);
}
}
public void Down()
{
if (!isDie)
{
if (this.getFrame() > 17 && this.getFrame() < 23)
{
this.nextFrame();
}
else
{
this.setFrame(18);
}
this.move(0,2);
}
}
public void shoushang()
{
if (isOK && !isDie)
{
MyGame.isTime = false;
threadNum++;
if(getFrame() != getFrameSequenceLength()-1)
{
currentFrame = getFrame();
}
if ( threadNum <= 30)
{
if (threadNum % 7 == 1 || threadNum % 7 == 4)
{
this.setFrame(getFrameSequenceLength()-1);
}
else
{
this.setFrame(currentFrame);
}
}
else
{
MyGame.isTime = true;
threadNum = 0;
isOK = false;
}
}
else
{
threadNum = 0;
isOK = false;
}
}
public void shan()
{
if (isEnd)
{
MyGame.isTime = false;
thread++;
if(getFrame() != getFrameSequenceLength()-1)
{
currentFrame = getFrame();
}
if ( thread <= 30)
{
if (thread % 7 == 1 || thread % 7 == 4)
{
this.setFrame(getFrameSequenceLength()-1);
}
else
{
this.setFrame(currentFrame);
}
}
else
{
MyGame.isTime = true;
isEnd = false;
thread = 0;
}
}
}
public void die(int play_x,int play_y)
{
if (isDie)
{
MyGame.isTime =false;
if (this.getFrame() >= 24 && this.getFrame()
<= 40)
{
this.nextFrame();
if(this.getFrame() == 39)
{
this.setPosition(play_x,play_y);
this.setFrame(0);
MyGame.isTime = true;
isDie = false;
isEnd = true;
}
}
else
{
this.setFrame(24);
}
}
}
public void Jump()
{
switch (MyGame.stage)
{
case 2:
if (this.collidesWith(MyGame.tlqiang,false))
{
if(this.getX() >11 && this.getX() < 176 - this.getWidth() - 7)
{
if (this.getY() == 185
||this.getY() == 85
||this.getY() == 135)
{
MyGame.isJump = true;
}
}
}
else if (this.collidesWith(MyGame.spgunR2,false)
||this.collidesWith(MyGame.spgunD1,false)
||this.collidesWith(MyGame.spgunR1,false))
{
MyGame.isJump = true;
MyGame.isDown = false;
}
else if (this.collidesWith(MyGame.splouti,false) && MyGame.isUandD)
{
MyGame.isDown = false;
isGoU = true;
}
else
{
isGoU = false;
MyGame.isJump = false;
MyGame.isDown = true;
}
break;
case 3:
if (this.collidesWith(MyGame.tlqiang,false))
{
if(this.getX() >11 && this.getX() < 176 - this.getWidth() - 7)
{
if (this.getY() == 45
||this.getY() == 95
||this.getY() == 185
||this.getY() == 125)
{
MyGame.isJump = true;
}
}
}
else if (this.collidesWith(MyGame.spgunD1,false))
{
MyGame.isJump = true;
MyGame.isDown = false;
}
else if (this.collidesWith(MyGame.splouti,false)
&& MyGame.isUandD
|| this.collidesWith(MyGame.sprock,false)
&& MyGame.isUandD)
{
MyGame.isDown = false;
isGoU = true;
}
else
{
isGoU = false;
MyGame.isJump = false;
MyGame.isDown = true;
}
break;
case 4:
if (this.collidesWith(MyGame.tlqiang,false))
{
if(this.getX() >11 && this.getX() < 176 - this.getWidth() - 7)
{
if (this.getY() ==185 ||
this.getY() ==175 ||
this.getY() ==165 ||
this.getY() ==155 ||
this.getY() ==135 ||
this.getY() ==105 ||
this.getY() ==45 )
{
MyGame.isJump = true;
}
}
}
else if (this.collidesWith(MyGame.spgunD1,false)
||this.collidesWith(MyGame.spgunR1,false))
{
MyGame.isJump = true;
MyGame.isDown = false;
}
else if (this.collidesWith(MyGame.splouti,false)
&& MyGame.isUandD
|| this.collidesWith(MyGame.splouti1,false)
&& MyGame.isUandD
|| this.collidesWith(MyGame.splouti2,false)
&& MyGame.isUandD)
{
MyGame.isDown = false;
isGoU = true;
}
else
{
isGoU = false;
MyGame.isJump = false;
MyGame.isDown = true;
}
break;
}
}
public void isPeng(int x,int y,int Array[][],int N)
{
for(int i = 0; i < 20; i++)
{
for(int j = 0; j< 18; j++)
{
if(Array[i][j] == N)
{
if (!MyGame.isU)
{
// 上
if( Length * j <= x + this.getWidth() /2 && Length *(j + 1) >= x + this.getWidth() /2
&& 10 + Length * i <= y && 10 + Length *(i + 1) >= y)
{
this.setPosition(this.getX(), 20 + Length *(i + 1));
// System.out.println("UP: "+getY());
}
//左
if( Length * j <= x + 1 && Length *(j + 1) >= x + 1
&& 10 + Length * i <= y + this.getHeight()/2 && 10 + Length *(i + 1) >= y + this.getHeight()/2)
{
this.setPosition( Length * (j + 1),this.getY());
// System.out.println("LEFT: "+getY());
}
//右
if( Length * j <=x + this.getWidth() - 1 && Length *(j + 1) >= x + this.getWidth() -1
&& 10 + Length * i <= y + this.getHeight()/2 && 10 + Length *(i + 1) >= y + this.getHeight()/2)
{
this.setPosition(Length * j - this.getWidth() ,this.getY());
// System.out.println("RIGHT: "+getY());
}
}
//左下
if( Length * j <= x + 3&& Length *(j + 1) >= x + 3
&& 10 + Length * i <= y + this.getHeight() && 10 + Length *(i + 1) >= y + this.getHeight())
{
this.setPosition(this.getX(), Length * (i -1));
// System.out.println("LEFT_DOWN: "+getY());
}
//右下
if( Length * j <= x +this.getWidth() -3 && Length *(j + 1) >= x + this.getWidth() -3
&& 10 + Length * i <= y + this.getHeight() && 10 + Length *(i + 1) >= y + this.getHeight())
{
this.setPosition(this.getX(), Length * (i - 1));
// System.out.println("RIGHT_DOWN: "+getY()+" Length * i:"+Length * (i-1));
}
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -