初次提交
7
第2章 游戏开发基础知识/Sample2_8/.classpath
Normal file
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="src" path="gen"/>
|
||||
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
33
第2章 游戏开发基础知识/Sample2_8/.project
Normal file
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Sample2_8</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>com.android.ide.eclipse.adt.ApkBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
12
第2章 游戏开发基础知识/Sample2_8/.settings/org.eclipse.jdt.core.prefs
Normal file
@@ -0,0 +1,12 @@
|
||||
#Fri Jul 01 08:06:54 CST 2011
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.6
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.6
|
||||
17
第2章 游戏开发基础知识/Sample2_8/AndroidManifest.xml
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:versionCode="1"
|
||||
android:versionName="1.0" package="com.bn.pp8">
|
||||
<application android:icon="@drawable/icon" android:label="@string/app_name">
|
||||
<activity android:name=".Sample2_8_Activity"
|
||||
android:label="@string/app_name">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
</application>
|
||||
|
||||
|
||||
</manifest>
|
||||
BIN
第2章 游戏开发基础知识/Sample2_8/bin/Sample2_8.apk
Normal file
BIN
第2章 游戏开发基础知识/Sample2_8/bin/classes.dex
Normal file
BIN
第2章 游戏开发基础知识/Sample2_8/bin/com/bn/pp8/Bullet.class
Normal file
BIN
第2章 游戏开发基础知识/Sample2_8/bin/com/bn/pp8/Constant.class
Normal file
BIN
第2章 游戏开发基础知识/Sample2_8/bin/com/bn/pp8/DrawThread.class
Normal file
BIN
第2章 游戏开发基础知识/Sample2_8/bin/com/bn/pp8/Explosion.class
Normal file
BIN
第2章 游戏开发基础知识/Sample2_8/bin/com/bn/pp8/MySurfaceView.class
Normal file
BIN
第2章 游戏开发基础知识/Sample2_8/bin/com/bn/pp8/R$attr.class
Normal file
BIN
第2章 游戏开发基础知识/Sample2_8/bin/com/bn/pp8/R$drawable.class
Normal file
BIN
第2章 游戏开发基础知识/Sample2_8/bin/com/bn/pp8/R$id.class
Normal file
BIN
第2章 游戏开发基础知识/Sample2_8/bin/com/bn/pp8/R$layout.class
Normal file
BIN
第2章 游戏开发基础知识/Sample2_8/bin/com/bn/pp8/R$string.class
Normal file
BIN
第2章 游戏开发基础知识/Sample2_8/bin/com/bn/pp8/R.class
Normal file
BIN
第2章 游戏开发基础知识/Sample2_8/bin/com/bn/pp8/Sample2_8_Activity.class
Normal file
BIN
第2章 游戏开发基础知识/Sample2_8/bin/resources.ap_
Normal file
11
第2章 游戏开发基础知识/Sample2_8/default.properties
Normal file
@@ -0,0 +1,11 @@
|
||||
# This file is automatically generated by Android Tools.
|
||||
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
|
||||
#
|
||||
# This file must be checked in Version Control Systems.
|
||||
#
|
||||
# To customize properties used by the Ant build system use,
|
||||
# "build.properties", and override values to adapt the script to your
|
||||
# project structure.
|
||||
|
||||
# Project target.
|
||||
target=android-7
|
||||
39
第2章 游戏开发基础知识/Sample2_8/gen/com/bn/pp8/R.java
Normal file
@@ -0,0 +1,39 @@
|
||||
/* AUTO-GENERATED FILE. DO NOT MODIFY.
|
||||
*
|
||||
* This class was automatically generated by the
|
||||
* aapt tool from the resource data it found. It
|
||||
* should not be modified by hand.
|
||||
*/
|
||||
|
||||
package com.bn.pp8;
|
||||
|
||||
public final class R {
|
||||
public static final class attr {
|
||||
}
|
||||
public static final class drawable {
|
||||
public static final int bg=0x7f020000;
|
||||
public static final int bullet=0x7f020001;
|
||||
public static final int explode0=0x7f020002;
|
||||
public static final int explode1=0x7f020003;
|
||||
public static final int explode2=0x7f020004;
|
||||
public static final int explode3=0x7f020005;
|
||||
public static final int explode4=0x7f020006;
|
||||
public static final int explode5=0x7f020007;
|
||||
public static final int icon=0x7f020008;
|
||||
}
|
||||
public static final class id {
|
||||
public static final int Button01=0x7f050003;
|
||||
public static final int EditText01=0x7f050002;
|
||||
public static final int EditText02=0x7f050005;
|
||||
public static final int LinearLayout01=0x7f050000;
|
||||
public static final int ScrollView01=0x7f050004;
|
||||
public static final int TextView01=0x7f050001;
|
||||
}
|
||||
public static final class layout {
|
||||
public static final int main=0x7f030000;
|
||||
}
|
||||
public static final class string {
|
||||
public static final int app_name=0x7f040001;
|
||||
public static final int hello=0x7f040000;
|
||||
}
|
||||
}
|
||||
BIN
第2章 游戏开发基础知识/Sample2_8/res/drawable-hdpi/icon.png
Normal file
|
After Width: | Height: | Size: 4.0 KiB |
BIN
第2章 游戏开发基础知识/Sample2_8/res/drawable-ldpi/icon.png
Normal file
|
After Width: | Height: | Size: 1.7 KiB |
BIN
第2章 游戏开发基础知识/Sample2_8/res/drawable-mdpi/bg.jpg
Normal file
|
After Width: | Height: | Size: 40 KiB |
BIN
第2章 游戏开发基础知识/Sample2_8/res/drawable-mdpi/bullet.png
Normal file
|
After Width: | Height: | Size: 3.6 KiB |
BIN
第2章 游戏开发基础知识/Sample2_8/res/drawable-mdpi/explode0.png
Normal file
|
After Width: | Height: | Size: 2.2 KiB |
BIN
第2章 游戏开发基础知识/Sample2_8/res/drawable-mdpi/explode1.png
Normal file
|
After Width: | Height: | Size: 2.5 KiB |
BIN
第2章 游戏开发基础知识/Sample2_8/res/drawable-mdpi/explode2.png
Normal file
|
After Width: | Height: | Size: 2.6 KiB |
BIN
第2章 游戏开发基础知识/Sample2_8/res/drawable-mdpi/explode3.png
Normal file
|
After Width: | Height: | Size: 2.6 KiB |
BIN
第2章 游戏开发基础知识/Sample2_8/res/drawable-mdpi/explode4.png
Normal file
|
After Width: | Height: | Size: 2.4 KiB |
BIN
第2章 游戏开发基础知识/Sample2_8/res/drawable-mdpi/explode5.png
Normal file
|
After Width: | Height: | Size: 2.2 KiB |
BIN
第2章 游戏开发基础知识/Sample2_8/res/drawable-mdpi/icon.png
Normal file
|
After Width: | Height: | Size: 2.5 KiB |
53
第2章 游戏开发基础知识/Sample2_8/res/layout/main.xml
Normal file
@@ -0,0 +1,53 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent"
|
||||
>
|
||||
<TextView
|
||||
android:text="文件IO--读取Assets文件"
|
||||
android:layout_width="fill_parent"
|
||||
android:textSize="16dip"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center"
|
||||
/>
|
||||
<LinearLayout
|
||||
android:id="@+id/LinearLayout01"
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content">
|
||||
<TextView
|
||||
android:text="文件名:"
|
||||
android:textSize="18dip"
|
||||
android:id="@+id/TextView01"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content">
|
||||
</TextView>
|
||||
<EditText
|
||||
android:text="AndroidIO.txt"
|
||||
android:id="@+id/EditText01"
|
||||
android:singleLine="true"
|
||||
android:layout_weight="1"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content">
|
||||
</EditText>
|
||||
<Button
|
||||
android:text="打开文件"
|
||||
android:id="@+id/Button01"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content">
|
||||
</Button>
|
||||
</LinearLayout>
|
||||
<ScrollView
|
||||
android:id="@+id/ScrollView01"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent">
|
||||
<EditText
|
||||
android:text=""
|
||||
android:id="@+id/EditText02"
|
||||
android:editable="false"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent">
|
||||
</EditText>
|
||||
</ScrollView>
|
||||
</LinearLayout>
|
||||
5
第2章 游戏开发基础知识/Sample2_8/res/values/strings.xml
Normal file
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="hello">Hello World, Sample3_3_Activity!</string>
|
||||
<string name="app_name">Sample2_8</string>
|
||||
</resources>
|
||||
55
第2章 游戏开发基础知识/Sample2_8/src/com/bn/pp8/Bullet.java
Normal file
@@ -0,0 +1,55 @@
|
||||
package com.bn.pp8;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Paint;
|
||||
|
||||
public class Bullet {
|
||||
MySurfaceView gameView;
|
||||
private Bitmap bitmap;// 位图
|
||||
private Bitmap[] bitmaps;// 爆炸动画图组
|
||||
float x;// x方向位移
|
||||
float y;// y方向位移
|
||||
float vx;// x方向速度
|
||||
float vy;// y方向速度
|
||||
private float t = 0;// 时间
|
||||
private float timeSpan = 0.5f;// 时间间隔
|
||||
int size;// 子弹尺寸
|
||||
boolean explodeFlag = false;// 是否绘制子弹的标记
|
||||
Explosion mExplosion;// 爆炸对象引用
|
||||
|
||||
// 构造器
|
||||
public Bullet(MySurfaceView gameView, Bitmap bitmap, Bitmap[] bitmaps,
|
||||
float x, float y, float vx, float vy) {
|
||||
this.gameView = gameView;// 成员变量赋值
|
||||
this.bitmap = bitmap;
|
||||
this.bitmaps = bitmaps;
|
||||
this.x = x;// 成员变量赋值
|
||||
this.y = y;
|
||||
this.vx = vx;
|
||||
this.vy = vy;
|
||||
size = bitmap.getHeight();// 获得图片的高度
|
||||
}
|
||||
|
||||
// 绘制子弹的方法
|
||||
public void drawSelf(Canvas canvas, Paint paint) {
|
||||
if (explodeFlag && mExplosion != null) {// 如果已经爆炸,绘制爆炸动画
|
||||
mExplosion.drawSelf(canvas, paint);
|
||||
} else {
|
||||
go();// 子弹前进
|
||||
canvas.drawBitmap(bitmap, x, y, paint);// 绘制子弹
|
||||
}
|
||||
}
|
||||
|
||||
// 子弹前进的方法
|
||||
public void go() {
|
||||
x += vx * t;// 水平方向匀速直线运动
|
||||
y += vy * t + 0.5f * Constant.G * t * t;// 竖直方向匀加速直线运动
|
||||
if (x >= Constant.EXPLOSION_X || y >= Constant.SCREEN_HEIGHT) {// 子弹在特定位置爆炸
|
||||
mExplosion = new Explosion(gameView, bitmaps, x, y);// 创建爆炸对象
|
||||
explodeFlag = true;// 不再绘制子弹
|
||||
return;
|
||||
}
|
||||
t += timeSpan;// 时间间隔
|
||||
}
|
||||
}
|
||||
8
第2章 游戏开发基础知识/Sample2_8/src/com/bn/pp8/Constant.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package com.bn.pp8;
|
||||
//用于统一管理常量的类
|
||||
public class Constant {
|
||||
public static final int SCREEN_WIDTH=480;//屏幕宽度
|
||||
public static final int SCREEN_HEIGHT=320;//屏幕高度
|
||||
public static final int EXPLOSION_X=270;//爆炸X位置
|
||||
public static final float G = 1.0f;//重力加速度的值
|
||||
}
|
||||
43
第2章 游戏开发基础知识/Sample2_8/src/com/bn/pp8/DrawThread.java
Normal file
@@ -0,0 +1,43 @@
|
||||
package com.bn.pp8;
|
||||
|
||||
import android.graphics.Canvas;
|
||||
import android.view.SurfaceHolder;
|
||||
|
||||
public class DrawThread extends Thread {
|
||||
private boolean flag = true;//线程工作标志位
|
||||
private int sleepSpan = 100;//线程休眠时间
|
||||
MySurfaceView gameView;//父界面引用
|
||||
SurfaceHolder surfaceHolder;//surfaceHolder引用
|
||||
|
||||
public DrawThread(MySurfaceView gameView) {//构造器
|
||||
this.gameView = gameView;
|
||||
this.surfaceHolder = gameView.getHolder();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
Canvas c;//声明画布
|
||||
while (this.flag) {
|
||||
c = null;
|
||||
try {
|
||||
// 锁定整个画布,在内存要求比较高的情况下,建议参数不要为null
|
||||
c = this.surfaceHolder.lockCanvas(null);
|
||||
synchronized (this.surfaceHolder) {
|
||||
gameView.onDraw(c);// 绘制
|
||||
}
|
||||
} finally {
|
||||
if (c != null) {// 并释放锁
|
||||
this.surfaceHolder.unlockCanvasAndPost(c);
|
||||
}
|
||||
}
|
||||
try {
|
||||
Thread.sleep(sleepSpan);// 睡眠指定毫秒数
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();// 打印堆栈信息
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setFlag(boolean flag) {//设置标志位的方法
|
||||
this.flag = flag;
|
||||
}
|
||||
}
|
||||
29
第2章 游戏开发基础知识/Sample2_8/src/com/bn/pp8/Explosion.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package com.bn.pp8;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Paint;
|
||||
|
||||
public class Explosion {
|
||||
MySurfaceView gameView;
|
||||
private Bitmap[] bitmaps;// 位图
|
||||
float x;// x方向位移
|
||||
float y;// y方向位移
|
||||
private int anmiIndex = 0;// 爆炸动画帧索引
|
||||
|
||||
public Explosion(MySurfaceView gameView, Bitmap[] bitmaps, float x, float y) {
|
||||
this.gameView = gameView;
|
||||
this.bitmaps = bitmaps;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
// 绘制背景的方法
|
||||
public void drawSelf(Canvas canvas, Paint paint) {
|
||||
if (anmiIndex >= bitmaps.length - 1) {// 如果动画播放完毕,不再绘制爆炸效果
|
||||
return;
|
||||
}
|
||||
canvas.drawBitmap(bitmaps[anmiIndex], x, y, paint);// 绘制数组中某一幅图
|
||||
anmiIndex++;// 当前下标加1
|
||||
}
|
||||
}
|
||||
68
第2章 游戏开发基础知识/Sample2_8/src/com/bn/pp8/MySurfaceView.java
Normal file
@@ -0,0 +1,68 @@
|
||||
package com.bn.pp8;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Paint;
|
||||
import android.view.SurfaceHolder;
|
||||
import android.view.SurfaceView;
|
||||
|
||||
/**
|
||||
*
|
||||
* 2D SurfaceView
|
||||
*
|
||||
*/
|
||||
public class MySurfaceView extends SurfaceView implements
|
||||
SurfaceHolder.Callback {
|
||||
Sample2_8_Activity activity;// activity的引用
|
||||
Paint paint;// 画笔引用
|
||||
DrawThread drawThread;// 绘制线程引用
|
||||
Bitmap bgBmp;//背景图片
|
||||
Bitmap bulletBmp;// 子弹位图
|
||||
Bitmap[] explodeBmps;//爆炸位图数组
|
||||
Bullet bullet;//子弹对象引用
|
||||
public MySurfaceView(Sample2_8_Activity activity) {//构造器
|
||||
super(activity);
|
||||
this.activity = activity;
|
||||
// 获得焦点并设置为可触控
|
||||
this.requestFocus();
|
||||
this.setFocusableInTouchMode(true);
|
||||
getHolder().addCallback(this);// 注册回调接口
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {//绘制界面的方法
|
||||
super.onDraw(canvas);
|
||||
canvas.drawBitmap(bgBmp, 0, 0, paint);//绘制背景
|
||||
bullet.drawSelf(canvas, paint);//绘制子弹
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceChanged(SurfaceHolder holder, int format, int width,
|
||||
int height) {//界面变化时调用的方法
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
paint = new Paint();// 创建画笔
|
||||
paint.setAntiAlias(true);// 打开抗锯齿
|
||||
//加载图片资源
|
||||
bulletBmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.bullet);
|
||||
bgBmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.bg);
|
||||
explodeBmps=new Bitmap[]{
|
||||
BitmapFactory.decodeResource(this.getResources(), R.drawable.explode0),
|
||||
BitmapFactory.decodeResource(this.getResources(), R.drawable.explode1),
|
||||
BitmapFactory.decodeResource(this.getResources(), R.drawable.explode2),
|
||||
BitmapFactory.decodeResource(this.getResources(), R.drawable.explode3),
|
||||
BitmapFactory.decodeResource(this.getResources(), R.drawable.explode4),
|
||||
BitmapFactory.decodeResource(this.getResources(), R.drawable.explode5),
|
||||
};
|
||||
bullet = new Bullet(this, bulletBmp,explodeBmps,0,290,1.3f,-5.9f);//创建子弹对象
|
||||
drawThread = new DrawThread(this);//创建绘制线程
|
||||
drawThread.start();//启动绘制线程
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceDestroyed(SurfaceHolder holder) {//界面销毁时调用的方法
|
||||
drawThread.setFlag(false);//停止绘制线程
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.bn.pp8;
|
||||
import android.app.Activity;
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.os.Bundle;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
public class Sample2_8_Activity extends Activity {
|
||||
MySurfaceView gameView;//游戏界面
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {//重写onCreate方法
|
||||
super.onCreate(savedInstanceState);
|
||||
//全屏
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN ,
|
||||
WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
||||
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//设置为横屏
|
||||
gameView = new MySurfaceView(this);//创建游戏界面对象
|
||||
this.setContentView(gameView);//将该界面设置为窗体内容
|
||||
}
|
||||
}
|
||||