Android 打飞机 小游戏 的实现 - 画面移动 对象封装 碰撞效果

打飞机小游戏


*1、画背景
 *2、画飞机
 *3、画小怪
 *4、画子弹
 *——背景,飞机、小怪、子弹各有draw和move方法
 

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GameView gameView = new GameView(MainActivity.this);
setContentView(gameView);
}
}


public class GameView extends SurfaceView implements Callback {
private int indexbo=0;
private SurfaceHolder holder;
private Paint paint;
private Background background;
private Hero hero;
private ArrayList<BulletHero> bullets_hero;
private int [][]enemysData={{1,1},{0,0},{1,1,1,0},{0,1,0,1},{1,0,0,1},{1,1,1},{1,0,1,1},{1,1},{0,0},{1,1,1,0,1},{0,1,0,1},{1,0,0,1},{1,1,1},{1,0,1,1},{1,1},{0,0},{1,1,1,0,1},{0,1,0,1},{1,0,0,1},{1,1,1},{1,0,1,1},{1,1},{0,0},{1,1,1,0,1},{0,1,0,1},{1,0,0,1},{1,1,1},{1,0,1,1},{1,1},{0,0},{1,1,1,0,1},{0,1,0,1},{1,0,0,1},{1,1,1},{1,0,1,1},{1,1},{0,0},{1,1,1,0,1},{0,1,0,1},{1,0,0,1},{1,1,1},{1,0,1,1},{1,1},{0,0},{1,1,1,0,1},{0,1,0,1},{1,0,0,1},{1,1,1},{1,0,1,1},{1,1},{0,0},{1,1,1,0,1},{0,1,0,1},{1,0,0,1},{1,1,1},{1,0,1,1}};
private ArrayList<Enemy> enemys;
Canvas canvas ;
public GameView(Context context) {
super(context);
holder = getHolder();
holder.addCallback(this);
paint = new Paint();
background = new Background(context);
hero = new Hero(context);
bullets_hero = new ArrayList<BulletHero>();
enemys = new ArrayList<Enemy>();
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
isRunning = true;
new ViewThread().start();
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
isRunning = false;
}
private boolean isRunning = false;
class ViewThread extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
while (isRunning) {
canvas = holder.lockCanvas();
if (canvas == null)
continue;
canvas.drawColor(0xFFFFFFFF);
drawView(canvas);
logic();
if (canvas != null)
holder.unlockCanvasAndPost(canvas);
try {
sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
int count = 0;
private void logic() {
// TODO Auto-generated method stub
background.logic();
count++;
if (count % 2 == 0) {
BulletHero bullet = new BulletHero(getContext(), hero.getX()
+ hero.getBitmap1().getWidth() / 2, hero.getY());
bullets_hero.add(bullet);
}
if(count%20==0){
int[]is=enemysData[indexbo];
for(int i=0;i<is.length;i++){
Enemy enemy = new Enemy(getContext(),is[i]);
enemys.add(enemy);
}
indexbo++;
if(indexbo>enemysData.length-1)
indexbo=0;
}
//检测碰撞
for(int i=0;i<enemys.size();i++){
Enemy enemy=enemys.get(i);
for(int j=0;j<bullets_hero.size();j++){
BulletHero bullet=bullets_hero.get(j);
if(enemy.isCollection(bullet)){
enemys.remove(i);
enemy.drawCollection(canvas,paint);
bullets_hero.remove(j);
}
}
}
}

private void drawView(Canvas canvas) {
// TODO Auto-generated method stub
background.draw(canvas, paint);
hero.draw(canvas, paint);
//画子弹
for (BulletHero bullet : bullets_hero) {
bullet.draw(canvas, paint);
bullet.move();
}
//画小怪
for(Enemy e:enemys){
e.draw(canvas,paint);
e.move();
}
}
}
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
hero.move(x, y);
return true;
}
}


public class Background {
private Bitmap bitmap;
private int y,y2,speed,width,height;
public Background(Context context) {
super();
bitmap=BitmapFactory.decodeResource(context.getResources(), R.drawable.background);
y=0;
y2=-852;
speed=10;
}
public void draw(Canvas canvas,Paint paint){
canvas.drawBitmap(bitmap,0,y,paint);
canvas.drawBitmap(bitmap,0,y2,paint);
}
public void logic(){
y+=speed;
y2+=speed;
if(y>800)
// y=-height;
y=-852;
if(y2>800)
// y2=-height;
y2=-852;
}
}




public class BulletHero {
private Bitmap bitmap;
private int x,y,speed;
public Bitmap getBitmap() {
return bitmap;
}
public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}
public BulletHero(Context context,int x,int y) {
// TODO Auto-generated constructor stub
bitmap=BitmapFactory.decodeResource(context.getResources(), R.drawable.bullet1);
this.x=x;
this.y=y;
speed=40;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void draw(Canvas canvas,Paint paint){
canvas.drawBitmap(bitmap, x, y, paint);
}
public void move(){
y=y-speed;
}
}


public class Enemy {
private Bitmap bitmap;
private int x, y,speed,type;
public static final int TYPE_UP=0;
public static final int TYPE_DOWN=1;
Context context;
private Bitmap bitmap3;
private Bitmap bitmap4;
public Enemy(Context context,int type){
this.type=type;
this.context=context;
switch(type){
case TYPE_UP:
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.enemy1);
speed=40;
break;
case TYPE_DOWN:
bitmap=BitmapFactory.decodeResource(context.getResources(), R.drawable.enemy2);
speed=30;
break;
}
y=bitmap.getHeight()-100;
x=(int)(Math.random()*480);
if(x>(480-bitmap.getWidth())){
x=480-bitmap.getWidth();
}
}
public void draw(Canvas canvas,Paint paint){
canvas.drawBitmap(bitmap, x, y, paint);
}
public void move(){
y+=speed;
if(type==TYPE_UP)
speed-=2;
}
//碰撞检测
public boolean isCollection(BulletHero bullet){
int left=bullet.getX();
int right=bullet.getX()+bullet.getBitmap().getWidth();
int top=bullet.getY();
int bottom=bullet.getY()+bullet.getBitmap().getHeight();
//左边
if(right<x)
return false;
//右边
if(x+bitmap.getWidth()<left)
return false;
//上边
if(bottom<y)
return false;
//下边
if(y+bitmap.getHeight()<top)
return false;
return true;
}
public void drawCollection(Canvas canvas,Paint paint) {
// TODO Auto-generated method stub
switch(type){
case 0:
bitmap3=BitmapFactory.decodeResource(context.getResources(), R.drawable.enemy1_down1);
canvas.drawBitmap(bitmap3, x, y, paint);
break;
case 1:
bitmap4 = BitmapFactory.decodeResource(context.getResources(), R.drawable.enemy2_down2);
canvas.drawBitmap(bitmap4, x, y, paint);
}
}
}


public class Hero {
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public Bitmap getBitmap1() {
return bitmap1;
}
public void setBitmap1(Bitmap bitmap1) {
this.bitmap1 = bitmap1;
}
public Bitmap getBitmap2() {
return bitmap2;
}
public void setBitmap2(Bitmap bitmap2) {
this.bitmap2 = bitmap2;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
private int x,y;
private Bitmap bitmap1;
private Bitmap bitmap2;
private int count;
public Hero(Context context){ 
super();
bitmap1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.hero1);
bitmap2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.hero2);
x=240-bitmap1.getWidth()/2;
y=800-bitmap1.getHeight();
}
public void draw(Canvas canvas,Paint paint){
count++;
if(count%2==1)
canvas.drawBitmap(bitmap1, x, y, paint);
else
canvas.drawBitmap(bitmap2, x, y, paint);
}
public void move(int x,int y){
this.x=x-bitmap1.getWidth()/2;
this.y=y-bitmap1.getHeight()/2;
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值