📄 gamestart.java
字号:
package Src;
/*
* 精灵类开始运行
*/
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class Gamestart extends GameCanvas implements Runnable {
private boolean isPlay;//是否开始
private long delay; //延迟时间
private Plane plane;
private Bullets bullets;
private Image Planeimg,bulletimg,backgroudimg;
private int width,heigh;
private int planex,planey;//飞机x,y坐标
private int Gamedef;//游戏难度
//定义缓冲区
private Image buffer;
private FugitiveGame midlet;
private int back;
public Gamestart(int def,FugitiveGame midlet) {
super(true);
super.setFullScreenMode(true);
delay =50;
try{
Planeimg=Image.createImage("/res/Plane.png");
bulletimg=Image.createImage("/res/bullet.png");
backgroudimg=Image.createImage("/res/GameBack.png");
}catch(Exception e){}
width=this.getWidth();
heigh=this.getHeight();
this.midlet=midlet;
back=1;
//缓冲区
buffer=Image.createImage(this.getWidth(),this.getHeight());
//资源加载
plane=new Plane(Planeimg,30,40);
bullets=new Bullets(bulletimg,5,5);
this.Gamedef=def;
plane.Planeinto(width,heigh);
bullets.intonum(15,width,heigh);
this.paintBuffer();
}
private void paintBuffer()//画缓冲区
{
Graphics g=buffer.getGraphics();
g.drawImage(this.backgroudimg, 0, 0,g.TOP|g.LEFT);
plane.DrawPlane(g);//绘制飞机
bullets.draw(g);//绘制子弹
}
public void start() {
isPlay = true;
new Thread(this).start();
}
public void stop() { isPlay = false; }
public void run() {
Graphics g = getGraphics();
long newtime;
int alltime=0;//记录延迟时间,然后计算存活时间
while (isPlay) {
alltime+=(int)delay;
newtime=System.currentTimeMillis();
this.drawScreen(g);
g.setColor(0xffffff);
flushGraphics();
if (bullets.collidesWith(plane))
{
this.midlet.Gametime=alltime;
this.midlet.GameStop();
isPlay=false;
}
Planeinput();
bullets.fly();
this.paintBuffer();
newtime=System.currentTimeMillis()-newtime;
try{
if (newtime<this.delay)
{
Thread.currentThread().sleep(delay-newtime);
}
}
catch (InterruptedException ie) {}
}
}
private void Planeinput() {
int keyStates = getKeyStates();
if ((keyStates & LEFT_PRESSED) != 0)
{
planex=plane.GetPosionx();
plane.setPosionx(Math.max(30,planex-1));
}
if ((keyStates & RIGHT_PRESSED) !=0 )
{
planex=plane.GetPosionx();
plane.setPosionx(Math.min(width,planex+1));
}
if ((keyStates & UP_PRESSED) != 0)
{
planey=plane.GetPosiony();
plane.setPosiony(Math.max(40,planey-1));
}
if ((keyStates & DOWN_PRESSED) !=0)
{
planey=plane.GetPosiony();
plane.setPosiony(Math.min(heigh,planey+1));
}
}
private void drawScreen(Graphics g) {
g.drawImage(this.buffer, 0, 0,g.TOP|g.LEFT);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -