package com.yaozun;
import java.util.ArrayList;
import java.util.Random;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Message;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
//222
public class RaidenView extends SurfaceView implements SurfaceHolder.Callback,
Runnable {
private SurfaceHolder sh;
private Bitmap player;
private Bullet bullet;
private ArrayList<Bullet> bullets = new ArrayList<Bullet>();
private Enemy enemy;
private ArrayList<Enemy> enemys = new ArrayList<Enemy>();
private Bullet bull;
private ArrayList<Bullet> ebullets = new ArrayList<Bullet>();
private ArrayList<Explosion> explosions = new ArrayList<Explosion>();
private Paint paint = new Paint();
private Bitmap explosion;
private Bitmap background;
private Bitmap boss;
private ArrayList<Bullet> bossbullets = new ArrayList<Bullet>();
private ArrayList<Bullet> bossbullets1 = new ArrayList<Bullet>();
private ArrayList<Bullet> bossbullets2 = new ArrayList<Bullet>();
private Bitmap life;
private Canvas canvas;
Bitmap bombs;
Bitmap bombe;
public RaidenView(Context context,Bitmap background,int score ,int ilife) {
super(context);
sh = this.getHolder();
sh.addCallback(this);
player = BitmapFactory.decodeResource(this.getResources(),
R.drawable.player);
explosion = BitmapFactory.decodeResource(this.getResources(), R.drawable.explosion);
boss = BitmapFactory.decodeResource(this.getResources(), R.drawable.boss);
bombs = BitmapFactory.decodeResource(this.getResources(), R.drawable.bonus);
bombe = Bitmap.createBitmap(bombs, 0, 0, 28, 24);
x = 100;
y = 200;
flag = true;
flagb = false;
bossx = 100;
bossy = 50;
this.score = score;
this.ilife = ilife;
this.background = background;
this.setFocusable(true);
}
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
}
int width;
int height;
public void surfaceCreated(SurfaceHolder arg0) {
new Thread(this).start();
width = this.getWidth();
height = this.getHeight();
lifegray = height-30;
bomby = height;
}
public void surfaceDestroyed(SurfaceHolder arg0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
flag = false;
}
int height1;
int height2=-430;
int bossx;
int bossy;
int time;
int times;
int ilife=5;
int score;
boolean flagboss=true ;
boolean warning = false;
boolean bulletStormCreate=true;
boolean game=true;
Paint paint1;
int bosslife=500;
int lifegray;
int bomb = 3;
float bomby ;
//炸弹效果
public void bombEffect(){
if(bomby>-97){
Bitmap myplane = BitmapFactory.decodeResource(this.getResources(), R.drawable.myplane);
Matrix matrix = new Matrix();
bomby -= 24;
matrix.setTranslate((width-172)/2, bomby);
canvas.drawBitmap(myplane, matrix, paint);
ebullets.removeAll(ebullets);
bossbullets.removeAll(bossbullets);
bossbullets1.removeAll(bossbullets1);
bossbullets2.removeAll(bossbullets2);
}else{
bombon=false;
}
}
//花形弹幕绘制
public void bulletStorm(){
Bitmap cbullet = BitmapFactory.decodeResource(this.getResources(), R.drawable.bossbullet);
for(int r = 30,d = 0;r<90;r++){
d = d+10;
int bx = (int) (160+r*Math.cos(d));
int by = (int) (100+r*Math.sin(d));
Bullet cbull = new Bullet(bx-80+bossx,by,cbullet,0);
cbull.draw4(canvas);
bossbullets.add(cbull);
}
}
//矩形弹幕绘制
public void bulletStorm1(){
if(time%30==0&time%90<=40){
Bitmap bbullet = BitmapFactory.decodeResource(this.getResources(), R.drawable.bossbullet);
int i = time/90;
Bullet bossbullet = null;
if(i<=85){
if(i<=16){
bossbullet = new Bullet(20*i,50,bbullet,time);
bossbullets2.add(bossbullet);
}else if(i>16&i<=20){
bossbullet = new Bullet(300,30+20*(i-15),bbullet,time);
bossbullets2.add(bossbullet);
}else if(i>20&i<=35){
bossbullet = new Bullet(340-20*(i-19),150,bbullet,time);
bossbullets2.add(bossbullet);
}else if(i>35&i<=40){
bossbullet = new Bullet(0,210-20*(i-33),bbullet,time);
bossbullets2.add(bossbullet);
}
}else{
time = 0;
}
}
}
//绘图(飞机,子弹,分数,血条等)
public void draw() {
if(flag){
if(time%2100==0){
bulletStormCreate = true;
}
canvas = sh.lockCanvas();
//背景循环显示
Rect rect1 = new Rect(0,0,background.getWidth(),background.getHeight());
Rect rect2 = new Rect(0,height1,320,430+height1);
Rect rect3 = new Rect(0,height2,320,430+height2);
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(background, rect1,rect2,null);
canvas.drawBitmap(background, rect1,rect3,null);
height1+=10;
height2+=10;
if(height1>=430){
height1 = -430;
}
if(height2>=430){
height2 = -430;
}
//生命值显示
life = BitmapFactory.decodeResource(this.getResources(), R.drawable.life);
for(int i = 0;i<ilife;i++){
canvas.drawBitmap(life, 10+20*i,10, null);
}
//boss的左右循环移动
Matrix matrix = new Matrix();
if(flagboss){
matrix.setTranslate(bossx++, bossy);
if(bossx==144){
flagboss = false;
}
}else{
matrix.setTranslate(bossx--, bossy);
if(bossx==0){
flagboss = true;
}
}
//分数绘制
paint.setColor(Color.WHITE);
canvas.drawText("Score:"+ score, 140, 30, paint);
//boss出来后 撒弹幕 显boss血条
if(warning){
canvas.drawBitmap(boss, matrix, null);
bulletStorm1();
time+=30;
canvas.drawText("boss:", 255, 50, paint);
int lifered = 30+(bosslife*(RaidenView.this.getHeight()-60)/500);
if(lifegray>lifered){
lifegray-=2;
}
System.out.println(lifered+" "+lifegray);
paint.setColor(Color.RED);
Rect rect4 = new Rect(RaidenView.this.getWidth()-30,30,RaidenView.this.getWidth()-10,lifered);
canvas.drawRect(rect4, paint);
paint.setColor(Color.GRAY);
Rect rect5 = new Rect(RaidenView.this.getWidth()-30,lifered,RaidenView.this.getWidth()-10,lifegray);
canvas.drawRect(rect5, paint);
int a = new Random().nextInt(20);
if(a==0){
Bitmap bbullet = BitmapFactory.decodeResource(this.getResources(), R.drawable.bossbullet);
Bullet bossbull = new Bullet(bossx+80,bossy+80,bbullet,time);
bossbullets1.add(bossbull);
}
}
//绘boss弹幕时机
if(bulletStormCreate&warning){
bulletStorm();
bulletStormCreate=false;
}else{
for(int i =0;i<bossbullets.size();i++){
Bullet cbullet = bossbullets.get(i);
cbullet.draw4(canvas);
}
}
//将处理后的弹幕绘制出来
for(int i = 0;i<bossbullets2.size();i++){
Bullet bbu = bossbullets2.get(i);
bbu.draw3(canvas,x,y,time);
}
for(int j=0;j<bossbullets1.size();j++){
Bullet bbu = bossbullets1.get(j);
bbu.draw4(canvas);
}
canvas.drawBitmap(player, x, y, null);
Bitmap bu = BitmapFactory.decodeResource(this.getResources(), R.drawable.bullet_3);
canvas.drawBitmap(bu, x+11,y+11, null);
paint.setColor(Color.WHITE);
//警告boss将出现
if(score>=400&score<=600){
if(times/90%2==0){
paint.setColor(Color.RED);
}else{
paint.setColor(Color.WHITE);
}
canvas.drawText("WARNING:BulletsStorm Coming", 75, 170, paint);
}
//绘制爆炸
for(int m = 0;m<explosions.size();m++){
Explosion explo = explosions.get(m);
if(explo.m<32){
explo.draw(canvas);
}else{
explosions.remove(explo);
}
}
//玩家子弹
for (int i = 0; i < bullets.size(); i++) {
Bullet bull = bullets.get(i);
f
- 1
- 2
- 3
- 4
- 5
- 6
前往页