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.graphics.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
*
*/
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 GAME_MENU = 0;//游戏菜单
public static final int GAMEING = 1;//游戏中
public static final int GAME_WIN = 2;//游戏胜利
public static final int GAME_LOST = 3;//游戏失败
public static final int GAME_PAUSE = -1;//游戏菜单
//当前游戏状态(默认初始在游戏菜单界面)
public static int gameState = GAME_MENU;
//声明一个Resources实例便于加载图片
private Resources res = this.getResources();
//声明游戏需要用到的图片资源(图片声明)
private Bitmap bmpBackGround;//游戏背景
private Bitmap bmpBoom;//爆炸效果
private Bitmap bmpBoosBoom;//Boos爆炸效果
private Bitmap bmpButton;//游戏开始按钮
private Bitmap bmpButtonPress;//游戏开始按钮被点击
private Bitmap bmpEnemyDuck;//怪物鸭子
private Bitmap bmpEnemyFly;//怪物苍蝇
private Bitmap bmpEnemyBoos;//怪物猪头Boos
private Bitmap bmpGameWin;//游戏胜利背景
private Bitmap bmpGameLost;//游戏失败背景
private Bitmap bmpPlayer;//游戏主角飞机
private Bitmap bmpPlayerHp;//主角飞机血量
private Bitmap bmpMenu;//菜单背景
public static Bitmap bmpBullet;//子弹
public static Bitmap bmpEnemyBullet;//敌机子弹
public static Bitmap bmpBossBullet;//Boss子弹
//声明一个菜单对象
private GameMenu gameMenu;
//声明一个滚动游戏背景对象
private GameBg backGround;
//声明主角对象
private Player player;
//声明一个敌机容器
private Vector<Enemy> vcEnemy;
//每次生成敌机的时间(毫秒)
private int createEnemyTime = 50;
private int count;//计数器
//敌人数组: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 enemyArrayIndex;
//是否出现Boss标识位
private boolean isBoss;
//随机库,为创建的敌机赋予随即坐标
private Random random;
//敌机子弹容器
private Vector<Bullet> vcBullet;
//添加子弹的计数器
private int countEnemyBullet;
//主角子弹容器
private Vector<Bullet> vcBulletPlayer;
//添加子弹的计数器
private int countPlayerBullet;
//爆炸效果容器
private Vector<Boom> vcBoom;
//声明Boss
private Boss boss;
//Boss的子弹容器
public static Vector<Bullet> vcBulletBoss;
/**
* 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视图创建,响应此函数
*/
@Override
public void surfaceCreated(SurfaceHolder holder) {
screenW = this.getWidth();
screenH = this.getHeight();
initGame();
flag = true;
//实例线程
th = new Thread(this);
//启动线程
th.start();
}
/*
* 自定义的游戏初始化函数
*/
private void initGame() {
//放置游戏切入后台重新进入游戏时,游戏被重置!
//当游戏状态处于菜单时,才会重置游戏
if (gameState == GAME_MENU) {
//加载游戏资源
bmpBackGround = BitmapFactory.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, 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.gamewin);
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);
bmpBossBullet = BitmapFactory.decodeResource(res, R.drawable.boosbullet);
//爆炸效果容器实例
vcBoom = new Vector<Boom>();
//敌机子弹容器实例
vcBullet = new Vector<Bullet>();
//主角子弹容器实例
vcBulletPlayer = new Vector<Bullet>();
//菜单类实例
gameMenu = new GameMenu(bmpMenu, bmpButton, bmpButtonPress);
//实例游戏背景
backGround = new GameBg(bmpBackGround);
//实例主角
player = new Player(bmpPlayer, bmpPlayerHp);
//实例敌机容器
vcEnemy = new Vector<Enemy>();
//实例随机库
random = new Random();
//---Boss相关
//实例boss对象
boss = new Boss(bmpEnemyBoos);
//实例Boss子弹容器
vcBulletBoss = new Vector<Bullet>();
}
}
/**
* 游戏绘图
*/
public void myDraw() {
try {
canvas = sfh.lockCanvas();
if (canvas != null) {
canvas.drawColor(Color.WHITE);
//绘图函数根据游戏状态不同进行不同绘制
switch (gameState) {
case GAME_MENU:
//菜单的绘图函数
gameMenu.draw(canvas, paint);
break;
case GAMEING:
//游戏背景
backGround.draw(canvas, paint);
//主角绘图函数
player.draw(canvas, paint);
if (isBoss == false) {
//敌机绘制
for (int i = 0; i < vcEnemy.size(); i++) {
vcEnemy.elementAt(i).draw(canvas, paint);
}
//敌机子弹绘制
for (int i = 0; i < vcBullet.size(); i++) {
vcBullet.elementAt(i).draw(canvas, paint);
}
} else {
//Boos的绘制
boss.draw(canvas, paint);
//Boss子弹逻辑
for (int i = 0; i < vcBulletBoss.size(); i++) {
vcBulletBoss.elementAt(i).draw(canvas, paint);
}
}
//处理主角子弹绘制
for (int i = 0; i < vcBulletPlayer.size(); i++) {
vcBulletPlayer.elementAt(i).draw(canvas, paint);
}
//爆炸效果绘制
for (int i = 0; i < vcBoom.size(); i++) {
vcBoom.elementAt(i).draw(canvas, paint);
}
break;
case GAME_PAUSE:
break;
case GAME_WIN:
canvas.drawBitmap(bmpGameWin, 0, 0, paint);
break;
case GAME_LOST:
canvas.drawBitmap(bmpGameLost, 0, 0, paint);
break;
}
}
} catch (Exception e) {
// TODO: handle exception
} finally {
if (canvas != null)
sfh.unlockCanvasAndPost(canvas);
}
}
/**
* 触屏事件监听
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
//触屏监听事件函数根据游戏状态不同进行不同监听
switch (gameState) {
case GAME_MENU:
//菜单的触屏事件处理
gameMenu.onTouchEvent(event);
break;
case GAMEING:
break;
case GAME_PAUSE:
break;
case GAME_WIN:
break;
case GAME_LOST:
break;
}
return true;
}
/**
* 按键按下事件监听
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//处理back返回按键
if (keyCode == KeyEvent.KEYCODE_BACK) {
//游戏胜利、失败、进行时都默认返回菜单
if (gameState == GAMEING || gameState == GAME_WIN || gameState == GAME_LOST) {
gameS
在Android平台上进行应用开发,尤其是游戏开发,是一个充满挑战与乐趣的过程。本示例是一个飞行射击游戏的实战项目,适合于计算机科学与技术专业的学生作为毕业设计参考。在这个项目中,我们将探讨Android游戏开发的基本原理、关键技术以及源码分析。 1. **Android游戏架构** - 游戏通常包含游戏引擎、渲染引擎、物理引擎、音频引擎等多个组件。在Android中,这些组件可能以Activity、Service、BroadcastReceiver等形式存在,通过Handler或AsyncTask进行异步处理。 - 项目中的主要类可能包括GameActivity(游戏主界面)、GameSurfaceView(自定义绘图视图)和GameThread(游戏逻辑线程)。 2. **OpenGL ES** - Android游戏开发常使用OpenGL ES进行图形渲染。OpenGL ES是针对嵌入式设备的图形库,用于2D和3D图形绘制。 - 游戏中的飞机、子弹、敌机等对象可能以顶点数组、纹理坐标和颜色数据的形式存储,通过OpenGL ES的绘制函数进行渲染。 3. **Android多媒体支持** - 音效和背景音乐是游戏的重要组成部分。Android提供了MediaPlayer和SoundPool类来处理音频资源,分别适用于长音频和短音效的播放。 4. **碰撞检测** - 游戏中的碰撞检测是关键算法之一,用于判断飞机、子弹和敌机是否相撞。可以使用简单的矩形碰撞检测或更复杂的几何形状碰撞算法。 5. **游戏逻辑与状态管理** - 游戏逻辑包括飞机移动、射击、敌机生成、得分计算等。这些逻辑通常在GameThread中执行,确保游戏流畅运行。 - 状态管理涉及游戏的开始、暂停、结束等状态,通过状态机模式实现。 6. **用户输入与触摸事件处理** - Android设备的触摸屏提供了丰富的交互方式。游戏需监听MotionEvent,处理用户的滑动、点击等操作,控制飞机的移动和射击。 7. **资源管理与优化** - 资源如图片、音频和纹理需要合理加载和释放,避免内存泄漏。使用AssetManager加载非资源文件,避免使用大图,进行图像压缩和纹理 atlasing。 8. **AndroidManifest配置** - 游戏的权限管理、Activity声明、硬件加速设置等都在AndroidManifest.xml中配置。例如,可能需要访问网络权限以实现在线排行榜。 9. **性能调优** - 对于性能敏感的游戏,需要关注帧率(FPS)和CPU/GPU使用率。可能需要优化渲染过程、减少不必要的计算和内存分配。 10. **毕业设计论文撰写** - 在完成项目后,你需要编写毕业设计论文,阐述项目的设计思路、关键技术、实现过程和创新点,同时分析和评估项目的效果。 这个飞行射击游戏的源码是一个很好的学习材料,可以帮助开发者了解Android游戏开发的全貌,从基础的UI设计到复杂的物理模拟,从资源管理到性能优化。通过对源码的深入分析,可以提升自己的Android编程技能,并为毕业设计提供实际案例参考。



















































































- 1

- #完美解决问题
- #运行顺畅
- #内容详尽
- #全网独家
- #注释完整

- 粉丝: 6w+
- 资源: 2万+





我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 基于HTML/CSS/JavaScript的TNTwinter无人便利店响应式后台模板设计源码
- 基于SpringBoot+Vue的综合性图书管理系统设计源码
- 基于Vue框架开发的客栈信息展示项目设计源码
- 基于PHP和HTML的红色主题密码保护设计源码
- 基于JavaScript的随访系统设计与实现源码
- 基于Python的活动管理系统基础教程
- 基于Spark的协同过滤音乐推荐系统源码:利用SpringBoot和Vue框架及ALS模型进行个性化音乐推荐的技术实践,基于SpringBoot与Vue框架的ALS协同过滤音乐推荐系统源码:融合用户行
- 基于微信小程序的棋牌室自助管理系统设计源码
- C#编程与汇川PLC的MODBUS TCP通讯实践:实现实时曲线展示与自定义控件的集成应用,C#编程与汇川PLC的MODBUS TCP通讯实践:实现实时曲线绘制与自定义控件应用,C#与汇川PLC MO
- Screenshot_2025-02-19-00-20-28-411_com.zhihu.android.jpg
- DeepSeek智能搜索系统开发基础教程
- 基于Typescript的淘宝后端开发实战源码
- COMSOL技术下的压电超声表面波检测:高频响应揭示裂纹实景画面,利用压电超声表面波检测技术,1.5MHz压电片精准探测钢板裂纹-动画演示,comsol压电超声表面波检测 利用1.5MHz频率的压电
- 基于SSM框架的JSP+Bootstrap在线电影票购买系统设计源码
- FPGA XDMA中断模式PCIE测速例程:XDMA中断模块驱动交互与AXI-BRAM读写访问测试,FPGA XDMA中断模式下的PCIE测速例程:基于Xilinx XDMA方案与QT上位机的数据交互
- STM32F103C8T6的卡尔曼中位值滤波ADC采集算法:同步对比输出源程序与波形展示,STM32F103C8T6的卡尔曼滤波与中位值滤波算法融合下的ADC采集同步对比源程序,STM32 ADC采集


