Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android彈球游戲

Android彈球游戲

編輯:關於Android編程

\

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.Window;
import android.view.WindowManager;

public class PinBall extends Activity
{
	// 桌面的寬度
	private int tableWidth;
	// 桌面的高度
	private int tableHeight;
	// 球拍的垂直位置
	private int racketY;
	// 下面定義球拍的高度和寬度
	private final int RACKET_HEIGHT = 20;
	private final int RACKET_WIDTH = 70;
	// 小球的大小
	private final int BALL_SIZE = 12;
	// 小球縱向的運行速度
	private int ySpeed = 10;
	Random rand = new Random();
	// 返回一個-0.5~0.5的比率,用於控制小球的運行方向。
	private double xyRate = rand.nextDouble() - 0.5;
	// 小球橫向的運行速度
	private int xSpeed = (int) (ySpeed * xyRate * 2);
	// ballX和ballY代表小球的座標
	private int ballX = rand.nextInt(200) + 20;
	private int ballY = rand.nextInt(10) + 20;
	// racketX代表球拍的水平位置
	private int racketX = rand.nextInt(200);
	// 游戲是否結束的旗標
	private boolean isLose = false;
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		// 去掉窗口標題
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// 全屏顯示
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
			WindowManager.LayoutParams.FLAG_FULLSCREEN);
		// 創建GameView組件
		final GameView gameView = new GameView(this);
		setContentView(gameView);
		// 獲取窗口管理器
		WindowManager windowManager = getWindowManager();
		Display display = windowManager.getDefaultDisplay();
		DisplayMetrics metrics = new DisplayMetrics();
		display.getMetrics(metrics);
		// 獲得屏幕寬和高
		tableWidth = metrics.widthPixels;
		tableHeight = metrics.heightPixels;
		racketY = tableHeight - 80;
		final Handler handler = new Handler()
		{
			public void handleMessage(Message msg)
			{
				if (msg.what == 0x123)
				{
					gameView.invalidate();
				}
			}
		};
		gameView.setOnKeyListener(new OnKeyListener() // 
		{
			@Override
			public boolean onKey(View source, int keyCode, KeyEvent event)
			{
				// 獲取由哪個鍵觸發的事件
				switch (event.getKeyCode())
				{
					// 控制擋板左移  A左移
					case KeyEvent.KEYCODE_A:
						if (racketX > 0) racketX -= 10;
						break;
					// 控制擋板右移,D右移
					case KeyEvent.KEYCODE_D:
						if (racketX < tableWidth - RACKET_WIDTH) racketX += 10;
						break;
				}
				// 通知gameView組件重繪
				gameView.invalidate();
				return true;
			}
		});
		final Timer timer = new Timer();
		timer.schedule(new TimerTask() // 
			{
				@Override
				public void run()
				{
					// 如果小球碰到左邊邊框
					if (ballX <= 0 || ballX >= tableWidth - BALL_SIZE)
					{
						xSpeed = -xSpeed;
					}
					// 如果小球高度超出了球拍位置,且橫向不在球拍范圍之內,游戲結束。
					if (ballY >= racketY - BALL_SIZE && (ballX < racketX || ballX > racketX
									+ RACKET_WIDTH))
					{
						timer.cancel();
						// 設置游戲是否結束的旗標為true。
						isLose = true;
					}
					// 如果小球位於球拍之內,且到達球拍位置,小球反彈
			else if (ballY <= 0	|| (ballY >= racketY - BALL_SIZE && ballX > racketX && ballX <= racketX
							+ RACKET_WIDTH))
					{
						ySpeed = -ySpeed;
					}
					// 小球座標增加
					ballY += ySpeed;
					ballX += xSpeed;
					// 發送消息,通知系統重繪組件
					handler.sendEmptyMessage(0x123);
				}
			}, 0, 100);
	}

	class GameView extends View
	{
		Paint paint = new Paint();

		public GameView(Context context)
		{
			super(context);
			setFocusable(true);
		}
		// 重寫View的onDraw方法,實現繪畫
		public void onDraw(Canvas canvas)
		{
			paint.setStyle(Paint.Style.FILL);
			// 設置去鋸齒
			paint.setAntiAlias(true);
			// 如果游戲已經結束
			if (isLose)
			{
				paint.setColor(Color.RED);
				paint.setTextSize(30);
				canvas.drawText("游戲已結束", 50, 200, paint);
			}
			// 如果游戲還未結束
			else
			{
				// 設置顏色,並繪制小球
				paint.setColor(Color.rgb(240, 240, 80));
				canvas.drawCircle(ballX, ballY, BALL_SIZE, paint);
				// 設置顏色,並繪制球拍
				paint.setColor(Color.rgb(80, 80, 200));
				canvas.drawRect(racketX, racketY, racketX + RACKET_WIDTH,
						racketY + RACKET_HEIGHT, paint);
			}
		}
	}
}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved