📄 mygame.java
字号:
if (player.collidesWith(spjiguan1,false))
{
sprock.isOK = true;
spjiguan1.setPosition(-50,-50);
lm.remove(spjiguan1);
}
if (sprock.collidesWith(spguai2,false))
{
spguai2.isDie = true;
sprock.isOK = false;
sprock.setPosition(-100,-100);
lm.remove(sprock);
}
if (player.collidesWith(spmen,false) && isOK)
{
GetXue = spxue.getFrame();
GetShengming= spshengming.getFrame();
try
{
isKey = false;
flag = false;
pp.Music_close();
Thread.sleep(1000);
}
catch(Exception e) {}
}
break;
}
}
public void INPUT_KEY()
{
int keystate = getKeyStates();
if (score >= 2000 * i && score < 2000 * (i+1))
{
isMOV = true;
}
else
{
isMOV = false;
}
if (player.getX() < 9)
{
player.setPosition(9,player.getY());
}
if (player.getX() > this.getWidth() - player.getWidth() - 5)
{
player.setPosition(getWidth() - player.getWidth() - 5,player.getY());
}
if (sprock.getX() < 10)
{
sprock.setPosition(10,sprock.getY());
}
if (sprock.getX() > this.getWidth() - sprock.getWidth() - 5)
{
sprock.setPosition(getWidth() - sprock.getWidth() - 5,sprock.getY());
}
if (((keystate & UP_PRESSED) != 0) && !GamePlayer.isGoU)
{
if (isJump)
{
player.moveJump();
}
}
if((keystate & UP_PRESSED)!=0 && GamePlayer.isGoU )
{
if (isU)
{
player.Up();
}
}
if ((keystate & LEFT_PRESSED) != 0)
{
if (isL)
{
player.moveLeft();
if ( stage == 2 )
{
if (player.collidesWith(sprock,false))
{
if (player.getX() >= sprock.getX() +sprock.getWidth() /2 )
{
sprock.move(-2,0);
player.setPosition(sprock.getX()+sprock.getWidth() - 1,player.getY());
}
}
}
}
}
if ((keystate & RIGHT_PRESSED) != 0)
{
if (isR)
{
player.moveRight();
if (stage == 2 )
{
if (player.collidesWith(sprock,false))
{
if (player.getX() + player.getWidth() <= sprock.getX() +sprock.getWidth() /2 )
{
sprock.move(2,0);
player.setPosition(sprock.getX()-player.getWidth()+ 1,player.getY());
}
}
}
}
}
if ((keystate & DOWN_PRESSED)!=0)
{
if (isD)
{
player.Down();
}
}
if (!GameSpringe.isNoDown)
{
if ((keystate & UP_PRESSED)!= 0
||(keystate & DOWN_PRESSED)!= 0)
{
switch (stage)
{
case 2:
if (player.collidesWith(splouti,false)
&& MyGame.player.getX() + 3 < splouti.getX()
&& player.getX() +MyGame.player.getWidth() - 3> splouti.getX()
&& !GamePlayer.isDie)
{
player.setFrame(20);
isUandD = true;
splouti.isOK = true;
isDown = false;
isU = true;
isD = true;
isL = false;
isR = false;
}
else
{
isUandD = false;
splouti.isOK = false;
isDown = true;
isU = false;
isD = false;
isL = true;
isR = true;
}
break;
case 3:
if (player.collidesWith(splouti,false)
&& MyGame.player.getX() + 3 < splouti.getX()
&& MyGame.player.getX() +MyGame.player.getWidth() - 3> splouti.getX()
&& !GamePlayer.isDie)
{
player.setFrame(20);
isUandD = true;
splouti.isOK = true;
isDown = false;
isU = true;
isD = true;
isL = false;
isR = false;
}
else if (player.collidesWith(sprock,false)
&& MyGame.player.getX() + 3 < sprock.getX()
&& MyGame.player.getX() +MyGame.player.getWidth() - 3> sprock.getX()
&& !GamePlayer.isDie)
{
player.setFrame(20);
isUandD = true;
sprock.isOK = true;
isDown = false;
isU = true;
isD = true;
isL = false;
isR = false;
}
else
{
isUandD = false;
splouti.isOK = false;
sprock.isOK = false;
isDown = true;
isU = false;
isD = false;
isL = true;
isR = true;
}
break;
case 4:
if (player.collidesWith(splouti,false)
&& MyGame.player.getX() + 3 < splouti.getX()
&& MyGame.player.getX() +MyGame.player.getWidth() - 3> splouti.getX()
&& !GamePlayer.isDie)
{
player.setFrame(20);
isUandD = true;
splouti.isOK = true;
isDown = false;
isU = true;
isD = true;
isL = false;
isR = false;
}
else if (player.collidesWith(splouti1,false)
&& MyGame.player.getX() + 3 < splouti1.getX()
&& MyGame.player.getX() +MyGame.player.getWidth() - 3> splouti1.getX()
&& !GamePlayer.isDie)
{
player.setFrame(20);
isUandD = true;
splouti1.isOK = true;
isDown = false;
isU = true;
isD = true;
isL = false;
isR = false;
}
else if (player.collidesWith(splouti2,false)
&& MyGame.player.getX() + 3 < splouti2.getX()
&& MyGame.player.getX() +MyGame.player.getWidth() - 3> splouti2.getX()
&& !GamePlayer.isDie)
{
player.setFrame(20);
isUandD = true;
splouti2.isOK = true;
isDown = false;
isU = true;
isD = true;
isL = false;
isR = false;
}
else
{
isUandD = false;
splouti.isOK = false;
splouti1.isOK = false;
splouti2.isOK = false;
isDown = true;
isU = false;
isD = false;
isL = true;
isR = true;
}
break;
}
}
}
}
public void mpaint(Graphics g)
{
g.setColor(0,0,0);
g.fillRect(0,0,getWidth(),getHeight());
lm.paint(g,0,0);
if (!ispause)
{
g.drawImage(caidan,getWidth(),getHeight(),Graphics.BOTTOM|Graphics.RIGHT);
}
g.setColor(120,190,190);
g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_SMALL));
g.drawString(String.valueOf(score),this.getWidth(),0,Graphics.TOP|Graphics.RIGHT);
flushGraphics();
}
protected void keyPressed(int key)
{
if (isKey)
{
if (key == -7)
{
ispause = true;
pp.Music_stop();
GameMenu.display.setCurrent(MyCommand.getInstance());
}
}
}
int rest = 70;
public void run()
{
long st = 0;
long et = 0;
Graphics g = getGraphics();
for(stage = 2;stage <= 4;stage++)
{
flag = true;
g.setColor(0,0,0);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(255,255,0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE));
g.drawString("第 " + (stage -1)+ " 关",getWidth()/2-30,getHeight()/2-10,Graphics.TOP|Graphics.LEFT);
flushGraphics();
try
{
Thread.sleep(1000);
} catch (InterruptedException e1) {}
// 调用创建关卡的方法
CREAT_STAGE();
try
{
Thread.sleep(500);
}
catch(Exception e)
{
System.out.println("sleep出问题");
}
try
{
Thread.sleep(500);
}
catch (InterruptedException e1)
{}
mpaint(g);
isKey = true;
//游戏正式开始
while(flag)
{
//判断是否按下暂停
while(ispause)
{
try
{
Thread.sleep(100);
}
catch(Exception e){}
}
st = System.currentTimeMillis();
StartGameTime = System.currentTimeMillis();
//调用按键方法
INPUT_KEY();
//调用各种判断和行动的方法
PENG_ZHUANG();
//调用刷新画面的方法
movMing();
mpaint(g);
et = System.currentTimeMillis();
if((et-st)<rest)
{
try
{
Thread.sleep(rest-(et-st));
}catch(Exception e){}
}
}
//判断是否输掉游戏
if(isEndGame)
{
if(islose)
{
EedGameTime = System.currentTimeMillis();
g.setColor(0,0,0);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(255,255,0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_LARGE));
g.drawString("你失败了!",getWidth()/2-40,getHeight()/2-20,Graphics.TOP|Graphics.LEFT);
flushGraphics();
try
{
Thread.sleep(3000);
}catch(Exception e){}
// init_Game();
break;
}
}
}
//判断是否赢得游戏
if(isEndGame)
{
if(!islose)
{
EedGameTime = System.currentTimeMillis();
g.setColor(0,0,0);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(255,255,0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_LARGE));
g.drawString("你胜利了!",getWidth()/2-40,getHeight()/2-20,Graphics.TOP|Graphics.LEFT);
flushGraphics();
try
{
Thread.sleep(3000);
}catch(Exception e){}
// init_Game();
}
// GetScore();
if (score > 0)
{
GameName in = new GameName();
GameMenu.display.setCurrent(in);
in.start();
}
else
{
GameMenu.current = GameMenu.MY_MENU;
GameMenu.show();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -