forked from hzuapps/android-labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…apps#95 Lab1~9 重交。 Lab1~9 重交。之前代码冲突。期末统计那里没我的学号。
Showing
22 changed files
with
1,045 additions
and
0 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901140/Com1314080901140.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package com.hzu.xu.planewar; | ||
|
||
import android.app.Activity; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.os.Bundle; | ||
import android.view.View; | ||
import android.view.Window; | ||
import android.widget.Button; | ||
import android.widget.TextView; | ||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.util.Properties; | ||
|
||
public class Com1314080901140 extends Activity { | ||
private TextView mTextView; | ||
private Button Button0; | ||
private Button Button1; | ||
private boolean mChanged = false; | ||
//final Data dd=(Data) getApplication(); | ||
public int Count; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
requestWindowFeature(Window.FEATURE_NO_TITLE);//设置窗口没有标题 | ||
setContentView(R.layout.com1314080901140activity_main); | ||
mTextView = (TextView)findViewById(R.id.show_textview); | ||
Button1=(Button)findViewById(R.id.button1); | ||
Button0=(Button)findViewById(R.id.button0); | ||
|
||
|
||
//read the mChanged is true or false | ||
readLoadFile(); | ||
refreTextView(); | ||
initButton(); | ||
} | ||
public void Btn1OnClick(View view){ | ||
Intent intent = new Intent(); | ||
intent.setClass(Com1314080901140.this, GameActivity.class); | ||
startActivity(intent); | ||
} | ||
private void initButton() | ||
{ | ||
Button1.setOnClickListener(new View.OnClickListener(){ | ||
public void onClick(View arg0) { | ||
readLoadFile(); | ||
mChanged = true; | ||
refreTextView(); | ||
Count=Count+2; | ||
saveFileStorage(); | ||
} | ||
}); | ||
} | ||
public void refreTextView() | ||
{ | ||
mTextView.setText("当前分数为:"+Count+"!"); | ||
} | ||
|
||
private void readLoadFile() | ||
{ | ||
//make the Properties | ||
|
||
Properties properties = new Properties(); | ||
|
||
try{ | ||
FileInputStream istream = this.openFileInput("dmfile.cfg"); | ||
properties.load(istream); | ||
} | ||
catch(FileNotFoundException e){ | ||
return; | ||
} | ||
catch(IOException e){ | ||
return; | ||
} | ||
mChanged = Boolean.valueOf(properties.getProperty("mChanging").toString()); | ||
Count = Integer.valueOf(properties.getProperty("mButton").toString()); | ||
} | ||
|
||
private boolean saveFileStorage() | ||
{ | ||
Properties properties = new Properties(); | ||
properties.put("mChanging", String.valueOf(mChanged)); | ||
properties.put("mButton", String.valueOf(Count)); | ||
try{ | ||
FileOutputStream ostream = this.openFileOutput("dmfile.cfg", Context.MODE_WORLD_WRITEABLE); | ||
properties.store(ostream, ""); | ||
} | ||
catch(FileNotFoundException e) | ||
{ | ||
return false; | ||
} | ||
catch(IOException e) | ||
{ | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
|
116 changes: 116 additions & 0 deletions
116
app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901140/XuBulletHero.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package com.hzu.xu.planewar; | ||
|
||
import java.io.InputStream; | ||
|
||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.graphics.Canvas; | ||
|
||
public class XuBulletHero { | ||
// ------------ 子弹 | ||
Bitmap img_bullet; | ||
String img_bullet_name = "bullet-3.png";; | ||
int bullet_speed; | ||
int bullet_x, bullet_y, bullet_w, bullet_h; | ||
|
||
public int bullet_statu; | ||
public static final int BULLET_MOVE = 0X0001; | ||
public static final int BULLET_DIE = 0X0002; | ||
|
||
// ----------- 爆炸 | ||
Bitmap img_boom; | ||
int boom_x, boom_y, boom_w, boom_h, boom_col, boom_row; | ||
public int boom_statu; | ||
public static final int BOOM_POWER = 0X0001; | ||
public static final int BOOM_DIE = 0X0002; | ||
|
||
|
||
public BulletHero() { | ||
|
||
} | ||
|
||
public void setImgBulletName(String name){ | ||
this.img_bullet_name = name; | ||
} | ||
|
||
public void Bullet_Init() { | ||
InputStream is = this.getImgStream(img_bullet_name); | ||
Bitmap temp = BitmapFactory.decodeStream(is); | ||
img_bullet = GameUtils.ZomeBitamp(temp, GameUtils.IMG_ENLARGE, | ||
GameUtils.IMG_ENLARGE); | ||
bullet_w = img_bullet.getWidth(); | ||
bullet_h = img_bullet.getHeight(); | ||
bullet_x = -MainGame.Screen_w; | ||
bullet_y = -MainGame.Screen_h; | ||
bullet_statu = BULLET_DIE; | ||
bullet_speed = 12; | ||
this.Boom_Init(); | ||
} | ||
|
||
public void Deal_Bullet() { | ||
this.Bullet_Move(); | ||
this.Deal_Boom(); | ||
} | ||
|
||
public void Draw_Bullet(Canvas canvas) { | ||
if (this.bullet_statu == BULLET_MOVE) { | ||
GameUtils.Brush(canvas, img_bullet, bullet_x, bullet_y, bullet_w, | ||
bullet_h, 0, 0); | ||
} else if (this.bullet_statu == BULLET_DIE) { | ||
bullet_x = -MainGame.Screen_w; | ||
bullet_y = -MainGame.Screen_h; | ||
} | ||
this.Draw_Boom(canvas); | ||
} | ||
|
||
public void Bullet_Move() { | ||
if (this.bullet_y + this.bullet_h > 0) { | ||
bullet_y -= this.bullet_speed; | ||
} else { | ||
this.bullet_statu = BULLET_DIE; | ||
} | ||
} | ||
|
||
public void Update_Bullet_Local(int x, int y) { | ||
this.bullet_x = x; | ||
this.bullet_y = y; | ||
this.bullet_statu = BULLET_MOVE; | ||
} | ||
|
||
// ----------- 爆炸 | ||
public void Boom_Init(){ | ||
InputStream is = this.getImgStream("smallplanebom_.png"); | ||
Bitmap temp = BitmapFactory.decodeStream(is); | ||
img_boom = GameUtils.ZomeBitamp(temp, GameUtils.IMG_ENLARGE, | ||
GameUtils.IMG_ENLARGE); | ||
boom_w = img_boom.getWidth(); | ||
boom_h = img_boom.getHeight()/9; | ||
boom_x = -MainGame.Screen_w; | ||
boom_y = -MainGame.Screen_h; | ||
boom_col = 0; | ||
boom_row = 0; | ||
boom_statu = BOOM_DIE; | ||
} | ||
|
||
public void Deal_Boom(){ | ||
if(boom_statu == BOOM_POWER){ | ||
if(boom_row <9){ | ||
boom_row ++; | ||
}else{ | ||
boom_row = 0; | ||
boom_x = -MainGame.Screen_w; | ||
boom_y = -MainGame.Screen_h; | ||
boom_statu = BOOM_DIE; | ||
} | ||
} | ||
} | ||
|
||
public void Draw_Boom(Canvas canvas){ | ||
GameUtils.Brush(canvas, img_boom, boom_x, boom_y, boom_w, boom_h, boom_col, boom_row); | ||
} | ||
|
||
public InputStream getImgStream(String name){ | ||
return this.getClass().getResourceAsStream( | ||
GameUtils.IMG_PATH+name); | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901140/XuBulletNpc.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package com.hzu.xu.planewar; | ||
|
||
import java.io.InputStream; | ||
|
||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.graphics.Canvas; | ||
|
||
public class XuBulletNpc { | ||
// ------------ 子弹 | ||
Bitmap img_bullet; | ||
String img_bullet_name = "bullet-7.png";; | ||
int bullet_speed; | ||
int bullet_x, bullet_y, bullet_w, bullet_h; | ||
|
||
public int bullet_statu; | ||
public static final int BULLET_MOVE = 0X0001; | ||
public static final int BULLET_DIE = 0X0002; | ||
|
||
// ----------- 爆炸 | ||
Bitmap img_boom; | ||
int boom_x, boom_y, boom_w, boom_h, boom_col, boom_row; | ||
public int boom_statu; | ||
public static final int BOOM_POWER = 0X0001; | ||
public static final int BOOM_DIE = 0X0002; | ||
|
||
|
||
public BulletNpc() { | ||
|
||
} | ||
|
||
public void setImgBulletName(String name){ | ||
this.img_bullet_name = name; | ||
} | ||
|
||
public void Bullet_Init() { | ||
InputStream is = this.getImgStream(img_bullet_name); | ||
Bitmap temp = BitmapFactory.decodeStream(is); | ||
img_bullet = GameUtils.ZomeBitamp(temp, GameUtils.IMG_ENLARGE, | ||
GameUtils.IMG_ENLARGE); | ||
bullet_w = img_bullet.getWidth(); | ||
bullet_h = img_bullet.getHeight(); | ||
bullet_x = -MainGame.Screen_w; | ||
bullet_y = -MainGame.Screen_h; | ||
bullet_statu = BULLET_DIE; | ||
bullet_speed = 5; | ||
this.Boom_Init(); | ||
} | ||
|
||
public void Deal_Bullet() { | ||
this.Bullet_Move(); | ||
this.Deal_Boom(); | ||
} | ||
|
||
public void Draw_Bullet(Canvas canvas) { | ||
if (this.bullet_statu == BULLET_MOVE) { | ||
GameUtils.Brush(canvas, img_bullet, bullet_x, bullet_y, bullet_w, | ||
bullet_h, 0, 0); | ||
} else if (this.bullet_statu == BULLET_DIE) { | ||
bullet_x = -MainGame.Screen_w; | ||
bullet_y = -MainGame.Screen_h; | ||
} | ||
this.Draw_Boom(canvas); | ||
} | ||
|
||
public void Bullet_Move() { | ||
if (this.bullet_y < MainGame.Screen_h) { | ||
bullet_y += this.bullet_speed; | ||
} else { | ||
this.bullet_statu = BULLET_DIE; | ||
} | ||
} | ||
|
||
public void Update_Bullet_Local(int x, int y) { | ||
this.bullet_x = x; | ||
this.bullet_y = y; | ||
this.bullet_statu = BULLET_MOVE; | ||
} | ||
|
||
// ----------- 爆炸 | ||
public void Boom_Init(){ | ||
InputStream is = this.getImgStream("smallplanebom_.png"); | ||
Bitmap temp = BitmapFactory.decodeStream(is); | ||
img_boom = GameUtils.ZomeBitamp(temp, GameUtils.IMG_ENLARGE, | ||
GameUtils.IMG_ENLARGE); | ||
boom_w = img_boom.getWidth(); | ||
boom_h = img_boom.getHeight()/9; | ||
boom_x = -MainGame.Screen_w; | ||
boom_y = -MainGame.Screen_h; | ||
boom_col = 0; | ||
boom_row = 0; | ||
boom_statu = BOOM_DIE; | ||
} | ||
|
||
public void Deal_Boom(){ | ||
if(boom_statu == BOOM_POWER){ | ||
if(boom_row <9){ | ||
boom_row ++; | ||
}else{ | ||
boom_row = 0; | ||
boom_x = -MainGame.Screen_w; | ||
boom_y = -MainGame.Screen_h; | ||
boom_statu = BOOM_DIE; | ||
} | ||
} | ||
} | ||
|
||
public void Draw_Boom(Canvas canvas){ | ||
GameUtils.Brush(canvas, img_boom, boom_x, boom_y, boom_w, boom_h, boom_col, boom_row); | ||
} | ||
|
||
public InputStream getImgStream(String name){ | ||
return this.getClass().getResourceAsStream( | ||
GameUtils.IMG_PATH+name); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901140/XuData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.hzu.xu.planewar; | ||
|
||
import android.app.Application; | ||
|
||
public class XuData extends Application{ | ||
public int b; | ||
|
||
|
||
|
||
public int getNumber() { | ||
return b; | ||
} | ||
|
||
public void setNumber(int number) { | ||
this.b = number; | ||
} | ||
@Override | ||
public void onCreate() { | ||
// TODO Auto-generated method stub | ||
super.onCreate(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901140/XuGameActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.hzu.xu.planewar; | ||
|
||
import android.app.Activity; | ||
import android.graphics.Color; | ||
import android.os.Bundle; | ||
import android.view.Display; | ||
import android.view.Window; | ||
import android.view.WindowManager; | ||
|
||
public class XuGameActivity extends Activity { | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
// setContentView(R.layout.activity_main); | ||
requestWindowFeature(Window.FEATURE_NO_TITLE);//设置窗口没有标题 | ||
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏显示 | ||
Init(); | ||
} | ||
|
||
public void Init(){ | ||
WindowManager wm = this.getWindowManager(); | ||
Display display = wm.getDefaultDisplay(); | ||
MainGame gameView = new MainGame(this, display); | ||
gameView.setBackgroundColor(Color.BLACK); | ||
this.setContentView(gameView); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901140/XuGameUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.hzu.xu.planewar; | ||
|
||
import java.io.InputStream; | ||
import java.util.Random; | ||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.graphics.Canvas; | ||
import android.graphics.Matrix; | ||
import android.graphics.Region.Op; | ||
|
||
public class XuGameUtils { | ||
|
||
//------------- 常量 | ||
/** | ||
* 图片放大率 | ||
*/ | ||
final static float IMG_ENLARGE = 2; | ||
|
||
/** | ||
* 图片路径 | ||
*/ | ||
final static String IMG_PATH = "/assets/img/"; | ||
|
||
// ----------------- 工具方法 | ||
public Bitmap LoadImage(String path) { | ||
InputStream is = this.getClass().getResourceAsStream( | ||
"/assets/img/" + path); | ||
return BitmapFactory.decodeStream(is); | ||
} | ||
|
||
/** | ||
* 绘画剪裁 | ||
*/ | ||
public static void Brush(Canvas canvas, Bitmap bmp, int x, int y, int w, | ||
int h, int col, int row) { | ||
canvas.clipRect(x, y, x + w, y + h, Op.REPLACE); | ||
canvas.drawBitmap(bmp, x - col * w, y - row * h, MainGame.paint); | ||
canvas.clipRect(0, 0, MainGame.Screen_w, MainGame.Screen_h, Op.REPLACE); | ||
} | ||
|
||
public static Bitmap ZomeBitamp(Bitmap img, float size_w, float size_h) { | ||
/* 产生resize后的Bitmap对象 */ | ||
Matrix matrix = new Matrix(); | ||
matrix.setScale(size_w, size_h); | ||
Bitmap resizeBmp = Bitmap.createBitmap(img, 0, 0, img.getWidth(), | ||
img.getHeight(), matrix, true); | ||
return resizeBmp; | ||
} | ||
/*public static Bitmap ZomeBitamp(Bitmap img, int new_w, int new_h) { | ||
float w = (float) new_w / img.getWidth(); | ||
float h = (float) new_h / img.getHeight(); | ||
产生resize后的Bitmap对象 | ||
Matrix matrix = new Matrix(); | ||
matrix.setScale(w, h); | ||
Bitmap resizeBmp = Bitmap.createBitmap(img, 0, 0, img.getWidth(), | ||
img.getHeight(), matrix, true); | ||
return resizeBmp; | ||
}*/ | ||
|
||
/** | ||
* 获取随机数 | ||
* | ||
* @param min | ||
* 最小值 | ||
* @param max | ||
* 最大值 | ||
* @return | ||
*/ | ||
public static int GetRadomNumber(int min, int max) { | ||
return new Random().nextInt(max - min) + min; | ||
} | ||
|
||
//是否发生碰撞 | ||
public static boolean isRam(int r_x, int r_y, int r_w, int r_h, int b_x, | ||
int b_y, int b_w, int b_h) { | ||
if (r_x + r_w < b_x || b_x + b_w < r_x || r_y + r_h < b_y | ||
|| b_y + b_h < r_y) { | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
} | ||
|
||
} |
112 changes: 112 additions & 0 deletions
112
app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901140/XuHero.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package com.hzu.xu.planewar; | ||
|
||
import java.io.InputStream; | ||
|
||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.graphics.Canvas; | ||
|
||
public class XuHero { | ||
// ----------------------------- 人物hero | ||
Bitmap img_hero; | ||
|
||
final int hero_spreed = 15; | ||
|
||
int hero_x, hero_y, hero_w, hero_h, hero_col, hero_row; | ||
|
||
public int hero_statu; | ||
public static final int HERO_MOVE = 0X000001; | ||
public static final int HERO_DIE = 0X000002; | ||
|
||
// ----------------------子弹 | ||
BulletHero bullets[]; | ||
// 子弹计数器 | ||
int bullet_count; | ||
|
||
public Hero() { | ||
Hero_Init(); | ||
Bullet_Init(); | ||
} | ||
|
||
public void Hero_Init() { | ||
InputStream is = this.getClass().getResourceAsStream( | ||
"/assets/img/Plane_1.png"); | ||
Bitmap temp = BitmapFactory.decodeStream(is); | ||
img_hero = GameUtils.ZomeBitamp(temp, GameUtils.IMG_ENLARGE, | ||
GameUtils.IMG_ENLARGE); | ||
hero_w = img_hero.getWidth(); | ||
hero_h = img_hero.getHeight() / 3; | ||
hero_x = MainGame.Screen_w / 2; | ||
hero_y = MainGame.Screen_h - hero_h; | ||
hero_col = 0; | ||
hero_row = 1; | ||
hero_statu = HERO_MOVE; | ||
} | ||
|
||
public void Deal_Hero() { | ||
this.Hero_Move(); | ||
this.Deal_Bullet(); | ||
} | ||
|
||
public void Draw_Hero(Canvas canvas) { | ||
if (this.hero_statu == HERO_MOVE) { | ||
GameUtils.Brush(canvas, this.img_hero, hero_x, hero_y, hero_w, | ||
hero_h, hero_col, hero_row); | ||
this.Draw_Bullet(canvas); | ||
} | ||
} | ||
|
||
public void Hero_Move() { | ||
if (MainGame.isPoint) { | ||
// 左 | ||
if (MainGame.Point_x < this.hero_x) { | ||
this.hero_x -= this.hero_spreed; | ||
this.hero_row = 2; | ||
} | ||
// 上 | ||
if (MainGame.Point_y < this.hero_y) { | ||
this.hero_y -= this.hero_spreed; | ||
} | ||
// 右 | ||
if (MainGame.Point_x > this.hero_x + this.hero_w) { | ||
this.hero_x += this.hero_spreed; | ||
this.hero_row = 0; | ||
} | ||
// 下 | ||
if (MainGame.Point_y > this.hero_y + this.hero_h) { | ||
this.hero_y += this.hero_spreed; | ||
} | ||
} | ||
} | ||
|
||
// ----------------------- 子弹 | ||
public void Bullet_Init() { | ||
bullets = new BulletHero[25]; | ||
for (int i = 0; i < bullets.length; i++) { | ||
bullets[i] = new BulletHero(); | ||
bullets[i].Bullet_Init(); | ||
} | ||
} | ||
|
||
public void Deal_Bullet() { | ||
for (int i = 0; i < bullets.length; i++) { | ||
bullets[i].Deal_Bullet(); | ||
} | ||
if (this.bullet_count++ % 4 == 0) { | ||
for (int i = 0; i < bullets.length; i++) { | ||
if (bullets[i].bullet_statu == BulletHero.BULLET_DIE) { | ||
bullets[i].Update_Bullet_Local(this.hero_x + this.hero_w | ||
/ 2 - bullets[i].bullet_w / 2, this.hero_y); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
public void Draw_Bullet(Canvas canvas) { | ||
for (int i = 0; i < bullets.length; i++) { | ||
bullets[i].Draw_Bullet(canvas); | ||
} | ||
} | ||
|
||
} |
309 changes: 309 additions & 0 deletions
309
app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901140/XuMainGame.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,309 @@ | ||
package com.hzu.xu.planewar; | ||
|
||
import android.content.Context; | ||
import android.graphics.Canvas; | ||
import android.graphics.Color; | ||
import android.graphics.Paint; | ||
import android.graphics.Paint.Style; | ||
import android.view.Display; | ||
import android.view.MotionEvent; | ||
import android.view.View; | ||
import android.app.Activity; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.util.Properties; | ||
|
||
public class XuMainGame extends View implements Runnable { | ||
Data d; | ||
private int Count=0; | ||
|
||
int Game_State; | ||
// 游戏的Logo 状态 | ||
public static final int GAME_LOGO = 0X0001; | ||
// 游戏的加载 状态 | ||
public static final int GAME_INIT = 0X0002; | ||
// 游戏的菜单 状态 | ||
public static final int GAME_MENU = 0X0003; | ||
// 游戏的进行 状态 | ||
public static final int GAME_PLAY = 0X0004; | ||
// 游戏的暂停 状态 | ||
public static final int GAME_PAUSE = 0X0005; | ||
// 游戏的退出 状态 | ||
public static final int GAME_EXIT = 0X0006; | ||
|
||
// 屏幕宽和高 | ||
static int Screen_w, Screen_h; | ||
|
||
// 是否触摸了屏幕 | ||
static boolean isPoint; | ||
// 触点x,y的位置 | ||
static int Point_x, Point_y; | ||
|
||
// 画笔 | ||
static Paint paint; | ||
|
||
// 游戏运行线程 | ||
Thread thread; | ||
// 游戏线程是否在运行 | ||
boolean isRun; | ||
|
||
// ---------------- 主人公 | ||
Hero hero; | ||
|
||
// ---------------- Npc | ||
Npc npcs[]; | ||
|
||
public MainGame(Context context, Display display) { | ||
super(context); | ||
Screen_w = display.getWidth(); | ||
Screen_h = display.getHeight(); | ||
this.Game_State = GAME_LOGO; | ||
paint = new Paint(); | ||
this.isRun = true; | ||
this.thread = new Thread(this); | ||
this.thread.start(); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
while (this.isRun) { | ||
this.Deal(); | ||
this.postInvalidate(); | ||
try { | ||
Thread.sleep(50); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
public void Deal() { | ||
switch (this.Game_State) { | ||
case GAME_LOGO: | ||
this.Deal_Logo(); | ||
break; | ||
case GAME_INIT: | ||
this.Deal_Init(); | ||
break; | ||
case GAME_MENU: | ||
this.Deal_Menu(); | ||
break; | ||
case GAME_PLAY: | ||
this.Deal_Play(); | ||
break; | ||
case GAME_PAUSE: | ||
this.Deal_Pause(); | ||
break; | ||
case GAME_EXIT: | ||
this.Deal_Exit(); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
public void Deal_Logo() { | ||
this.Game_State = GAME_INIT; | ||
} | ||
|
||
public void Deal_Init() { | ||
this.Hero_Init(); | ||
this.Npc_Init(); | ||
this.Game_State = GAME_MENU; | ||
} | ||
|
||
public void Deal_Menu() { | ||
this.Game_State = GAME_PLAY; | ||
|
||
} | ||
|
||
public void Deal_Play() { | ||
this.Deal_Hero(); | ||
this.Deal_Npc(); | ||
this.HeroBulletCollNpc(); | ||
this.NpcBulletCollHero(); | ||
} | ||
|
||
public void Deal_Pause() { | ||
|
||
} | ||
|
||
public void Deal_Exit() { | ||
|
||
} | ||
|
||
@Override | ||
protected void onDraw(Canvas canvas) { | ||
super.onDraw(canvas); | ||
switch (this.Game_State) { | ||
case GAME_LOGO: | ||
this.Draw_Logo(canvas); | ||
break; | ||
case GAME_INIT: | ||
this.Draw_Init(canvas); | ||
break; | ||
case GAME_MENU: | ||
this.Draw_Menu(canvas); | ||
break; | ||
case GAME_PLAY: | ||
this.Draw_Play(canvas); | ||
break; | ||
case GAME_PAUSE: | ||
this.Draw_Pause(canvas); | ||
break; | ||
case GAME_EXIT: | ||
this.Draw_Exit(canvas); | ||
break; | ||
} | ||
} | ||
|
||
public void Draw_Logo(Canvas canvas) { | ||
|
||
} | ||
|
||
public void Draw_Init(Canvas canvas) { | ||
|
||
} | ||
|
||
public void Draw_Menu(Canvas canvas) { | ||
|
||
} | ||
|
||
public void Draw_Play(Canvas canvas) { | ||
this.Draw_Hero(canvas); | ||
this.Draw_Npc(canvas); | ||
if(this.hero.hero_statu == Hero.HERO_DIE){ | ||
this.Draw_Game_Over(canvas); | ||
} | ||
} | ||
|
||
public void Draw_Pause(Canvas canvas) { | ||
|
||
} | ||
|
||
public void Draw_Exit(Canvas canvas) { | ||
|
||
} | ||
|
||
public void Draw_Game_Over(Canvas canvas) { | ||
paint.setTextSize(70); | ||
paint.setColor(Color.RED); | ||
canvas.drawText("得分为"+Count+"!!!", 20, Screen_h/2, paint); | ||
paint.reset(); | ||
} | ||
|
||
// ------------------- 触屏 | ||
@Override | ||
public boolean onTouchEvent(MotionEvent event) { | ||
Point_x = (int) event.getRawX(); | ||
Point_y = (int) event.getRawY(); | ||
switch (event.getAction()) { | ||
case MotionEvent.ACTION_DOWN: | ||
isPoint = true; | ||
break; | ||
case MotionEvent.ACTION_UP: | ||
isPoint = false; | ||
this.hero.hero_row = 1; | ||
break; | ||
default: | ||
break; | ||
} | ||
return true; | ||
} | ||
|
||
// ---------------- 主人公 | ||
|
||
public void Hero_Init() { | ||
hero = new Hero(); | ||
} | ||
|
||
public void Deal_Hero() { | ||
hero.Deal_Hero(); | ||
} | ||
|
||
public void Draw_Hero(Canvas canvas) { | ||
hero.Draw_Hero(canvas); | ||
} | ||
|
||
// ---------------- Npc | ||
|
||
public void Npc_Init() { | ||
npcs = new Npc[10]; | ||
for (int i = 0; i < npcs.length; i++) { | ||
npcs[i] = new Npc(); | ||
} | ||
} | ||
|
||
public void Deal_Npc() { | ||
for (int i = 0; i < npcs.length; i++) { | ||
npcs[i].Deal_Npc(); | ||
} | ||
} | ||
|
||
public void Draw_Npc(Canvas canvas) { | ||
for (int i = 0; i < npcs.length; i++) { | ||
if (npcs[i].npc_statu == Npc.NPC_MOVE) { | ||
npcs[i].Draw_Npc(canvas); | ||
} | ||
} | ||
} | ||
|
||
// --------------- Hero的子弹碰撞npc | ||
public void HeroBulletCollNpc() { | ||
for (int i = 0; i < this.hero.bullets.length; i++) { | ||
for (int j = 0; j < this.npcs.length; j++) { | ||
if (GameUtils.isRam(this.hero.bullets[i].bullet_x, | ||
this.hero.bullets[i].bullet_y, | ||
this.hero.bullets[i].bullet_w, | ||
this.hero.bullets[i].bullet_h, this.npcs[j].npc_x, | ||
this.npcs[j].npc_y, this.npcs[j].npc_w, | ||
this.npcs[j].npc_h)) { | ||
this.hero.bullets[i].boom_statu = BulletHero.BOOM_POWER; | ||
this.hero.bullets[i].boom_x = this.hero.bullets[i].bullet_x; | ||
this.hero.bullets[i].boom_y = this.hero.bullets[i].bullet_y; | ||
Count++; | ||
this.hero.bullets[i].bullet_statu = BulletHero.BULLET_DIE; | ||
this.hero.bullets[i].bullet_x = 0; | ||
this.hero.bullets[i].bullet_y = -Screen_h; | ||
|
||
//将npc移出屏幕,不然子弹打在该处还会爆炸 | ||
this.npcs[j].npc_statu = Npc.NPC_DIE; | ||
|
||
this.npcs[j].npc_x = 0; | ||
this.npcs[j].npc_y = Screen_h * 3; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// --------------- npc的子弹碰撞hero | ||
public void NpcBulletCollHero() { | ||
for (int a = 0; a < this.npcs.length; a++) { | ||
for (int i = 0; i < this.npcs[a].bullets.length; i++) { | ||
if (GameUtils.isRam(this.npcs[a].bullets[i].bullet_x, | ||
this.npcs[a].bullets[i].bullet_y, | ||
this.npcs[a].bullets[i].bullet_w, | ||
this.npcs[a].bullets[i].bullet_h, this.hero.hero_x, | ||
this.hero.hero_y, this.hero.hero_w, this.hero.hero_h)) { | ||
this.npcs[a].bullets[i].boom_statu = BulletNpc.BOOM_POWER; | ||
this.npcs[a].bullets[i].boom_x = this.hero.hero_x; | ||
this.npcs[a].bullets[i].boom_y = this.hero.hero_y; | ||
|
||
this.npcs[a].bullets[i].bullet_statu = BulletHero.BULLET_DIE; | ||
this.npcs[a].bullets[i].bullet_x = 0; | ||
this.npcs[a].bullets[i].bullet_y = -Screen_h; | ||
|
||
//将英雄移出屏幕,不然子弹打在英雄处还是会爆炸 | ||
this.hero.hero_statu = Hero.HERO_DIE; | ||
this.hero.hero_x = 0; | ||
this.hero.hero_y = Screen_h * 3; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
105 changes: 105 additions & 0 deletions
105
app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901140/XuNpc.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package com.hzu.xu.planewar; | ||
|
||
import java.io.InputStream; | ||
|
||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.graphics.Canvas; | ||
|
||
public class XuNpc { | ||
Bitmap img_npc; | ||
int npc_speed; | ||
int npc_x, npc_y, npc_w, npc_h, npc_col, npc_row; | ||
|
||
public int npc_statu; | ||
public static final int NPC_MOVE = 0X000001; | ||
public static final int NPC_DIE = 0X000002; | ||
|
||
// ----------------------子弹 | ||
BulletNpc bullets[]; | ||
// 子弹计数器 | ||
int bullet_count; | ||
|
||
public Npc() { | ||
Npc_Init(); | ||
this.Bullet_Init(); | ||
} | ||
|
||
public void Npc_Init() { | ||
InputStream is = this.getClass().getResourceAsStream( | ||
"/assets/img/smallplane_.png"); | ||
Bitmap temp = BitmapFactory.decodeStream(is); | ||
img_npc = GameUtils.ZomeBitamp(temp, GameUtils.IMG_ENLARGE, | ||
GameUtils.IMG_ENLARGE); | ||
npc_w = img_npc.getWidth(); | ||
npc_h = img_npc.getHeight() / 6; | ||
npc_y = GameUtils.GetRadomNumber(-npc_h * 2, -npc_h); | ||
npc_x = GameUtils.GetRadomNumber(0, MainGame.Screen_w - npc_w); | ||
npc_col = 0; | ||
npc_row = GameUtils.GetRadomNumber(0, 4); | ||
npc_speed = GameUtils.GetRadomNumber(2, 6); | ||
npc_statu = NPC_MOVE; | ||
} | ||
|
||
public void Deal_Npc() { | ||
this.Npc_Move(); | ||
this.Deal_Bullet(); | ||
} | ||
|
||
public void Npc_Move() { | ||
if (npc_statu == NPC_MOVE) { | ||
npc_y += this.npc_speed; | ||
|
||
} else if (npc_statu == NPC_DIE) { | ||
npc_y = GameUtils.GetRadomNumber(-npc_h * 2, -npc_h); | ||
npc_x = GameUtils.GetRadomNumber(0, MainGame.Screen_w - npc_w); | ||
npc_row = GameUtils.GetRadomNumber(0, 4); | ||
npc_speed = GameUtils.GetRadomNumber(2, 6); | ||
npc_statu = NPC_MOVE; | ||
|
||
} | ||
if (npc_y > MainGame.Screen_h) { | ||
npc_statu = NPC_DIE; | ||
} | ||
} | ||
|
||
public void Draw_Npc(Canvas canvas) { | ||
if (this.npc_statu == NPC_MOVE) { | ||
GameUtils.Brush(canvas, img_npc, npc_x, npc_y, npc_w, npc_h, | ||
npc_col, npc_row); | ||
} | ||
this.Draw_Bullet(canvas); | ||
} | ||
|
||
// ----------------------- 子弹 | ||
public void Bullet_Init() { | ||
bullets = new BulletNpc[25]; | ||
for (int i = 0; i < bullets.length; i++) { | ||
bullets[i] = new BulletNpc(); | ||
// bullets[i].setImgBulletName("bullet-4.png"); | ||
bullets[i].Bullet_Init(); | ||
} | ||
} | ||
|
||
public void Deal_Bullet() { | ||
for (int i = 0; i < bullets.length; i++) { | ||
bullets[i].Deal_Bullet(); | ||
} | ||
if (this.bullet_count++ % 30 == 0) { | ||
for (int i = 0; i < bullets.length; i++) { | ||
if (bullets[i].bullet_statu == BulletNpc.BULLET_DIE) { | ||
bullets[i].Update_Bullet_Local(this.npc_x + this.npc_w | ||
/ 2 - bullets[i].bullet_w / 2, this.npc_y+this.npc_h); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
public void Draw_Bullet(Canvas canvas) { | ||
for (int i = 0; i < bullets.length; i++) { | ||
bullets[i].Draw_Bullet(canvas); | ||
} | ||
} | ||
|
||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context="com.hzu.xu.planewar.Com1314080901140"> | ||
|
||
|
||
<ImageView | ||
android:layout_width="190dp" | ||
android:layout_height="190dp" | ||
android:background="@drawable/xuxu" | ||
android:id="@+id/imageView" | ||
android:layout_alignParentTop="true" | ||
android:layout_centerHorizontal="true" | ||
android:layout_marginTop="72dp" /> | ||
|
||
<Button | ||
android:layout_width="200dp" | ||
android:layout_height="80dp" | ||
android:text="开始游戏" | ||
android:id="@+id/button0" | ||
android:singleLine="true" | ||
android:textSize="30dp" | ||
android:onClick="Btn1OnClick" | ||
android:layout_above="@+id/button1" | ||
android:layout_alignStart="@+id/imageView" /> | ||
|
||
<Button | ||
android:layout_width="200dp" | ||
android:layout_height="80dp" | ||
android:text="更新分数" | ||
android:id="@+id/button1" | ||
android:singleLine="true" | ||
android:textSize="30dp" | ||
android:layout_marginBottom="46dp" | ||
android:layout_alignParentBottom="true" | ||
android:layout_alignStart="@+id/button0" /> | ||
|
||
<TextView | ||
android:id="@+id/show_textview" | ||
android:layout_width="fill_parent" | ||
android:layout_height="wrap_content" | ||
android:gravity="center" | ||
android:layout_above="@+id/button0" | ||
android:layout_below="@+id/imageView" | ||
android:layout_alignParentStart="true" /></RelativeLayout> |