1.添加两张或更多图片,x,y1坐标为屏幕的顶点,y2= y1-photo.getHeight();
2.进行两张图片的逻辑判断:
if (y1>=MySurfaceView.height) {
y1=y2-photo.getHeight();
}if (y2>=MySurfaceView.height){
y2=y1-photo.getHeight();
}
1.定义飞机初始坐标
x=MySurfaceView.width/2-(MyPlane.getWidth()/2);
y=MySurfaceView.height-MyPlane.getHeight();
2.显示飞机canvas.drawBitmap(MyPlane,x,y,paint);
3.MotionEvent event动作监听事件,判断点击移动飞机
public void onTouch(MotionEvent event){
if (event.getAction() == MotionEvent.ACTION_MOVE){
float ex = event.getX();
float ey = event.getY();
if (ex>x&&exy&&eyint) ex-width/2;
y = (int) ey-height/2;
if(y<0){
y=0;
}
if (y+height>MySurfaceView.height) {
y=MySurfaceView.height-height;
}
}
}
}
1.绘制canvas.drawBitmap(bullet,x,y,paint);
2.switch判断:0为我方子弹,1为敌机子弹
switch(type){
case 0:
y-=speed;
if (y<0){
isDead=true;
}
break;
case 1:
y+=Bspeed;
if (y>MySurfaceView.height){
isDead=true;
}
break;
}
3.isDead()与bullet数量判断
for (int i=0;i<mybulletVector.size();i++) {
if (mybulletVector.elementAt(i).isDead()) {
mybulletVector.remove(i);
}
}
for (int i=0;i<bossBulletVector.size();i++) {
if (bossBulletVector.elementAt(i).isDead()) {
bossBulletVector.remove(i);
}
}
public boolean isCollision(BossPlane bossPlane) {
if (noCollision) {
return false;
} else {
if (x + MyPlane.getWidth() < bossPlane.getX() || x > bossPlane.getX() + bossPlane.getFrameW() || y > bossPlane.getY() + bossPlane.getFrameH() || y + MyPlane.getHeight() < bossPlane.getY()) {
} else {
HpCount--;
x=MySurfaceView.width/2-(MyPlane.getWidth()/2);
y=MySurfaceView.height-MyPlane.getHeight();
noCollision = true;
return true;
}
}
return false;
}
public boolean isCollision(Mybullet bullet){
if(noCollision){
return false;
}else{
if (bullet.getX()>x&&bullet.getX()y&&bullet.getY()if (HpCount>0){
HpCount--;
x=MySurfaceView.width/2-(MyPlane.getWidth()/2);
y=MySurfaceView.height-MyPlane.getHeight();
}
noCollision=true;
return true;
}
}
return false;
}
public void Appear(Canvas canvas, Paint paint){
if (HpCount<=0){
MySurfaceView.Game_STATE=1;
}
if(noCollision){
noCollisionCount++;
if(noCollisionCount%10==0){
canvas.drawBitmap(MyPlane,x,y,paint);
}if (noCollisionCount>100){
noCollision=false;
noCollisionCount=0;
}
}else{
canvas.drawBitmap(MyPlane,x,y,paint);
}for (int i = 0;i
canvas.save();
canvas.clipRect(x,y,x+framW,y+framH);
canvas.drawBitmap(boom,x-currentFrame*framW,y,paint);//图片一帧一帧显示
canvas.restore();`
Boom boom = new Boom(BitmapFactory.decodeResource(this.getResources(),R.mipmap.boom),bossPlane.getX()+bossPlane.getFrameW()/2,bossPlane.getY(),7);
boomVector.add(boom);
}
}
//判断boss飞机爆炸 删除爆炸
for (int i =0;i
boomVector.elementAt(i).Appear(canvas,paint);
if(boomVector.elementAt(i).isEnd()){
boomVector.remove(i);
}
}
public GameSoundPool(Context context){
this.soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC,0);
s1 = soundPool.load(context,R.raw.shoot,0);
s2 = soundPool.load(context,R.raw.explosion,0);
s3 = soundPool.load(context,R.raw.explosion2,0);
}
if (count % 10 == 0) {
gameSoundPool.playSound(1);
Mybullet mybullet = new Mybullet(BitmapFactory.decodeResource(this.getResources(), R.mipmap.mybullet),myPlane.getX()+(myPlane.getWidth()/2)-15,myPlane.getY(),0);
mybulletVector.add(mybullet);
}
if (bossPlane.isCollision(mybulletVector.elementAt(i))){
gameSoundPool.playSound(2);
mybulletVector.remove(i);
Boom boom = new Boom(BitmapFactory.decodeResource(this.getResources(),R.mipmap.boom),bossPlane.getX()+bossPlane.getFrameW()/2,bossPlane.getY(),7);
boomVector.add(boom);
}
if(myPlane.isCollision(bossBulletVector.elementAt(i))){//boss子弹碰撞我的飞机
gameSoundPool.playSound(3);
bossBulletVector.remove(i);
}
public class MySurfaceView extends SurfaceView {
public MySurfaceView(Context context) {
super(context);
gameSoundPool =new GameSoundPool(context);
init();
}
}
public boolean isCollision(Mybullet bullet){
if (bullet.getX()>x&&bullet.getX()+bullet.bullet.getWidth()y&&bullet.getY()+bullet.bullet.getHeight()if (hp>0){
hp--;
}else{
MySurfaceView.Game_STATE=1;
}
return true;
}
return false;
}
public boolean isCollision(Mybullet bullet){
if(noCollision){
return false;
}else{
if (bullet.getX()>x&&bullet.getX()y&&bullet.getY()if (HpCount>0){
HpCount--;
x=MySurfaceView.width/2-(MyPlane.getWidth()/2);
y=MySurfaceView.height-MyPlane.getHeight();
}
noCollision=true;
return true;
}
}
return false;
}
public boolean isCollision(Mybullet bullet){
if(noCollision){
return false;
}else{
if (bullet.getX()>x&&bullet.getX()y&&bullet.getY()if (HpCount>0){
HpCount--;
x=MySurfaceView.width/2-(MyPlane.getWidth()/2);
y=MySurfaceView.height-MyPlane.getHeight();
}
noCollision=true;
return true;
}
}
return false;
}
public boolean isCollision(BossPlane bossPlane) {
if (noCollision) {
return false;
} else {
if (x + MyPlane.getWidth() < bossPlane.getX() || x > bossPlane.getX() + bossPlane.getFrameW() || y > bossPlane.getY() + bossPlane.getFrameH() || y + MyPlane.getHeight() < bossPlane.getY()) {
} else {
HpCount--;
x=MySurfaceView.width/2-(MyPlane.getWidth()/2);
y=MySurfaceView.height-MyPlane.getHeight();
noCollision = true;
return true;
}
}
return false;
}
public class MySurfaceView implements SurfaceHolder.Callback,Runnable {
public void run() {
} catch (Exception e) {
e.printStackTrace();
} finally {
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
实训课从本身对java基础并不是太熟悉,经过3个星期的java学到了很多,包括基础概念,简单方法应用,参数调用。比起原先进步了不少。
最后一个星期学习了AndroidStudio,从一开始听到要做一个飞机大战的游戏感觉挺有意思并且也是第一次接触,新的软件新的项目。到今天整个飞机大战的基础差不多学完了,创建背景,飞机,敌机,子弹。创建飞机闪烁,爆炸,移动飞机。创建特效,音效。虽然都是些基础但学到了不少,接下来就是个人发挥,改善,升华的时间。下次接触的时候可能是大二的这个时候,和江哥在学习上的接触差不多就到这了,平日里一呼喊就会过来帮助我们解决我们自身解决不了的问题,可能只是转个弯就能出来,但自己觉得什么也看不懂。
很感谢我们的实训老师,江哥。