Android打飞机游戏源代码_第1页
Android打飞机游戏源代码_第2页
Android打飞机游戏源代码_第3页
Android打飞机游戏源代码_第4页
Android打飞机游戏源代码_第5页
已阅读5页,还剩27页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、package com.pg;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Paint;import android.view.KeyEvent;/* * author Himi * */public class Player /主角的血量与血量位图/默认3血private int 主角的血量 = 3;private Bitmap 主角的血量位图;/主角的坐标以及位图public int 主角的坐标x, 主角的坐标y;private Bitmap 主角的位图;/主角移动

2、速度private int 主角移动速度 = 5;/主角移动标识(基础章节已讲解,你懂得)private boolean isUp, isDown, isLeft, isRight;/碰撞后处于无敌时间/计时器private int 无敌计时器 = 0;/因为无敌时间private int 无敌时间 = 60;/是否碰撞的标识位private boolean 是否碰撞;/主角的构造函数public Player(Bitmap bmpPlayer, Bitmap bmpPlayerHp) this.主角的位图 = bmpPlayer;this.主角的血量位图 = bmpPlayerHp;主角的坐

3、标x = MySurfaceView.screenW / 2 - bmpPlayer.getWidth() / 2;主角的坐标y = MySurfaceView.screenH - bmpPlayer.getHeight();/主角的绘图函数public void draw(Canvas canvas, Paint paint) /绘制主角/当处于无敌时间时,让主角闪烁if (是否碰撞) /每2次游戏循环,绘制一次主角if (无敌计时器 % 2 = 0) canvas.drawBitmap(主角的位图, 主角的坐标x, 主角的坐标y, paint); else canvas.drawBitma

4、p(主角的位图, 主角的坐标x, 主角的坐标y, paint);/绘制主角血量for (int i = 0; i = MySurfaceView.screenW) 主角的坐标x = MySurfaceView.screenW - 主角的位图.getWidth(); else if (主角的坐标x = MySurfaceView.screenH) 主角的坐标y = MySurfaceView.screenH - 主角的位图.getHeight(); else if (主角的坐标y = 无敌时间) /无敌时间过后,接触无敌状态及初始化计数器是否碰撞 = false;无敌计时器 = 0;/设置主角血

5、量public void setPlayerHp(int hp) this.主角的血量 = hp;/获取主角血量public int getPlayerHp() return 主角的血量;/判断碰撞(主角与敌机)public boolean isCollsionWith(Enemy en) /是否处于无敌时间if (是否碰撞 = false) int x2 = en.敌机坐标x;int y2 = en.敌机坐标y;int w2 = en.敌机每帧的宽;int h2 = en.敌机每帧的高;if (主角的坐标x = x2 & 主角的坐标x = x2 + w2) return false; els

6、e if (主角的坐标x = x2 & 主角的坐标x + 主角的位图.getWidth() = y2 & 主角的坐标y = y2 + h2) return false; else if (主角的坐标y = y2 & 主角的坐标y + 主角的位图.getHeight() = x2 & 主角的坐标x = x2 + w2) return false; else if (主角的坐标x = x2 & 主角的坐标x + 主角的位图.getWidth() = y2 & 主角的坐标y = y2 + h2) return false; else if (主角的坐标y = y2 & 主角的坐标y + 主角的位图.

7、getHeight() MySurfaceView.screenH) bg1y = bg2y - bmp游戏背景的图片1.getHeight() + 111;/当第二张图片的Y坐标超出屏幕,/立即将其坐标设置到第一张图的上方if (bg2y MySurfaceView.screenH) bg2y = bg1y - bmp游戏背景的图片1.getHeight() + 111;/* * */package com.pg;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Pain

8、t;/* * Boss * author Himi * */public class Boss /Boss的血量public int Boss的血量 = 50;/Boss的图片资源private Bitmap Boss的图片;/Boss坐标public int Boss坐标x, Boss坐标y;/Boss每帧的宽高public int Boss每帧的宽, Boss每帧的高;/Boss当前帧下标private int Boss当前帧下标;/Boss运动的速度private int Boss运动的速度 = 5;/Boss的运动轨迹/一定时间会向着屏幕下方运动,并且发射大范围子弹,(是否狂态)/正常

9、状态下 ,子弹垂直朝下运动private boolean 子弹垂直朝下;/进入疯狂状态的状态时间间隔private int 进入疯狂状态的状态时间间隔 = 200;/计数器private int count;/Boss的构造函数public Boss(Bitmap bmpBoss) this.Boss的图片 = bmpBoss;Boss每帧的宽 = bmpBoss.getWidth() / 10;Boss每帧的高 = bmpBoss.getHeight();/Boss的X坐标居中Boss坐标x = MySurfaceView.screenW / 2 - Boss每帧的宽 / 2;Boss坐标y

10、 = 0;/Boss的绘制public void draw(Canvas canvas, Paint paint) canvas.save();canvas.clipRect(Boss坐标x, Boss坐标y, Boss坐标x + Boss每帧的宽, Boss坐标y + Boss每帧的高);canvas.drawBitmap(Boss的图片, Boss坐标x - Boss当前帧下标 * Boss每帧的宽, Boss坐标y, paint);canvas.restore();/Boss的逻辑public void logic() /不断循环播放帧形成动画Boss当前帧下标+;if (Boss当前帧

11、下标 = 10) Boss当前帧下标 = 0;/没有疯狂的状态if (子弹垂直朝下 = false) Boss坐标x += Boss运动的速度;if (Boss坐标x + Boss每帧的宽 = MySurfaceView.screenW) Boss运动的速度 = -Boss运动的速度; else if (Boss坐标x = 0) Boss运动的速度 = -Boss运动的速度;count+;if (count % 进入疯狂状态的状态时间间隔 = 0) 子弹垂直朝下 = true;Boss运动的速度 = 24;/疯狂的状态 else Boss运动的速度 -= 1;/当Boss返回时创建大量子弹if

12、 (Boss运动的速度 = 0) /添加8方向子弹MySurfaceView.Boss的子弹容器.add(new Bullet(MySurfaceView.bmpBossBullet, Boss坐标x+40, Boss坐标y+10, Bullet.Boss的子弹, Bullet.DIR_UP);MySurfaceView.Boss的子弹容器.add(new Bullet(MySurfaceView.bmpBossBullet, Boss坐标x+40, Boss坐标y+10, Bullet.Boss的子弹, Bullet.DIR_DOWN);MySurfaceView.Boss的子弹容器.add

13、(new Bullet(MySurfaceView.bmpBossBullet, Boss坐标x+40, Boss坐标y+10, Bullet.Boss的子弹, Bullet.DIR_LEFT);MySurfaceView.Boss的子弹容器.add(new Bullet(MySurfaceView.bmpBossBullet, Boss坐标x+40, Boss坐标y+10, Bullet.Boss的子弹, Bullet.DIR_RIGHT);MySurfaceView.Boss的子弹容器.add(new Bullet(MySurfaceView.bmpBossBullet, Boss坐标x+

14、40, Boss坐标y+10, Bullet.Boss的子弹, Bullet.DIR_UP_LEFT);MySurfaceView.Boss的子弹容器.add(new Bullet(MySurfaceView.bmpBossBullet, Boss坐标x+40, Boss坐标y+10, Bullet.Boss的子弹, Bullet.DIR_UP_RIGHT);MySurfaceView.Boss的子弹容器.add(new Bullet(MySurfaceView.bmpBossBullet, Boss坐标x+40, Boss坐标y+10, Bullet.Boss的子弹, Bullet.DIR_

15、DOWN_LEFT);MySurfaceView.Boss的子弹容器.add(new Bullet(MySurfaceView.bmpBossBullet, Boss坐标x+40, Boss坐标y+10, Bullet.Boss的子弹, Bullet.DIR_DOWN_RIGHT);Boss坐标y += Boss运动的速度;if (Boss坐标y = x2 & Boss坐标x = x2 + w2) return false; else if (Boss坐标x = x2 & Boss坐标x + Boss每帧的宽 = y2 & Boss坐标y = y2 + h2) return false; el

16、se if (Boss坐标y = y2 & Boss坐标y + Boss每帧的高 = y2) return false;return true;/设置Boss血量public void setHp(int hp) this.Boss的血量 = hp;=package com.pg;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Paint;/* * author Himi * */public class Boom /爆炸效果资源图private Bitmap 爆炸图;/

17、爆炸效果的位置坐标private int 爆炸图X, 爆炸图Y;/爆炸动画播放当前的帧下标private int 爆炸动画播放当前的帧下标;/爆炸效果的总帧数private int 爆炸效果的总帧数;/每帧的宽高private int 每帧的宽, 每帧的高;/是否播放完毕,优化处理public boolean 是否播放完毕;/爆炸效果的构造函数public Boom(Bitmap bmpBoom, int x, int y, int totleFrame) this.爆炸图 = bmpBoom;this.爆炸图X = x;this.爆炸图Y = y;this.爆炸效果的总帧数 = totleF

18、rame;每帧的宽 = bmpBoom.getWidth() / totleFrame;每帧的高 = bmpBoom.getHeight();/爆炸效果绘制public void draw(Canvas canvas, Paint paint) canvas.save();canvas.clipRect(爆炸图X, 爆炸图Y, 爆炸图X + 每帧的宽, 爆炸图Y + 每帧的高);canvas.drawBitmap(爆炸图, 爆炸图X - 爆炸动画播放当前的帧下标 * 每帧的宽, 爆炸图Y, paint);canvas.restore();/爆炸效果的逻辑public void logic()

19、if (爆炸动画播放当前的帧下标 爆炸效果的总帧数) 爆炸动画播放当前的帧下标+; else 是否播放完毕 = true;package com.pg;import android.app.Activity;import android.os.Bundle;import android.view.Window;import android.view.WindowManager;public class MainActivity extends Activity public static MainActivity instance;Overridepublic void onCreate(Bu

20、ndle savedInstanceState) super.onCreate(savedInstanceState);instance = this;/设置全屏this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);requestWindowFeature(Window.FEATURE_NO_TITLE);/显示自定义的SurfaceView视图setContentView(new MySurfaceView(this);

21、package com.pg;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Paint;/* * author Himi * */public class Bullet /子弹图片资源public Bitmap 子弹图片;/子弹的坐标public int 子弹X, 子弹Y;/子弹的速度public int 子弹的速度;/子弹的种类以及常量public int 子弹的种类;/主角的public static final int 主角的子弹 = -1;/鸭子的public

22、static final int 鸭子的子弹 = 1;/苍蝇的public static final int 苍蝇的子弹 = 2;/Boss的public static final int Boss的子弹 = 3;/子弹是否超屏, 优化处理public boolean 子弹是否超屏;/Boss疯狂状态下子弹相关成员变量private int 当前Boss子弹方向;/当前Boss子弹方向/8方向常量public static final int DIR_UP = -1;public static final int DIR_DOWN = 2;public static final int DIR

23、_LEFT = 3;public static final int DIR_RIGHT = 4;public static final int DIR_UP_LEFT = 5;public static final int DIR_UP_RIGHT = 6;public static final int DIR_DOWN_LEFT = 7;public static final int DIR_DOWN_RIGHT = 8;/子弹当前方向public Bullet(Bitmap bmpBullet, int bulletX, int bulletY, int bulletType) this.

24、子弹图片 = bmpBullet;this.子弹X = bulletX;this.子弹Y = bulletY;this.子弹的种类 = bulletType;/不同的子弹类型速度不一switch (bulletType) case 主角的子弹:子弹的速度 = 4;break;case 鸭子的子弹:子弹的速度 = 3;break;case 苍蝇的子弹:子弹的速度 = 4;break;case Boss的子弹:子弹的速度 = 5;break;/* * 专用于处理Boss疯狂状态下创建的子弹 * param bmpBullet * param bulletX * param bulletY * pa

25、ram bulletType * param Dir */public Bullet(Bitmap bmpBullet, int bulletX, int bulletY, int bulletType, int dir) this.子弹图片 = bmpBullet;this.子弹X = bulletX;this.子弹Y = bulletY;this.子弹的种类 = bulletType;子弹的速度 = 5;this.当前Boss子弹方向 = dir;/子弹的绘制public void draw(Canvas canvas, Paint paint) canvas.drawBitmap(子弹图

26、片, 子弹X, 子弹Y, paint);/子弹的逻辑public void logic() /不同的子弹类型逻辑不一/主角的子弹垂直向上运动switch (子弹的种类) case 主角的子弹:子弹Y -= 子弹的速度;if (子弹Y MySurfaceView.screenH) 子弹是否超屏 = true;break;/Boss疯狂状态下的8方向子弹逻辑case Boss的子弹:/Boss疯狂状态下的子弹逻辑待实现switch (当前Boss子弹方向) /方向上的子弹case DIR_UP:子弹Y -= 子弹的速度;break;/方向下的子弹case DIR_DOWN:子弹Y += 子弹的速度

27、;break;/方向左的子弹case DIR_LEFT:子弹X -= 子弹的速度;break;/方向右的子弹case DIR_RIGHT:子弹X += 子弹的速度;break;/方向左上的子弹case DIR_UP_LEFT:子弹Y -= 子弹的速度;子弹X -= 子弹的速度;break;/方向右上的子弹case DIR_UP_RIGHT:子弹X += 子弹的速度;子弹Y -= 子弹的速度;break;/方向左下的子弹case DIR_DOWN_LEFT:子弹X -= 子弹的速度;子弹Y += 子弹的速度;break;/方向右下的子弹case DIR_DOWN_RIGHT:子弹Y += 子弹的

28、速度;子弹X += 子弹的速度;break;/边界处理if (子弹Y MySurfaceView.screenH | 子弹Y MySurfaceView.screenW | 子弹X = -40) 子弹是否超屏 = true;break;package com.pg;import java.util.Random;import java.util.Vector;import android.content.Context;import android.content.res.Resources;import android.graphics.Bitmap;import android.graph

29、ics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.view.KeyEvent;import android.view.MotionEvent;import android.view.SurfaceHolder;import android.view.SurfaceView;import android.view.SurfaceHolder.Callback;/* * * author Himi *

30、*/public class MySurfaceView extends SurfaceView implements Callback, Runnable private SurfaceHolder sfh;private Paint paint;private Thread th;private boolean flag;private Canvas canvas;public static int screenW, screenH;/定义游戏状态常量public static final int 游戏菜单 = 0;/游戏菜单public static final int 游戏中 = 1;

31、/游戏中public static final int 游戏胜利 = 2;/游戏胜利public static final int 游戏失败 = 3;/游戏失败public static final int GAME_PAUSE = -1;/游戏菜单/当前游戏状态(默认初始在游戏菜单界面)public static int 当前游戏状态 = 游戏菜单;/声明一个Resources实例便于加载图片private Resources res = this.getResources();/声明游戏需要用到的图片资源(图片声明)private Bitmap bmpBackGround;/游戏背景pri

32、vate Bitmap bmpBoom;/爆炸效果private Bitmap bmpBoosBoom;/Boos爆炸效果private Bitmap bmpButton;/游戏开始按钮private Bitmap bmpButtonPress;/游戏开始按钮被点击private Bitmap bmpEnemyDuck;/怪物鸭子private Bitmap bmpEnemyFly;/怪物苍蝇private Bitmap bmpEnemyBoos;/怪物猪头Boosprivate Bitmap bmpGameWin;/游戏胜利背景private Bitmap bmpGameLost;/游戏失败

33、背景private Bitmap bmpPlayer;/游戏主角飞机private Bitmap bmpPlayerHp;/主角飞机血量private Bitmap bmpMenu;/菜单背景public static Bitmap bmpBullet;/子弹public static Bitmap bmpEnemyBullet;/敌机子弹public static Bitmap bmpBossBullet;/Boss子弹/声明一个菜单对象private GameMenu 菜单;/声明一个滚动游戏背景对象private GameBg 滚动游戏背景对象;/声明主角对象private Player

34、 主角;/声明一个敌机容器private Vector 敌机容器;/每次生成敌机的时间(毫秒)private int 每次生成敌机的时间 = 50;private int 计数器;/计数器/敌人数组:1和2表示敌机的种类,-1表示Boss/二维数组的每一维都是一组怪物private int enemyArray = 1, 2 , 1, 1 , 1, 3, 1, 2 , 1, 2 , 2, 3 , 3, 1, 3 , 2, 2 , 1, 2 , 2, 2 , 1, 3, 1, 1 , 2, 1 , 1, 3 , 2, 1 , -1 ;/当前取出一维数组的下标private int 当前取出一维数

35、组的下标;/是否出现Boss标识位private boolean 是否出现Boss;/随机库,为创建的敌机赋予随即坐标private Random random;/敌机子弹容器private Vector 敌机子弹容器;/添加子弹的计数器private int 添加子弹的计数器;/主角子弹容器private Vector 主角子弹容器;/添加子弹的计数器private int 主角添加子弹的计数器;/爆炸效果容器private Vector 爆炸效果容器;/声明Bossprivate Boss boss;/Boss的子弹容器public static Vector Boss的子弹容器;/* *

36、 SurfaceView初始化函数 */public MySurfaceView(Context context) super(context);sfh = this.getHolder();sfh.addCallback(this);paint = new Paint();paint.setColor(Color.WHITE);paint.setAntiAlias(true);setFocusable(true);setFocusableInTouchMode(true);/设置背景常亮this.setKeepScreenOn(true);/* * SurfaceView视图创建,响应此函数

37、 */public void surfaceCreated(SurfaceHolder holder) screenW = this.getWidth();screenH = this.getHeight();initGame();flag = true;/实例线程th = new Thread(this);/启动线程th.start();/* * 自定义的游戏初始化函数 */private void initGame() /放置游戏切入后台重新进入游戏时,游戏被重置!/当游戏状态处于菜单时,才会重置游戏if (当前游戏状态 = 游戏菜单) /加载游戏资源bmpBackGround = Bit

38、mapFactory.decodeResource(res, R.drawable.background);bmpBoom = BitmapFactory.decodeResource(res, R.drawable.boom);bmpBoosBoom = BitmapFactory.decodeResource(res, R.drawable.boos_boom);bmpButton = BitmapFactory.decodeResource(res, R.drawable.button);bmpButtonPress = BitmapFactory.decodeResource(res,

39、 R.drawable.button_press);bmpEnemyDuck = BitmapFactory.decodeResource(res, R.drawable.enemy_duck);bmpEnemyFly = BitmapFactory.decodeResource(res, R.drawable.enemy_fly);bmpEnemyBoos = BitmapFactory.decodeResource(res, R.drawable.enemy_pig);bmpGameWin = BitmapFactory.decodeResource(res, R.drawable.gam

40、ewin);bmpGameLost = BitmapFactory.decodeResource(res, R.drawable.gamelost);bmpPlayer = BitmapFactory.decodeResource(res, R.drawable.player);bmpPlayerHp = BitmapFactory.decodeResource(res, R.drawable.hp);bmpMenu = BitmapFactory.decodeResource(res, R.drawable.menu);bmpBullet = BitmapFactory.decodeResource(res, R.drawable.bullet);bmpEnemyBullet = BitmapFactory.decodeResource(res, R.drawable.bullet_enemy);bmpBossB

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论