关闭

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

标签: android布局listviewxml
532人阅读 评论(0) 收藏 举报
分类:

打飞机小游戏


*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;
}
}


0
0
查看评论

android 游戏 实战打飞机游戏 子弹生成与碰撞 以及爆炸效果(5)

android 子弹的生成 以及爆炸效果
  • liudao7994
  • liudao7994
  • 2016-06-01 13:51
  • 4728

如何使用Python基于Pygame写一个简易的微信打飞机游戏。

人狠话不多,直接上码: import pygame from pygame.locals import * import time import random """ 需求: 设计一个打飞机游戏,实现的功能如下: 1. 能显示我机,可以通过键盘控制飞机位置,按下s...
  • birdflyinhigh
  • birdflyinhigh
  • 2017-10-03 00:05
  • 618

[GitHub开源]Android自定义View实现微信打飞机游戏

之前写了很多自定义View理论方面的文章,具体可以参见《Android中自定义View、ViewGroup理论基础详解》 。 理论指导实践,本博文演示了如何通过自定义View实现微信打飞机游戏。全部源码已经开源到GitHub,如果觉得不错,欢迎大家Star和Fork!GitHub: https:/...
  • sunqunsunqun
  • sunqunsunqun
  • 2016-07-24 17:26
  • 7024

第三十七天 一乐在其中—Android的小游戏打飞机(一)环境搭建

cocos2d是一个开源的游戏开发框架,利用它可以非常容易的开发2D游戏。如果你想在Andriod上使用Java语言进行 cocos2d游戏的开发,可以到这个网址下载相关开发文件: https://github.com/ZhouWeikuan/cocos2d。。。
  • zwszws
  • zwszws
  • 2014-08-05 09:26
  • 1379

IOS实现微信打飞机代码(带注释)

.h文件代码// // ZQAppDelegate.h // Plant // // Created by mac on 15-4-26. // Copyright (c) 2015年 KangZhiQiang. All rights reserved. //#import <UIKi...
  • u011005737
  • u011005737
  • 2015-04-26 14:40
  • 1775

Java版打飞机小游戏

放假写的一个Java端打飞机小游戏。 复习到的知识点有:java图形界面,多线程,集合框架等。 主要的收获是知道了处理图层的方式,即JLayeredPane层次面板,主要思路 如下: 1.        创建一个JLayeredPane...
  • qq_22770457
  • qq_22770457
  • 2016-04-24 08:48
  • 2038

JS原生实现多个小球碰撞反弹

实现思路:小球的移动,是通过改变小球的left和top值来改变,坐标分别为(x,y)当x/y值加到最大,即加到父级的宽度或者高度时,使x值或者y值减小,同理当x值或者y值减到最小时,同样的使x值或者y值增加,以上的思路可以实现小球的碰壁反弹 小球与小球之间的碰撞,要判断小球在被撞小球的哪个方向,从...
  • qq_37506861
  • qq_37506861
  • 2017-07-17 09:03
  • 1062

Python编写微信打飞机小游戏(十一)

在这篇博文中,我们准备为打飞机小游戏添加一个暂停的功能,即用户在游戏过程中随时可以通过单击屏幕右上方的一个暂停按钮来暂停和恢复游戏。这个功能看似比较简单,但其中涉及了鼠标操作、图片切换、代码结构的重置等等,接下来我们一一进行介绍。   1、加载暂停按钮图标   在image文件夹下一共有四张暂停...
  • u013088062
  • u013088062
  • 2015-11-18 11:17
  • 3993

unity3d开发 打飞机小游戏(一)(场景布置)

看了一下SIKI老师的课程,自己试一下做一个“打飞机”游戏,unity3d使用的版本是最新的5.5   首先创建工程选2D    打开之后导入一些资源包,直接拖进去就能导入的了 讲image文件夹里的background拖进场景,并且re...
  • qq_21057881
  • qq_21057881
  • 2017-01-19 15:43
  • 1254

关于安卓实训(打飞机游戏)的心得体会

这几天在做安卓手机打飞机游戏,
  • loveliwenyan2012
  • loveliwenyan2012
  • 2014-07-01 22:44
  • 916
    个人资料
    • 访问:36518次
    • 积分:626
    • 等级:
    • 排名:千里之外
    • 原创:36篇
    • 转载:0篇
    • 译文:0篇
    • 评论:3条
    最新评论