雷电战机---附源码

自己无聊写着玩的,没有注意代码架构和规范阿,大家就当娱乐一下,挺好玩的!我玩了一下午....

package com.example.phonegaptest;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
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.Point;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity {
	int planeX = 160;
	int planeY = 550;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 设置无标题
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// 设置全屏
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(new MyView(this));
	}

	class MyView extends View {
		final int BACK_HEIGHT = 1700;
		private Bitmap back;
		private Bitmap plane;
		private Bitmap plane2;
		ArrayList<Point> bomb = new ArrayList<Point>();
		ArrayList<Point> foemanBomb = new ArrayList<Point>();
		int foemanBombTimes = 0;
		Point foeman = new Point();
		boolean foemanDied = true;
		boolean imDied = false;
		Random random = new Random();
		Matrix m = new Matrix();
		int count = 0;

		final int WIDTH = 320;
		final int HEIGHT = 600;
		private int startY = BACK_HEIGHT - HEIGHT;

		public MyView(Context context) {
			super(context);
			setFocusable(true);
			back = BitmapFactory.decodeResource(getResources(),
					R.drawable.back_img);
			plane = BitmapFactory.decodeResource(getResources(),
					R.drawable.plane);
			m.setRotate(180);
			plane2 = Bitmap.createBitmap(plane, 0, 0, plane.getWidth(),
					plane.getHeight(), m, true);

			final Handler handler = new Handler() {
				@Override
				public void handleMessage(Message msg) {
					super.handleMessage(msg);
					if (msg.what == 0x123) {
						startY -= 30;
						if (startY < 0) {
							startY = BACK_HEIGHT - HEIGHT;
						}
					}
					invalidate();
				}

			};

			new Timer().schedule(new TimerTask() {
				@Override
				public void run() {
					if (foemanDied) {
						int x = random.nextInt(320 - 34);
						int y = random.nextInt(600 - 90);
						foeman.x = x;
						foeman.y = y;
						foemanDied = false;
					}

					if (foemanBombTimes++ > 10) {
						foemanBombTimes = 0;
						Point point = new Point(foeman.x + 17, foeman.y
								+ plane2.getHeight());
						foemanBomb.add(point);
					}
					handler.sendEmptyMessage(0x123);
				}
			}, 0, 100);
		}

		@Override
		public boolean onKeyDown(int keyCode, KeyEvent event) {
			switch (keyCode) {
			case KeyEvent.KEYCODE_DPAD_LEFT:
				planeX -= 30;
				if (planeX < 0) {
					planeX = 0;
				}
				break;
			case KeyEvent.KEYCODE_DPAD_RIGHT:
				planeX += 30;
				if (planeX > WIDTH - 34) {
					planeX = WIDTH - 34;
				}
				break;
			case KeyEvent.KEYCODE_DPAD_DOWN:
				planeY += 30;
				if (planeY > HEIGHT - 45) {
					planeY = HEIGHT - 45;
				}
				break;
			case KeyEvent.KEYCODE_DPAD_UP:
				planeY -= 30;
				if (planeY < 0) {
					planeY = 0;
				}
				break;
			case KeyEvent.KEYCODE_DPAD_CENTER:
				if (!imDied) {
					Point point = new Point(planeX + 17, planeY);
					bomb.add(point);
				}
				break;
			case KeyEvent.KEYCODE_BACK:
				if (imDied) {
					count = 0;
					imDied = false;
					foeman.x = random.nextInt(320 - 34);
					foeman.y = random.nextInt(600 - 90);
				}
				break;
			}
			invalidate();
			return true;
		}

		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			Bitmap bitmap2 = Bitmap
					.createBitmap(back, 0, startY, WIDTH, HEIGHT);
			canvas.drawBitmap(bitmap2, 0, 0, null);
			if (!imDied) {
				canvas.drawBitmap(plane, planeX, planeY, null);
			}
			Paint paint = new Paint();
			Paint paint2 = new Paint();
			paint.setColor(Color.RED);
			paint2.setColor(Color.WHITE);
			Iterator iterator = bomb.iterator();
			if (!foemanDied) {
				canvas.drawBitmap(plane2, foeman.x, foeman.y, null);

				Iterator iterator2 = foemanBomb.iterator();
				while (iterator2.hasNext()) {
					Point p = (Point) iterator2.next();
					p.y += 20;
					if (p.y > 595) {
						iterator2.remove();
					}
					canvas.drawCircle(p.x, p.y, 5, paint2);

					if (p.x > planeX && p.x < planeX + 34 && p.y > planeY
							&& p.y < planeY + 45) {
						imDied = true;
					}
				}
			}
			while (iterator.hasNext()) {
				Point p = (Point) iterator.next();
				p.y -= 10;
				if (p.y < 0) {
					iterator.remove();
				}
				canvas.drawCircle(p.x, p.y, 5, paint);

				if (p.x > foeman.x && p.x < foeman.x + 34 && p.y > foeman.y
						&& p.y < foeman.y + 45) {
					foemanDied = true;
					canvas.drawText("nice", foeman.x, foeman.y, paint);
					count++;
				}
			}

			canvas.drawText("Your score:" + count, 5, 10, paint);
			if (imDied) {
				canvas.drawText("Game is over! Press the back key.", planeX,
						planeY, paint2);
			}
		}

	}
}


源码:http://download.csdn.net/detail/h3c4lenovo/4565429


  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值